6 use Test::More tests => 4 * 8 + 4 * (2 * 4 + 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 1");
157 local $@ = $local_out ? 'xxx' : undef;
159 local $@ = 'yyy' if $local_in;
163 $check->("$desc at eval STRING 1");
165 local $@ = $local_out ? 'xxx' : undef;
167 local $@ = 'yyy' if $local_in;
172 $check->("$desc at eval BLOCK 2");
174 local $@ = $local_out ? 'xxx' : undef;
176 local $@ = 'yyy' if $local_in;
181 $check->("$desc at eval STRING 2");
183 local $@ = $local_out ? 'xxx' : undef;
185 local $@ = 'yyy' if $local_in;
190 $check->("$desc at eval BLOCK 3");
192 local $@ = $local_out ? 'xxx' : undef;
194 local $@ = 'yyy' if $local_in;
199 $check->("$desc at eval STRING 3");
201 local $@ = $local_out ? 'xxx' : undef;
203 local $@ = 'yyy' if $local_in;
209 $check->("$desc at block in eval BLOCK");
211 local $@ = $local_out ? 'xxx' : undef;
213 local $@ = 'yyy' if $local_in;
219 $check->("$desc at block in eval STRING");
221 ok defined($desc), "$desc did not over-unwind the save stack";
229 $wiz = wizard data => sub { $_[1] },
230 free => sub { $_[1]->(); () };
232 cast $x, $wiz, sub { die "spinach" };
235 like $@, expect('spinach', $0), 'die in sub in free callback';
238 $wiz = wizard free => sub { die 'zucchini' };
247 like $@, expect('zucchini', $0),
248 'die in free callback in block in eval with $@ unset';
251 $wiz = wizard free => sub { die 'eggplant' };
257 die 'not reached again';
260 like $@, expect('eggplant', $0),
261 'die in free callback in block in eval with $@ set';
264 $wiz = wizard free => sub { die 'onion' };
269 like $@, expect('onion', undef, "\nBEGIN.*"), 'die in free callback in BEGIN';
272 $wiz = wizard data => sub { $_[1] },
273 len => sub { $_[1]->(); $_[2] },
274 free => sub { my $x = @{$_[0]}; () };
276 cast @a, $wiz, sub { die "pepperoni" };
279 like $@, expect('pepperoni', undef, "\nBEGIN.*"),
280 'die in free callback in len callback in BEGIN';
282 # Inspired by B::Hooks::EndOfScope
285 $wiz = wizard data => sub { $_[1] },
286 free => sub { $_[1]->(); () };
288 cast %^H, $wiz, sub { die 'cabbage' };
291 like $@, expect('cabbage'), 'die in free callback at end of scope';
295 my $vm_tse_file = 't/lib/Variable/Magic/TestScopeEnd.pm';
297 eval "use Variable::Magic::TestScopeEnd";
298 like $@, expect('turnip', $vm_tse_file, "\nBEGIN(?s:.*)"),
299 'die in BEGIN in require in eval string triggers hints hash destructor';
302 Variable::Magic::TestScopeEnd::hook {
303 pass 'in hints hash destructor 2';
308 like $@, expect('tomato', undef, "\nBEGIN.*"),
309 'die in BEGIN in eval triggers hints hash destructor';
314 my ($SystemRoot, $PATH) = @ENV{qw<SystemRoot PATH>};
316 $ENV{SystemRoot} = $SystemRoot if $^O eq 'MSWin32' and defined $SystemRoot;
317 $ENV{PATH} = $PATH if $^O eq 'cygwin' and defined $PATH;
319 system { $^X } $^X, '-T', map("-I$_", @INC), '-e', $code;
322 my $has_capture_tiny = do {
325 require Capture::Tiny;
326 Capture::Tiny->VERSION('0.08');
329 if ($has_capture_tiny) {
332 Capture::Tiny::capture_merged(sub { run_perl <<' CODE' });
333 print STDOUT "pants\n";
334 print STDERR "trousers\n";
337 unless (defined $output and $output =~ /pants/ and $output =~ /trousers/) {
338 $has_capture_tiny = 0;
341 if ($has_capture_tiny) {
342 defined and diag "Using Capture::Tiny $_" for $Capture::Tiny::VERSION;
349 skip 'No working Capture::Tiny is installed'=> $count unless $has_capture_tiny;
351 my $output = Capture::Tiny::capture_merged(sub { run_perl <<' CODE' });
352 use Variable::Magic qw<wizard cast>; { BEGIN { $^H |= 0x020000; cast %^H, wizard free => sub { die q[cucumber] } } }
354 skip 'Test code didn\'t run properly' => $count unless defined $output;
355 like $output, expect('cucumber', '-e', "\nExecution(?s:.*)"),
356 'die in free callback at compile time and not in eval string';
366 skip 'No nice uvar magic for this perl' => $count unless VMG_UVAR;
367 skip 'No working Capture::Tiny is installed'=> $count unless $has_capture_tiny;
369 my $output = Capture::Tiny::capture_merged(sub { run_perl <<' CODE' });
370 use Variable::Magic qw<wizard cast>; BEGIN { cast %derp::, wizard fetch => sub { die q[raddish] } } derp::hlagh()
372 skip 'Test code didn\'t run properly' => $count unless defined $output;
373 like $output, expect('raddish', '-e', "\nExecution(?s:.*)"),
374 'die in free callback at compile time and not in eval string';