From: Vincent Pit Date: Tue, 14 Jul 2009 10:32:57 +0000 (+0200) Subject: Revamp t/21-bad.t X-Git-Tag: rt47866~1 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;ds=sidebyside;h=676ba859b2e1c5e7808bf1a12432e28997d660c9;hp=bdaded2ebf07853d6c3270293769ff933b78fcff;p=perl%2Fmodules%2Findirect.git Revamp t/21-bad.t --- diff --git a/t/21-bad.t b/t/21-bad.t index 1d2fab9..9f8f3ff 100644 --- a/t/21-bad.t +++ b/t/21-bad.t @@ -9,19 +9,41 @@ package main; use strict; use warnings; -use Test::More tests => 52 * 6 + 2; +my ($tests, $reports); +BEGIN { + $tests = 52; + $reports = 53; +} + +use Test::More tests => 3 * (4 * $tests + $reports) + 2; my ($obj, $x); our ($y, $bloop); sub expect { - my ($pkg) = @_; - qr/^warn:Indirect\s+call\s+of\s+method\s+ - "(?:new|meh|feh|$pkg$pkg)" - \s+on\s+object\s+ - "(?:$pkg|newnew|feh|\$(?:[xyz_\$]|(?:sploosh::)?sploosh|(?:main::)?bloop))" - \s+at\s+\(eval\s+\d+\)\s+line\s+\d+ - /x + my ($expected) = @_; + + die unless $expected; + + map { + my ($meth, $obj, $file, $line) = @$_; + $_ = quotemeta for $meth, $obj; + $file = '\(eval \d+\)' unless defined $file; + $line = '\d+' unless defined $line; + qr/^Indirect call of method "$meth" on object "$obj" at $file line $line/ + } eval $expected; +} + +my @warns; + +sub try { + my ($code) = @_; + + @warns = (); + { + local $SIG{__WARN__} = sub { push @warns, join(' ', @_) }; + eval $code; + } } { @@ -37,32 +59,57 @@ sub expect { 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; + { + 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"; + } + } } } } @@ -78,128 +125,232 @@ eval { __DATA__ $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 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/); +---- +[ '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; +---- +[ 'meh', '$y' ] #### $] < 5.010 # use feature 'state'; state $z meh $z; +---- +[ 'meh', '$z' ] #### $] < 5.010 # use feature 'state'; state $z meh $z 1, 2; +---- +[ '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' ]