6 use Test::More tests => 9 + 4 * 7 + 3 + ((5 * 4 * 4) * 3 + 1) + 5 + 3 + 2 + 6;
8 use Scope::Upper qw<uplevel HERE UP>;
13 uplevel { pass 'no @_: callback' };
14 is "@_", 'dummy', 'no @_: @_ outside';
18 uplevel { is "@_", '', "no arguments, no context" }
22 uplevel { is "@_", '', "no arguments, with context" } HERE
26 uplevel { is "@_", '1', "one const argument" } 1, HERE
31 uplevel { is "@_", '2', "one lexical argument" } $x, HERE
36 uplevel { is "@_", '3', "one global argument" } $y, HERE
40 uplevel { is "@_", '4 5', "two const arguments" } 4, 5, HERE
44 uplevel { is "@_", '1 2 3 4 5 6 7 8 9 10', "ten const arguments" }
54 is $r, 1, 'shift: result';
55 is_deeply \@_, [ 2 .. 10 ], 'shift: @_ inside';
57 is_deeply \@args, [ 1 .. 10 ], 'shift: args';
58 is_deeply \@_, [ 'dummy' ], 'shift: @_ outside';
65 is $r, 10, 'pop: result';
66 is_deeply \@_, [ 1 .. 9 ], 'pop: @_ inside';
68 is_deeply \@args, [ 1 .. 10 ], 'pop: args';
69 is_deeply \@_, [ 'dummy' ], 'pop: @_ outside';
75 my $r = unshift @_, 0;
76 is $r, 11, 'unshift: result';
77 is_deeply \@_, [ 0 .. 10 ], 'unshift: @_ inside';
79 is_deeply \@args, [ 1 .. 10 ], 'unshift: args';
80 is_deeply \@_, [ 'dummy' ], 'unshift: @_ outside';
87 is $r, 11, 'push: result';
88 is_deeply \@_, [ 1 .. 11 ], 'push: @_ inside';
90 is_deeply \@args, [ 1 .. 10 ], 'push: args';
91 is_deeply \@_, [ 'dummy' ], 'push: @_ outside';
97 my ($r) = splice @_, 4, 1;
98 is $r, 5, 'splice: result';
99 is_deeply \@_, [ 1 .. 4, 6 .. 10 ], 'splice: @_ inside';
101 is_deeply \@args, [ 1 .. 10 ], 'splice: args';
102 is_deeply \@_, [ 'dummy' ], 'splice: @_ outside';
106 my @args = (1 .. 10);
108 my ($r, $s, $t, @rest) = @_;
109 is_deeply [ $r, $s, $t, \@rest ], [ 1 .. 3, [ 4 .. 10 ] ], 'unpack 1: result';
110 is_deeply \@_, [ 1 .. 10 ], 'unpack 1: @_ inside';
112 is_deeply \@args, [ 1 .. 10 ], 'unpack 1: args';
113 is_deeply \@_, [ 'dummy' ], 'unpack 1: @_ outside';
119 my ($r, $s, $t, @rest) = @_;
120 is_deeply [ $r, $s, $t, \@rest ], [ 1, 2, undef, [ ] ], 'unpack 2: result';
121 is_deeply \@_, [ 1, 2 ], 'unpack 2: @_ inside';
123 is_deeply \@args, [ 1, 2 ], 'unpack 2: args';
124 is_deeply \@_, [ 'dummy' ], 'unpack 2: @_ outside';
134 is $s, 'xyz', 'aliasing, one layer';
143 is $_[0], 'xyz', 'aliasing, two layers 1';
145 is $s, 'xyz', 'aliasing, two layers 2';
152 my $cb = sub { fail 'should not be executed' };
154 eval { sub { uplevel { goto $cb } HERE }->() };
155 like $@, qr/^uplevel\(\) can't execute code that calls goto before perl 5\.8/,
157 skip "goto to an uplevel'd stack frame does not work on perl 5\.6"
158 => ((5 * 4 * 4) * 3 + 1) - 1;
164 [ [ 'b' ], [ 'n' ] ],
165 [ [ 'c' ], [ 'o', 'p' ] ],
166 [ [ 'd', 'e' ], [ 'q' ] ],
169 for my $args (@args) {
170 my ($out, $in) = @$args;
175 for my $reify_out (0, 1) {
176 for my $reify_in (0, 1) {
179 my $base_test = sub {
181 is_deeply \@_, $in, "$desc: \@_ inside";
183 is "@_", "@in", "$desc: \@_ inside";
187 my $goto_test = sub { goto $base_test };
188 my $uplevel_test = sub { &uplevel($base_test, @_, HERE) };
189 my $goto_uplevel_test = sub { &uplevel($goto_test, @_, HERE) };
192 [ 'goto' => sub { goto $base_test } ],
193 [ 'goto in goto' => sub { goto $goto_test } ],
194 [ 'uplevel in goto' => sub { goto $uplevel_test } ],
195 [ 'goto in uplevel in goto' => sub { goto $goto_uplevel_test } ],
198 for my $test (@tests) {
199 ($desc, my $cb) = @$test;
200 $desc .= ' (' . @out . ' out, ' . @in . ' in';
201 $desc .= ', reify out' if $reify_out;
202 $desc .= ', reify in' if $reify_in;
208 &uplevel($cb, @in, HERE);
210 is_deeply \@_, $out, "$desc: \@_ outside";
212 is "@_", "@out", "$desc: \@_ outside";
216 is $@, '', "$desc: no error";
232 is $s, 'brutus', 'aliasing and goto';
239 skip "goto to an uplevel'd stack frame does not work on perl 5\.6" => 5
242 my $desc = 'uplevel() calling goto &uplevel';
246 my $outer_cxt = HERE;
248 my $inner_cxt = HERE;
251 is HERE, $inner_cxt, "$desc: context inside first uplevel";
252 is "@_", '1 2 3', "$desc: arguments inisde first uplevel";
256 is HERE, $outer_cxt, "$desc: context inside second uplevel";
257 is "@_", '0 1 2 3 4', "$desc: arguments inisde second uplevel";
266 is $@, '', "$desc: no error";
269 # uplevel() to uplevel()
272 my $desc = '\&uplevel as the uplevel() callback';
279 # Note that an XS call does not need a context, so after the first uplevel
280 # call UP will point to the scope above the first target.
281 uplevel(\&uplevel => (sub {
282 is "@_", '1 2 3', "$desc: arguments inisde";
283 is HERE, $cxt, "$desc: context inside";
284 } => 1 .. 3 => UP) => UP);
286 }->(sub { die 'wut' } => HERE);
289 is $@, '', "$desc: no error";
295 package Scope::Upper::TestMagic;
298 my ($class, $cb) = @_;
299 bless { cb => $cb }, $class;
302 sub FETCH { $_[0]->{cb}->(@_) }
304 sub STORE { die "Read only magic scalar" }
307 tie my $mg, 'Scope::Upper::TestMagic', sub { $$ };
309 uplevel { is_deeply \@_, [ $$ ], "one magical argument" } $mg, HERE
312 tie my $mg2, 'Scope::Upper::TestMagic', sub { $mg };
314 uplevel { is_deeply \@_, [ $$ ], "one double magical argument" } $mg2, HERE
320 package Scope::Upper::TestTimelyDestruction;
323 my ($class, $flag) = @_;
325 bless { flag => $flag }, $class;
334 skip 'This fails even with a plain subroutine call on 5.8.0' => 6
339 my $z = Scope::Upper::TestTimelyDestruction->new(\$destroyed);
340 is $destroyed, 0, 'destruction: not yet 1';
342 is $destroyed, 0, 'destruction: not yet 2';
344 is $destroyed, 0, 'destruction: not yet 3';
346 is $destroyed, 0, 'destruction: not yet 4';
348 is $destroyed, 0, 'destruction: not yet 5';
350 is $destroyed, 1, 'destruction: destroyed';