File Coverage

blib/lib/Perl/Lint/Policy/TestingAndDebugging/RequireTestLabels.pm
Criterion Covered Total %
statement 75 75 100.0
branch 43 46 93.4
condition 34 47 72.3
subroutine 8 8 100.0
pod 0 1 0.0
total 160 177 90.4


line stmt bran cond sub pod time code
1             package Perl::Lint::Policy::TestingAndDebugging::RequireTestLabels;
2 133     133   69836 use strict;
  133         171  
  133         3032  
3 133     133   398 use warnings;
  133         131  
  133         2707  
4 133     133   394 use List::Util qw/any/;
  133         126  
  133         6193  
5 133     133   828 use Perl::Lint::Constants::Type;
  133         163  
  133         58737  
6 133     133   536 use parent "Perl::Lint::Policy";
  133         137  
  133         519  
7              
8             use constant {
9 133         64052 DESC => 'Test without a label',
10             EXPL => 'Add a label argument to all Test::More functions',
11 133     133   6672 };
  133         148  
12              
13             sub evaluate {
14 8     8 0 19 my ($class, $file, $tokens, $src, $args) = @_;
15              
16 8         15 my @target_test_module = ('Test::More');
17              
18 8 100       20 if (my $this_policies_arg = $args->{require_test_labels}) {
19 2   50     12 push @target_test_module, split / /, ($this_policies_arg->{modules} || '');
20             }
21              
22 8         10 my @violations;
23 8         22 my $is_loaded = 0;
24 8         9 my $token_num = scalar @$tokens;
25 8         22 for (my $i = 0; $i < $token_num; $i++) {
26 91         73 my $token = $tokens->[$i];
27 91         65 my $token_type = $token->{type};
28 91         70 my $token_data = $token->{data};
29              
30             # for checking Test::More is loaded
31 91 100 66     228 if ($token_type == USE_DECL || $token_type == REQUIRE_DECL) {
32 6 50       13 next if $is_loaded;
33              
34 6         9 my $used_module_name = '';
35 6         18 for ($i++; $i < $token_num; $i++) {
36 24         16 my $token = $tokens->[$i];
37 24         23 my $token_type = $token->{type};
38 24 100 100     58 if ($token_type == NAMESPACE || $token_type == NAMESPACE_RESOLVER) {
39 18         37 $used_module_name .= $token->{data};
40             }
41             else {
42 6         10 last;
43             }
44             }
45              
46 6 100   10   60 if (any {$_ eq $used_module_name} @target_test_module) {
  10         22  
47 5         6 $is_loaded = 1;
48             }
49 6         26 next;
50             }
51              
52 85 100       124 if ($token_type == KEY) {
53 48 100 100     125 if ($token_data eq 'pass' || $token_data eq 'fail') {
54 8 100 66     31 if (
      66        
55             $tokens->[$i+1]->{type} == SEMI_COLON ||
56             (
57             $tokens->[$i+1]->{type} == LEFT_PAREN &&
58             $tokens->[$i+2]->{type} == RIGHT_PAREN
59             )
60             ) {
61             push @violations, {
62             filename => $file,
63             line => $token->{line},
64 6         13 description => DESC,
65             explanation => EXPL,
66             policy => __PACKAGE__,
67             };
68             }
69 8         13 next;
70             }
71              
72 40         30 my $expected_commma_num = 0;
73 40 100 100     183 if ($token_data eq 'ok') {
    100 100        
    50 100        
      66        
74 11         9 $expected_commma_num = 1;
75             }
76             elsif ($token_data eq 'cmp_ok') {
77 3         3 $expected_commma_num = 3;
78             }
79             elsif (
80             $token_data eq 'is' ||
81             $token_data eq 'isnt' ||
82             $token_data eq 'like' ||
83             $token_data eq 'unlike' ||
84             $token_data eq 'is_deeply'
85             ) {
86 26         20 $expected_commma_num = 2;
87             }
88              
89 40 50       73 if ($expected_commma_num) {
90 40         31 my $left_paren_num = 0;
91 40         26 my $left_brace_num = 0;
92 40         26 my $left_bracket_num = 0;
93 40         30 my $comma_num = 0;
94              
95 40 100       71 $i++ if $tokens->[$i+1]->{type} == LEFT_PAREN;
96              
97 40         60 for ($i++; $i < $token_num; $i++) {
98 257         161 my $token = $tokens->[$i];
99 257         189 my $token_type = $token->{type};
100 257         161 my $token_data = $token->{data};
101              
102 257 100 66     1242 if ($token_type == LEFT_PAREN) {
    100 66        
    100 33        
    100 66        
    100 66        
    100 33        
    100          
    100          
103 1         3 $left_paren_num++;
104             }
105             elsif ($token_type == LEFT_BRACE) {
106 10         14 $left_brace_num++;
107             }
108             elsif ($token_type == LEFT_BRACKET) {
109 6         9 $left_bracket_num++;
110             }
111             elsif ($token_type == RIGHT_PAREN) {
112 40         52 $left_paren_num--;
113             }
114             elsif ($token_type == RIGHT_BRACE) {
115 10         11 $left_brace_num--;
116             }
117             elsif ($token_type == RIGHT_BRACKET) {
118 6         7 $left_bracket_num--;
119             }
120             elsif (
121             $token_type == COMMA &&
122             $left_paren_num <= 0 &&
123             $left_brace_num <= 0 &&
124             $left_bracket_num <= 0
125             ) {
126 44         60 $comma_num++;
127             }
128             elsif (
129             $token_type == SEMI_COLON &&
130             $left_paren_num <= 0 &&
131             $left_brace_num <= 0 &&
132             $left_bracket_num <= 0
133             ) {
134 40 100       55 if ($comma_num < $expected_commma_num) {
135             push @violations, {
136             filename => $file,
137             line => $token->{line},
138 28         78 description => DESC,
139             explanation => EXPL,
140             policy => __PACKAGE__,
141             };
142             }
143 40         80 last;
144             }
145             }
146             }
147             }
148             }
149              
150 8 100       35 return \@violations if $is_loaded;
151 3         20 return [];
152             }
153              
154             1;
155