+sub expect {
+ my ($name, $where, $suffix) = @_;
+ $where = defined $where ? quotemeta $where : '\(eval \d+\)';
+ my $end = defined $suffix ? "$suffix\$" : '$';
+ qr/^\Q$name\E at $where line \d+\.$end/
+}
+
+my @scalar_tests = (
+ [ 'data', sub { \(my $x) }, sub { } ],
+ [ 'get', sub { \(my $x) }, sub { my $y = ${$_[0]} } ],
+ [ 'set', sub { \(my $x) }, sub { ${$_[0]} = 1 } ],
+ [ 'len', sub { [ 1 .. 3 ] }, sub { my $res = @{$_[0]} } ],
+);
+
+# Data, get, set, len
+
+for my $t (@scalar_tests) {
+ my ($name, $init, $code) = @$t;
+
+ my $wiz = wizard $name => sub { die 'leek' };
+
+ {
+ local $@;
+ eval {
+ my $x = $init->();
+ &cast($x, $wiz);
+ $code->($x);
+ };
+ like $@, expect('leek', $0),
+ "die in $name callback (direct, \$@ unset) in eval";
+ }
+
+ {
+ local $@;
+ eval {
+ my $x = $init->();
+ &cast($x, $wiz);
+ $@ = 'artichoke';
+ $code->($x);
+ };
+ like $@, expect('leek', $0),
+ "die in $name callback (direct, \$@ set) in eval";
+ }
+
+ {
+ local $@;
+ eval q{BEGIN {
+ my $x = $init->();
+ &cast($x, $wiz);
+ $code->($x);
+ }};
+ like $@, expect('leek', $0, "\nBEGIN.*"),
+ "die in $name callback (direct, \$@ unset) in BEGIN";
+ }
+
+ {
+ local $@;
+ eval q{BEGIN {
+ my $x = $init->();
+ &cast($x, $wiz);
+ $@ = 'artichoke';
+ $code->($x);
+ }};
+ like $@, expect('leek', $0, "\nBEGIN.*"),
+ "die in $name callback (direct, \$@ set) in BEGIN";
+ }
+
+ $wiz = wizard(
+ ($name eq 'data' ? () : (data => sub { $_[1] })),
+ $name => sub { $_[1]->(); () },
+ );
+
+ {
+ local $@;
+ eval {
+ my $x = $init->();
+ &cast($x, $wiz, sub { die 'lettuce' });
+ $code->($x);
+ };
+ like $@, expect('lettuce', $0),
+ "die in $name callback (indirect, \$@ unset) in eval";
+ }
+
+ {
+ local $@;
+ eval {
+ my $x = $init->();
+ &cast($x, $wiz, sub { die 'carrot' });
+ $@ = 'artichoke';
+ $code->($x);
+ };
+ like $@, expect('carrot', $0),
+ "die in $name callback (indirect, \$@ unset) in eval";
+ }
+
+ {
+ local $@;
+ eval q{BEGIN {
+ my $x = $init->();
+ &cast($x, $wiz, sub { die "pumpkin" });
+ $code->($x);
+ }};
+ like $@, expect('pumpkin', undef, "\nBEGIN.*"),
+ "die in $name callback (indirect, \$@ unset) in BEGIN";
+ }
+
+ {
+ local $@;
+ eval q{BEGIN {
+ my $x = $init->();
+ &cast($x, $wiz, sub { die "chard" });
+ $@ = 'artichoke';
+ $code->($x);
+ }};
+ like $@, expect('chard', undef, "\nBEGIN.*"),
+ "die in $name callback (indirect, \$@ set) in BEGIN";
+ }
+}
+
+# Free
+
+{
+ my $wiz = wizard free => sub { die 'avocado' };
+ my $check = sub { like $@, expect('avocado', $0), $_[0] };
+
+ for my $local_out (0, 1) {
+ for my $local_in (0, 1) {
+ my $desc = "die in free callback";
+ if ($local_in or $local_out) {
+ $desc .= ' with $@ localized ';
+ if ($local_in and $local_out) {
+ $desc .= 'inside and outside';
+ } elsif ($local_in) {
+ $desc .= 'inside';
+ } else {
+ $desc .= 'outside';
+ }
+ }
+
+ local $@ = $local_out ? 'xxx' : undef;
+ eval {
+ local $@ = 'yyy' if $local_in;
+ my $x;
+ cast $x, $wiz;
+ };
+ $check->("$desc at eval BLOCK 1");
+
+ local $@ = $local_out ? 'xxx' : undef;
+ eval q{
+ local $@ = 'yyy' if $local_in;
+ my $x;
+ cast $x, $wiz;
+ };
+ $check->("$desc at eval STRING 1");
+
+ local $@ = $local_out ? 'xxx' : undef;
+ eval {
+ local $@ = 'yyy' if $local_in;
+ my $x;
+ my $y = \$x;
+ &cast($y, $wiz);
+ };
+ $check->("$desc at eval BLOCK 2");
+
+ local $@ = $local_out ? 'xxx' : undef;
+ eval q{
+ local $@ = 'yyy' if $local_in;
+ my $x;
+ my $y = \$x;
+ &cast($y, $wiz);
+ };
+ $check->("$desc at eval STRING 2");
+
+ local $@ = $local_out ? 'xxx' : undef;
+ eval {
+ local $@ = 'yyy' if $local_in;
+ my $x;
+ cast $x, $wiz;
+ my $y = 1;
+ };
+ $check->("$desc at eval BLOCK 3");
+
+ local $@ = $local_out ? 'xxx' : undef;
+ eval q{
+ local $@ = 'yyy' if $local_in;
+ my $x;
+ cast $x, $wiz;
+ my $y = 1;
+ };
+ $check->("$desc at eval STRING 3");
+
+ local $@ = $local_out ? 'xxx' : undef;
+ eval {
+ local $@ = 'yyy' if $local_in;
+ {
+ my $x;
+ cast $x, $wiz;
+ }
+ };
+ $check->("$desc at block in eval BLOCK");
+
+ local $@ = $local_out ? 'xxx' : undef;
+ eval q{
+ local $@ = 'yyy' if $local_in;
+ {
+ my $x;
+ cast $x, $wiz;
+ }
+ };
+ $check->("$desc at block in eval STRING");
+
+ ok defined($desc), "$desc did not over-unwind the save stack";
+ }
+ }
+}