Move Package::Constants from lib to ext
[p5sagit/p5-mst-13.2.git] / ext / B-Lint / t / lint.t
CommitLineData
94011a57 1#!./perl -w
2
3BEGIN {
cb122419 4 unshift @INC, 't';
5 push @INC, "../../t";
9cd8f857 6 require Config;
9b494a7e 7 if ( ( $Config::Config{'extensions'} !~ /\bB\b/ ) ) {
9cd8f857 8 print "1..0 # Skip -- Perl configured without B module\n";
9 exit 0;
10 }
5638aaac 11 require 'test.pl';
94011a57 12}
354c4f67 13use strict;
14use warnings;
94011a57 15
2adc4a42 16plan tests => 29;
94011a57 17
18# Runs a separate perl interpreter with the appropriate lint options
19# turned on
20sub runlint ($$$;$) {
9b494a7e 21 my ( $opts, $prog, $result, $testname ) = @_;
94011a57 22 my $res = runperl(
9b494a7e 23 switches => ["-MO=Lint,$opts"],
24 prog => $prog,
25 stderr => 1,
94011a57 26 );
27 $res =~ s/-e syntax OK\n$//;
354c4f67 28 local $::Level = $::Level + 1;
94011a57 29 is( $res, $result, $testname || $opts );
30}
31
9b494a7e 32runlint 'magic-diamond', 'while(<>){}', <<'RESULT';
33Use of <> at -e line 1
34RESULT
35
36runlint 'magic-diamond', 'while(<ARGV>){}', <<'RESULT';
37Use of <> at -e line 1
38RESULT
39
40runlint 'magic-diamond', 'while(<FOO>){}', <<'RESULT';
41RESULT
42
94011a57 43runlint 'context', '$foo = @bar', <<'RESULT';
44Implicit scalar context for array in scalar assignment at -e line 1
45RESULT
46
47runlint 'context', '$foo = length @bar', <<'RESULT';
48Implicit scalar context for array in length at -e line 1
49RESULT
50
2e9e4ed7 51runlint 'context', 'our @bar', '';
52
53runlint 'context', 'exists $BAR{BAZ}', '';
54
94011a57 55runlint 'implicit-read', '/foo/', <<'RESULT';
56Implicit match on $_ at -e line 1
57RESULT
58
3ee1325f 59runlint 'implicit-read', 'grep /foo/, ()', '';
60
61runlint 'implicit-read', 'grep { /foo/ } ()', '';
62
94011a57 63runlint 'implicit-write', 's/foo/bar/', <<'RESULT';
64Implicit substitution on $_ at -e line 1
65RESULT
66
9b494a7e 67runlint 'implicit-read', 'for ( @ARGV ) { 1 }',
68 <<'RESULT', 'implicit-read in foreach';
94011a57 69Implicit use of $_ in foreach at -e line 1
70RESULT
71
9b494a7e 72runlint 'implicit-read', '1 for @ARGV', '', 'implicit-read in foreach';
2e9e4ed7 73
9b494a7e 74runlint 'dollar-underscore', '$_ = 1', <<'RESULT';
94011a57 75Use of $_ at -e line 1
76RESULT
77
2adc4a42 78runlint 'dollar-underscore', 'sub foo {}; foo( $_ ) for @A', '';
79runlint 'dollar-underscore', 'sub foo {}; map { foo( $_ ) } @A', '';
80runlint 'dollar-underscore', 'sub foo {}; grep { foo( $_ ) } @A', '';
2e9e4ed7 81
9b494a7e 82runlint 'dollar-underscore', 'print',
83 <<'RESULT', 'dollar-underscore in print';
94011a57 84Use of $_ at -e line 1
85RESULT
86
9b494a7e 87runlint 'private-names', 'sub A::_f{};A::_f()', <<'RESULT';
88Illegal reference to private name '_f' at -e line 1
94011a57 89RESULT
90
9b494a7e 91runlint 'private-names', '$A::_x', <<'RESULT';
92Illegal reference to private name '_x' at -e line 1
94011a57 93RESULT
94
9b494a7e 95runlint 'private-names', 'sub A::_f{};A->_f()', <<'RESULT',
96Illegal reference to private method name '_f' at -e line 1
94011a57 97RESULT
bfecbe02 98 'private-names (method)';
94011a57 99
9b494a7e 100runlint 'undefined-subs', 'foo()', <<'RESULT';
101Nonexistant subroutine 'foo' called at -e line 1
102RESULT
103
104runlint 'undefined-subs', 'foo();sub foo;', <<'RESULT';
105Undefined subroutine 'foo' called at -e line 1
94011a57 106RESULT
107
9b494a7e 108runlint 'regexp-variables', 'print $&', <<'RESULT';
94011a57 109Use of regexp variable $& at -e line 1
110RESULT
111
9b494a7e 112runlint 'regexp-variables', 's/./$&/', <<'RESULT';
94011a57 113Use of regexp variable $& at -e line 1
114RESULT
94011a57 115
9b494a7e 116runlint 'bare-subs', 'sub bare(){1};$x=bare', '';
40f1df11 117
9b494a7e 118runlint 'bare-subs', 'sub bare(){1}; $x=[bare=>0]; $x=$y{bare}', <<'RESULT';
40f1df11 119Bare sub name 'bare' interpreted as string at -e line 1
120Bare sub name 'bare' interpreted as string at -e line 1
121RESULT
2adc4a42 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(
2adbc9b6 140 switches => [ '-It/pluglib', '-MO=Lint,none' ],
2adc4a42 141 prog => 1,
142 stderr => 1,
143 );
144 like( $res, qr/Module::Pluggable ok\./, 'Lint uses Module::Pluggable' );
145}