X-Git-Url: http://git.vpit.fr/?p=perl%2Fmodules%2FVariable-Magic.git;a=blobdiff_plain;f=t%2F17-ctl.t;h=88345409e64d6c50d6e6fd54ac507f745a3e2089;hp=bedfdb0428b440eeec3b80c8fb3279e6b0cb0f63;hb=34c317825d931337c0d5cdc6b365ca9a16906310;hpb=cf48e6bb084037da1980bde10e21dca795fd84d0 diff --git a/t/17-ctl.t b/t/17-ctl.t index bedfdb0..8834540 100644 --- a/t/17-ctl.t +++ b/t/17-ctl.t @@ -3,9 +3,12 @@ use strict; use warnings; -use Test::More tests => 4 * 8 + 10 + 1 + 1; +use Test::More tests => 4 * 8 + 4 * (2 * 6 + 1) + 10 + 1 + 1; -use Variable::Magic qw/wizard cast VMG_UVAR/; +use lib 't/lib'; +use VPIT::TestHelpers 'capture'; + +use Variable::Magic qw; sub expect { my ($name, $where, $suffix) = @_; @@ -128,6 +131,135 @@ for my $t (@scalar_tests) { # 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 1a"); + + local $@ = $local_out ? 'xxx' : undef; + eval q{ + local $@ = 'yyy' if $local_in; + my $x; + cast $x, $wiz; + }; + $check->("$desc at eval STRING 1a"); + + local $@ = $local_out ? 'xxx' : undef; + eval { + my $x; + local $@ = 'yyy' if $local_in; + cast $x, $wiz; + }; + $check->("$desc at eval BLOCK 1b"); + + local $@ = $local_out ? 'xxx' : undef; + eval q{ + my $x; + local $@ = 'yyy' if $local_in; + cast $x, $wiz; + }; + $check->("$desc at eval STRING 1b"); + + local $@ = $local_out ? 'xxx' : undef; + eval { + local $@ = 'yyy' if $local_in; + my $x; + my $y = \$x; + &cast($y, $wiz); + }; + $check->("$desc at eval BLOCK 2a"); + + 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 2a"); + + local $@ = $local_out ? 'xxx' : undef; + eval { + my $x; + my $y = \$x; + local $@ = 'yyy' if $local_in; + &cast($y, $wiz); + }; + $check->("$desc at eval BLOCK 2b"); + + local $@ = $local_out ? 'xxx' : undef; + eval q{ + my $x; + my $y = \$x; + local $@ = 'yyy' if $local_in; + &cast($y, $wiz); + }; + $check->("$desc at eval STRING 2b"); + + 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"; + } + } +} + my $wiz; eval { @@ -137,7 +269,7 @@ eval { cast $x, $wiz, sub { die "spinach" }; }; -like $@, expect('spinach', $0), 'die in free callback'; +like $@, expect('spinach', $0), 'die in sub in free callback'; eval { $wiz = wizard free => sub { die 'zucchini' }; @@ -213,30 +345,15 @@ eval q{BEGIN { like $@, expect('tomato', undef, "\nBEGIN.*"), 'die in BEGIN in eval triggers hints hash destructor'; -sub run_perl { - my $code = shift; - - my $SystemRoot = $ENV{SystemRoot}; - local %ENV; - $ENV{SystemRoot} = $SystemRoot if $^O eq 'MSWin32' and defined $SystemRoot; - - system { $^X } $^X, '-T', map("-I$_", @INC), '-e', $code; -} - -my $has_capture_tiny = do { local $@; eval 'use Capture::Tiny 0.08 (); 1' }; - -SKIP: -{ +SKIP: { my $count = 1; - skip 'Capture::Tiny 0.08 is not installed' => $count unless $has_capture_tiny; - - my $output = Capture::Tiny::capture_merged(sub { run_perl <<' CODE' }); -use Variable::Magic qw/wizard cast/; { BEGIN { $^H |= 0x020000; cast %^H, wizard free => sub { die q[cucumber] } } } + my ($stat, $out, $err) = capture_perl <<' CODE'; +use Variable::Magic qw; { BEGIN { $^H |= 0x020000; cast %^H, wizard free => sub { die q[cucumber] } } } CODE - skip 'Test code didn\'t run properly' => 1 unless defined $output; - like $output, expect('cucumber', '-e', "\nExecution(?s:.*)"), - 'die in free callback at compile time and not in eval string'; + skip CAPTURE_PERL_FAILED($out) => $count unless defined $stat; + like $err, expect('cucumber', '-e', "\nExecution(?s:.*)"), + 'die in free callback at compile time and not in eval string'; --$count; } @@ -246,20 +363,13 @@ SKIP: { my $count = 1; - skip 'No nice uvar magic for this perl' => $count unless VMG_UVAR; - skip 'Capture::Tiny 0.08 is not installed' => $count unless $has_capture_tiny; + skip 'No nice uvar magic for this perl' => $count unless VMG_UVAR; - my $output = Capture::Tiny::capture_merged(sub { run_perl <<' CODE' }); -use Variable::Magic qw/wizard cast/; BEGIN { cast %::, wizard fetch => sub { die q[salsify] } } hlagh() + my ($stat, $out, $err) = capture_perl <<' CODE'; +use Variable::Magic qw; BEGIN { cast %derp::, wizard fetch => sub { die q[raddish] } } derp::hlagh() CODE - skip 'Test code didn\'t run properly' => $count unless defined $output; - my $suffix = "\nExecution(?s:.*)"; - if ($] >= 5.011005) { - $suffix = "(?:\nsalsify at -e line \\d+.){12}" . $suffix; - } elsif ($] >= 5.011) { - $suffix = "(?:\nsalsify at -e line \\d+.){3}" . $suffix; - } - like $output, expect('salsify', '-e', $suffix), - 'die in free callback at compile time and not in eval string'; + skip CAPTURE_PERL_FAILED($out) => $count unless defined $stat; + like $err, expect('raddish', '-e', "\nExecution(?s:.*)"), + 'die in free callback at compile time and not in eval string'; --$count; }