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