6 use Test::More tests => 41 + 30 + 4 * 7;
8 use Scope::Upper qw<reap UP HERE>;
21 is $x, 2, 'goto 1 [not yet - x]';
22 is $y, undef, 'goto 1 [not yet - y]';
30 is $x, 1, 'goto 1 [ok - x]';
31 is $y, 1, 'goto 1 [ok - y]';
42 reap \&check => UP UP;
44 is $x, 3, 'goto 2 [not yet - x]';
45 is $y, undef, 'goto 2 [not yet - y]';
54 is $x, 1, 'goto 2 [ok - x]';
55 is $y, 1, 'goto 2 [ok - y]';
67 reap \&check => UP UP UP;
68 is $x, 3, 'die - reap outside eval [not yet 1 - x]';
69 is $y, undef, 'die - reap outside eval [not yet 1 - y]';
71 is $x, 2, 'die - reap outside eval [not yet 2 - x]';
72 is $y, undef, 'die - reap outside eval [not yet 2 - y]';
76 is $x, 1, 'die - reap outside eval [not yet 3 - x]';
77 is $y, undef, 'die - reap outside eval [not yet 3 - y]';
78 } # should trigger here
79 is $x, 1, 'die - reap outside eval [ok - x]';
80 is $y, 1, 'die - reap outside eval [ok - y]';
91 reap \&check => UP UP;
92 is $x, 3, 'die - reap at eval [not yet 1 - x]';
93 is $y, undef, 'die - reap at eval [not yet 1 - y]';
95 is $x, 2, 'die - reap at eval [not yet 2 - x]';
96 is $y, undef, 'die - reap at eval [not yet 2 - y]';
99 }; # should trigger here
100 is $x, 1, 'die - reap at eval [ok - x]';
101 is $y, 1, 'die - reap at eval [ok - y]';
113 is $x, 3, 'die - reap inside eval [not yet 1 - x]';
114 is $y, undef, 'die - reap inside eval [not yet 1 - y]';
116 is $x, 2, 'die - reap inside eval [not yet 2 - x]';
117 is $y, undef, 'die - reap inside eval [not yet 2 - y]';
119 } # should trigger here
121 is $x, 1, 'die - reap inside eval [ok - x]';
122 is $y, 1, 'die - reap inside eval [ok - y]';
129 reap { $reaped = 1 };
130 is $reaped, 0, 'died of natural death - not reaped yet';
134 is $reaped, 1, 'died of natural death - reaped';
135 like $err, qr/division by zero/, 'died of natural death - divided by zero';
140 skip 'Perl 5.10 required to test given/when' => 30 if "$]" < 5.010;
142 eval <<' GIVEN_TEST_1';
143 use feature 'switch';
152 is $x, 3, 'given/when - reap at given [not yet - x]';
153 is $y, undef, 'given/when - reap at given [not yet - y]';
157 is $x, 1, 'given/when - reap at given [ok - x]';
158 is $y, 1, 'given/when - reap at given [ok - y]';
163 eval <<' GIVEN_TEST_2';
164 use feature 'switch';
173 is $x, 3, 'given/when/continue - reap at given [not yet 1 - x]';
174 is $y, undef, 'given/when/continue - reap at given [not yet 1 - y]';
177 is $x, 2, 'given/when/continue - reap at given [not yet 2 - x]';
178 is $y, undef, 'given/when/continue - reap at given [not yet 2 - y]';
180 is $x, 1, 'given/when/continue - reap at given [ok - x]';
181 is $y, 1, 'given/when/continue - reap at given [ok - y]';
186 eval <<' GIVEN_TEST_3';
187 use feature 'switch';
196 is $x, 3, 'given/default - reap at given [not yet - x]';
197 is $y, undef, 'given/default - reap at given [not yet - y]';
201 is $x, 1, 'given/default - reap at given [ok - x]';
202 is $y, 1, 'given/default - reap at given [ok - y]';
207 eval <<' GIVEN_TEST_4';
208 use feature 'switch';
217 is $x, 3, 'given/default/continue - reap at given [not yet 1 - x]';
218 is $y, undef, 'given/default/continue - reap at given [not yet 1 - y]';
221 is $x, 2, 'given/default/continue - reap at given [not yet 2 - x]';
222 is $y, undef, 'given/default/continue - reap at given [not yet 2 - y]';
224 is $x, 1, 'given/default/continue - reap at given [ok - x]';
225 is $y, 1, 'given/default/continue - reap at given [ok - y]';
230 eval <<' GIVEN_TEST_5';
231 use feature 'switch';
243 reap \&check => UP UP;
244 is $x, 5, 'given/default/given/when - reap at default [not yet 1 - x]';
245 is $y, undef, 'given/default/given/when - reap at default [not yet 1 - y]';
248 is $x, 4, 'given/default/given/when - reap at default [not yet 2 - x]';
249 is $y, undef, 'given/default/given/when - reap at default [not yet 2 - y]';
251 is $x, 3, 'given/default/given/when - reap at default [not yet 3 - x]';
252 is $y, undef, 'given/default/given/when - reap at default [not yet 3 - y]';
255 is $x, 2, 'given/default/given/when - reap at default [ok 1 - x]';
256 is $y, 1, 'given/default/given/when - reap at default [ok 1 - y]';
258 is $x, 1, 'given/default/given/when - reap at default [ok 2 - x]';
259 is $y, 1, 'given/default/given/when - reap at default [ok 2 - y]';
272 reap { ++$y; die "reaped\n" } => HERE;
273 is $x, 3, 'die in reap at eval [not yet - x]';
274 is $y, undef, 'die in reap at eval [not yet - y]';
275 }; # should trigger here, but the die isn't catched by this eval
278 is $@, "reaped\n", 'die in reap at eval [ok - $@]';
279 is $x, 1, 'die in reap at eval [ok - x]';
280 is $y, 1, 'die in reap at eval [ok - y]';
290 reap { ++$y; die "reaped\n" } => HERE;
291 is $x, 3, 'die in reap inside eval [not yet - x]';
292 is $y, undef, 'die in reap inside eval [not yet - y]';
293 } # should trigger here
296 is $@, "reaped\n", 'die in reap inside eval [ok - $@]';
297 is $x, 1, 'die in reap inside eval [ok - x]';
298 is $y, 1, 'die in reap inside eval [ok - y]';
302 my ($cb, $desc) = @_;
307 is $x, 3, "$desc [not yet 1 - x]";
308 is $y, undef, "$desc [not yet 1 - y]";
310 is $x, 2, "$desc [not yet 2 - x]";
311 is $y, undef, "$desc [not yet 2 - y]";
315 for ([ sub { ++$y; 15, 16, 17, 18 }, 'implicit ' ],
316 [ sub { ++$y; return 15, 16, 17, 18 }, '' ]) {
317 my ($cb, $imp) = @$_;
318 $imp = "RT #44204 - ${imp}return from reap";
322 $desc = "$imp in list context";
324 my @l = hijacked($cb, $desc);
325 is $x, 1, "$desc [ok - x]";
326 is $y, 1, "$desc [ok - y]";
327 is_deeply \@l, [ 11, 12 ], "$desc [ok - l]";
331 $desc = "$imp in list context";
333 my $s = hijacked($cb, $desc);
334 is $x, 1, "$desc [ok - x]";
335 is $y, 1, "$desc [ok - y]";
336 is $s, 12, "$desc [ok - s]";