use strict;
use warnings;
-use Test::More tests => 50 * 6 + 2;
+my ($tests, $reports);
+BEGIN {
+ $tests = 82;
+ $reports = 94;
+}
+
+use Test::More tests => 3 * (4 * $tests + $reports) + 4;
+
+BEGIN { delete $ENV{PERL_INDIRECT_PM_DISABLE} }
my ($obj, $x);
our ($y, $bloop);
sub expect {
- my ($pkg) = @_;
- return qr/^warn:Indirect call of method "(?:new|meh|$pkg$pkg)" on object "(?:$pkg|newnew|\$(?:[xyz_\$]|(?:sploosh::)?sploosh|(?:main::)?bloop))"/
+ my ($expected) = @_;
+
+ die unless $expected;
+
+ map {
+ my ($meth, $obj, $file, $line) = @$_;
+ $meth = quotemeta $meth;
+ $obj = ($obj =~ /^\s*\{/) ? "a block" : "object \"\Q$obj\E\"";
+ $file = '\((?:re_)?eval \d+\)' unless defined $file;
+ $line = '\d+' unless defined $line;
+ qr/^Indirect call of method "$meth" on $obj at $file line $line/
+ } eval $expected;
+}
+
+my @warns;
+
+sub try {
+ my ($code) = @_;
+
+ @warns = ();
+ {
+ local $SIG{__WARN__} = sub { push @warns, @_ };
+ eval $code;
+ }
}
{
SKIP:
{
- skip "$_: $skip" => 6 if eval $skip;
+ my ($code, $expected) = split /^-{4,}$/m, $_, 2;
+ my @expected = expect($expected);
- local $SIG{__WARN__} = sub { die 'warn:' . join(' ', @_) };
+ skip "$_: $skip" => 3 * (4 + @expected) if eval $skip;
- eval "die qq{ok\\n}; $prefix; use indirect; $_";
- is($@, "ok\n", "use indirect: $_");
+ {
+ try "return; $prefix; use indirect; $code";
+ is $@, '', "use indirect: $code";
+ is @warns, 0, 'correct number of reports';
- eval "die qq{the code compiled but it shouldn't have\n}; $prefix; no indirect; $_";
- like($@, expect('Hlagh'), "no indirect: $_");
+ try "return; $prefix; no indirect; $code";
+ is $@, '', "no indirect: $code";
+ is @warns, @expected, 'correct number of reports';
+ for my $i (0 .. $#expected) {
+ like $warns[$i], $expected[$i], "report $i is correct";
+ }
+ }
- s/Hlagh/Dongs/g;
+ {
+ local $_ = $_;
+ s/Hlagh/Dongs/g;
+ my ($code, $expected) = split /^-{4,}$/m, $_, 2;
+ my @expected = expect($expected);
- eval "die qq{ok\\n}; $prefix; use indirect; $_";
- is($@, "ok\n", "use indirect, defined: $_");
+ try "return; $prefix; use indirect; $code";
+ is $@, '', "use indirect, defined: $code";
+ is @warns, 0, 'correct number of reports';
- eval "die qq{the code compiled but it shouldn't have\n}; $prefix; no indirect; $_";
- like($@, expect('Dongs'), "no indirect, defined: $_");
+ try "return; $prefix; no indirect; $code";
+ is $@, '', "no indirect, defined: $code";
+ is @warns, @expected, 'correct number of reports';
+ for my $i (0 .. $#expected) {
+ like $warns[$i], $expected[$i], "report $i is correct";
+ }
+ }
- s/\$/\$ \n\t /g;
- s/Dongs/Hlagh/g;
+SKIP:
+ {
+ skip 'No space tests on perl 5.11' => 4 + @expected
+ if "$]" >= 5.011 and "$]" < 5.012;
+ my $code = $code;
+ $code =~ s/\$/\$ \n\t /g;
- eval "die qq{ok\\n}; $prefix; use indirect; $_";
- is($@, "ok\n", "use indirect, spaces: $_");
+ try "return; $prefix; use indirect; $code";
+ is $@, '', "use indirect, spaces: $code";
+ is @warns, 0, 'correct number of reports';
- eval "die qq{the code compiled but it shouldn't have\n}; $prefix; no indirect; $_";
- like($@, expect('Hlagh'), "no indirect, spaces: $_");
+ try "return; $prefix; no indirect; $code";
+ is $@, '', "no indirect, spaces: $code";
+ is @warns, @expected, 'correct number of reports';
+ for my $i (0 .. $#expected) {
+ like $warns[$i], $expected[$i], "report $i is correct";
+ }
+ }
}
}
}
eval {
- my $warn;
- local $SIG{__WARN__} = sub { $warn = join ' ', @_ };
- eval "die qq{ok\n}; no indirect 'hlagh'; \$obj = new Hlagh1;";
- is($@, "ok\n", 'no indirect "hlagh" didn\'t croak');
- like($warn, qr/^Indirect\s+call\s+of\s+method\s+"new"\s+on\s+object\s+"Hlagh1"/, 'no indirect "hlagh" enables the pragma');
+ my @warns;
+ {
+ local $SIG{__WARN__} = sub { push @warns, @_ };
+ eval "return; no indirect 'hlagh'; \$obj = new Hlagh1;";
+ }
+ is $@, '', 'no indirect "hlagh" didn\'t croak';
+ is @warns, 1, 'only one warning';
+ my $warn = shift @warns;
+ like $warn, qr/^Indirect call of method "new" on object "Hlagh1"/,
+ 'no indirect "hlagh" enables the pragma';
+ is_deeply \@warns, [ ], 'nothing more';
}
__DATA__
$obj = new Hlagh;
+----
+[ 'new', 'Hlagh' ]
+####
+$obj = new Hlagh if 0;
+----
+[ 'new', 'Hlagh' ]
####
$obj = new Hlagh();
+----
+[ 'new', 'Hlagh' ]
####
$obj = new Hlagh(1);
+----
+[ 'new', 'Hlagh' ]
####
$obj = new Hlagh(1, 2);
+----
+[ 'new', 'Hlagh' ]
####
$obj = new Hlagh ;
+----
+[ 'new', 'Hlagh' ]
####
$obj = new Hlagh ( ) ;
+----
+[ 'new', 'Hlagh' ]
####
$obj = new Hlagh ( 1 ) ;
+----
+[ 'new', 'Hlagh' ]
####
$obj = new Hlagh ( 1 , 2 ) ;
+----
+[ 'new', 'Hlagh' ]
####
$obj = new
Hlagh
;
+----
+[ 'new', 'Hlagh' ]
####
$obj = new
Hlagh (
) ;
+----
+[ 'new', 'Hlagh' ]
####
$obj =
new
Hlagh ( 1
) ;
+----
+[ 'new', 'Hlagh' ]
####
$obj =
new
Hlagh
( 1 ,
2 ) ;
+----
+[ 'new', 'Hlagh' ]
####
$obj = new $x;
+----
+[ 'new', '$x' ]
####
$obj = new $x();
+----
+[ 'new', '$x' ]
####
$obj = new $x('foo');
+----
+[ 'new', '$x' ]
####
$obj = new $x qq{foo}, 1;
+----
+[ 'new', '$x' ]
####
$obj = new $x qr{foo\s+bar}, 1 .. 1;
+----
+[ 'new', '$x' ]
####
-$obj = new $x(qw/bar baz/);
+$obj = new $x(qw<bar baz>);
+----
+[ 'new', '$x' ]
####
$obj = new
$_;
+----
+[ 'new', '$_' ]
####
$obj = new
$_ ( );
+----
+[ 'new', '$_' ]
####
$obj = new $_ qr/foo/ ;
+----
+[ 'new', '$_' ]
####
$obj = new $_ qq(bar baz);
+----
+[ 'new', '$_' ]
####
meh $_;
+----
+[ 'meh', '$_' ]
####
meh $_ 1, 2;
+----
+[ 'meh', '$_' ]
####
meh $$;
+----
+[ 'meh', '$$' ]
####
meh $$ 1, 2;
+----
+[ 'meh', '$$' ]
####
meh $x;
+----
+[ 'meh', '$x' ]
####
meh $x 1, 2;
+----
+[ 'meh', '$x' ]
####
meh $x, 1, 2;
+----
+[ 'meh', '$x' ]
####
meh $y;
+----
+[ 'meh', '$y' ]
####
meh $y 1, 2;
+----
+[ 'meh', '$y' ]
####
meh $y, 1, 2;
-#### $] < 5.010 # use feature 'state'; state $z
+----
+[ 'meh', '$y' ]
+#### "$]" < 5.010 # use feature 'state'; state $z
meh $z;
-#### $] < 5.010 # use feature 'state'; state $z
+----
+[ 'meh', '$z' ]
+#### "$]" < 5.010 # use feature 'state'; state $z
meh $z 1, 2;
-#### $] < 5.010 # use feature 'state'; state $z
+----
+[ 'meh', '$z' ]
+#### "$]" < 5.010 # use feature 'state'; state $z
meh $z, 1, 2;
+----
+[ 'meh', '$z' ]
####
package sploosh;
our $sploosh;
meh $sploosh::sploosh;
+----
+[ 'meh', '$sploosh::sploosh' ]
####
package sploosh;
our $sploosh;
meh $sploosh;
+----
+[ 'meh', '$sploosh' ]
####
package sploosh;
meh $main::bloop;
+----
+[ 'meh', '$main::bloop' ]
####
package sploosh;
meh $bloop;
+----
+[ 'meh', '$bloop' ]
####
package ma;
meh $bloop;
+----
+[ 'meh', '$bloop' ]
####
package sploosh;
our $sploosh;
package main;
meh $sploosh::sploosh;
+----
+[ 'meh', '$sploosh::sploosh' ]
####
new Hlagh->wut;
+----
+[ 'new', 'Hlagh' ]
####
new Hlagh->wut();
+----
+[ 'new', 'Hlagh' ]
####
new Hlagh->wut, "Wut";
+----
+[ 'new', 'Hlagh' ]
####
$obj = HlaghHlagh Hlagh;
+----
+[ 'HlaghHlagh', 'Hlagh' ]
####
$obj = HlaghHlagh Hlagh; # HlaghHlagh Hlagh
+----
+[ 'HlaghHlagh', 'Hlagh' ]
####
$obj = new newnew;
+----
+[ 'new', 'newnew' ]
####
$obj = new newnew; # new newnew
+----
+[ 'new', 'newnew' ]
+####
+$obj = feh feh;
+----
+[ 'feh', 'feh' ]
+####
+$obj = feh feh; # feh feh
+----
+[ 'feh', 'feh' ]
####
new Hlagh (meh $x)
+----
+[ 'meh', '$x' ], [ 'new', 'Hlagh' ]
####
Hlagh->new(meh $x)
+----
+[ 'meh', '$x' ]
+####
+$obj = "apple ${\(new Hlagh)} pear"
+----
+[ 'new', 'Hlagh' ]
+####
+$obj = "apple @{[new Hlagh]} pear"
+----
+[ 'new', 'Hlagh' ]
+####
+$obj = "apple ${\(new $x)} pear"
+----
+[ 'new', '$x' ]
+####
+$obj = "apple @{[new $x]} pear"
+----
+[ 'new', '$x' ]
+####
+$obj = "apple ${\(new $y)} pear"
+----
+[ 'new', '$y' ]
+####
+$obj = "apple @{[new $y]} pear"
+----
+[ 'new', '$y' ]
+####
+$obj = "apple ${\(new $x qq|${\(stuff $y)}|)} pear"
+----
+[ 'stuff', '$y' ], [ 'new', '$x' ]
+####
+$obj = "apple @{[new $x qq|@{[stuff $y]}|]} pear"
+----
+[ 'stuff', '$y' ], [ 'new', '$x' ]
+#### # local $_ = "foo";
+s/foo/return; new Hlagh/e;
+----
+[ 'new', 'Hlagh' ]
+#### # local $_ = "bar";
+s/foo/return; new Hlagh/e;
+----
+[ 'new', 'Hlagh' ]
+#### # local $_ = "foo";
+s/foo/return; new $x/e;
+----
+[ 'new', '$x' ]
+#### # local $_ = "bar";
+s/foo/return; new $x/e;
+----
+[ 'new', '$x' ]
+#### # local $_ = "foo";
+s/foo/return; new $y/e;
+----
+[ 'new', '$y' ]
+#### # local $_ = "bar";
+s/foo/return; new $y/e;
+----
+[ 'new', '$y' ]
+####
+"foo" =~ /(?{new Hlagh})/;
+----
+[ 'new', 'Hlagh' ]
+####
+"foo" =~ /(?{new $x})/;
+----
+[ 'new', '$x' ]
+####
+"foo" =~ /(?{new $y})/;
+----
+[ 'new', '$y' ]
+####
+"foo" =~ /(??{new Hlagh})/;
+----
+[ 'new', 'Hlagh' ]
+####
+"foo" =~ /(??{new $x})/;
+----
+[ 'new', '$x' ]
+####
+"foo" =~ /(??{new $y})/;
+----
+[ 'new', '$y' ]
+####
+meh { };
+----
+[ 'meh', '{' ]
+####
+meh {
+ 1;
+};
+----
+[ 'meh', '{' ]
+####
+meh {
+ 1;
+ 1;
+};
+----
+[ 'meh', '{' ]
+####
+meh { new Hlagh; 1; };
+----
+[ 'new', 'Hlagh' ], [ 'meh', '{' ]
+####
+meh { feh $x; 1; };
+----
+[ 'feh', '$x' ], [ 'meh', '{' ]
+####
+meh { feh $x; use indirect; new Hlagh; 1; };
+----
+[ 'feh', '$x' ], [ 'meh', '{' ]
+####
+meh { feh $y; 1; };
+----
+[ 'feh', '$y' ], [ 'meh', '{' ]
+####
+meh { feh $x; 1; } new Hlagh, feh $y;
+----
+[ 'feh', '$x' ], [ 'new', 'Hlagh' ], [ 'feh', '$y' ], [ 'meh', '{' ]
+####
+$obj = "apple @{[new { feh $x; meh $y; 1 }]} pear"
+----
+[ 'feh', '$x' ], [ 'meh', '$y' ], [ 'new', '{' ]