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   96725 use strict;
  133         241  
  133         4785  
3 133     133   554 use warnings;
  133         200  
  133         3654  
4 133     133   599 use List::Util qw/any/;
  133         225  
  133         8949  
5 133     133   1261 use Perl::Lint::Constants::Type;
  133         191  
  133         81909  
6 133     133   737 use parent "Perl::Lint::Policy";
  133         228  
  133         832  
7              
8             use constant {
9 133         88339 DESC => 'Test without a label',
10             EXPL => 'Add a label argument to all Test::More functions',
11 133     133   8604 };
  133         289  
12              
13             sub evaluate {
14 8     8 0 22 my ($class, $file, $tokens, $src, $args) = @_;
15              
16 8         20 my @target_test_module = ('Test::More');
17              
18 8 100       27 if (my $this_policies_arg = $args->{require_test_labels}) {
19 2   50     18 push @target_test_module, split / /, ($this_policies_arg->{modules} || '');
20             }
21              
22 8         12 my @violations;
23 8         11 my $is_loaded = 0;
24 8         12 my $token_num = scalar @$tokens;
25 8         29 for (my $i = 0; $i < $token_num; $i++) {
26 91         74 my $token = $tokens->[$i];
27 91         85 my $token_type = $token->{type};
28 91         84 my $token_data = $token->{data};
29              
30             # for checking Test::More is loaded
31 91 100 66     254 if ($token_type == USE_DECL || $token_type == REQUIRE_DECL) {
32 6 50       17 next if $is_loaded;
33              
34 6         9 my $used_module_name = '';
35 6         23 for ($i++; $i < $token_num; $i++) {
36 24         27 my $token = $tokens->[$i];
37 24         26 my $token_type = $token->{type};
38 24 100 100     73 if ($token_type == NAMESPACE || $token_type == NAMESPACE_RESOLVER) {
39 18         38 $used_module_name .= $token->{data};
40             }
41             else {
42 6         19 last;
43             }
44             }
45              
46 6 100   10   85 if (any {$_ eq $used_module_name} @target_test_module) {
  10         30  
47 5         10 $is_loaded = 1;
48             }
49 6         29 next;
50             }
51              
52 85 100       140 if ($token_type == KEY) {
53 48 100 100     154 if ($token_data eq 'pass' || $token_data eq 'fail') {
54 8 100 66     39 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 6         17 push @violations, {
62             filename => $file,
63             line => $token->{line},
64             description => DESC,
65             explanation => EXPL,
66             policy => __PACKAGE__,
67             };
68             }
69 8         14 next;
70             }
71              
72 40         29 my $expected_commma_num = 0;
73 40 100 100     210 if ($token_data eq 'ok') {
    100 100        
    50 100        
      66        
74 11         15 $expected_commma_num = 1;
75             }
76             elsif ($token_data eq 'cmp_ok') {
77 3         4 $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         24 $expected_commma_num = 2;
87             }
88              
89 40 50       58 if ($expected_commma_num) {
90 40         35 my $left_paren_num = 0;
91 40         29 my $left_brace_num = 0;
92 40         35 my $left_bracket_num = 0;
93 40         31 my $comma_num = 0;
94              
95 40 100       80 $i++ if $tokens->[$i+1]->{type} == LEFT_PAREN;
96              
97 40         63 for ($i++; $i < $token_num; $i++) {
98 257         195 my $token = $tokens->[$i];
99 257         197 my $token_type = $token->{type};
100 257         200 my $token_data = $token->{data};
101              
102 257 100 66     1546 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         18 $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         66 $left_paren_num--;
113             }
114             elsif ($token_type == RIGHT_BRACE) {
115 10         17 $left_brace_num--;
116             }
117             elsif ($token_type == RIGHT_BRACKET) {
118 6         13 $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         61 $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       59 if ($comma_num < $expected_commma_num) {
135 28         98 push @violations, {
136             filename => $file,
137             line => $token->{line},
138             description => DESC,
139             explanation => EXPL,
140             policy => __PACKAGE__,
141             };
142             }
143 40         85 last;
144             }
145             }
146             }
147             }
148             }
149              
150 8 100       45 return \@violations if $is_loaded;
151 3         27 return [];
152             }
153              
154             1;
155