Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 139 lines (114 sloc) 2.709 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
use v6;

use Test;

plan 21;

# L<S04/The C<repeat> statement/"more Pascal-like repeat loop">

{
  my $x = 0; repeat { $x++ } while $x < 10;
  is($x, 10, 'repeat {} while');
}

{
  my $x = 1; repeat { $x++ } while 0;
  is($x, 2, 'ensure repeat {} while runs at least once');
}

{
  my $x = 0;
  repeat { $x++; redo if $x < 10 } while 0;
  is($x, 10, 'redo works in repeat');
}

# L<S04/The C<repeat> statement/"or equivalently">

{
  my $x = 0; repeat { $x++ } until $x >= 10;
  is($x, 10, 'repeat {} until');
}

{
  my $x = 1; repeat { $x++ } until 1;
  is($x, 2, 'ensure repeat {} until runs at least once');
}

{
  my $x = 0; repeat { $x++; redo if $x < 10 } until 1;
  is($x, 10, 'redo works in repeat {} until');
}

# L<S04/The C<repeat> statement/"loop conditional" on
# "repeat block" required>
{
    my $x = 0;
    repeat {
        $x++;
        $x += 2;
    } while $x < 10;

    is $x, 12, 'repeat with "} while"';
}

{
    my $x = 0;
    repeat {
        $x++;
        $x += 2;
    }
    while $x < 10;

    is $x, 12, 'repeat with "}\n while"';
}

# L<S04/The C<repeat> statement/put "loop conditional" "at the front">
{
  my $x = 0; repeat while $x < 10 { $x++ }
  is($x, 10, 'repeat {} while');
}

{
  my $x = 1; repeat while 0 { $x++ }
  is($x, 2, 'ensure repeat {} while runs at least once');
}

{
  my $x = 0; repeat while 0 { $x++; redo if $x < 10 };
  is($x, 10, 'redo works in repeat');
}

{
  my $x = 0; repeat until $x >= 10 { $x++ }
  is($x, 10, 'repeat until {}');
}

# L<S04/The C<repeat> statement/"bind the result">
#?pugs skip 'Cannot bind to non-existing variable: "$another_x"'
{
  my $x = 0; repeat until $x >= 10 -> $another_x {
      pass('repeat until with binding starts undefined') unless $another_x.defined;
      $x++
  }
  is($x, 10, 'repeat until -> {}');
}

{
  my $x = 1; repeat until 1 { $x++ }
  is($x, 2, 'ensure repeat until {} runs at least once');
}

{
  my $x = 0; repeat until 1 { $x++; redo if $x < 10 };
  is($x, 10, 'redo works in repeat until {}');
}

# RT #69178
#?niecza skip 'No value in ANON available for parameter $a'
#?pugs todo
{
    my $b = 1;
    my $tracker;
    repeat while $b < 10 {
        $tracker = $^a;
        $b++;
    }
    ok $tracker === True, 'placeholders and "repeat while" mix';
}

# RT #114836
{
    lives_ok {
        my $condition;
        repeat {
            $condition = False;
        } while $condition;
    }, 'can share variable between loop body and condition';
}

# RT #114432
#?niecza skip "throws_like"
#?pugs skip 'Test Util parsefail'
#?DOES 3
{
    use lib "t/spec/packages";
    use Test::Util;
    throws_like 'repeat { "but I myself" }', X::Syntax::Missing, what => '"while" or "until"';
}
# vim: ft=perl6
Something went wrong with that request. Please try again.