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