Login
Further cleanup after moving that policy to P-C-More.
[gknop/Perl-Critic.git] / t / 03_pragmas.t
CommitLineData
faa35de4
JRT
1#!perl
2
e68db767 3##############################################################################
27c1472f
JRT
4# $URL$
5# $Date$
6# $Author$
7# $Revision$
e68db767 8##############################################################################
27c1472f 9
df6dee2b 10use 5.006001;
59b05e08
JRT
11use strict;
12use warnings;
b9e520c1
JRT
13
14use Test::More (tests => 28);
3c5791fe 15use Perl::Critic::PolicyFactory (-test => 1);
59b05e08 16
bf159007 17# common P::C testing tools
1c2cd49f 18use Perl::Critic::TestUtils qw(critique);
b185fa17
ES
19
20#-----------------------------------------------------------------------------
21
be4331b3 22our $VERSION = '1.090';
b185fa17
ES
23
24#-----------------------------------------------------------------------------
25
1c2cd49f 26Perl::Critic::TestUtils::block_perlcriticrc();
59b05e08 27
dff08b70 28# Configure Critic not to load certain policies. This
d50d85bf 29# just makes it a little easier to create test cases
22ae9686 30my $profile = {
57aab077
JRT
31 '-CodeLayout::RequireTidyCode' => {},
32 '-Documentation::PodSpelling' => {},
33 '-ErrorHandling::RequireCheckingReturnValueOfEval' => {},
34 '-Miscellanea::RequireRcsKeywords' => {},
35 '-ValuesAndExpressions::ProhibitMagicNumbers' => {},
dff08b70
JRT
36};
37
38my $code = undef;
39
e68db767 40#-----------------------------------------------------------------------------
59b05e08
JRT
41
42$code = <<'END_PERL';
43package FOO;
44use strict;
45use warnings;
46our $VERSION = 1.0;
47
48require 'some_library.pl'; ## no critic
49print $crap if $condition; ## no critic
aa353719
JRT
50
511;
59b05e08
JRT
52END_PERL
53
d6dc5ff8
ES
54is(
55 critique(
56 \$code,
57 {-profile => $profile, -severity => 1, -theme => 'core'}
58 ),
59 0,
60 'inline no-critic'
61);
59b05e08 62
e68db767 63#-----------------------------------------------------------------------------
59b05e08
JRT
64
65$code = <<'END_PERL';
66package FOO;
67use strict;
68use warnings;
69our $VERSION = 1.0;
70
71$foo = $bar;
72
73## no critic
74
75require 'some_library.pl';
76print $crap if $condition;
77
78## use critic
79
80$baz = $nuts;
aa353719 811;
59b05e08
JRT
82END_PERL
83
d6dc5ff8
ES
84is(
85 critique(
86 \$code,
87 {-profile => $profile, -severity => 1, -theme => 'core'},
88 ),
89 0,
90 'region no-critic',
91);
59b05e08 92
e68db767 93#-----------------------------------------------------------------------------
59b05e08
JRT
94
95$code = <<'END_PERL';
96package FOO;
97use strict;
98use warnings;
99our $VERSION = 1.0;
100
101for my $foo (@list) {
102 ## no critic
103 $long_int = 12345678;
104 $oct_num = 033;
105}
106
107my $noisy = '!';
aa353719
JRT
108
1091;
59b05e08
JRT
110END_PERL
111
d6dc5ff8
ES
112is(
113 critique(
114 \$code,
115 {-profile => $profile, -severity => 1, -theme => 'core'},
116 ),
117 1,
118 'scoped no-critic',
119);
59b05e08 120
e68db767 121#-----------------------------------------------------------------------------
59b05e08
JRT
122
123$code = <<'END_PERL';
124package FOO;
125use strict;
126use warnings;
127our $VERSION = 1.0;
128
209eb6db
JRT
129{
130 ## no critic
131 $long_int = 12345678;
132 $oct_num = 033;
133}
134
135my $noisy = '!';
136
1371;
138END_PERL
139
d6dc5ff8
ES
140is(
141 critique(
142 \$code,
143 {-profile => $profile, -severity => 1, -theme => 'core'},
144 ),
145 1,
146 'scoped no-critic',
147);
209eb6db 148
e68db767 149#-----------------------------------------------------------------------------
209eb6db
JRT
150
151$code = <<'END_PERL';
152package FOO;
153use strict;
154use warnings;
155our $VERSION = 1.0;
156
59b05e08
JRT
157## no critic
158for my $foo (@list) {
159 $long_int = 12345678;
160 $oct_num = 033;
161}
162
163## use critic
164my $noisy = '!';
165
aa353719 1661;
59b05e08
JRT
167END_PERL
168
d6dc5ff8
ES
169is(
170 critique(
171 \$code,
172 {-profile => $profile, -severity => 1, -theme => 'core'},
173 ),
174 1,
175 'region no-critic across a scope',
176);
59b05e08 177
e68db767 178#-----------------------------------------------------------------------------
59b05e08
JRT
179
180$code = <<'END_PERL';
181package FOO;
182use strict;
183use warnings;
184our $VERSION = 1.0;
185
186for my $foo (@list) {
187 ## no critic
188 $long_int = 12345678;
189 $oct_num = 033;
190 ## use critic
191}
192
193my $noisy = '!';
194my $empty = '';
195
aa353719 1961;
59b05e08
JRT
197END_PERL
198
d6dc5ff8
ES
199is(
200 critique(
201 \$code,
202 {-profile => $profile, -severity => 1, -theme => 'core'},
203 ),
204 2,
205 'scoped region no-critic',
206);
59b05e08 207
e68db767 208#-----------------------------------------------------------------------------
59b05e08
JRT
209
210$code = <<'END_PERL';
211package FOO;
212use strict;
213use warnings;
214our $VERSION = 1.0;
215
216## no critic
217for my $foo (@list) {
218 $long_int = 12345678;
219 $oct_num = 033;
220}
221
222my $noisy = '!';
223my $empty = '';
aa353719
JRT
224
225#No final '1;'
59b05e08
JRT
226END_PERL
227
d6dc5ff8
ES
228is(
229 critique(
230 \$code,
231 {-profile => $profile, -severity => 1, -theme => 'core'},
232 ),
233 0,
234 'unterminated no-critic across a scope',
235);
59b05e08 236
e68db767 237#-----------------------------------------------------------------------------
59b05e08
JRT
238
239$code = <<'END_PERL';
240package FOO;
241use strict;
242use warnings;
243our $VERSION = 1.0;
244
245$long_int = 12345678; ## no critic
246$oct_num = 033; ## no critic
247my $noisy = '!'; ## no critic
248my $empty = ''; ## no critic
249my $empty = ''; ## use critic
aa353719
JRT
250
2511;
59b05e08
JRT
252END_PERL
253
d6dc5ff8
ES
254is(
255 critique(
256 \$code,
257 {-profile => $profile, -severity => 1, -theme => 'core'},
258 ),
259 1,
260 'inline use-critic',
261);
59b05e08 262
e68db767 263#-----------------------------------------------------------------------------
59b05e08
JRT
264
265$code = <<'END_PERL';
266package FOO;
267use strict;
268use warnings;
269our $VERSION = 1.0;
270
271$long_int = 12345678; ## no critic
272$oct_num = 033; ## no critic
273my $noisy = '!'; ## no critic
274my $empty = ''; ## no critic
275
276$long_int = 12345678;
277$oct_num = 033;
278my $noisy = '!';
279my $empty = '';
aa353719
JRT
280
281#No final '1;'
59b05e08
JRT
282END_PERL
283
d6dc5ff8
ES
284is(
285 critique(
286 \$code,
287 {-profile => $profile, -severity => 1, -theme => 'core'},
288 ),
289 5,
290 'inline no-critic',
291);
59b05e08 292
e68db767 293#-----------------------------------------------------------------------------
59b05e08
JRT
294
295$code = <<'END_PERL';
296package FOO;
297use strict;
298use warnings;
299our $VERSION = 1.0;
300
301$long_int = 12345678; ## no critic
302$oct_num = 033; ## no critic
303my $noisy = '!'; ## no critic
304my $empty = ''; ## no critic
305
aa353719 306## no critic
59b05e08
JRT
307$long_int = 12345678;
308$oct_num = 033;
309my $noisy = '!';
310my $empty = '';
aa353719
JRT
311
312#No final '1;'
59b05e08
JRT
313END_PERL
314
d6dc5ff8
ES
315is(
316 critique(
317 \$code,
318 {
319 -profile => $profile,
320 -severity => 1,
321 -theme => 'core',
322 -force => 1,
323 }
324 ),
325 9,
326 'force option',
327);
59b05e08 328
e68db767 329#-----------------------------------------------------------------------------
59b05e08
JRT
330
331$code = <<'END_PERL';
332package FOO;
333use strict;
334use warnings;
335our $VERSION = 1.0;
336
337for my $foo (@list) {
aa353719 338 ## no critic
59b05e08
JRT
339 $long_int = 12345678;
340 $oct_num = 033;
341}
342
aa353719
JRT
343my $noisy = '!'; ## no critic
344my $empty = ''; ## no critic
345
3461;
59b05e08
JRT
347END_PERL
348
d6dc5ff8
ES
349is(
350 critique(
351 \$code,
352 {
353 -profile => $profile,
354 -severity => 1,
355 -theme => 'core',
356 -force => 1,
357 }
358 ),
359 4,
360 'force option',
361);
59b05e08 362
e68db767 363#-----------------------------------------------------------------------------
59b05e08
JRT
364
365$code = <<'END_PERL';
366package FOO;
367use strict;
368use warnings;
369our $VERSION = 1.0;
370
371for my $foo (@list) {
aa353719 372 ## no critic
59b05e08
JRT
373 $long_int = 12345678;
374 $oct_num = 033;
375}
376
aa353719 377## no critic
59b05e08
JRT
378my $noisy = '!';
379my $empty = '';
aa353719
JRT
380
381#No final '1;'
59b05e08
JRT
382END_PERL
383
d6dc5ff8
ES
384is(
385 critique(
386 \$code,
387 {
388 -profile => $profile,
389 -severity => 1,
390 -theme => 'core',
391 -force => 1,
392 }
393 ),
394 5,
395 'force option',
396);
209eb6db 397
e68db767 398#-----------------------------------------------------------------------------
209eb6db
JRT
399# Check that '## no critic' on the top of a block doesn't extend
400# to all code within the block. See RT bug #15295
401
402$code = <<'END_PERL';
403package FOO;
404use strict;
405use warnings;
406our $VERSION = 1.0;
407
408for ($i;$i++;$i<$j) { ## no critic
409 my $long_int = 12345678;
410 my $oct_num = 033;
411}
412
413unless ( $condition1
414 && $condition2 ) { ## no critic
415 my $noisy = '!';
416 my $empty = '';
417}
418
4191;
420END_PERL
421
d6dc5ff8
ES
422is(
423 critique(
424 \$code,
425 {-profile => $profile, -severity => 1, -theme => 'core'},
426 ),
427 4,
428 'RT bug 15295',
429);
209eb6db 430
e68db767 431#-----------------------------------------------------------------------------
209eb6db
JRT
432# Check that '## no critic' on the top of a block doesn't extend
433# to all code within the block. See RT bug #15295
434
435$code = <<'END_PERL';
436package FOO;
437use strict;
438use warnings;
439our $VERSION = 1.0;
440
441for ($i; $i++; $i<$j) { ## no critic
442 my $long_int = 12345678;
443 my $oct_num = 033;
444}
445
446#Between blocks now
447$Global::Variable = "foo"; #Package var; double-quotes
448
449unless ( $condition1
450 && $condition2 ) { ## no critic
451 my $noisy = '!';
452 my $empty = '';
453}
454
4551;
456END_PERL
457
d6dc5ff8
ES
458is(
459 critique(
460 \$code,
461 {-profile => $profile, -severity => 1, -theme => 'core'}
462 ),
463 6,
464 'RT bug 15295',
465);
c70a9ff6 466
e68db767 467#-----------------------------------------------------------------------------
c70a9ff6
JRT
468
469$code = <<'END_PERL';
470package FOO;
471use strict;
472use warnings;
473our $VERSION = 1.0;
474
5c1bf20d
JRT
475sub grep { ## no critic;
476 return $foo;
477}
478
479sub grep { return $foo; } ## no critic
4801;
481END_PERL
482
d6dc5ff8
ES
483is(
484 critique(
485 \$code,
486 {-profile => $profile, -severity => 1, -theme => 'core'},
487 ),
488 0,
489 'no-critic on sub name',
490);
5c1bf20d 491
e68db767 492#-----------------------------------------------------------------------------
5c1bf20d
JRT
493
494$code = <<'END_PERL';
495package FOO;
496use strict;
497use warnings;
498our $VERSION = 1.0;
499
500sub grep { ## no critic;
501 return undef; #Should find this!
502}
503
5041;
505END_PERL
506
d6dc5ff8
ES
507is(
508 critique(
509 \$code,
510 {-profile => $profile, -severity =>1, -theme => 'core'}
511 ),
512 1,
513 'no-critic on sub name',
514);
5c1bf20d 515
e68db767 516#-----------------------------------------------------------------------------
5c1bf20d
JRT
517
518$code = <<'END_PERL';
519package FOO;
520use strict;
521use warnings;
522our $VERSION = 1.0;
523
c48094aa 524## no critic (NoisyQuotes)
c70a9ff6
JRT
525my $noisy = '!';
526my $empty = '';
527eval $string;
528
5291;
530END_PERL
531
d6dc5ff8
ES
532is(
533 critique(
534 \$code,
535 {-profile => $profile, -severity => 1, -theme => 'core'}
536 ),
537 2,
538 'per-policy no-critic',
539);
c70a9ff6 540
e68db767 541#-----------------------------------------------------------------------------
c70a9ff6
JRT
542
543$code = <<'END_PERL';
544package FOO;
545use strict;
546use warnings;
547our $VERSION = 1.0;
548
c48094aa 549## no critic (ValuesAndExpressions)
c70a9ff6
JRT
550my $noisy = '!';
551my $empty = '';
552eval $string;
553
5541;
555END_PERL
556
d6dc5ff8
ES
557is(
558 critique(
559 \$code,
560 {-profile => $profile, -severity => 1, -theme => 'core'}
561 ),
562 1,
563 'per-policy no-critic',
564);
c70a9ff6 565
e68db767 566#-----------------------------------------------------------------------------
c70a9ff6
JRT
567
568$code = <<'END_PERL';
569package FOO;
570use strict;
571use warnings;
572our $VERSION = 1.0;
573
c48094aa 574## no critic (Noisy, Empty)
c70a9ff6
JRT
575my $noisy = '!';
576my $empty = '';
577eval $string;
578
5791;
580END_PERL
581
d6dc5ff8
ES
582is(
583 critique(
584 \$code,
585 {-profile => $profile, -severity => 1, -theme => 'core'}
586 ),
587 1,
588 'per-policy no-critic',
589);
c70a9ff6 590
e68db767 591#-----------------------------------------------------------------------------
c70a9ff6
JRT
592
593$code = <<'END_PERL';
594package FOO;
595use strict;
596use warnings;
597our $VERSION = 1.0;
598
c48094aa 599## no critic (NOISY, EMPTY, EVAL)
c70a9ff6
JRT
600my $noisy = '!';
601my $empty = '';
602eval $string;
603
6041;
605END_PERL
606
d6dc5ff8
ES
607is(
608 critique(
609 \$code,
610 {-profile => $profile, -severity => 1, -theme => 'core'}
611 ),
612 0,
613 'per-policy no-critic',
614);
c70a9ff6 615
e68db767 616#-----------------------------------------------------------------------------
c70a9ff6
JRT
617
618$code = <<'END_PERL';
619package FOO;
620use strict;
621use warnings;
622our $VERSION = 1.0;
623
c48094aa 624## no critic (Noisy, Empty, Eval)
c70a9ff6
JRT
625my $noisy = '!';
626my $empty = '';
627eval $string;
628
629## use critic
630my $noisy = '!';
631my $empty = '';
632eval $string;
633
6341;
635END_PERL
636
d6dc5ff8
ES
637is(
638 critique(
639 \$code,
640 {-profile => $profile, -severity => 1, -theme => 'core'}
641 ),
642 3,
643 'per-policy no-critic',
644);
c70a9ff6 645
e68db767 646#-----------------------------------------------------------------------------
c70a9ff6
JRT
647
648$code = <<'END_PERL';
649package FOO;
650use strict;
651use warnings;
652our $VERSION = 1.0;
653
c48094aa 654## no critic (Critic::Policy)
c70a9ff6
JRT
655my $noisy = '!';
656my $empty = '';
657eval $string;
658
6591;
660END_PERL
661
d6dc5ff8
ES
662is(
663 critique(
664 \$code,
665 {-profile => $profile, -severity => 1, -theme => 'core'}
666 ),
667 0,
668 'per-policy no-critic',
669);
c70a9ff6 670
e68db767 671#-----------------------------------------------------------------------------
c70a9ff6
JRT
672
673$code = <<'END_PERL';
674package FOO;
675use strict;
676use warnings;
677our $VERSION = 1.0;
678
c48094aa 679## no critic (Foo::Bar, Baz, Boom)
c70a9ff6
JRT
680my $noisy = '!';
681my $empty = '';
682eval $string;
683
6841;
685END_PERL
686
d6dc5ff8
ES
687is(
688 critique(
689 \$code,
690 {-profile => $profile, -severity => 1, -theme => 'core'}
691 ),
692 3,
693 'per-policy no-critic',
694);
55ae7242 695
e68db767 696#-----------------------------------------------------------------------------
55ae7242
JRT
697
698$code = <<'END_PERL';
699package FOO;
700use strict;
701use warnings;
702our $VERSION = 1.0;
703
c48094aa 704## no critic (Noisy)
55ae7242
JRT
705my $noisy = '!'; #Should not find this
706my $empty = ''; #Should find this
707
708sub foo {
709
c48094aa 710 ## no critic (Empty)
55ae7242
JRT
711 my $nosiy = '!'; #Should not find this
712 my $empty = ''; #Should not find this
713 ## use critic;
714
715 return 1;
716}
717
718my $nosiy = '!'; #Should not find this
719my $empty = ''; #Should find this
720
7211;
722END_PERL
723
d6dc5ff8
ES
724is(
725 critique(
726 \$code,
727 {-profile => $profile, -severity => 1, -theme => 'core'}
728 ),
729 2,
730 'per-policy no-critic',
731);
1bef6e21 732
e68db767 733#-----------------------------------------------------------------------------
c0137eab
JRT
734$code = <<'END_PERL';
735package FOO;
736
737use strict;
738use warnings;
739our $VERSION = 1.0;
740
c296c678 741# with parentheses
c0137eab
JRT
742my $noisy = '!'; ##no critic (NoisyQuotes)
743barf() unless $$ eq ''; ##no critic (Postfix,Empty,Punctuation)
744barf() unless $$ eq ''; ##no critic (Postfix , Empty , Punctuation)
745barf() unless $$ eq ''; ##no critic (Postfix Empty Punctuation)
746
3cab0bc9 747# qw() style
c0137eab
JRT
748my $noisy = '!'; ##no critic qw(NoisyQuotes);
749barf() unless $$ eq ''; ##no critic qw(Postfix,Empty,Punctuation)
750barf() unless $$ eq ''; ##no critic qw(Postfix , Empty , Punctuation)
751barf() unless $$ eq ''; ##no critic qw(Postfix Empty Punctuation)
752
c296c678 753# no parentheses
3cab0bc9
JRT
754my $noisy = '!'; ##no critic NoisyQuotes;
755barf() unless $$ eq ''; ##no critic Postfix,Empty,Punctuation;
756barf() unless $$ eq ''; ##no critic Postfix , Empty , Punctuation;
757barf() unless $$ eq ''; ##no critic Postfix Empty Punctuation;
758
c0137eab
JRT
7591;
760END_PERL
761
d6dc5ff8
ES
762is(
763 critique(
764 \$code,
765 {-profile => $profile, -severity => 1, -theme => 'core'},
766 ),
767 0,
768 'no critic: syntaxes',
769);
c0137eab 770
e68db767 771#-----------------------------------------------------------------------------
1bef6e21
JRT
772# Most policies apply to a particular type of PPI::Element and usually
773# only return one Violation at a time. But the next three cases
774# involve policies that apply to the whole document and can return
775# multiple violations at a time. These tests make sure that the 'no
776# critic' pragmas are effective with those Policies
e68db767 777#-----------------------------------------------------------------------------
1bef6e21
JRT
778
779$code = <<'END_PERL';
780package FOO;
781
782#Code before 'use strict'
783my $foo = 'baz'; ## no critic
c0137eab 784my $bar = 42; # Should find this
1bef6e21
JRT
785
786use strict;
787use warnings;
788our $VERSION = 1.0;
789
7901;
791END_PERL
792
d6dc5ff8
ES
793is(
794 critique(
795 \$code,
796 {-profile => $profile, -severity => 5, -theme => 'core'},
797 ),
798 1,
799 'no critic & RequireUseStrict',
800);
1bef6e21 801
e68db767 802#-----------------------------------------------------------------------------
1bef6e21
JRT
803
804$code = <<'END_PERL';
805package FOO;
c0137eab 806use strict;
1bef6e21
JRT
807
808#Code before 'use warnings'
809my $foo = 'baz'; ## no critic
810my $bar = 42; # Should find this
811
1bef6e21
JRT
812use warnings;
813our $VERSION = 1.0;
814
8151;
816END_PERL
817
d6dc5ff8
ES
818is(
819 critique(
820 \$code,
821 {-profile => $profile, -severity => 4, -theme => 'core'},
822 ),
823 1,
824 'no critic & RequireUseWarnings',
825);
1bef6e21 826
e68db767 827#-----------------------------------------------------------------------------
1bef6e21
JRT
828
829$code = <<'END_PERL';
55944022
JRT
830use strict; ##no critic
831use warnings; #should find this
832my $bar = 42; #this one will be squelched
1bef6e21
JRT
833
834package FOO;
835
1bef6e21
JRT
836our $VERSION = 1.0;
837
8381;
839END_PERL
840
d6dc5ff8
ES
841is(
842 critique(
843 \$code,
844 {-profile => $profile, -severity => 4, -theme => 'core'},
845 ),
846 1,
847 'no critic & RequireExplicitPackage',
848);
737d3b65 849
34cc6052
ES
850#-----------------------------------------------------------------------------
851
852# ensure we run true if this test is loaded by
853# t/03_pragmas.t_without_optional_dependencies.t
8541;
855
55944022 856##############################################################################
737d3b65
CD
857# Local Variables:
858# mode: cperl
859# cperl-indent-level: 4
860# fill-column: 78
861# indent-tabs-mode: nil
862# c-indentation-style: bsd
863# End:
96fed375 864# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :