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