]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blob - t/17-ctl.t
Exception propagation fixes
[perl/modules/Variable-Magic.git] / t / 17-ctl.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 14 + 1;
7
8 use Variable::Magic qw/wizard cast/;
9
10 my $wiz;
11
12 sub expect {
13  my ($name, $where, $suffix) = @_;
14  $where  = defined $where ? quotemeta $where : '\(eval \d+\)';
15  my $end = defined $suffix ? "$suffix\$" : '$';
16  qr/^\Q$name\E at $where line \d+\.$end/
17 }
18
19 eval {
20  $wiz = wizard data => sub { $_[1]->() };
21  my $x;
22  cast $x, $wiz, sub { die "carrot" };
23 };
24
25 like $@, expect('carrot', $0), 'die in data callback';
26
27 eval {
28  $wiz = wizard data => sub { $_[1] },
29                set  => sub { $_[1]->(); () };
30  my $x;
31  cast $x, $wiz, sub { die "lettuce" };
32  $x = 5;
33 };
34
35 like $@, expect('lettuce', $0), 'die in set callback';
36
37 my $res = eval {
38  $wiz = wizard data => sub { $_[1] },
39                len  => sub { $_[1]->(); () };
40  my @a = (1 .. 3);
41  cast @a, $wiz, sub { die "potato" };
42  @a;
43 };
44
45 like $@, expect('potato', $0), 'die in len callback';
46
47 eval {
48  $wiz = wizard data => sub { $_[1] },
49                free => sub { $_[1]->(); () };
50  my $x;
51  cast $x, $wiz, sub { die "spinach" };
52 };
53
54 like $@, expect('spinach', $0), 'die in free callback';
55
56 eval {
57  $wiz = wizard free => sub { die 'zucchini' };
58  $@ = "";
59  {
60   my $x;
61   cast $x, $wiz;
62  }
63  die 'not reached';
64 };
65
66 like $@, expect('zucchini', $0),
67                           'die in free callback in block in eval with $@ unset';
68
69 eval {
70  $wiz = wizard free => sub { die 'eggplant' };
71  $@ = "vuvuzela";
72  {
73   my $x;
74   cast $x, $wiz;
75  }
76  die 'not reached again';
77 };
78
79 like $@, expect('eggplant', $0),
80                             'die in free callback in block in eval with $@ set';
81
82 eval q{BEGIN {
83  $wiz = wizard free => sub { die 'onion' };
84  my $x;
85  cast $x, $wiz;;
86 }};
87
88 like $@, expect('onion', undef, "\nBEGIN.*"), 'die in free callback in BEGIN';
89
90 # Inspired by B::Hooks::EndOfScope
91
92 eval q{BEGIN {
93  $wiz = wizard data => sub { $_[1]->() };
94  my $x;
95  cast $x, $wiz, sub { die "pumpkin" };
96 }};
97
98 like $@, expect('pumpkin', undef, "\nBEGIN.*"), 'die in data callback in BEGIN';
99
100 eval q{BEGIN {
101  $wiz = wizard data => sub { $_[1] },
102                free => sub { $_[1]->(); () };
103  $^H |= 0x020000;
104  cast %^H, $wiz, sub { die "macaroni" };
105 }};
106
107 like $@, expect('macaroni'), 'die in free callback at end of scope';
108
109 eval q{BEGIN {
110  $wiz = wizard data => sub { $_[1] },
111                len  => sub { $_[1]->(); $_[2] },
112                free => sub { my $x = @{$_[0]}; () };
113  my @a = (1 .. 5);
114  cast @a, $wiz, sub { die "pepperoni" };
115 }};
116
117 like $@, expect('pepperoni', undef, "\nBEGIN.*"),'die in len callback in BEGIN';
118
119 use lib 't/lib';
120
121
122 eval "use Variable::Magic::TestScopeEnd";
123
124 like $@,
125      expect('turnip', 't/lib/Variable/Magic/TestScopeEnd.pm', "\nBEGIN(?s:.*)"),
126      'die in BEGIN in require in eval string triggers hints hash destructor';
127
128 eval q{BEGIN {
129  Variable::Magic::TestScopeEnd::hook {
130   pass 'in hints hash destructor 2';
131  };
132  die "tomato";
133 }};
134
135 like $@, expect('tomato', undef, "\nBEGIN.*"),
136                           'die in BEGIN in eval triggers hints hash destructor';
137
138 sub run_perl {
139  my $code = shift;
140
141  my $SystemRoot   = $ENV{SystemRoot};
142  local %ENV;
143  $ENV{SystemRoot} = $SystemRoot if $^O eq 'MSWin32' and defined $SystemRoot;
144
145  system { $^X } $^X, '-T', map("-I$_", @INC), '-e', $code;
146 }
147
148 SKIP:
149 {
150  skip 'Capture::Tiny 0.08 is not installed' => 1
151                                      unless eval "use Capture::Tiny 0.08 (); 1";
152  my $code = 'use Variable::Magic qw/wizard cast/; { BEGIN { $^H |= 0x020000; cast %^H, wizard free => sub { die q[cucumber] } } }';
153  my $output = Capture::Tiny::capture_merged(sub { run_perl $code });
154  skip 'Test code didn\'t run properly' => 1 unless defined $output;
155  like $output, expect('cucumber', '-e', "\nExecution(?s:.*)"),
156                                    'die at compile time and not in eval string';
157 }