7 use English qw(-no_match_vars);
9 # common P::C testing tools
10 use Perl::Critic::Utils qw( :characters );
11 use Perl::Critic::TestUtils qw(
12 pcritique_with_violations
13 fcritique_with_violations
16 Perl::Critic::TestUtils::block_perlcriticrc();
18 my $subtests = subtests_in_tree( 't' );
20 # Check for cmdline limit on policies. Example:
21 # perl -Ilib t/20_policies.t BuiltinFunctions::ProhibitLvalueSubstr
23 # perl -Ilib t/20_policies.t t/BuiltinFunctions/ProhibitLvalueSubstr.run
25 my @policies = keys %{$subtests}; # get a list of all tests
26 # This is inefficient, but who cares...
29 if (!s{\A t[\\/](\w+)[\\/](\w+)\.run \z}{$1\::$2}xms) {
30 die 'Unknown argument ' . $_;
33 for my $p (@policies) {
34 if (0 == grep {$_ eq $p} @ARGV) {
35 delete $subtests->{$p};
40 # count how many tests there will be
42 for my $s (values %$subtests) {
43 $nsubtests += @$s; # one [pf]critique() test per subtest
45 my $npolicies = scalar keys %$subtests; # one can() test per policy
47 plan tests => $nsubtests + $npolicies;
49 for my $policy ( sort keys %$subtests ) {
50 can_ok( "Perl::Critic::Policy::$policy", 'violates' );
51 for my $subtest ( @{$subtests->{$policy}} ) {
52 local $TODO = $subtest->{TODO}; # Is NOT a TODO if it's not set
55 join ' - ', $policy, "line $subtest->{lineno}", $subtest->{name};
57 my @violations = $subtest->{filename}
59 fcritique_with_violations(
67 pcritique_with_violations(
73 my $err = $EVAL_ERROR;
76 if ($subtest->{error}) {
77 if ( 'Regexp' eq ref $subtest->{error} ) {
78 $test_passed = like($err, $subtest->{error}, $desc);
81 $test_passed = ok($err, $desc);
85 if ($err =~ m/\A Unable [ ] to [ ] create [ ] policy [ ] [']/xms) {
86 # We most likely hit a configuration that a parameter didn't like.
96 my $expected_failures = $subtest->{failures};
98 # If any optional modules are NOT installed, then there should be no failures.
99 if ($subtest->{optional_modules}) {
101 for my $module (split m/,\s*/xms, $subtest->{optional_modules}) {
102 eval "require $module";
104 $expected_failures = 0;
110 $test_passed = is(scalar @violations, $expected_failures, $desc);
113 if (not $test_passed) {
114 diag("Violation found: $_") foreach @violations;
119 #-----------------------------------------------------------------------------
122 # cperl-indent-level: 4
124 # indent-tabs-mode: nil
125 # c-indentation-style: bsd
127 # ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :