Better line diagnostics - runlint's caller rather than runlint itself.
[p5sagit/p5-mst-13.2.git] / ext / B / t / lint.t
CommitLineData
94011a57 1#!./perl -w
2
3BEGIN {
9b494a7e 4 if ( $ENV{PERL_CORE} ) {
5 chdir('t') if -d 't';
6 @INC = ( '.', '../lib' );
7 }
8 else {
9 unshift @INC, 't';
10 push @INC, "../../t";
5638aaac 11 }
9cd8f857 12 require Config;
9b494a7e 13 if ( ( $Config::Config{'extensions'} !~ /\bB\b/ ) ) {
9cd8f857 14 print "1..0 # Skip -- Perl configured without B module\n";
15 exit 0;
16 }
5638aaac 17 require 'test.pl';
94011a57 18}
19
2adc4a42 20plan tests => 29;
94011a57 21
22# Runs a separate perl interpreter with the appropriate lint options
23# turned on
24sub runlint ($$$;$) {
9b494a7e 25 my ( $opts, $prog, $result, $testname ) = @_;
94011a57 26 my $res = runperl(
9b494a7e 27 switches => ["-MO=Lint,$opts"],
28 prog => $prog,
29 stderr => 1,
94011a57 30 );
31 $res =~ s/-e syntax OK\n$//;
b810cbf3 32 local $Level = $Level + 1;
94011a57 33 is( $res, $result, $testname || $opts );
34}
35
9b494a7e 36runlint 'magic-diamond', 'while(<>){}', <<'RESULT';
37Use of <> at -e line 1
38RESULT
39
40runlint 'magic-diamond', 'while(<ARGV>){}', <<'RESULT';
41Use of <> at -e line 1
42RESULT
43
44runlint 'magic-diamond', 'while(<FOO>){}', <<'RESULT';
45RESULT
46
94011a57 47runlint 'context', '$foo = @bar', <<'RESULT';
48Implicit scalar context for array in scalar assignment at -e line 1
49RESULT
50
51runlint 'context', '$foo = length @bar', <<'RESULT';
52Implicit scalar context for array in length at -e line 1
53RESULT
54
2e9e4ed7 55runlint 'context', 'our @bar', '';
56
57runlint 'context', 'exists $BAR{BAZ}', '';
58
94011a57 59runlint 'implicit-read', '/foo/', <<'RESULT';
60Implicit match on $_ at -e line 1
61RESULT
62
3ee1325f 63runlint 'implicit-read', 'grep /foo/, ()', '';
64
65runlint 'implicit-read', 'grep { /foo/ } ()', '';
66
94011a57 67runlint 'implicit-write', 's/foo/bar/', <<'RESULT';
68Implicit substitution on $_ at -e line 1
69RESULT
70
9b494a7e 71runlint 'implicit-read', 'for ( @ARGV ) { 1 }',
72 <<'RESULT', 'implicit-read in foreach';
94011a57 73Implicit use of $_ in foreach at -e line 1
74RESULT
75
9b494a7e 76runlint 'implicit-read', '1 for @ARGV', '', 'implicit-read in foreach';
2e9e4ed7 77
9b494a7e 78runlint 'dollar-underscore', '$_ = 1', <<'RESULT';
94011a57 79Use of $_ at -e line 1
80RESULT
81
2adc4a42 82runlint 'dollar-underscore', 'sub foo {}; foo( $_ ) for @A', '';
83runlint 'dollar-underscore', 'sub foo {}; map { foo( $_ ) } @A', '';
84runlint 'dollar-underscore', 'sub foo {}; grep { foo( $_ ) } @A', '';
2e9e4ed7 85
9b494a7e 86runlint 'dollar-underscore', 'print',
87 <<'RESULT', 'dollar-underscore in print';
94011a57 88Use of $_ at -e line 1
89RESULT
90
9b494a7e 91runlint 'private-names', 'sub A::_f{};A::_f()', <<'RESULT';
92Illegal reference to private name '_f' at -e line 1
94011a57 93RESULT
94
9b494a7e 95runlint 'private-names', '$A::_x', <<'RESULT';
96Illegal reference to private name '_x' at -e line 1
94011a57 97RESULT
98
9b494a7e 99runlint 'private-names', 'sub A::_f{};A->_f()', <<'RESULT',
100Illegal reference to private method name '_f' at -e line 1
94011a57 101RESULT
bfecbe02 102 'private-names (method)';
94011a57 103
9b494a7e 104runlint 'undefined-subs', 'foo()', <<'RESULT';
105Nonexistant subroutine 'foo' called at -e line 1
106RESULT
107
108runlint 'undefined-subs', 'foo();sub foo;', <<'RESULT';
109Undefined subroutine 'foo' called at -e line 1
94011a57 110RESULT
111
9b494a7e 112runlint 'regexp-variables', 'print $&', <<'RESULT';
94011a57 113Use of regexp variable $& at -e line 1
114RESULT
115
9b494a7e 116runlint 'regexp-variables', 's/./$&/', <<'RESULT';
94011a57 117Use of regexp variable $& at -e line 1
118RESULT
94011a57 119
9b494a7e 120runlint 'bare-subs', 'sub bare(){1};$x=bare', '';
40f1df11 121
9b494a7e 122runlint 'bare-subs', 'sub bare(){1}; $x=[bare=>0]; $x=$y{bare}', <<'RESULT';
40f1df11 123Bare sub name 'bare' interpreted as string at -e line 1
124Bare sub name 'bare' interpreted as string at -e line 1
125RESULT
2adc4a42 126
127{
128
129 # Check for backwards-compatible plugin support. This was where
130 # preloaded mdoules would register themselves with B::Lint.
131 my $res = runperl(
132 switches => ["-MB::Lint"],
133 prog =>
134 'BEGIN{B::Lint->register_plugin(X=>[q[x]])};use O(qw[Lint x]);sub X::match{warn qq[X ok.\n]};dummy()',
135 stderr => 1,
136 );
137 like( $res, qr/X ok\./, 'Lint legacy plugin' );
138}
139
140{
141
142 # Check for Module::Plugin support
143 my $res = runperl(
144 switches => [ '-I../ext/B/t/pluglib', '-MO=Lint,none' ],
145 prog => 1,
146 stderr => 1,
147 );
148 like( $res, qr/Module::Pluggable ok\./, 'Lint uses Module::Pluggable' );
149}