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