Re: perlfunc.pod/split; concerning trailing fields
[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$//;
32 is( $res, $result, $testname || $opts );
33}
34
9b494a7e 35runlint 'magic-diamond', 'while(<>){}', <<'RESULT';
36Use of <> at -e line 1
37RESULT
38
39runlint 'magic-diamond', 'while(<ARGV>){}', <<'RESULT';
40Use of <> at -e line 1
41RESULT
42
43runlint 'magic-diamond', 'while(<FOO>){}', <<'RESULT';
44RESULT
45
94011a57 46runlint 'context', '$foo = @bar', <<'RESULT';
47Implicit scalar context for array in scalar assignment at -e line 1
48RESULT
49
50runlint 'context', '$foo = length @bar', <<'RESULT';
51Implicit scalar context for array in length at -e line 1
52RESULT
53
2e9e4ed7 54runlint 'context', 'our @bar', '';
55
56runlint 'context', 'exists $BAR{BAZ}', '';
57
94011a57 58runlint 'implicit-read', '/foo/', <<'RESULT';
59Implicit match on $_ at -e line 1
60RESULT
61
3ee1325f 62runlint 'implicit-read', 'grep /foo/, ()', '';
63
64runlint 'implicit-read', 'grep { /foo/ } ()', '';
65
94011a57 66runlint 'implicit-write', 's/foo/bar/', <<'RESULT';
67Implicit substitution on $_ at -e line 1
68RESULT
69
9b494a7e 70runlint 'implicit-read', 'for ( @ARGV ) { 1 }',
71 <<'RESULT', 'implicit-read in foreach';
94011a57 72Implicit use of $_ in foreach at -e line 1
73RESULT
74
9b494a7e 75runlint 'implicit-read', '1 for @ARGV', '', 'implicit-read in foreach';
2e9e4ed7 76
9b494a7e 77runlint 'dollar-underscore', '$_ = 1', <<'RESULT';
94011a57 78Use of $_ at -e line 1
79RESULT
80
2adc4a42 81runlint 'dollar-underscore', 'sub foo {}; foo( $_ ) for @A', '';
82runlint 'dollar-underscore', 'sub foo {}; map { foo( $_ ) } @A', '';
83runlint 'dollar-underscore', 'sub foo {}; grep { foo( $_ ) } @A', '';
2e9e4ed7 84
9b494a7e 85runlint 'dollar-underscore', 'print',
86 <<'RESULT', 'dollar-underscore in print';
94011a57 87Use of $_ at -e line 1
88RESULT
89
9b494a7e 90runlint 'private-names', 'sub A::_f{};A::_f()', <<'RESULT';
91Illegal reference to private name '_f' at -e line 1
94011a57 92RESULT
93
9b494a7e 94runlint 'private-names', '$A::_x', <<'RESULT';
95Illegal reference to private name '_x' at -e line 1
94011a57 96RESULT
97
9b494a7e 98runlint 'private-names', 'sub A::_f{};A->_f()', <<'RESULT',
99Illegal reference to private method name '_f' at -e line 1
94011a57 100RESULT
bfecbe02 101 'private-names (method)';
94011a57 102
9b494a7e 103runlint 'undefined-subs', 'foo()', <<'RESULT';
104Nonexistant subroutine 'foo' called at -e line 1
105RESULT
106
107runlint 'undefined-subs', 'foo();sub foo;', <<'RESULT';
108Undefined subroutine 'foo' called at -e line 1
94011a57 109RESULT
110
9b494a7e 111runlint 'regexp-variables', 'print $&', <<'RESULT';
94011a57 112Use of regexp variable $& at -e line 1
113RESULT
114
9b494a7e 115runlint 'regexp-variables', 's/./$&/', <<'RESULT';
94011a57 116Use of regexp variable $& at -e line 1
117RESULT
94011a57 118
9b494a7e 119runlint 'bare-subs', 'sub bare(){1};$x=bare', '';
40f1df11 120
9b494a7e 121runlint 'bare-subs', 'sub bare(){1}; $x=[bare=>0]; $x=$y{bare}', <<'RESULT';
40f1df11 122Bare sub name 'bare' interpreted as string at -e line 1
123Bare sub name 'bare' interpreted as string at -e line 1
124RESULT
2adc4a42 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}