]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blobdiff - t/17-ctl.t
Exception propagation fixes
[perl/modules/Variable-Magic.git] / t / 17-ctl.t
index e694a16b244f10c997d405a410d1816904d9818c..b25c5ee6812fac6154cc727027cb88706415f944 100644 (file)
-#!perl -T
+#!perl
 
 use strict;
 use warnings;
 
-use Test::More tests => 1;
+use Test::More tests => 14 + 1;
 
-use Variable::Magic qw/wizard cast getdata/;
+use Variable::Magic qw/wizard cast/;
+
+my $wiz;
+
+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/
+}
+
+eval {
+ $wiz = wizard data => sub { $_[1]->() };
+ my $x;
+ cast $x, $wiz, sub { die "carrot" };
+};
+
+like $@, expect('carrot', $0), 'die in data callback';
+
+eval {
+ $wiz = wizard data => sub { $_[1] },
+               set  => sub { $_[1]->(); () };
+ my $x;
+ cast $x, $wiz, sub { die "lettuce" };
+ $x = 5;
+};
+
+like $@, expect('lettuce', $0), 'die in set callback';
+
+my $res = eval {
+ $wiz = wizard data => sub { $_[1] },
+               len  => sub { $_[1]->(); () };
+ my @a = (1 .. 3);
+ cast @a, $wiz, sub { die "potato" };
+ @a;
+};
+
+like $@, expect('potato', $0), 'die in len callback';
+
+eval {
+ $wiz = wizard data => sub { $_[1] },
+               free => sub { $_[1]->(); () };
+ my $x;
+ cast $x, $wiz, sub { die "spinach" };
+};
+
+like $@, expect('spinach', $0), 'die in free callback';
+
+eval {
+ $wiz = wizard free => sub { die 'zucchini' };
+ $@ = "";
+ {
+  my $x;
+  cast $x, $wiz;
+ }
+ die 'not reached';
+};
+
+like $@, expect('zucchini', $0),
+                          'die in free callback in block in eval with $@ unset';
+
+eval {
+ $wiz = wizard free => sub { die 'eggplant' };
+ $@ = "vuvuzela";
+ {
+  my $x;
+  cast $x, $wiz;
+ }
+ die 'not reached again';
+};
+
+like $@, expect('eggplant', $0),
+                            'die in free callback in block in eval with $@ set';
+
+eval q{BEGIN {
+ $wiz = wizard free => sub { die 'onion' };
+ my $x;
+ cast $x, $wiz;;
+}};
+
+like $@, expect('onion', undef, "\nBEGIN.*"), 'die in free callback in BEGIN';
 
 # Inspired by B::Hooks::EndOfScope
-# This test is better be left at the beginning of the file, since problems
-# happen at UNITCHECK time
 
-my $wiz;
+eval q{BEGIN {
+ $wiz = wizard data => sub { $_[1]->() };
+ my $x;
+ cast $x, $wiz, sub { die "pumpkin" };
+}};
 
-BEGIN {
- $wiz = wizard data => sub { $_[1] }, free => sub { $_[1]->(); () };
+like $@, expect('pumpkin', undef, "\nBEGIN.*"), 'die in data callback in BEGIN';
+
+eval q{BEGIN {
+ $wiz = wizard data => sub { $_[1] },
+               free => sub { $_[1]->(); () };
  $^H |= 0x020000;
- cast %^H, $wiz, sub { die "harmless" };
+ cast %^H, $wiz, sub { die "macaroni" };
+}};
+
+like $@, expect('macaroni'), 'die in free callback at end of scope';
+
+eval q{BEGIN {
+ $wiz = wizard data => sub { $_[1] },
+               len  => sub { $_[1]->(); $_[2] },
+               free => sub { my $x = @{$_[0]}; () };
+ my @a = (1 .. 5);
+ cast @a, $wiz, sub { die "pepperoni" };
+}};
+
+like $@, expect('pepperoni', undef, "\nBEGIN.*"),'die in len callback in BEGIN';
+
+use lib 't/lib';
+
+
+eval "use Variable::Magic::TestScopeEnd";
+
+like $@,
+     expect('turnip', 't/lib/Variable/Magic/TestScopeEnd.pm', "\nBEGIN(?s:.*)"),
+     'die in BEGIN in require in eval string triggers hints hash destructor';
+
+eval q{BEGIN {
+ Variable::Magic::TestScopeEnd::hook {
+  pass 'in hints hash destructor 2';
+ };
+ die "tomato";
+}};
+
+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;
 }
 
-pass 'die in free callback in BEGIN didn\'t segfault';
+SKIP:
+{
+ skip 'Capture::Tiny 0.08 is not installed' => 1
+                                     unless eval "use Capture::Tiny 0.08 (); 1";
+ my $code = 'use Variable::Magic qw/wizard cast/; { BEGIN { $^H |= 0x020000; cast %^H, wizard free => sub { die q[cucumber] } } }';
+ my $output = Capture::Tiny::capture_merged(sub { run_perl $code });
+ skip 'Test code didn\'t run properly' => 1 unless defined $output;
+ like $output, expect('cucumber', '-e', "\nExecution(?s:.*)"),
+                                   'die at compile time and not in eval string';
+}