/
next.t
189 lines (165 loc) · 3.56 KB
/
next.t
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
use v6;
use Test;
plan 16;
# L<S04/Phasers/NEXT executes "only if"
# "end of the loop block" or "explicit next">
{
my $str = '';
for 1..5 {
NEXT { $str ~= ':' }
next if $_ % 2 == 1;
$str ~= $_;
}
is $str, ':2::4::', 'NEXT called by both next and normal falling out';
}
# NEXT is positioned at the bottom:
{
my $str = '';
for 1..5 {
next if $_ % 2 == 1;
$str ~= $_;
NEXT { $str ~= ':' }
}
is $str, ':2::4::', 'NEXT called by both next and normal falling out';
}
# NEXT is positioned in the middle:
{
my $str = '';
for 1..5 {
next if $_ % 2 == 1;
NEXT { $str ~= ':' }
$str ~= $_;
}
is $str, ':2::4::', 'NEXT called by both next and normal falling out';
}
# NEXT is evaluated even at the last iteration
{
my $str = '';
for 1..2 {
NEXT { $str ~= 'n'; }
LAST { $str ~= 'l'; }
}
is $str, 'nnl', 'NEXT are LAST blocks may not be exclusive';
}
# L<S04/Phasers/NEXT "not executed" if exited
# "via any exception other than" next>
{
my $str = '';
try {
for 1..5 {
NEXT { $str ~= $_ }
die if $_ > 3;
}
0;
}
is $str, '123', "die didn't trigger NEXT \{}";
}
#RT #124951
{
my $str = '';
try {
for 1..5 {
NEXT { $str ~= $_ }
leave if $_ > 3;
}
0;
}
is $str, '123', "leave didn't trigger NEXT \{}";
}
{
my $str = '';
my sub foo {
for 1..5 {
NEXT { $str ~= $_ }
return if $_ > 3;
}
0;
}
foo();
is $str, '123', "return didn't trigger NEXT \{}";
}
# L<S04/Phasers/last bypasses evaluation of NEXT phasers>
{
my $str = '';
for 1..5 {
NEXT { $str ~= $_; }
last if $_ > 3;
}
is $str, '123', "last bypass NEXT \{}";
}
# L<S04/Phasers/NEXT "before any LEAVE">
#?rakudo todo 'NEXT/LEAVE ordering RT #124952'
{
my $str = '';
for 1..2 {
NEXT { $str ~= 'n' }
LEAVE { $str ~= 'l' }
}
is $str, 'nlnl', 'NEXT {} ran before LEAVE {} (1)';
}
# reversed order
#?rakudo todo 'NEXT/LEAVE ordering RT #124952'
{
my $str = '';
for 1..2 {
LEAVE { $str ~= 'l' }
NEXT { $str ~= 'n' }
}
is $str, 'nlnl', 'NEXT {} ran before LEAVE {} (2)';
}
# L<S04/Phasers/NEXT "at loop continuation time">
# L<http://groups.google.com/group/perl.perl6.language/msg/07370316d32890dd>
{
my $str = '';
my $n = 0;
my $i;
while $n < 5 {
NEXT { ++$n } # this gets run second (LIFO)
NEXT { $str ~= $n } # this gets run first (LIFO)
last if $i++ > 100; # recursion prevension
}
is $str, '01234', 'NEXT {} ran in reversed order';
}
{
my $str = '';
loop (my $n = 0; $n < 5; ++$n) {
NEXT { $str ~= $n }
}
is $str, '01234', 'NEXT {} works in loop (;;) {}';
}
{
my @x = 0..4;
my $str = '';
for @x {
NEXT { $str ~= $_; }
}
is($str, '01234', 'NEXT {} works in for loop');
}
# RT #123731
{
my $str = '';
for ^5 {
$str ~= $_;
NEXT { last }
}
is $str, '0', 'last in a NEXT in a for loop terminates the loop';
}
{
my $str = '';
for ^5 {
$str ~= $_;
next;
$str ~= 'oops';
NEXT { last }
}
is $str, '0', 'last in a NEXT in a for loop terminates the loop, when triggered by next';
}
{
my $str = '';
loop (my $i = 0; $i < 5; $i++) {
$str ~= $i;
NEXT { last }
}
is $str, '0', 'last in a NEXT in a loop terminates the loop';
}
# vim: ft=perl6