Login
Rewrote SYNOPSIS section to look more like the typical Unix stuff.
[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;
10use Test::More tests => 8;
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';
59our $var1 = 'foo';
60our (%var2, %var3) = 'foo';
61our (%VAR4, $var5) = ();
62$Package::foo;
63@Package::list = ('nuts');
64%Package::hash = ('nuts');
65$::foo = $bar;
66@::foo = ($bar);
67%::foo = ();
68use vars qw($FOO $BAR);
69END_PERL
70
71$policy = 'Variables::ProhibitPackageVars';
72is( pcritique($policy, \$code), 10, $policy);
73
74#----------------------------------------------------------------
75
76$code = <<'END_PERL';
77our $VAR1 = 'foo';
78our (%VAR2, %VAR3) = ();
79our $VERSION = '1.0';
80our @EXPORT = qw(some symbols);
81$Package::VERSION = '1.2';
82%Package::VAR = ('nuts');
83@Package::EXPORT = ();
84$::VERSION = '1.2';
85%::VAR = ('nuts');
86@::EXPORT = ();
87&Package::my_sub();
88&::my_sub();
89END_PERL
90
91$policy = 'Variables::ProhibitPackageVars';
92is( pcritique($policy, \$code), 0, $policy);
93
94#----------------------------------------------------------------
95
96$code = <<'END_PERL';
97my $var1 = 'foo';
98my %var2 = 'foo';
99my ($foo, $bar) = ();
100END_PERL
101
102$policy = 'Variables::ProhibitPackageVars';
103is( pcritique($policy, \$code), 0, $policy);
104
105#----------------------------------------------------------------
106
107$code = <<'END_PERL';
108$/ = undef;
109$| = 1;
110$> = 3;
111END_PERL
112
113$policy = 'Variables::ProhibitPunctuationVars';
114is( pcritique($policy, \$code), 3, $policy);
115
116#----------------------------------------------------------------
117
118$code = <<'END_PERL';
119$RS = undef;
120$INPUT_RECORD_SEPARATOR = "\n";
121$OUTPUT_AUTOFLUSH = 1;
122print $foo, $baz;
123END_PERL
124
125$policy = 'Variables::ProhibitPunctuationVars';
126is( pcritique($policy, \$code), 0, $policy);
127
128#----------------------------------------------------------------
129
130$code = <<'END_PERL';
131$string =~ /((foo)bar)/;
132$foobar = $1;
133$foo = $2;
134$3;
135$stat = stat(_);
136@list = @_;
137my $line = $_;
138END_PERL
139
140$policy = 'Variables::ProhibitPunctuationVars';
141is( pcritique($policy, \$code), 0, $policy);
142