9 use Test::More tests => (1 + $tests + 1) + 2 + 3 + 3 + 3 + 5 + 4 + 5 + 4;
11 BEGIN { delete $ENV{PERL_INDIRECT_PM_DISABLE} }
15 my %wrong = map { $_ => 1 } 2, 3, 5, 7, 9, 10, 14, 15, 17, 18;
18 my ($obj, $file, $prefix) = @_;
19 $obj = quotemeta $obj;
20 $file = $file ? quotemeta $file : '\(eval \d+\)';
21 $prefix = defined $prefix ? quotemeta $prefix : 'warn:';
22 qr/^${prefix}Indirect call of method "new" on object "$obj" at $file line \d+/;
26 my $code = do { local $/; <DATA> };
27 my (%res, $num, @left);
30 local $SIG{__WARN__} = sub {
32 my $w = join '', 'warn:', @_;
33 if ($w =~ /"P(\d+)"/ and not exists $res{$1}) {
36 push @left, "[$num] $w";
41 is $@, '', 'DATA compiled fine';
46 like $w, expect("P$_"), "$_ should warn";
48 is $w, undef, "$_ shouldn't warn";
52 is @left, 0, 'nothing left';
53 diag "Extraneous warnings:\n", @left if @left;
59 local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
60 eval 'return; no indirect; my $x = new Foo';
62 is $@, '', "eval 'no indirect; my \$x = new Foo'";
63 is @w, 1, 'got one warning';
64 diag join "\n", 'All warnings:', @w if @w > 1;
65 like $w[0], expect('Foo'), 'correct warning';
71 local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
74 eval 'return; my $x = new Bar';
77 is $@, '', "no indirect; eval 'my \$x = new Bar'";
78 if ("$]" < 5.009_005) {
79 is @w, 0, 'no warnings caught';
82 is @w, 1, 'got one warning';
83 diag join "\n", 'All warnings:', @w if @w > 1;
84 like $w[0], expect('Bar'), 'correct warning';
89 skip 'The pragma doesn\'t propagte into eval STRING before perl 5.10' => 2
92 my $test = sub { eval 'return; new XYZ' };
94 local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
95 eval 'return; no indirect; BEGIN { $test->() }';
97 is $@, '', 'eval test doesn\'t croak prematurely';
98 is @w, 0, 'eval did not throw a warning';
99 diag join "\n", 'All warnings:', @w if @w;
105 local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
106 eval "return; no indirect; use indirect::TestRequired1; my \$x = new Foo;";
108 is $@, '', 'first require test doesn\'t croak prematurely';
109 is @w, 1, 'first require threw only one warning';
110 diag join "\n", 'All warnings:', @w if @w > 1;
111 like $w[0], expect('Foo'), 'first require test catch errors in current scope';
117 local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
118 eval "return; no indirect; use indirect::TestRequired2; my \$x = new Bar;";
120 is $@, '', 'second require test doesn\'t croak prematurely';
121 @w = grep !/^warn:Attempt\s+to\s+free\s+unreferenced/, @w if "$]" <= 5.008_003;
123 like $w, expect('Baz', 't/lib/indirect/TestRequired2.pm'),
124 'second require test caught error for Baz';
126 skip 'The pragma doesn\'t propagte into eval STRING before perl 5.10' => 1
129 like $w, expect('Blech'), 'second require test caught error for Blech';
132 like $w, expect('Bar'), 'second require test caught error for Bar';
133 is_deeply \@w, [ ], 'second require test doesn\'t have more errors';
139 sub cb3 { push @err, $_[0] };
140 local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
141 eval <<' TESTREQUIRED3';
143 package indirect::TestRequired3Z;
144 sub new { push @main::new, __PACKAGE__ }
145 no indirect hook => \&main::cb3;
146 use indirect::TestRequired3X;
147 use indirect::TestRequired3Y;
148 new indirect::TestRequired3Z;
151 @w = grep !/^warn:Attempt\s+to\s+free\s+unreferenced/, @w if "$]" <= 5.008_003;
153 "pragma leak when reusing callback test doesn't croak prematurely";
155 "pragma leak when reusing callback test doesn't warn";
156 is_deeply \@err, [ map "indirect::TestRequired3$_", qw<X Z> ],
157 "pragma leak when reusing callback test caught the right errors";
158 is_deeply \@main::new, [ map "indirect::TestRequired3$_", qw<X Y Z> ],
159 "pragma leak when reusing callback test ran the three constructors";
165 no indirect ':fatal';
166 use indirect::Test1::il1 ();
167 use indirect::Test1::il2 ();
169 is $@, '', 'RT #47902';
172 # This test may not fail for the old version when ran in taint mode
174 my $err = eval <<' SNIP';
175 use indirect::TestRequired4::a0;
176 indirect::TestRequired4::a0::error();
178 like $err, qr/^Can't locate object method "new" via package "X"/, 'RT #50570';
181 # This test must be in the topmost scope
182 BEGIN { eval 'use indirect::TestRequired5::a0' }
183 my $err = indirect::TestRequired5::a0::error();
184 like $err, qr/^Can't locate object method "new" via package "X"/,
185 'identifying requires by their eval context pointer is not enough';
189 no indirect hook => sub { push @w, indirect::msg(@_) };
190 use indirect::TestRequired6;
191 indirect::TestRequired6::bar();
192 is_deeply \@w, [ ], 'indirect syntax in sub';
194 indirect::TestRequired6::baz();
195 is_deeply \@w, [ ], 'indirect syntax in eval in sub';
200 eval { require indirect::Test2 };
201 is $@, '', 'direct call in string is not fooled by newlines';
206 eval { require indirect::Test3 };
207 like $@, expect('$x', 't/lib/indirect/Test3.pm', ''),
208 'indirect call in string is not fooled by newlines';
213 eval { require indirect::Test4 };
214 is $@, '', 'direct call in string is not fooled by more newlines';
219 eval { require indirect::Test5 };
220 is $@, '', 'direct call in sort in string is not fooled by newlines';
251 eval { my $i = new P9 };
254 eval { no indirect; my $j = new P10 };
258 new P11 do { use indirect; new P12 };
263 new P13 do { no indirect; new P14 };
268 new P15 do { use indirect; new P16 };
273 new P17 do { no indirect; new P18 };