« lib/Perl/Critic/Policy/RegularExpressions/ProhibitUselessTopic.pm
↑ Summary
lib/Perl/Critic/Policy/Modules/RequireFilenameMatchesPackage.pm »
| 1 | package Test::Perl::Critic::Policy; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 1 | ▶ | use 5.010001; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 1 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 1 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 1 | ▶ | use Carp qw< croak confess >; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 1 | ▶ | use English qw< -no_match_vars >; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 1 | ▶ | use List::SomeUtils qw( all none ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 1 | ▶ | use Readonly; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 1 | ▶ | use Test::Builder qw<>; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | 1 | ▶ | use Test::More; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | 1 | ▶ | use Perl::Critic::Violation; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 17 | 1 | ▶ | use Perl::Critic::TestUtils qw< | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | pcritique_with_violations fcritique_with_violations subtests_in_tree | ||||||||||||||
| 19 | 1 | >; | |||||||||||||
| 20 | |||||||||||||||
| 21 | #----------------------------------------------------------------------------- | ||||||||||||||
| 22 | |||||||||||||||
| 23 | 1 | our $VERSION = '1.156'; | |||||||||||||
| 24 | |||||||||||||||
| 25 | #----------------------------------------------------------------------------- | ||||||||||||||
| 26 | |||||||||||||||
| 27 | 1 | ▶ | use Exporter 'import'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 28 | |||||||||||||||
| 29 | 1 | Readonly::Array our @EXPORT_OK => qw< all_policies_ok >; | |||||||||||||
| 30 | 1 | Readonly::Hash our %EXPORT_TAGS => (all => \@EXPORT_OK); | |||||||||||||
| 31 | |||||||||||||||
| 32 | #----------------------------------------------------------------------------- | ||||||||||||||
| 33 | |||||||||||||||
| 34 | 1 | Perl::Critic::Violation::set_format( "%m at line %l, column %c. (%r)\n" ); | |||||||||||||
| 35 | 1 | Perl::Critic::TestUtils::block_perlcriticrc(); | |||||||||||||
| 36 | |||||||||||||||
| 37 | #----------------------------------------------------------------------------- | ||||||||||||||
| 38 | |||||||||||||||
| 39 | 1 | my $TEST = Test::Builder->new(); | |||||||||||||
| 40 | |||||||||||||||
| 41 | #----------------------------------------------------------------------------- | ||||||||||||||
| 42 | |||||||||||||||
| 43 | 0 | ▶ | sub all_policies_ok { | ||||||||||||
|
Subroutine
all_policies_ok: not called CC=8 SCAR=42.8
Pod
documented
| |||||||||||||||
| 44 | 0 | my (%args) = @_; | |||||||||||||
| 45 | 0 | my $wanted_policies = $args{-policies}; | |||||||||||||
| 46 | 0 | ▶ | my $test_dir = $args{'-test-directory'} || 't'; | ||||||||||||
|
Condition0%0/2
$args{'-test-directory'} || 't'
MC/DC0%0/1
$args{'-test-directory'} || 't'
$args{'-test-directory'}
Truth table0%0/2
A
A= $args{'-test-directory'}
| |||||||||||||||
| 47 | |||||||||||||||
| 48 | 0 | my $subtests_with_extras = subtests_in_tree( $test_dir, 'include extras' ); | |||||||||||||
| 49 | |||||||||||||||
| 50 | 0 | ▶ | if ($wanted_policies) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 51 | 0 | _validate_wanted_policy_names($wanted_policies, $subtests_with_extras); | |||||||||||||
| 52 | 0 | _filter_unwanted_subtests($wanted_policies, $subtests_with_extras); | |||||||||||||
| 53 | } | ||||||||||||||
| 54 | |||||||||||||||
| 55 | 0 | $TEST->plan( tests => _compute_test_count($subtests_with_extras) ); | |||||||||||||
| 56 | 0 | my $policies_to_test = join q{, }, keys %{$subtests_with_extras}; | |||||||||||||
| 57 | 0 | $TEST->note("Running tests for policies: $policies_to_test"); | |||||||||||||
| 58 | |||||||||||||||
| 59 | 0 | for my $policy ( sort keys %{$subtests_with_extras} ) { | |||||||||||||
| 60 | |||||||||||||||
| 61 | 0 | my ($full_policy_name, $method) = ("Perl::Critic::Policy::$policy", 'violates'); | |||||||||||||
| 62 | 0 | my $can_ok_label = qq{Class '$full_policy_name' has method '$method'}; | |||||||||||||
| 63 | 0 | $TEST->ok( $full_policy_name->can($method), $can_ok_label ); | |||||||||||||
| 64 | |||||||||||||||
| 65 | 0 | for my $subtest ( @{ $subtests_with_extras->{$policy}{subtests} } ) { | |||||||||||||
| 66 | 0 | my $todo = $subtest->{TODO}; | |||||||||||||
| 67 | 0 | ▶ | if ($todo) { $TEST->todo_start( $todo ); } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 68 | |||||||||||||||
| 69 | 0 | my ($error, @violations) = _run_subtest($policy, $subtest); | |||||||||||||
| 70 | 0 | my ($ok, @diag)= _evaluate_test_results($subtest, $error, \@violations); | |||||||||||||
| 71 | 0 | $TEST->ok( $ok, _create_test_name($policy, $subtest) ); | |||||||||||||
| 72 | |||||||||||||||
| 73 | 0 | ▶ | if (@diag) { $TEST->diag(@diag); } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 74 | 0 | ▶ | if ($todo) { $TEST->todo_end(); } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | } | ||||||||||||||
| 76 | } | ||||||||||||||
| 77 | |||||||||||||||
| 78 | 0 | return; | |||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | #----------------------------------------------------------------------------- | ||||||||||||||
| 82 | |||||||||||||||
| 83 | 0 | ▶ | sub _validate_wanted_policy_names { | ||||||||||||
|
Subroutine
_validate_wanted_policy_names: not called CC=3 SCAR=24.8
| |||||||||||||||
| 84 | 0 | my ($wanted_policies, $subtests_with_extras) = @_; | |||||||||||||
| 85 | 0 | ▶ | return 1 if not $wanted_policies; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | 0 | my @all_testable_policies = keys %{ $subtests_with_extras }; | |||||||||||||
| 87 | 0 | my @wanted_policies = @{ $wanted_policies }; | |||||||||||||
| 88 | |||||||||||||||
| 89 | |||||||||||||||
| 90 | 0 | ▶ | my @invalid = grep {my $p = $_; none { $_ =~ $p } @all_testable_policies} @wanted_policies; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 91 | 0 | ▶ | croak( q{No tests found for policies matching: } . join q{, }, @invalid ) if @invalid; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 92 | 0 | return 1; | |||||||||||||
| 93 | } | ||||||||||||||
| 94 | |||||||||||||||
| 95 | #----------------------------------------------------------------------------- | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 0 | ▶ | sub _filter_unwanted_subtests { | ||||||||||||
|
Subroutine
_filter_unwanted_subtests: not called CC=4 SCAR=30.0
| |||||||||||||||
| 98 | 0 | my ($wanted_policies, $subtests_with_extras) = @_; | |||||||||||||
| 99 | 0 | ▶ | return 1 if not $wanted_policies; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 100 | 0 | my @all_testable_policies = keys %{ $subtests_with_extras }; | |||||||||||||
| 101 | 0 | my @wanted_policies = @{ $wanted_policies }; | |||||||||||||
| 102 | |||||||||||||||
| 103 | 0 | for my $p (@all_testable_policies) { | |||||||||||||
| 104 | 0 | ▶ | if (none {$p =~ m/$_/xism} @wanted_policies) { | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
Branch: 1 branch
| |||||||||||||||
| 105 | 0 | delete $subtests_with_extras->{$p}; # side-effects! | |||||||||||||
| 106 | } | ||||||||||||||
| 107 | } | ||||||||||||||
| 108 | 0 | return 1; | |||||||||||||
| 109 | } | ||||||||||||||
| 110 | |||||||||||||||
| 111 | #----------------------------------------------------------------------------- | ||||||||||||||
| 112 | |||||||||||||||
| 113 | 0 | ▶ | sub _run_subtest { | ||||||||||||
|
Subroutine
_run_subtest: not called CC=6 SCAR=37.4
| |||||||||||||||
| 114 | 0 | my ($policy, $subtest) = @_; | |||||||||||||
| 115 | |||||||||||||||
| 116 | 0 | my @violations; | |||||||||||||
| 117 | my $error; | ||||||||||||||
| 118 | 0 | ▶ | if ( $subtest->{filename} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 119 | eval { | ||||||||||||||
| 120 | @violations = | ||||||||||||||
| 121 | fcritique_with_violations( | ||||||||||||||
| 122 | $policy, | ||||||||||||||
| 123 | \$subtest->{code}, | ||||||||||||||
| 124 | $subtest->{filename}, | ||||||||||||||
| 125 | $subtest->{parms}, | ||||||||||||||
| 126 | 0 | ); | |||||||||||||
| 127 | 0 | 1; | |||||||||||||
| 128 | 0 | ▶ | } or do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 129 | 0 | ▶ | $error = $EVAL_ERROR || 'An unknown problem occurred.'; | ||||||||||||
|
Condition0%0/2
$Test::Perl::Critic::Policy::EVAL_ERROR || 'An unknown problem occurred.'
MC/DC0%0/1
$Test::Perl::Critic::Policy::EVAL_ERROR || 'An unknown problem occurred.'
$Test::Perl::Critic::Policy::EVAL_ERROR
Truth table0%0/2
A
A= $Test::Perl::Critic::Policy::EVAL_ERROR
| |||||||||||||||
| 130 | }; | ||||||||||||||
| 131 | } | ||||||||||||||
| 132 | else { | ||||||||||||||
| 133 | eval { | ||||||||||||||
| 134 | @violations = | ||||||||||||||
| 135 | pcritique_with_violations( | ||||||||||||||
| 136 | $policy, | ||||||||||||||
| 137 | \$subtest->{code}, | ||||||||||||||
| 138 | $subtest->{parms}, | ||||||||||||||
| 139 | 0 | ); | |||||||||||||
| 140 | 0 | 1; | |||||||||||||
| 141 | 0 | ▶ | } or do { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 142 | 0 | ▶ | $error = $EVAL_ERROR || 'An unknown problem occurred.'; | ||||||||||||
|
Condition0%0/2
$Test::Perl::Critic::Policy::EVAL_ERROR || 'An unknown problem occurred.'
MC/DC0%0/1
$Test::Perl::Critic::Policy::EVAL_ERROR || 'An unknown problem occurred.'
$Test::Perl::Critic::Policy::EVAL_ERROR
Truth table0%0/2
A
A= $Test::Perl::Critic::Policy::EVAL_ERROR
| |||||||||||||||
| 143 | }; | ||||||||||||||
| 144 | } | ||||||||||||||
| 145 | |||||||||||||||
| 146 | 0 | return ($error, @violations); | |||||||||||||
| 147 | } | ||||||||||||||
| 148 | |||||||||||||||
| 149 | #----------------------------------------------------------------------------- | ||||||||||||||
| 150 | |||||||||||||||
| 151 | 0 | ▶ | sub _evaluate_test_results { | ||||||||||||
|
Subroutine
_evaluate_test_results: not called CC=3 SCAR=24.8
| |||||||||||||||
| 152 | 0 | my ($subtest, $error, $violations) = @_; | |||||||||||||
| 153 | |||||||||||||||
| 154 | 0 | ▶ | if ($subtest->{error}) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 155 | 0 | return _evaluate_error_case($subtest, $error); | |||||||||||||
| 156 | } | ||||||||||||||
| 157 | elsif ($error) { | ||||||||||||||
| 158 | 0 | confess $error; | |||||||||||||
| 159 | } | ||||||||||||||
| 160 | else { | ||||||||||||||
| 161 | 0 | return _evaluate_violation_case($subtest, $violations); | |||||||||||||
| 162 | } | ||||||||||||||
| 163 | } | ||||||||||||||
| 164 | |||||||||||||||
| 165 | #----------------------------------------------------------------------------- | ||||||||||||||
| 166 | |||||||||||||||
| 167 | 0 | ▶ | sub _evaluate_violation_case { | ||||||||||||
|
Subroutine
_evaluate_violation_case: not called CC=3 SCAR=24.8
| |||||||||||||||
| 168 | 0 | my ($subtest, $violations) = @_; | |||||||||||||
| 169 | 0 | my ($ok, @diagnostics); | |||||||||||||
| 170 | |||||||||||||||
| 171 | 0 | my @violations = @{$violations}; | |||||||||||||
| 172 | 0 | my $have = @violations; | |||||||||||||
| 173 | 0 | my $want = _compute_wanted_violation_count($subtest); | |||||||||||||
| 174 | 0 | ▶ | if ( not $ok = $have == $want ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 175 | 0 | my $msg = qq(Expected $want violations, got $have. ); | |||||||||||||
| 176 | 0 | ▶ | if (@violations) { $msg .= q(Found violations follow...); } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 177 | 0 | push @diagnostics, $msg . "\n"; | |||||||||||||
| 178 | 0 | push @diagnostics, map { qq(Found violation: $_) } @violations; | |||||||||||||
| 179 | } | ||||||||||||||
| 180 | |||||||||||||||
| 181 | 0 | return ($ok, @diagnostics); | |||||||||||||
| 182 | } | ||||||||||||||
| 183 | |||||||||||||||
| 184 | #----------------------------------------------------------------------------- | ||||||||||||||
| 185 | |||||||||||||||
| 186 | 0 | ▶ | sub _evaluate_error_case { | ||||||||||||
|
Subroutine
_evaluate_error_case: not called CC=4 SCAR=30.0
| |||||||||||||||
| 187 | 0 | my ($subtest, $error) = @_; | |||||||||||||
| 188 | 0 | my ($ok, @diagnostics); | |||||||||||||
| 189 | |||||||||||||||
| 190 | 0 | ▶ | if ( 'Regexp' eq ref $subtest->{error} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 191 | $ok = $error =~ $subtest->{error} | ||||||||||||||
| 192 | 0 | ▶ | or push @diagnostics, qq(Error message '$error' doesn't match $subtest->{error}.); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 193 | } | ||||||||||||||
| 194 | else { | ||||||||||||||
| 195 | $ok = $subtest->{error} | ||||||||||||||
| 196 | 0 | ▶ | or push @diagnostics, q(Didn't get an error message when we expected one.); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 197 | } | ||||||||||||||
| 198 | |||||||||||||||
| 199 | 0 | return ($ok, @diagnostics); | |||||||||||||
| 200 | } | ||||||||||||||
| 201 | |||||||||||||||
| 202 | #----------------------------------------------------------------------------- | ||||||||||||||
| 203 | |||||||||||||||
| 204 | 0 | ▶ | sub _compute_test_count { | ||||||||||||
|
Subroutine
_compute_test_count: not called CC=2 SCAR=17.9
| |||||||||||||||
| 205 | 0 | my ($subtests_with_extras) = @_; | |||||||||||||
| 206 | |||||||||||||||
| 207 | # one can_ok() for each policy | ||||||||||||||
| 208 | 0 | my $npolicies = scalar keys %{ $subtests_with_extras }; | |||||||||||||
| 209 | |||||||||||||||
| 210 | 0 | my $nsubtests = 0; | |||||||||||||
| 211 | 0 | for my $subtest_with_extras ( values %{$subtests_with_extras} ) { | |||||||||||||
| 212 | # one [pf]critique() test per subtest | ||||||||||||||
| 213 | 0 | $nsubtests += @{ $subtest_with_extras->{subtests} }; | |||||||||||||
| 214 | } | ||||||||||||||
| 215 | |||||||||||||||
| 216 | 0 | return $nsubtests + $npolicies; | |||||||||||||
| 217 | } | ||||||||||||||
| 218 | |||||||||||||||
| 219 | #----------------------------------------------------------------------------- | ||||||||||||||
| 220 | |||||||||||||||
| 221 | 0 | ▶ | sub _compute_wanted_violation_count { | ||||||||||||
|
Subroutine
_compute_wanted_violation_count: not called CC=2 SCAR=17.9
| |||||||||||||||
| 222 | 0 | my ($subtest) = @_; | |||||||||||||
| 223 | |||||||||||||||
| 224 | # If any optional modules are NOT available, then there should be no violations. | ||||||||||||||
| 225 | 0 | ▶ | return 0 if not _all_optional_modules_are_available($subtest); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 226 | 0 | return $subtest->{failures}; | |||||||||||||
| 227 | } | ||||||||||||||
| 228 | |||||||||||||||
| 229 | #----------------------------------------------------------------------------- | ||||||||||||||
| 230 | |||||||||||||||
| 231 | 0 | ▶ | sub _all_optional_modules_are_available { | ||||||||||||
|
Subroutine
_all_optional_modules_are_available: not called CC=2 SCAR=17.9
| |||||||||||||||
| 232 | 0 | my ($subtest) = @_; | |||||||||||||
| 233 | 0 | ▶ | my $optional_modules = $subtest->{optional_modules} or return 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 234 | 0 | ▶ | return all {eval "require $_;" or 0;} split m/,\s*/xms, $optional_modules; | ||||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
| |||||||||||||||
| 235 | } | ||||||||||||||
| 236 | |||||||||||||||
| 237 | #----------------------------------------------------------------------------- | ||||||||||||||
| 238 | |||||||||||||||
| 239 | 0 | ▶ | sub _create_test_name { | ||||||||||||
|
Subroutine
_create_test_name: not called CC=1 SCAR=6.9
| |||||||||||||||
| 240 | 0 | my ($policy, $subtest) = @_; | |||||||||||||
| 241 | 0 | return join ' - ', $policy, "line $subtest->{lineno}", $subtest->{name}; | |||||||||||||
| 242 | } | ||||||||||||||
| 243 | |||||||||||||||
| 244 | #----------------------------------------------------------------------------- | ||||||||||||||
| 245 | 1 | 1; | |||||||||||||
| 246 | |||||||||||||||
| 247 | __END__ | ||||||||||||||