3 ##############################################################################
8 ##############################################################################
14 use Test::More (tests => 28);
15 use Perl::Critic::PolicyFactory (-test => 1);
17 # common P::C testing tools
18 use Perl::Critic::TestUtils qw(critique);
20 #-----------------------------------------------------------------------------
22 our $VERSION = '1.093_01';
24 #-----------------------------------------------------------------------------
26 Perl::Critic::TestUtils::block_perlcriticrc();
28 # Configure Critic not to load certain policies. This
29 # just makes it a little easier to create test cases
31 '-CodeLayout::RequireTidyCode' => {},
32 '-Documentation::PodSpelling' => {},
33 '-ErrorHandling::RequireCheckingReturnValueOfEval' => {},
34 '-Miscellanea::ProhibitUnrestrictedNoCritic' => {},
35 '-Miscellanea::RequireRcsKeywords' => {},
36 '-ValuesAndExpressions::ProhibitMagicNumbers' => {},
41 #-----------------------------------------------------------------------------
49 require 'some_library.pl'; ## no critic
50 print $crap if $condition; ## no critic
58 {-profile => $profile, -severity => 1, -theme => 'core'}
64 #-----------------------------------------------------------------------------
76 require 'some_library.pl';
77 print $crap if $condition;
88 {-profile => $profile, -severity => 1, -theme => 'core'},
94 #-----------------------------------------------------------------------------
102 for my $foo (@list) {
104 $long_int = 12345678;
116 {-profile => $profile, -severity => 1, -theme => 'core'},
122 #-----------------------------------------------------------------------------
124 $code = <<'END_PERL';
132 $long_int = 12345678;
144 {-profile => $profile, -severity => 1, -theme => 'core'},
150 #-----------------------------------------------------------------------------
152 $code = <<'END_PERL';
159 for my $foo (@list) {
160 $long_int = 12345678;
173 {-profile => $profile, -severity => 1, -theme => 'core'},
176 'region no-critic across a scope',
179 #-----------------------------------------------------------------------------
181 $code = <<'END_PERL';
187 for my $foo (@list) {
189 $long_int = 12345678;
203 {-profile => $profile, -severity => 1, -theme => 'core'},
206 'scoped region no-critic',
209 #-----------------------------------------------------------------------------
211 $code = <<'END_PERL';
218 for my $foo (@list) {
219 $long_int = 12345678;
232 {-profile => $profile, -severity => 1, -theme => 'core'},
235 'unterminated no-critic across a scope',
238 #-----------------------------------------------------------------------------
240 $code = <<'END_PERL';
246 $long_int = 12345678; ## no critic
247 $oct_num = 033; ## no critic
248 my $noisy = '!'; ## no critic
249 my $empty = ''; ## no critic
250 my $empty = ''; ## use critic
258 {-profile => $profile, -severity => 1, -theme => 'core'},
264 #-----------------------------------------------------------------------------
266 $code = <<'END_PERL';
272 $long_int = 12345678; ## no critic
273 $oct_num = 033; ## no critic
274 my $noisy = '!'; ## no critic
275 my $empty = ''; ## no critic
277 $long_int = 12345678;
288 {-profile => $profile, -severity => 1, -theme => 'core'},
294 #-----------------------------------------------------------------------------
296 $code = <<'END_PERL';
302 $long_int = 12345678; ## no critic
303 $oct_num = 033; ## no critic
304 my $noisy = '!'; ## no critic
305 my $empty = ''; ## no critic
308 $long_int = 12345678;
320 -profile => $profile,
330 #-----------------------------------------------------------------------------
332 $code = <<'END_PERL';
338 for my $foo (@list) {
340 $long_int = 12345678;
344 my $noisy = '!'; ## no critic
345 my $empty = ''; ## no critic
354 -profile => $profile,
364 #-----------------------------------------------------------------------------
366 $code = <<'END_PERL';
372 for my $foo (@list) {
374 $long_int = 12345678;
389 -profile => $profile,
399 #-----------------------------------------------------------------------------
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
403 $code = <<'END_PERL';
409 for ($i;$i++;$i<$j) { ## no critic
410 my $long_int = 12345678;
415 && $condition2 ) { ## no critic
426 {-profile => $profile, -severity => 1, -theme => 'core'},
432 #-----------------------------------------------------------------------------
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
436 $code = <<'END_PERL';
442 for ($i; $i++; $i<$j) { ## no critic
443 my $long_int = 12345678;
448 $Global::Variable = "foo"; #Package var; double-quotes
451 && $condition2 ) { ## no critic
462 {-profile => $profile, -severity => 1, -theme => 'core'}
468 #-----------------------------------------------------------------------------
470 $code = <<'END_PERL';
476 sub grep { ## no critic;
480 sub grep { return $foo; } ## no critic
487 {-profile => $profile, -severity => 1, -theme => 'core'},
490 'no-critic on sub name',
493 #-----------------------------------------------------------------------------
495 $code = <<'END_PERL';
501 sub grep { ## no critic;
502 return undef; #Should find this!
511 {-profile => $profile, -severity =>1, -theme => 'core'}
514 'no-critic on sub name',
517 #-----------------------------------------------------------------------------
519 $code = <<'END_PERL';
525 ## no critic (NoisyQuotes)
536 {-profile => $profile, -severity => 1, -theme => 'core'}
539 'per-policy no-critic',
542 #-----------------------------------------------------------------------------
544 $code = <<'END_PERL';
550 ## no critic (ValuesAndExpressions)
561 {-profile => $profile, -severity => 1, -theme => 'core'}
564 'per-policy no-critic',
567 #-----------------------------------------------------------------------------
569 $code = <<'END_PERL';
575 ## no critic (Noisy, Empty)
586 {-profile => $profile, -severity => 1, -theme => 'core'}
589 'per-policy no-critic',
592 #-----------------------------------------------------------------------------
594 $code = <<'END_PERL';
600 ## no critic (NOISY, EMPTY, EVAL)
611 {-profile => $profile, -severity => 1, -theme => 'core'}
614 'per-policy no-critic',
617 #-----------------------------------------------------------------------------
619 $code = <<'END_PERL';
625 ## no critic (Noisy, Empty, Eval)
641 {-profile => $profile, -severity => 1, -theme => 'core'}
644 'per-policy no-critic',
647 #-----------------------------------------------------------------------------
649 $code = <<'END_PERL';
655 ## no critic (Critic::Policy)
666 {-profile => $profile, -severity => 1, -theme => 'core'}
669 'per-policy no-critic',
672 #-----------------------------------------------------------------------------
674 $code = <<'END_PERL';
680 ## no critic (Foo::Bar, Baz, Boom)
691 {-profile => $profile, -severity => 1, -theme => 'core'}
694 'per-policy no-critic',
697 #-----------------------------------------------------------------------------
699 $code = <<'END_PERL';
706 my $noisy = '!'; #Should not find this
707 my $empty = ''; #Should find this
712 my $nosiy = '!'; #Should not find this
713 my $empty = ''; #Should not find this
719 my $nosiy = '!'; #Should not find this
720 my $empty = ''; #Should find this
728 {-profile => $profile, -severity => 1, -theme => 'core'}
731 'per-policy no-critic',
734 #-----------------------------------------------------------------------------
735 $code = <<'END_PERL';
743 my $noisy = '!'; ##no critic (NoisyQuotes)
744 barf() unless $$ eq ''; ##no critic (Postfix,Empty,Punctuation)
745 barf() unless $$ eq ''; ##no critic (Postfix , Empty , Punctuation)
746 barf() unless $$ eq ''; ##no critic (Postfix Empty Punctuation)
749 my $noisy = '!'; ##no critic qw(NoisyQuotes);
750 barf() unless $$ eq ''; ##no critic qw(Postfix,Empty,Punctuation)
751 barf() unless $$ eq ''; ##no critic qw(Postfix , Empty , Punctuation)
752 barf() unless $$ eq ''; ##no critic qw(Postfix Empty Punctuation)
755 my $noisy = '!'; ##no critic NoisyQuotes;
756 barf() unless $$ eq ''; ##no critic Postfix,Empty,Punctuation;
757 barf() unless $$ eq ''; ##no critic Postfix , Empty , Punctuation;
758 barf() unless $$ eq ''; ##no critic Postfix Empty Punctuation;
766 {-profile => $profile, -severity => 1, -theme => 'core'},
769 'no critic: syntaxes',
772 #-----------------------------------------------------------------------------
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
778 #-----------------------------------------------------------------------------
780 $code = <<'END_PERL';
783 #Code before 'use strict'
784 my $foo = 'baz'; ## no critic
785 my $bar = 42; # Should find this
797 {-profile => $profile, -severity => 5, -theme => 'core'},
800 'no critic & RequireUseStrict',
803 #-----------------------------------------------------------------------------
805 $code = <<'END_PERL';
809 #Code before 'use warnings'
810 my $foo = 'baz'; ## no critic
811 my $bar = 42; # Should find this
822 {-profile => $profile, -severity => 4, -theme => 'core'},
825 'no critic & RequireUseWarnings',
828 #-----------------------------------------------------------------------------
830 $code = <<'END_PERL';
831 use strict; ##no critic
832 use warnings; #should find this
833 my $bar = 42; #this one will be squelched
845 {-profile => $profile, -severity => 4, -theme => 'core'},
848 'no critic & RequireExplicitPackage',
851 #-----------------------------------------------------------------------------
853 # ensure we run true if this test is loaded by
854 # t/03_pragmas.t_without_optional_dependencies.t
857 ##############################################################################
860 # cperl-indent-level: 4
862 # indent-tabs-mode: nil
863 # c-indentation-style: bsd
865 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :