Login
Merged /t and /lib from Chris' branch (4dec63a..a093439) into the trunk.
[gknop/Perl-Critic.git] / t / 02_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);
45my $var1 = 'foo';
46our $var2 = 'bar';
47local $SIG{HUP} \&handler;
48local $INC{$module} = $path;
49END_PERL
50
51$policy = 'Variables::ProhibitLocalVars';
52is( pcritique($policy, \$code), 0, $policy);
53
54#----------------------------------------------------------------
55
56$code = <<'END_PERL';
57our $var1 = 'foo';
58our (%var2, %var3) = 'foo';
59our (%VAR4, $var5) = ();
60$Package::foo;
61@Package::list = ('nuts');
62%Package::hash = ('nuts');
63$::foo = $bar;
64@::foo = ($bar);
65%::foo = ();
66use vars qw($FOO $BAR);
67END_PERL
68
69$policy = 'Variables::ProhibitPackageVars';
70is( pcritique($policy, \$code), 10, $policy);
71
72#----------------------------------------------------------------
73
74$code = <<'END_PERL';
75our $VAR1 = 'foo';
76our (%VAR2, %VAR3) = ();
77our $VERSION = '1.0';
78our @EXPORT = qw(some symbols);
79$Package::VERSION = '1.2';
80%Package::VAR = ('nuts');
81@Package::EXPORT = ();
82$::VERSION = '1.2';
83%::VAR = ('nuts');
84@::EXPORT = ();
85&Package::my_sub();
86&::my_sub();
87END_PERL
88
89$policy = 'Variables::ProhibitPackageVars';
90is( pcritique($policy, \$code), 0, $policy);
91
92#----------------------------------------------------------------
93
94$code = <<'END_PERL';
95my $var1 = 'foo';
96my %var2 = 'foo';
97my ($foo, $bar) = ();
98END_PERL
99
100$policy = 'Variables::ProhibitPackageVars';
101is( pcritique($policy, \$code), 0, $policy);
102
103#----------------------------------------------------------------
104
105$code = <<'END_PERL';
106$/ = undef;
107$| = 1;
108$> = 3;
109END_PERL
110
111$policy = 'Variables::ProhibitPunctuationVars';
112is( pcritique($policy, \$code), 3, $policy);
113
114#----------------------------------------------------------------
115
116$code = <<'END_PERL';
117$RS = undef;
118$INPUT_RECORD_SEPARATOR = "\n";
119$OUTPUT_AUTOFLUSH = 1;
120print $foo, $baz;
121END_PERL
122
123$policy = 'Variables::ProhibitPunctuationVars';
124is( pcritique($policy, \$code), 0, $policy);
125
126#----------------------------------------------------------------
127
128$code = <<'END_PERL';
129$string =~ /((foo)bar)/;
130$foobar = $1;
131$foo = $2;
132$3;
133$stat = stat(_);
134@list = @_;
135my $line = $_;
136END_PERL
137
138$policy = 'Variables::ProhibitPunctuationVars';
139is( pcritique($policy, \$code), 0, $policy);
140