Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: rakudo-master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 162 lines (134 sloc) 2.835 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
use v6;

use Test;

# L<S04/"Loop statements"/next>

=begin pod
next
next if <condition>;
<condition> and next;
next <label>;
next in nested loops
next <label> in nested loops

=end pod

plan 12;

# test for loops with next

{
    my $tracker=0; for 1..2 { next; $tracker++;}
    is(
        $tracker,
        0,
        "tracker is 0 because next before increment",
    );
}

{
    my $tracker = 0; for 1..5 { next unless 2 < $_ < 4; $tracker = $_;}
    is(
        $tracker,
        3,
        "... nothing before or after 3 (next unless <cond>)",
    );
}

{
    my $tracker = 0; for 1..5 { $_ > 3 && next; $tracker = $_;}
    is(
        $tracker,
        3,
        "... nothing after 3 (<cond> && next)",
    );
}

{
    my $tracker = 0; for 1..5 { $_ > 3 and next; $tracker = $_;}
    is(
        $tracker,
        3,
        "... nothing after 3 (<cond> and next)",
    );
}

#?rakudo skip 'next BLOCK'
{
    my $tracker="err";
    $tracker = 0; DONE: for 1..2 { next DONE; $tracker++;};
    is(
        $tracker,
        0,
        "tracker is 0 because next before increment",
    );
}

{
    my $tracker=0; for 1..5 -> $out {for 10..11 -> $in { next if $out > 2; $tracker = $in + $out;}}
    is($tracker,
        13,
        'inner loop skips once inner is run twice (next inside nested loops)',
    );
}

#?rakudo skip 'next LOOP'
{
    my $tracker="err";
    $tracker = 0;
    OUT: for 1..2 {
        IN: for 1..2 {
            next OUT;
            $tracker++;
        }
    }
    is(
        $tracker,
        0,
        "tracker is 0 because next before increment in nested loop",
    );
}

=begin pod

Check that C<next> works on the correct loop/block

=end pod

{
  my $foo = '';
  for 1..2 -> $a {
    $foo ~= "A";
    for 1..2 -> $b {
        $foo ~= "B";
        next; # works on higher level loop, should work on inner
    }
  }
  is($foo, "ABBABB", "next works on inner loop of 2");
}

{
    my $bar = '';
    for 1..2 -> $a {
        $bar ~= "A";
        for 1..2 -> $b {
            $bar ~= "B";
            for 1..2 -> $c {
                $bar ~= "C";
                next; # same thing
            }
        }
    }
    is($bar, "ABCCBCCABCCBCC", "next works on inner loop of 3");
}

{
    my @log;
    my $i = 0;
    while ++$i < 2 {
        push @log, "before";
        next;
        push @log, "after";
    }
    
    is(~@log, "before", "statements after next are not executed");
}

{
    my $i = 0;
    
    for 1, 1, 0, 1, 0, 1 -> $x {
        if ($x) { next }
        $i++;
    }
    
    is($i, 2, '$i++ executed only twice, because next ')
}

{
    my $i = 0;
    my $j;
    
    loop ($j = 0; $j < 6; $j++) {
        if ($j % 2 == 0) { next }
        $i++;
    }
    
    is($i, 3, '$i++ was not executed when next was called before it in loop {}');
}

# vim: ft=perl6
Something went wrong with that request. Please try again.