]> git.vpit.fr Git - perl/modules/indirect.git/blob - t/30-scope.t
Replace $] by "$]" in tests
[perl/modules/indirect.git] / t / 30-scope.t
1 #!perl
2
3 use strict;
4 use warnings;
5
6 my $tests;
7 BEGIN { $tests = 18 }
8
9 use Test::More tests => (1 + $tests + 1) + 2 + 3 + 3 + 3 + 5 + 4 + 5;
10
11 BEGIN { delete $ENV{PERL_INDIRECT_PM_DISABLE} }
12
13 use lib 't/lib';
14
15 my %wrong = map { $_ => 1 } 2, 3, 5, 7, 9, 10, 14, 15, 17, 18;
16
17 sub expect {
18  my ($pkg, $file) = @_;
19  $file = $file ? quotemeta $file : '\(eval \d+\)';
20  qr/^warn:Indirect call of method "new" on object "$pkg" at $file line \d+/;
21 }
22
23 {
24  my $code = do { local $/; <DATA> };
25  my (%res, $num, @left);
26
27  {
28   local $SIG{__WARN__} = sub {
29    ++$num;
30    my $w = join '', 'warn:', @_;
31    if ($w =~ /"P(\d+)"/ and not exists $res{$1}) {
32     $res{$1} = $w;
33    } else {
34     push @left, "[$num] $w";
35    }
36   };
37   eval "return; $code";
38  }
39  is $@, '', 'DATA compiled fine';
40
41  for (1 .. $tests) {
42   my $w = $res{$_};
43   if ($wrong{$_}) {
44    like $w, expect("P$_"), "$_ should warn";
45   } else {
46    is   $w, undef,         "$_ shouldn't warn";
47   }
48  }
49
50  is @left, 0, 'nothing left';
51  diag "Extraneous warnings:\n", @left if @left;
52 }
53
54 {
55  my @w;
56  {
57   local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
58   eval 'return; no indirect; my $x = new Foo';
59  }
60  is   $@,    '',            "eval 'no indirect; my \$x = new Foo'";
61  is   @w,    1,             'got one warning';
62  diag join "\n", 'All warnings:', @w if @w > 1;
63  like $w[0], expect('Foo'), 'correct warning';
64 }
65
66 {
67  my @w;
68  {
69   local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
70   {
71    no indirect;
72    eval 'return; my $x = new Bar';
73   }
74  }
75  is $@, '', "no indirect; eval 'my \$x = new Bar'";
76  if ("$]" < 5.009005) {
77   is   @w,   0,              'no warnings caught';
78   pass 'placeholder';
79  } else {
80   is   @w,    1,             'got one warning';
81   diag join "\n", 'All warnings:', @w if @w > 1;
82   like $w[0], expect('Bar'), 'correct warning';
83  }
84 }
85
86 SKIP: {
87  skip 'The pragma doesn\'t propagte into eval STRING before perl 5.10' => 2
88                                                              if "$]" < 5.009005;
89  my @w;
90  my $test = sub { eval 'return; new XYZ' };
91  {
92   local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
93   eval 'return; no indirect; BEGIN { $test->() }';
94  }
95  is   $@,    '',            'eval test doesn\'t croak prematurely';
96  is   @w,    0,             'eval did not throw a warning';
97  diag join "\n", 'All warnings:', @w if @w;
98 }
99
100 {
101  my @w;
102  {
103   local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
104   eval "return; no indirect; use indirect::TestRequired1; my \$x = new Foo;";
105  }
106  is   $@,    '',            'first require test doesn\'t croak prematurely';
107  is   @w,    1,             'first require threw only one warning';
108  diag join "\n", 'All warnings:', @w if @w > 1;
109  like $w[0], expect('Foo'), 'first require test catch errors in current scope';
110 }
111
112 {
113  my @w;
114  {
115   local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
116   eval "return; no indirect; use indirect::TestRequired2; my \$x = new Bar;";
117  }
118  is   $@, '', 'second require test doesn\'t croak prematurely';
119  @w = grep !/^warn:Attempt\s+to\s+free\s+unreferenced/, @w if "$]" <= 5.008003;
120  my $w = shift @w;
121  like $w, expect('Baz', 't/lib/indirect/TestRequired2.pm'),
122                                      'second require test caught error for Baz';
123  SKIP: {
124   skip 'The pragma doesn\'t propagte into eval STRING before perl 5.10' => 1
125                                                              if "$]" < 5.009005;
126   $w = shift @w;
127   like $w, expect('Blech'), 'second require test caught error for Blech';
128  }
129  $w = shift @w;
130  like       $w, expect('Bar'), 'second require test caught error for Bar';
131  is_deeply \@w, [ ],           'second require test doesn\'t have more errors';
132 }
133
134 {
135  local @main::new;
136  my (@err, @w);
137  sub cb3 { push @err, $_[0] };
138  local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
139  eval <<' TESTREQUIRED3';
140   {
141    package indirect::TestRequired3Z;
142    sub new { push @main::new, __PACKAGE__ }
143    no indirect hook => \&main::cb3;
144    use indirect::TestRequired3X;
145    use indirect::TestRequired3Y;
146    new indirect::TestRequired3Z;
147   }
148  TESTREQUIRED3
149  @w = grep !/^warn:Attempt\s+to\s+free\s+unreferenced/, @w if "$]" <= 5.008003;
150  is        $@,          '',
151            "pragma leak when reusing callback test doesn't croak prematurely";
152  is_deeply \@w,         [ ],
153            "pragma leak when reusing callback test doesn't warn";
154  is_deeply \@err,       [ map "indirect::TestRequired3$_", qw<X Z> ],
155            "pragma leak when reusing callback test caught the right errors";
156  is_deeply \@main::new, [ map "indirect::TestRequired3$_", qw<X Y Z> ],
157            "pragma leak when reusing callback test ran the three constructors";
158 }
159
160 {
161  eval <<' SNIP';
162   return;
163   no indirect ':fatal';
164   use indirect::Test1::il1 ();
165   use indirect::Test1::il2 ();
166  SNIP
167  is $@, '', 'RT #47902';
168 }
169
170 # This test may not fail for the old version when ran in taint mode
171 {
172  my $err = eval <<' SNIP';
173   use indirect::TestRequired4::a0;
174   indirect::TestRequired4::a0::error();
175  SNIP
176  like $err, qr/^Can't locate object method "new" via package "X"/, 'RT #50570';
177 }
178
179 # This test must be in the topmost scope
180 BEGIN { eval 'use indirect::TestRequired5::a0' }
181 my $err = indirect::TestRequired5::a0::error();
182 like $err, qr/^Can't locate object method "new" via package "X"/,
183            'identifying requires by their eval context pointer is not enough';
184
185 {
186  my @w;
187  no indirect hook => sub { push @w, indirect::msg(@_) };
188  use indirect::TestRequired6;
189  indirect::TestRequired6::bar();
190  is_deeply \@w, [ ], 'indirect syntax in sub';
191  @w = ();
192  indirect::TestRequired6::baz();
193  is_deeply \@w, [ ], 'indirect syntax in eval in sub';
194 }
195
196 __DATA__
197 my $a = new P1;
198
199 {
200  no indirect;
201  my $b = new P2;
202  {
203   my $c = new P3;
204  }
205  {
206   use indirect;
207   my $d = new P4;
208  }
209  my $e = new P5;
210 }
211
212 my $f = new P6;
213
214 no indirect;
215
216 my $g = new P7;
217
218 use indirect;
219
220 my $h = new P8;
221
222 {
223  no indirect;
224  eval { my $i = new P9 };
225 }
226
227 eval { no indirect; my $j = new P10 };
228
229 {
230  use indirect;
231  new P11 do { use indirect; new P12 };
232 }
233
234 {
235  use indirect;
236  new P13 do { no indirect; new P14 };
237 }
238
239 {
240  no indirect;
241  new P15 do { use indirect; new P16 };
242 }
243
244 {
245  no indirect;
246  new P17 do { no indirect; new P18 };
247 }