6 use Test::More tests => 4 * 8 + 4 * (2 * 6 + 1) + 10 + 1 + 1;
9 use VPIT::TestHelpers 'capture';
11 use Variable::Magic qw<wizard cast VMG_UVAR>;
14 my ($name, $where, $suffix) = @_;
15 $where = defined $where ? quotemeta $where : '\(eval \d+\)';
16 my $end = defined $suffix ? "$suffix\$" : '$';
17 qr/^\Q$name\E at $where line \d+\.$end/
21 [ 'data', sub { \(my $x) }, sub { } ],
22 [ 'get', sub { \(my $x) }, sub { my $y = ${$_[0]} } ],
23 [ 'set', sub { \(my $x) }, sub { ${$_[0]} = 1 } ],
24 [ 'len', sub { [ 1 .. 3 ] }, sub { my $res = @{$_[0]} } ],
29 for my $t (@scalar_tests) {
30 my ($name, $init, $code) = @$t;
32 my $wiz = wizard $name => sub { die 'leek' };
41 like $@, expect('leek', $0),
42 "die in $name callback (direct, \$@ unset) in eval";
53 like $@, expect('leek', $0),
54 "die in $name callback (direct, \$@ set) in eval";
64 like $@, expect('leek', $0, "\nBEGIN.*"),
65 "die in $name callback (direct, \$@ unset) in BEGIN";
76 like $@, expect('leek', $0, "\nBEGIN.*"),
77 "die in $name callback (direct, \$@ set) in BEGIN";
81 ($name eq 'data' ? () : (data => sub { $_[1] })),
82 $name => sub { $_[1]->(); () },
89 &cast($x, $wiz, sub { die 'lettuce' });
92 like $@, expect('lettuce', $0),
93 "die in $name callback (indirect, \$@ unset) in eval";
100 &cast($x, $wiz, sub { die 'carrot' });
104 like $@, expect('carrot', $0),
105 "die in $name callback (indirect, \$@ unset) in eval";
112 &cast($x, $wiz, sub { die "pumpkin" });
115 like $@, expect('pumpkin', undef, "\nBEGIN.*"),
116 "die in $name callback (indirect, \$@ unset) in BEGIN";
123 &cast($x, $wiz, sub { die "chard" });
127 like $@, expect('chard', undef, "\nBEGIN.*"),
128 "die in $name callback (indirect, \$@ set) in BEGIN";
135 my $wiz = wizard free => sub { die 'avocado' };
136 my $check = sub { like $@, expect('avocado', $0), $_[0] };
138 for my $local_out (0, 1) {
139 for my $local_in (0, 1) {
140 my $desc = "die in free callback";
141 if ($local_in or $local_out) {
142 $desc .= ' with $@ localized ';
143 if ($local_in and $local_out) {
144 $desc .= 'inside and outside';
145 } elsif ($local_in) {
152 local $@ = $local_out ? 'xxx' : undef;
154 local $@ = 'yyy' if $local_in;
158 $check->("$desc at eval BLOCK 1a");
160 local $@ = $local_out ? 'xxx' : undef;
162 local $@ = 'yyy' if $local_in;
166 $check->("$desc at eval STRING 1a");
168 local $@ = $local_out ? 'xxx' : undef;
171 local $@ = 'yyy' if $local_in;
174 $check->("$desc at eval BLOCK 1b");
176 local $@ = $local_out ? 'xxx' : undef;
179 local $@ = 'yyy' if $local_in;
182 $check->("$desc at eval STRING 1b");
184 local $@ = $local_out ? 'xxx' : undef;
186 local $@ = 'yyy' if $local_in;
191 $check->("$desc at eval BLOCK 2a");
193 local $@ = $local_out ? 'xxx' : undef;
195 local $@ = 'yyy' if $local_in;
200 $check->("$desc at eval STRING 2a");
202 local $@ = $local_out ? 'xxx' : undef;
206 local $@ = 'yyy' if $local_in;
209 $check->("$desc at eval BLOCK 2b");
211 local $@ = $local_out ? 'xxx' : undef;
215 local $@ = 'yyy' if $local_in;
218 $check->("$desc at eval STRING 2b");
220 local $@ = $local_out ? 'xxx' : undef;
222 local $@ = 'yyy' if $local_in;
227 $check->("$desc at eval BLOCK 3");
229 local $@ = $local_out ? 'xxx' : undef;
231 local $@ = 'yyy' if $local_in;
236 $check->("$desc at eval STRING 3");
238 local $@ = $local_out ? 'xxx' : undef;
240 local $@ = 'yyy' if $local_in;
246 $check->("$desc at block in eval BLOCK");
248 local $@ = $local_out ? 'xxx' : undef;
250 local $@ = 'yyy' if $local_in;
256 $check->("$desc at block in eval STRING");
258 ok defined($desc), "$desc did not over-unwind the save stack";
266 $wiz = wizard data => sub { $_[1] },
267 free => sub { $_[1]->(); () };
269 cast $x, $wiz, sub { die "spinach" };
272 like $@, expect('spinach', $0), 'die in sub in free callback';
275 $wiz = wizard free => sub { die 'zucchini' };
284 like $@, expect('zucchini', $0),
285 'die in free callback in block in eval with $@ unset';
288 $wiz = wizard free => sub { die 'eggplant' };
294 die 'not reached again';
297 like $@, expect('eggplant', $0),
298 'die in free callback in block in eval with $@ set';
301 $wiz = wizard free => sub { die 'onion' };
306 like $@, expect('onion', undef, "\nBEGIN.*"), 'die in free callback in BEGIN';
309 $wiz = wizard data => sub { $_[1] },
310 len => sub { $_[1]->(); $_[2] },
311 free => sub { my $x = @{$_[0]}; () };
313 cast @a, $wiz, sub { die "pepperoni" };
316 like $@, expect('pepperoni', undef, "\nBEGIN.*"),
317 'die in free callback in len callback in BEGIN';
319 # Inspired by B::Hooks::EndOfScope
322 $wiz = wizard data => sub { $_[1] },
323 free => sub { $_[1]->(); () };
325 cast %^H, $wiz, sub { die 'cabbage' };
328 like $@, expect('cabbage'), 'die in free callback at end of scope';
332 my $vm_tse_file = 't/lib/Variable/Magic/TestScopeEnd.pm';
334 eval "use Variable::Magic::TestScopeEnd";
335 like $@, expect('turnip', $vm_tse_file, "\nBEGIN(?s:.*)"),
336 'die in BEGIN in require in eval string triggers hints hash destructor';
339 Variable::Magic::TestScopeEnd::hook {
340 pass 'in hints hash destructor 2';
345 like $@, expect('tomato', undef, "\nBEGIN.*"),
346 'die in BEGIN in eval triggers hints hash destructor';
351 my ($stat, $out, $err) = capture_perl <<' CODE';
352 use Variable::Magic qw<wizard cast>; { BEGIN { $^H |= 0x020000; cast %^H, wizard free => sub { die q[cucumber] } } }
354 skip CAPTURE_PERL_FAILED($out) => $count unless defined $stat;
355 like $err, 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;
368 my ($stat, $out, $err) = capture_perl <<' CODE';
369 use Variable::Magic qw<wizard cast>; BEGIN { cast %derp::, wizard fetch => sub { die q[raddish] } } derp::hlagh()
371 skip CAPTURE_PERL_FAILED($out) => $count unless defined $stat;
372 like $err, expect('raddish', '-e', "\nExecution(?s:.*)"),
373 'die in free callback at compile time and not in eval string';