6 use Test::More tests => 4 * 8 + 4 * (2 * 6 + 1) + 10 + 1 + 1;
8 use Variable::Magic qw<wizard cast VMG_UVAR>;
11 my ($name, $where, $suffix) = @_;
12 $where = defined $where ? quotemeta $where : '\(eval \d+\)';
13 my $end = defined $suffix ? "$suffix\$" : '$';
14 qr/^\Q$name\E at $where line \d+\.$end/
18 [ 'data', sub { \(my $x) }, sub { } ],
19 [ 'get', sub { \(my $x) }, sub { my $y = ${$_[0]} } ],
20 [ 'set', sub { \(my $x) }, sub { ${$_[0]} = 1 } ],
21 [ 'len', sub { [ 1 .. 3 ] }, sub { my $res = @{$_[0]} } ],
26 for my $t (@scalar_tests) {
27 my ($name, $init, $code) = @$t;
29 my $wiz = wizard $name => sub { die 'leek' };
38 like $@, expect('leek', $0),
39 "die in $name callback (direct, \$@ unset) in eval";
50 like $@, expect('leek', $0),
51 "die in $name callback (direct, \$@ set) in eval";
61 like $@, expect('leek', $0, "\nBEGIN.*"),
62 "die in $name callback (direct, \$@ unset) in BEGIN";
73 like $@, expect('leek', $0, "\nBEGIN.*"),
74 "die in $name callback (direct, \$@ set) in BEGIN";
78 ($name eq 'data' ? () : (data => sub { $_[1] })),
79 $name => sub { $_[1]->(); () },
86 &cast($x, $wiz, sub { die 'lettuce' });
89 like $@, expect('lettuce', $0),
90 "die in $name callback (indirect, \$@ unset) in eval";
97 &cast($x, $wiz, sub { die 'carrot' });
101 like $@, expect('carrot', $0),
102 "die in $name callback (indirect, \$@ unset) in eval";
109 &cast($x, $wiz, sub { die "pumpkin" });
112 like $@, expect('pumpkin', undef, "\nBEGIN.*"),
113 "die in $name callback (indirect, \$@ unset) in BEGIN";
120 &cast($x, $wiz, sub { die "chard" });
124 like $@, expect('chard', undef, "\nBEGIN.*"),
125 "die in $name callback (indirect, \$@ set) in BEGIN";
132 my $wiz = wizard free => sub { die 'avocado' };
133 my $check = sub { like $@, expect('avocado', $0), $_[0] };
135 for my $local_out (0, 1) {
136 for my $local_in (0, 1) {
137 my $desc = "die in free callback";
138 if ($local_in or $local_out) {
139 $desc .= ' with $@ localized ';
140 if ($local_in and $local_out) {
141 $desc .= 'inside and outside';
142 } elsif ($local_in) {
149 local $@ = $local_out ? 'xxx' : undef;
151 local $@ = 'yyy' if $local_in;
155 $check->("$desc at eval BLOCK 1a");
157 local $@ = $local_out ? 'xxx' : undef;
159 local $@ = 'yyy' if $local_in;
163 $check->("$desc at eval STRING 1a");
165 local $@ = $local_out ? 'xxx' : undef;
168 local $@ = 'yyy' if $local_in;
171 $check->("$desc at eval BLOCK 1b");
173 local $@ = $local_out ? 'xxx' : undef;
176 local $@ = 'yyy' if $local_in;
179 $check->("$desc at eval STRING 1b");
181 local $@ = $local_out ? 'xxx' : undef;
183 local $@ = 'yyy' if $local_in;
188 $check->("$desc at eval BLOCK 2a");
190 local $@ = $local_out ? 'xxx' : undef;
192 local $@ = 'yyy' if $local_in;
197 $check->("$desc at eval STRING 2a");
199 local $@ = $local_out ? 'xxx' : undef;
203 local $@ = 'yyy' if $local_in;
206 $check->("$desc at eval BLOCK 2b");
208 local $@ = $local_out ? 'xxx' : undef;
212 local $@ = 'yyy' if $local_in;
215 $check->("$desc at eval STRING 2b");
217 local $@ = $local_out ? 'xxx' : undef;
219 local $@ = 'yyy' if $local_in;
224 $check->("$desc at eval BLOCK 3");
226 local $@ = $local_out ? 'xxx' : undef;
228 local $@ = 'yyy' if $local_in;
233 $check->("$desc at eval STRING 3");
235 local $@ = $local_out ? 'xxx' : undef;
237 local $@ = 'yyy' if $local_in;
243 $check->("$desc at block in eval BLOCK");
245 local $@ = $local_out ? 'xxx' : undef;
247 local $@ = 'yyy' if $local_in;
253 $check->("$desc at block in eval STRING");
255 ok defined($desc), "$desc did not over-unwind the save stack";
263 $wiz = wizard data => sub { $_[1] },
264 free => sub { $_[1]->(); () };
266 cast $x, $wiz, sub { die "spinach" };
269 like $@, expect('spinach', $0), 'die in sub in free callback';
272 $wiz = wizard free => sub { die 'zucchini' };
281 like $@, expect('zucchini', $0),
282 'die in free callback in block in eval with $@ unset';
285 $wiz = wizard free => sub { die 'eggplant' };
291 die 'not reached again';
294 like $@, expect('eggplant', $0),
295 'die in free callback in block in eval with $@ set';
298 $wiz = wizard free => sub { die 'onion' };
303 like $@, expect('onion', undef, "\nBEGIN.*"), 'die in free callback in BEGIN';
306 $wiz = wizard data => sub { $_[1] },
307 len => sub { $_[1]->(); $_[2] },
308 free => sub { my $x = @{$_[0]}; () };
310 cast @a, $wiz, sub { die "pepperoni" };
313 like $@, expect('pepperoni', undef, "\nBEGIN.*"),
314 'die in free callback in len callback in BEGIN';
316 # Inspired by B::Hooks::EndOfScope
319 $wiz = wizard data => sub { $_[1] },
320 free => sub { $_[1]->(); () };
322 cast %^H, $wiz, sub { die 'cabbage' };
325 like $@, expect('cabbage'), 'die in free callback at end of scope';
329 my $vm_tse_file = 't/lib/Variable/Magic/TestScopeEnd.pm';
331 eval "use Variable::Magic::TestScopeEnd";
332 like $@, expect('turnip', $vm_tse_file, "\nBEGIN(?s:.*)"),
333 'die in BEGIN in require in eval string triggers hints hash destructor';
336 Variable::Magic::TestScopeEnd::hook {
337 pass 'in hints hash destructor 2';
342 like $@, expect('tomato', undef, "\nBEGIN.*"),
343 'die in BEGIN in eval triggers hints hash destructor';
348 my ($SystemRoot, $PATH) = @ENV{qw<SystemRoot PATH>};
350 $ENV{SystemRoot} = $SystemRoot if $^O eq 'MSWin32' and defined $SystemRoot;
351 $ENV{PATH} = $PATH if $^O eq 'cygwin' and defined $PATH;
353 system { $^X } $^X, '-T', map("-I$_", @INC), '-e', $code;
356 my $has_capture_tiny = do {
359 require Capture::Tiny;
360 Capture::Tiny->VERSION('0.08');
363 if ($has_capture_tiny) {
366 Capture::Tiny::capture_merged(sub { run_perl <<' CODE' });
367 print STDOUT "pants\n";
368 print STDERR "trousers\n";
371 unless (defined $output and $output =~ /pants/ and $output =~ /trousers/) {
372 $has_capture_tiny = 0;
375 if ($has_capture_tiny) {
376 defined and diag "Using Capture::Tiny $_" for $Capture::Tiny::VERSION;
383 skip 'No working Capture::Tiny is installed'=> $count unless $has_capture_tiny;
385 my $output = Capture::Tiny::capture_merged(sub { run_perl <<' CODE' });
386 use Variable::Magic qw<wizard cast>; { BEGIN { $^H |= 0x020000; cast %^H, wizard free => sub { die q[cucumber] } } }
388 skip 'Test code didn\'t run properly' => $count unless defined $output;
389 like $output, expect('cucumber', '-e', "\nExecution(?s:.*)"),
390 'die in free callback at compile time and not in eval string';
400 skip 'No nice uvar magic for this perl' => $count unless VMG_UVAR;
401 skip 'No working Capture::Tiny is installed'=> $count unless $has_capture_tiny;
403 my $output = Capture::Tiny::capture_merged(sub { run_perl <<' CODE' });
404 use Variable::Magic qw<wizard cast>; BEGIN { cast %derp::, wizard fetch => sub { die q[raddish] } } derp::hlagh()
406 skip 'Test code didn\'t run properly' => $count unless defined $output;
407 like $output, expect('raddish', '-e', "\nExecution(?s:.*)"),
408 'die in free callback at compile time and not in eval string';