Login
Add new policy Variables::ProhibitMatchVars
[gknop/Perl-Critic.git] / t / 20_policies_variables.t
CommitLineData
bf159007
JRT
1##################################################################
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
6##################################################################
7
8use strict;
9use warnings;
b672fd9e 10use Test::More tests => 10;
bf159007
JRT
11use Perl::Critic::Config;
12use Perl::Critic;
13
14# common P::C testing tools
15use lib qw(t/tlib);
16use PerlCriticTestUtils qw(pcritique);
17PerlCriticTestUtils::block_perlcriticrc();
18
19my $code ;
20my $policy;
21my %config;
22
23#----------------------------------------------------------------
24
25$code = <<'END_PERL';
26local $foo = $bar;
27local $/ = undef;
28local $| = 1;
29local ($foo, $bar) = ();
30local ($/) = undef;
31local ($RS, $>) = ();
32local ($foo, %SIG);
33END_PERL
34
35$policy = 'Variables::ProhibitLocalVars';
36is( pcritique($policy, \$code), 7, $policy);
37
38#----------------------------------------------------------------
39
40$code = <<'END_PERL';
41local ($RS);
42local $INPUT_RECORD_SEPARATOR;
43local $PROGRAM_NAME;
44local ($EVAL_ERROR, $OS_ERROR);
85b05bc4
JRT
45local $Other::Package::foo;
46local (@Other::Package::foo, $EVAL_ERROR);
bf159007
JRT
47my $var1 = 'foo';
48our $var2 = 'bar';
49local $SIG{HUP} \&handler;
50local $INC{$module} = $path;
51END_PERL
52
53$policy = 'Variables::ProhibitLocalVars';
54is( pcritique($policy, \$code), 0, $policy);
55
56#----------------------------------------------------------------
57
58$code = <<'END_PERL';
b672fd9e
CD
59use English;
60 use English qw($PREMATCH) ;
61use English qw($MATCH);
62use English qw($POSTMATCH);
63$`;
64$&;
65$';
66$PREMATCH;
67$MATCH;
68$POSTMATCH;
69END_PERL
70
71$policy = 'Variables::ProhibitMatchVars';
72is( pcritique($policy, \$code), 10, $policy);
73
74#----------------------------------------------------------------
75
76$code = <<'END_PERL';
77use English qw(-no_match_vars);
78use English qw($EVAL_ERROR);
79END_PERL
80
81$policy = 'Variables::ProhibitMatchVars';
82is( pcritique($policy, \$code), 0, $policy);
83
84#----------------------------------------------------------------
85
86$code = <<'END_PERL';
bf159007
JRT
87our $var1 = 'foo';
88our (%var2, %var3) = 'foo';
89our (%VAR4, $var5) = ();
7ce3b34e 90
bf159007
JRT
91$Package::foo;
92@Package::list = ('nuts');
93%Package::hash = ('nuts');
7ce3b34e 94
bf159007
JRT
95$::foo = $bar;
96@::foo = ($bar);
97%::foo = ();
7ce3b34e
JRT
98
99use vars qw($fooBar $baz);
100use vars qw($fooBar @EXPORT);
101use vars '$fooBar', "$baz";
102use vars '$fooBar', '@EXPORT';
103use vars ('$fooBar', '$baz');
104use vars ('$fooBar', '@EXPORT');
bf159007
JRT
105END_PERL
106
107$policy = 'Variables::ProhibitPackageVars';
7ce3b34e 108is( pcritique($policy, \$code), 15, $policy);
bf159007
JRT
109
110#----------------------------------------------------------------
111
112$code = <<'END_PERL';
113our $VAR1 = 'foo';
114our (%VAR2, %VAR3) = ();
115our $VERSION = '1.0';
116our @EXPORT = qw(some symbols);
7ce3b34e
JRT
117
118use vars qw($VERSION @EXPORT);
119use vars ('$VERSION, '@EXPORT');
120use vars '$VERSION, '@EXPORT';
121
122#local $Foo::bar;
123#local @This::that;
124#local %This::that;
125#local $This::that{ 'key' };
126#local $This::that[ 1 ];
127#local (@Baz::bar, %Baz::foo);
128
bf159007
JRT
129$Package::VERSION = '1.2';
130%Package::VAR = ('nuts');
131@Package::EXPORT = ();
7ce3b34e 132
bf159007
JRT
133$::VERSION = '1.2';
134%::VAR = ('nuts');
135@::EXPORT = ();
136&Package::my_sub();
137&::my_sub();
138END_PERL
139
140$policy = 'Variables::ProhibitPackageVars';
141is( pcritique($policy, \$code), 0, $policy);
142
143#----------------------------------------------------------------
144
145$code = <<'END_PERL';
146my $var1 = 'foo';
147my %var2 = 'foo';
148my ($foo, $bar) = ();
149END_PERL
150
151$policy = 'Variables::ProhibitPackageVars';
152is( pcritique($policy, \$code), 0, $policy);
153
154#----------------------------------------------------------------
155
156$code = <<'END_PERL';
157$/ = undef;
158$| = 1;
159$> = 3;
160END_PERL
161
162$policy = 'Variables::ProhibitPunctuationVars';
163is( pcritique($policy, \$code), 3, $policy);
164
165#----------------------------------------------------------------
166
167$code = <<'END_PERL';
168$RS = undef;
169$INPUT_RECORD_SEPARATOR = "\n";
170$OUTPUT_AUTOFLUSH = 1;
171print $foo, $baz;
172END_PERL
173
174$policy = 'Variables::ProhibitPunctuationVars';
175is( pcritique($policy, \$code), 0, $policy);
176
177#----------------------------------------------------------------
178
179$code = <<'END_PERL';
180$string =~ /((foo)bar)/;
181$foobar = $1;
182$foo = $2;
183$3;
184$stat = stat(_);
185@list = @_;
186my $line = $_;
187END_PERL
188
189$policy = 'Variables::ProhibitPunctuationVars';
190is( pcritique($policy, \$code), 0, $policy);
191