3 ##############################################################################
4 # $URL: http://perlcritic.tigris.org/svn/perlcritic/trunk/Perl-Critic/t/03_pragmas.t $
5 # $Date: 2008-06-29 11:47:22 -0700 (Sun, 29 Jun 2008) $
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);
19 Perl::Critic::TestUtils::block_perlcriticrc();
21 # Configure Critic not to load certain policies. This
22 # just makes it a little easier to create test cases
24 '-CodeLayout::RequireTidyCode' => {},
25 '-Documentation::PodSpelling' => {},
26 '-ErrorHandling::RequireCheckingReturnValueOfEval' => {},
27 '-Miscellanea::RequireRcsKeywords' => {},
28 '-ValuesAndExpressions::ProhibitMagicNumbers' => {},
33 #-----------------------------------------------------------------------------
41 require 'some_library.pl'; ## no critic
42 print $crap if $condition; ## no critic
50 {-profile => $profile, -severity => 1, -theme => 'core'}
56 #-----------------------------------------------------------------------------
68 require 'some_library.pl';
69 print $crap if $condition;
80 {-profile => $profile, -severity => 1, -theme => 'core'},
86 #-----------------------------------------------------------------------------
108 {-profile => $profile, -severity => 1, -theme => 'core'},
114 #-----------------------------------------------------------------------------
116 $code = <<'END_PERL';
124 $long_int = 12345678;
136 {-profile => $profile, -severity => 1, -theme => 'core'},
142 #-----------------------------------------------------------------------------
144 $code = <<'END_PERL';
151 for my $foo (@list) {
152 $long_int = 12345678;
165 {-profile => $profile, -severity => 1, -theme => 'core'},
168 'region no-critic across a scope',
171 #-----------------------------------------------------------------------------
173 $code = <<'END_PERL';
179 for my $foo (@list) {
181 $long_int = 12345678;
195 {-profile => $profile, -severity => 1, -theme => 'core'},
198 'scoped region no-critic',
201 #-----------------------------------------------------------------------------
203 $code = <<'END_PERL';
210 for my $foo (@list) {
211 $long_int = 12345678;
224 {-profile => $profile, -severity => 1, -theme => 'core'},
227 'unterminated no-critic across a scope',
230 #-----------------------------------------------------------------------------
232 $code = <<'END_PERL';
238 $long_int = 12345678; ## no critic
239 $oct_num = 033; ## no critic
240 my $noisy = '!'; ## no critic
241 my $empty = ''; ## no critic
242 my $empty = ''; ## use critic
250 {-profile => $profile, -severity => 1, -theme => 'core'},
256 #-----------------------------------------------------------------------------
258 $code = <<'END_PERL';
264 $long_int = 12345678; ## no critic
265 $oct_num = 033; ## no critic
266 my $noisy = '!'; ## no critic
267 my $empty = ''; ## no critic
269 $long_int = 12345678;
280 {-profile => $profile, -severity => 1, -theme => 'core'},
286 #-----------------------------------------------------------------------------
288 $code = <<'END_PERL';
294 $long_int = 12345678; ## no critic
295 $oct_num = 033; ## no critic
296 my $noisy = '!'; ## no critic
297 my $empty = ''; ## no critic
300 $long_int = 12345678;
312 -profile => $profile,
322 #-----------------------------------------------------------------------------
324 $code = <<'END_PERL';
330 for my $foo (@list) {
332 $long_int = 12345678;
336 my $noisy = '!'; ## no critic
337 my $empty = ''; ## no critic
346 -profile => $profile,
356 #-----------------------------------------------------------------------------
358 $code = <<'END_PERL';
364 for my $foo (@list) {
366 $long_int = 12345678;
381 -profile => $profile,
391 #-----------------------------------------------------------------------------
392 # Check that '## no critic' on the top of a block doesn't extend
393 # to all code within the block. See RT bug #15295
395 $code = <<'END_PERL';
401 for ($i;$i++;$i<$j) { ## no critic
402 my $long_int = 12345678;
407 && $condition2 ) { ## no critic
418 {-profile => $profile, -severity => 1, -theme => 'core'},
424 #-----------------------------------------------------------------------------
425 # Check that '## no critic' on the top of a block doesn't extend
426 # to all code within the block. See RT bug #15295
428 $code = <<'END_PERL';
434 for ($i; $i++; $i<$j) { ## no critic
435 my $long_int = 12345678;
440 $Global::Variable = "foo"; #Package var; double-quotes
443 && $condition2 ) { ## no critic
454 {-profile => $profile, -severity => 1, -theme => 'core'}
460 #-----------------------------------------------------------------------------
462 $code = <<'END_PERL';
468 sub grep { ## no critic;
472 sub grep { return $foo; } ## no critic
479 {-profile => $profile, -severity => 1, -theme => 'core'},
482 'no-critic on sub name',
485 #-----------------------------------------------------------------------------
487 $code = <<'END_PERL';
493 sub grep { ## no critic;
494 return undef; #Should find this!
503 {-profile => $profile, -severity =>1, -theme => 'core'}
506 'no-critic on sub name',
509 #-----------------------------------------------------------------------------
511 $code = <<'END_PERL';
517 ## no critic (NoisyQuotes)
528 {-profile => $profile, -severity => 1, -theme => 'core'}
531 'per-policy no-critic',
534 #-----------------------------------------------------------------------------
536 $code = <<'END_PERL';
542 ## no critic (ValuesAndExpressions)
553 {-profile => $profile, -severity => 1, -theme => 'core'}
556 'per-policy no-critic',
559 #-----------------------------------------------------------------------------
561 $code = <<'END_PERL';
567 ## no critic (Noisy, Empty)
578 {-profile => $profile, -severity => 1, -theme => 'core'}
581 'per-policy no-critic',
584 #-----------------------------------------------------------------------------
586 $code = <<'END_PERL';
592 ## no critic (NOISY, EMPTY, EVAL)
603 {-profile => $profile, -severity => 1, -theme => 'core'}
606 'per-policy no-critic',
609 #-----------------------------------------------------------------------------
611 $code = <<'END_PERL';
617 ## no critic (Noisy, Empty, Eval)
633 {-profile => $profile, -severity => 1, -theme => 'core'}
636 'per-policy no-critic',
639 #-----------------------------------------------------------------------------
641 $code = <<'END_PERL';
647 ## no critic (Critic::Policy)
658 {-profile => $profile, -severity => 1, -theme => 'core'}
661 'per-policy no-critic',
664 #-----------------------------------------------------------------------------
666 $code = <<'END_PERL';
672 ## no critic (Foo::Bar, Baz, Boom)
683 {-profile => $profile, -severity => 1, -theme => 'core'}
686 'per-policy no-critic',
689 #-----------------------------------------------------------------------------
691 $code = <<'END_PERL';
698 my $noisy = '!'; #Should not find this
699 my $empty = ''; #Should find this
704 my $nosiy = '!'; #Should not find this
705 my $empty = ''; #Should not find this
711 my $nosiy = '!'; #Should not find this
712 my $empty = ''; #Should find this
720 {-profile => $profile, -severity => 1, -theme => 'core'}
723 'per-policy no-critic',
726 #-----------------------------------------------------------------------------
727 $code = <<'END_PERL';
735 my $noisy = '!'; ##no critic (NoisyQuotes)
736 barf() unless $$ eq ''; ##no critic (Postfix,Empty,Punctuation)
737 barf() unless $$ eq ''; ##no critic (Postfix , Empty , Punctuation)
738 barf() unless $$ eq ''; ##no critic (Postfix Empty Punctuation)
741 my $noisy = '!'; ##no critic qw(NoisyQuotes);
742 barf() unless $$ eq ''; ##no critic qw(Postfix,Empty,Punctuation)
743 barf() unless $$ eq ''; ##no critic qw(Postfix , Empty , Punctuation)
744 barf() unless $$ eq ''; ##no critic qw(Postfix Empty Punctuation)
747 my $noisy = '!'; ##no critic NoisyQuotes;
748 barf() unless $$ eq ''; ##no critic Postfix,Empty,Punctuation;
749 barf() unless $$ eq ''; ##no critic Postfix , Empty , Punctuation;
750 barf() unless $$ eq ''; ##no critic Postfix Empty Punctuation;
758 {-profile => $profile, -severity => 1, -theme => 'core'},
761 'no critic: syntaxes',
764 #-----------------------------------------------------------------------------
765 # Most policies apply to a particular type of PPI::Element and usually
766 # only return one Violation at a time. But the next three cases
767 # involve policies that apply to the whole document and can return
768 # multiple violations at a time. These tests make sure that the 'no
769 # critic' pragmas are effective with those Policies
770 #-----------------------------------------------------------------------------
772 $code = <<'END_PERL';
775 #Code before 'use strict'
776 my $foo = 'baz'; ## no critic
777 my $bar = 42; # Should find this
789 {-profile => $profile, -severity => 5, -theme => 'core'},
792 'no critic & RequireUseStrict',
795 #-----------------------------------------------------------------------------
797 $code = <<'END_PERL';
801 #Code before 'use warnings'
802 my $foo = 'baz'; ## no critic
803 my $bar = 42; # Should find this
814 {-profile => $profile, -severity => 4, -theme => 'core'},
817 'no critic & RequireUseWarnings',
820 #-----------------------------------------------------------------------------
822 $code = <<'END_PERL';
823 use strict; ##no critic
824 use warnings; #should find this
825 my $bar = 42; #this one will be squelched
837 {-profile => $profile, -severity => 4, -theme => 'core'},
840 'no critic & RequireExplicitPackage',
843 #-----------------------------------------------------------------------------
845 # ensure we run true if this test is loaded by
846 # t/03_pragmas.t_without_optional_dependencies.t
849 ##############################################################################
852 # cperl-indent-level: 4
854 # indent-tabs-mode: nil
855 # c-indentation-style: bsd
857 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :