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::RequireRcsKeywords' => {},
35 '-ValuesAndExpressions::ProhibitMagicNumbers' => {},
40 #-----------------------------------------------------------------------------
48 require 'some_library.pl'; ## no critic
49 print $crap if $condition; ## no critic
57 {-profile => $profile, -severity => 1, -theme => 'core'}
63 #-----------------------------------------------------------------------------
75 require 'some_library.pl';
76 print $crap if $condition;
87 {-profile => $profile, -severity => 1, -theme => 'core'},
93 #-----------------------------------------------------------------------------
101 for my $foo (@list) {
103 $long_int = 12345678;
115 {-profile => $profile, -severity => 1, -theme => 'core'},
121 #-----------------------------------------------------------------------------
123 $code = <<'END_PERL';
131 $long_int = 12345678;
143 {-profile => $profile, -severity => 1, -theme => 'core'},
149 #-----------------------------------------------------------------------------
151 $code = <<'END_PERL';
158 for my $foo (@list) {
159 $long_int = 12345678;
172 {-profile => $profile, -severity => 1, -theme => 'core'},
175 'region no-critic across a scope',
178 #-----------------------------------------------------------------------------
180 $code = <<'END_PERL';
186 for my $foo (@list) {
188 $long_int = 12345678;
202 {-profile => $profile, -severity => 1, -theme => 'core'},
205 'scoped region no-critic',
208 #-----------------------------------------------------------------------------
210 $code = <<'END_PERL';
217 for my $foo (@list) {
218 $long_int = 12345678;
231 {-profile => $profile, -severity => 1, -theme => 'core'},
234 'unterminated no-critic across a scope',
237 #-----------------------------------------------------------------------------
239 $code = <<'END_PERL';
245 $long_int = 12345678; ## no critic
246 $oct_num = 033; ## no critic
247 my $noisy = '!'; ## no critic
248 my $empty = ''; ## no critic
249 my $empty = ''; ## use critic
257 {-profile => $profile, -severity => 1, -theme => 'core'},
263 #-----------------------------------------------------------------------------
265 $code = <<'END_PERL';
271 $long_int = 12345678; ## no critic
272 $oct_num = 033; ## no critic
273 my $noisy = '!'; ## no critic
274 my $empty = ''; ## no critic
276 $long_int = 12345678;
287 {-profile => $profile, -severity => 1, -theme => 'core'},
293 #-----------------------------------------------------------------------------
295 $code = <<'END_PERL';
301 $long_int = 12345678; ## no critic
302 $oct_num = 033; ## no critic
303 my $noisy = '!'; ## no critic
304 my $empty = ''; ## no critic
307 $long_int = 12345678;
319 -profile => $profile,
329 #-----------------------------------------------------------------------------
331 $code = <<'END_PERL';
337 for my $foo (@list) {
339 $long_int = 12345678;
343 my $noisy = '!'; ## no critic
344 my $empty = ''; ## no critic
353 -profile => $profile,
363 #-----------------------------------------------------------------------------
365 $code = <<'END_PERL';
371 for my $foo (@list) {
373 $long_int = 12345678;
388 -profile => $profile,
398 #-----------------------------------------------------------------------------
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
402 $code = <<'END_PERL';
408 for ($i;$i++;$i<$j) { ## no critic
409 my $long_int = 12345678;
414 && $condition2 ) { ## no critic
425 {-profile => $profile, -severity => 1, -theme => 'core'},
431 #-----------------------------------------------------------------------------
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
435 $code = <<'END_PERL';
441 for ($i; $i++; $i<$j) { ## no critic
442 my $long_int = 12345678;
447 $Global::Variable = "foo"; #Package var; double-quotes
450 && $condition2 ) { ## no critic
461 {-profile => $profile, -severity => 1, -theme => 'core'}
467 #-----------------------------------------------------------------------------
469 $code = <<'END_PERL';
475 sub grep { ## no critic;
479 sub grep { return $foo; } ## no critic
486 {-profile => $profile, -severity => 1, -theme => 'core'},
489 'no-critic on sub name',
492 #-----------------------------------------------------------------------------
494 $code = <<'END_PERL';
500 sub grep { ## no critic;
501 return undef; #Should find this!
510 {-profile => $profile, -severity =>1, -theme => 'core'}
513 'no-critic on sub name',
516 #-----------------------------------------------------------------------------
518 $code = <<'END_PERL';
524 ## no critic (NoisyQuotes)
535 {-profile => $profile, -severity => 1, -theme => 'core'}
538 'per-policy no-critic',
541 #-----------------------------------------------------------------------------
543 $code = <<'END_PERL';
549 ## no critic (ValuesAndExpressions)
560 {-profile => $profile, -severity => 1, -theme => 'core'}
563 'per-policy no-critic',
566 #-----------------------------------------------------------------------------
568 $code = <<'END_PERL';
574 ## no critic (Noisy, Empty)
585 {-profile => $profile, -severity => 1, -theme => 'core'}
588 'per-policy no-critic',
591 #-----------------------------------------------------------------------------
593 $code = <<'END_PERL';
599 ## no critic (NOISY, EMPTY, EVAL)
610 {-profile => $profile, -severity => 1, -theme => 'core'}
613 'per-policy no-critic',
616 #-----------------------------------------------------------------------------
618 $code = <<'END_PERL';
624 ## no critic (Noisy, Empty, Eval)
640 {-profile => $profile, -severity => 1, -theme => 'core'}
643 'per-policy no-critic',
646 #-----------------------------------------------------------------------------
648 $code = <<'END_PERL';
654 ## no critic (Critic::Policy)
665 {-profile => $profile, -severity => 1, -theme => 'core'}
668 'per-policy no-critic',
671 #-----------------------------------------------------------------------------
673 $code = <<'END_PERL';
679 ## no critic (Foo::Bar, Baz, Boom)
690 {-profile => $profile, -severity => 1, -theme => 'core'}
693 'per-policy no-critic',
696 #-----------------------------------------------------------------------------
698 $code = <<'END_PERL';
705 my $noisy = '!'; #Should not find this
706 my $empty = ''; #Should find this
711 my $nosiy = '!'; #Should not find this
712 my $empty = ''; #Should not find this
718 my $nosiy = '!'; #Should not find this
719 my $empty = ''; #Should find this
727 {-profile => $profile, -severity => 1, -theme => 'core'}
730 'per-policy no-critic',
733 #-----------------------------------------------------------------------------
734 $code = <<'END_PERL';
742 my $noisy = '!'; ##no critic (NoisyQuotes)
743 barf() unless $$ eq ''; ##no critic (Postfix,Empty,Punctuation)
744 barf() unless $$ eq ''; ##no critic (Postfix , Empty , Punctuation)
745 barf() unless $$ eq ''; ##no critic (Postfix Empty Punctuation)
748 my $noisy = '!'; ##no critic qw(NoisyQuotes);
749 barf() unless $$ eq ''; ##no critic qw(Postfix,Empty,Punctuation)
750 barf() unless $$ eq ''; ##no critic qw(Postfix , Empty , Punctuation)
751 barf() unless $$ eq ''; ##no critic qw(Postfix Empty Punctuation)
754 my $noisy = '!'; ##no critic NoisyQuotes;
755 barf() unless $$ eq ''; ##no critic Postfix,Empty,Punctuation;
756 barf() unless $$ eq ''; ##no critic Postfix , Empty , Punctuation;
757 barf() unless $$ eq ''; ##no critic Postfix Empty Punctuation;
765 {-profile => $profile, -severity => 1, -theme => 'core'},
768 'no critic: syntaxes',
771 #-----------------------------------------------------------------------------
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
777 #-----------------------------------------------------------------------------
779 $code = <<'END_PERL';
782 #Code before 'use strict'
783 my $foo = 'baz'; ## no critic
784 my $bar = 42; # Should find this
796 {-profile => $profile, -severity => 5, -theme => 'core'},
799 'no critic & RequireUseStrict',
802 #-----------------------------------------------------------------------------
804 $code = <<'END_PERL';
808 #Code before 'use warnings'
809 my $foo = 'baz'; ## no critic
810 my $bar = 42; # Should find this
821 {-profile => $profile, -severity => 4, -theme => 'core'},
824 'no critic & RequireUseWarnings',
827 #-----------------------------------------------------------------------------
829 $code = <<'END_PERL';
830 use strict; ##no critic
831 use warnings; #should find this
832 my $bar = 42; #this one will be squelched
844 {-profile => $profile, -severity => 4, -theme => 'core'},
847 'no critic & RequireExplicitPackage',
850 #-----------------------------------------------------------------------------
852 # ensure we run true if this test is loaded by
853 # t/03_pragmas.t_without_optional_dependencies.t
856 ##############################################################################
859 # cperl-indent-level: 4
861 # indent-tabs-mode: nil
862 # c-indentation-style: bsd
864 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :