Re: B::Lint changes
[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 => 28;
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 {
71     my $res = runperl(
72         switches => ["-MB::Lint"],
73         prog     =>
74             'BEGIN{B::Lint->register_plugin(X=>[q[x]])};use O(qw[Lint x]);sub X::match{warn qq[X ok.\n]};dummy()',
75         stderr => 1,
76     );
77     like( $res, qr/X ok\./, 'Lint plugin' );
78 }
79
80 runlint 'implicit-read', 'for ( @ARGV ) { 1 }',
81     <<'RESULT', 'implicit-read in foreach';
82 Implicit use of $_ in foreach at -e line 1
83 RESULT
84
85 runlint 'implicit-read', '1 for @ARGV', '', 'implicit-read in foreach';
86
87 runlint 'dollar-underscore', '$_ = 1', <<'RESULT';
88 Use of $_ at -e line 1
89 RESULT
90
91 runlint 'dollar-underscore', 'foo( $_ ) for @A',      '';
92 runlint 'dollar-underscore', 'map { foo( $_ ) } @A',  '';
93 runlint 'dollar-underscore', 'grep { foo( $_ ) } @A', '';
94
95 runlint 'dollar-underscore', 'print',
96     <<'RESULT', 'dollar-underscore in print';
97 Use of $_ at -e line 1
98 RESULT
99
100 runlint 'private-names', 'sub A::_f{};A::_f()', <<'RESULT';
101 Illegal reference to private name '_f' at -e line 1
102 RESULT
103
104 runlint 'private-names', '$A::_x', <<'RESULT';
105 Illegal reference to private name '_x' at -e line 1
106 RESULT
107
108 runlint 'private-names', 'sub A::_f{};A->_f()', <<'RESULT',
109 Illegal reference to private method name '_f' at -e line 1
110 RESULT
111     'private-names (method)';
112
113 runlint 'undefined-subs', 'foo()', <<'RESULT';
114 Nonexistant subroutine 'foo' called at -e line 1
115 RESULT
116
117 runlint 'undefined-subs', 'foo();sub foo;', <<'RESULT';
118 Undefined subroutine 'foo' called at -e line 1
119 RESULT
120
121 runlint 'regexp-variables', 'print $&', <<'RESULT';
122 Use of regexp variable $& at -e line 1
123 RESULT
124
125 runlint 'regexp-variables', 's/./$&/', <<'RESULT';
126 Use of regexp variable $& at -e line 1
127 RESULT
128
129 runlint 'bare-subs', 'sub bare(){1};$x=bare', '';
130
131 runlint 'bare-subs', 'sub bare(){1}; $x=[bare=>0]; $x=$y{bare}', <<'RESULT';
132 Bare sub name 'bare' interpreted as string at -e line 1
133 Bare sub name 'bare' interpreted as string at -e line 1
134 RESULT