9 use Test::More tests => (1 + $tests + 1) + 3 + 3 + 3 + 5 + 4 + 5;
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 ($pkg, $file) = @_;
19 $file = $file ? quotemeta $file : '\(eval \d+\)';
20 qr/^warn:Indirect call of method "new" on object "$pkg" at $file line \d+/;
24 my $code = do { local $/; <DATA> };
25 my (%res, $num, @left);
28 local $SIG{__WARN__} = sub {
30 my $w = join '', 'warn:', @_;
31 if ($w =~ /"P(\d+)"/ and not exists $res{$1}) {
34 push @left, "[$num] $w";
39 is $@, '', 'DATA compiled fine';
44 like $w, expect("P$_"), "$_ should warn";
46 is $w, undef, "$_ shouldn't warn";
50 is @left, 0, 'nothing left';
51 diag "Extraneous warnings:\n", @left if @left;
57 local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
58 eval 'return; no indirect; my $x = new Foo';
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';
69 local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
72 eval 'return; my $x = new Bar';
75 is $@, '', "no indirect; eval 'my \$x = new Bar'";
77 is @w, 0, 'no warnings caught';
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';
89 local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
90 eval "return; no indirect; use indirect::TestRequired1; my \$x = new Foo;";
92 is $@, '', 'first require test doesn\'t croak prematurely';
93 is @w, 1, 'first require threw only one warning';
94 diag join "\n", 'All warnings:', @w if @w > 1;
95 like $w[0], expect('Foo'), 'first require test catch errors in current scope';
101 local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
102 eval "return; no indirect; use indirect::TestRequired2; my \$x = new Bar;";
104 is $@, '', 'second require test doesn\'t croak prematurely';
105 @w = grep !/^warn:Attempt\s+to\s+free\s+unreferenced/, @w if $] <= 5.008003;
107 like $w, expect('Baz', 't/lib/indirect/TestRequired2.pm'),
108 'second require test caught error for Baz';
110 skip 'The pragma doesn\'t propagte into eval STRING before perl 5.10' => 1
113 like $w, expect('Blech'), 'second require test caught error for Blech';
116 like $w, expect('Bar'), 'second require test caught error for Bar';
117 is_deeply \@w, [ ], 'second require test doesn\'t have more errors';
123 sub cb3 { push @err, $_[0] };
124 local $SIG{__WARN__} = sub { push @w, join '', 'warn:', @_ };
125 eval <<' TESTREQUIRED3';
127 package indirect::TestRequired3Z;
128 sub new { push @main::new, __PACKAGE__ }
129 no indirect hook => \&main::cb3;
130 use indirect::TestRequired3X;
131 use indirect::TestRequired3Y;
132 new indirect::TestRequired3Z;
135 @w = grep !/^warn:Attempt\s+to\s+free\s+unreferenced/, @w if $] <= 5.008003;
137 "pragma leak when reusing callback test doesn't croak prematurely";
139 "pragma leak when reusing callback test doesn't warn";
140 is_deeply \@err, [ map "indirect::TestRequired3$_", qw<X Z> ],
141 "pragma leak when reusing callback test caught the right errors";
142 is_deeply \@main::new, [ map "indirect::TestRequired3$_", qw<X Y Z> ],
143 "pragma leak when reusing callback test ran the three constructors";
149 no indirect ':fatal';
150 use indirect::Test1::il1 ();
151 use indirect::Test1::il2 ();
153 is $@, '', 'RT #47902';
156 # This test may not fail for the old version when ran in taint mode
158 my $err = eval <<' SNIP';
159 use indirect::TestRequired4::a0;
160 indirect::TestRequired4::a0::error();
162 like $err, qr/^Can't locate object method "new" via package "X"/, 'RT #50570';
165 # This test must be in the topmost scope
166 BEGIN { eval 'use indirect::TestRequired5::a0' }
167 my $err = indirect::TestRequired5::a0::error();
168 like $err, qr/^Can't locate object method "new" via package "X"/,
169 'identifying requires by their eval context pointer is not enough';
173 no indirect hook => sub { push @w, indirect::msg(@_) };
174 use indirect::TestRequired6;
175 indirect::TestRequired6::bar();
176 is_deeply \@w, [ ], 'indirect syntax in sub';
178 indirect::TestRequired6::baz();
179 is_deeply \@w, [ ], 'indirect syntax in eval in sub';
210 eval { my $i = new P9 };
213 eval { no indirect; my $j = new P10 };
217 new P11 do { use indirect; new P12 };
222 new P13 do { no indirect; new P14 };
227 new P15 do { use indirect; new P16 };
232 new P17 do { no indirect; new P18 };