blib/lib/Test/Simpler.pm

filter: Statementstmt 99.0% 210 / 212 Branchbran 88.8% 64 / 72 Conditioncond 79.1% 19 / 24 MC/DCmcdc 40.0% 6 / 15 Subroutinesub 100% 31 / 31 Podpod 100% 1 / 1 totaltotal 93.2% 331 / 355 CC 50 SCAR 31.8
CC
50
Cov
95%
ok
43.1
_tidy_values
5.5
_rebuild_code
3.0
CRAP 24.0
↑ Summary
1 package Test::Simpler;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use autodie;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 our $VERSION = '0.000008';
9  
10 3 ▶use PadWalker qw< peek_my peek_our >;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Data::Dump qw< dump >;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use List::Util qw< max >;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 ▶use base 'Test::Builder::Module';
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # Export the module's interface...
17 3 our @EXPORT = ( 'ok' );
18 3 our @EXPORT_OK = ();
19 3 our %EXPORT_TAGS = ();
20  
21 10 ▶sub ok($;$) {
Subroutine ok: called CC=43 SCAR=37.6
Pod documented
22 10 my $outcome = shift;
23 10 ▶ my $desc = @_ ? "@_" : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 4 10
24  
25 # Grab the upscope variables...
26 10 my %value_for = ( %{peek_our(1)}, %{peek_my(1)} );
27  
28 # Cache for source code...
29 10 state %source;
30  
31 # Where were we called???
32 10 my ($package, $file, $line) = caller;
33  
34 # Grab the source...
35 10 ▶ if (!exists $source{$file}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $source{$file}) 2 8 10
36 2 open my $fh, '<', $file;
37 2 $source{$file} = do { local $/; readline $fh };
38 }
39 10 my $source = $source{$file};
40 10 my $remove_lines = $line - 1;
41 10 $source =~ s{ \A (?: \N*\n ){$remove_lines} }{}xms;
42  
43 # Extract code from source...
44 3 ▶ use PPI;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 10 my $doc = PPI::Document->new(\$source);
46  
47 # Extract statement from code...
48 10 my @target;
49 STATEMENT:
50 10 for my $statement (@{ $doc->find('PPI::Statement') }) {
51 10 my @token = $statement->children;
52 10 ▶ next STATEMENT if $token[0]->content ne 'ok';
Branch: 1 branch
BranchTrueFalseTotal
if $token[0]->content ne "ok" 0 10 10
53 10 @target = @token[1..$#token]; # don't need the 'ok'
54 10 last STATEMENT;
55 }
56  
57 # Did we find the statement?
58 10 ▶ die "Can't understand arguments to ok()" if !@target;
Branch: 1 branch
BranchTrueFalseTotal
unless @target 0 10 10
59  
60 # Flatten to a list of relevant tokens...
61 SKIPPED:
62 10 while (1) {
63 # Remove whitespaces...
64 24 ▶ if ($target[0]->isa('PPI::Token::Whitespace')) {
Branch: 3 branches
BranchTrueFalseTotal
if ($target[0]->isa('PPI::Token::Whitespace')) { } 10 14 24
elsif ($target[0]->isa('PPI::Structure::List')) { } 2 12 14
elsif ($target[0]->isa('PPI::Statement::Expression')) { } 2 10 12
65 10 shift @target;
66 }
67 # Step into lists...
68 elsif ($target[0]->isa('PPI::Structure::List')) {
69 2 @target = $target[0]->children;
70 }
71 # Step into expressions...
72 elsif ($target[0]->isa('PPI::Statement::Expression')) {
73 2 @target = $target[0]->children;
74 }
75 else {
76 10 last SKIPPED;
77 }
78 }
79  
80 # Find first comma or end-of-statement (i.e. end of first arg)...
81 TOKEN:
82 10 for my $n (0..$#target) {
83 73 my $target = $target[$n];
84  
85 # The comma is an operator...
86 73 ▶ if ($target->isa('PPI::Token::Operator')
Branch: 1 branch
BranchTrueFalseTotal
if ($target->isa("PPI::Token::Operator") or $target->isa("PPI::Token::Structure")) 20 53 73
Condition66%2/3
$target->isa("PPI::Token::Operator") or $target->isa("PPI::Token::Structure")
l!l&&r!l&&!r
20053
MC/DC50%1/2
$target->isa("PPI::Token::Operator") or $target->isa("PPI::Token::Structure")
$target->isa("PPI::Token::Operator") $target->isa("PPI::Token::Structure")
Truth table66%2/3
A or B
A= $target->isa("PPI::Token::Operator")B= $target->isa("PPI::Token::Structure")
ABresult
1X1
011
000
87 || $target->isa('PPI::Token::Structure')) {
88 # But is the operator the one we want???
89 20 my $content = $target->content;
90 20 ▶ if ($content =~ m{^(?: , | => | ; )$}x) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content =~ /^(?: , | => | ; )$/ux) 6 14 20
91 # IF so, truncate tokens here and escape...
92 6 splice @target, $n;
93 6 last TOKEN;
94 }
95 }
96 }
97  
98 # Compact and clean up the resulting code...
99 10 my $test_code = _rebuild_code(@target);
100  
101 # Split on a comparison operator...
102 10 state $COMPARATOR
103 = qr{\A(?:
104 eq | ne | lt | le | gt | ge
105 | == | != | < | <= | > | >=
106 | =~ | !~ | ~~
107 ) \Z }x;
108  
109 10 my $expected_code = $test_code;
110 10 my ($got_code, $comparator);
111 10 for my $n (0..$#target) {
112 67 my $target = $target[$n]->content;
113  
114 # Find a comparison operator to split upon...
115 67 ▶ if ($target =~ $COMPARATOR) {
Branch: 1 branch
BranchTrueFalseTotal
if ($target =~ /$COMPARATOR/u) 8 59 67
116 8 $got_code = _rebuild_code(@target[0..$n-1]);
117 8 $comparator = $target;
118 8 $expected_code = _rebuild_code(@target[$n+1..$#target]);
119 }
120 }
121  
122  
123 10 ▶ $desc //= $test_code;
Condition66%2/3
$desc //= $test_code
l!l&&r!l&&!r
640
MC/DC0%0/2
$desc //= $test_code
$desc $test_code
Truth table66%2/3
A //= B
A= $descB= $test_code
ABresult
1X1
011
000
124  
125 # Extract all the variables from the code...
126 10 my @symbols = _uniq( map { _get_symbols($_) } @target );
127  
128 10 my @symbol_names;
129 my @symbol_lookup;
130  
131 10 for my $symbol (@symbols) {
132 11 my $subscript;
133 11 my $symbol_source = $symbol->content;
134 11 my $next_symbol = $symbol;
135  
136 ACCUMULATE_SYMBOL:
137 11 ▶ while ($next_symbol = $next_symbol->snext_sibling) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_symbol = $next_symbol->snext_sibling) 20 0 20
138 # A simple array or hash look-up???
139 23 ▶ if ($next_symbol->isa('PPI::Structure::Subscript')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($next_symbol->isa('PPI::Structure::Subscript')) { } 8 15 23
elsif ($next_symbol->content eq '->') { } 4 11 15
140 8 $subscript .= $next_symbol->content;
141 8 $symbol_source .= $next_symbol->content;
142 }
143  
144 # A dereferenced look-up or method call???
145 elsif ($next_symbol->content eq '->') {
146 # What's after the arrow???
147 4 $next_symbol = $next_symbol->snext_sibling;
148  
149 # Is it a subscript??? Then deal with it on the next loop...
150 4 ▶ if ($next_symbol->isa('PPI::Structure::Subscript')) {
Branch: 2 branches
BranchTrueFalseTotal
if ($next_symbol->isa('PPI::Structure::Subscript')) { } 2 2 4
elsif ($next_symbol->isa('PPI::Token::Word') or $next_symbol->isa('PPI::Token::Symbol')) { } 2 0 2
151 2 redo ACCUMULATE_SYMBOL;
152 }
153  
154 # Is it a method call??? Then deal with it here...
155 ▶ elsif ($next_symbol->isa('PPI::Token::Word') || $next_symbol->isa('PPI::Token::Symbol') ) {
Condition66%2/3
$next_symbol->isa("PPI::Token::Word") or $next_symbol->isa("PPI::Token::Symbol")
l!l&&r!l&&!r
110
MC/DC0%0/2
$next_symbol->isa("PPI::Token::Word") or $next_symbol->isa("PPI::Token::Symbol")
$next_symbol->isa("PPI::Token::Word") $next_symbol->isa("PPI::Token::Symbol")
Truth table66%2/3
A or B
A= $next_symbol->isa("PPI::Token::Word")B= $next_symbol->isa("PPI::Token::Symbol")
ABresult
1X1
011
000
156 2 my $methname = $next_symbol->content;
157 2 ▶ if ($next_symbol->isa('PPI::Token::Symbol') && $value_for{$next_symbol->content}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($next_symbol->isa("PPI::Token::Symbol") and $value_for{$next_symbol->content}) 1 1 2
Condition66%2/3
$next_symbol->isa("PPI::Token::Symbol") and $value_for{$next_symbol->content}
!ll&&!rl&&r
101
MC/DC50%1/2
$next_symbol->isa("PPI::Token::Symbol") and $value_for{$next_symbol->content}
$next_symbol->isa("PPI::Token::Symbol") $value_for{$next_symbol->content}
Truth table66%2/3
A and B
A= $next_symbol->isa("PPI::Token::Symbol")B= $value_for{$next_symbol->content}
ABresult
0X0
100
111
158 1 $methname = ${ $value_for{$next_symbol->content} }
159 }
160  
161 # Save the arrow and method name...
162 2 $subscript .= '->' . $methname;
163 2 $symbol_source .= '->' . $next_symbol->content;
164  
165 # Look for a trailing argument list...
166 2 $next_symbol = $next_symbol->snext_sibling;
167  
168 # Ignore this symbol if it's not a list...
169 redo ACCUMULATE_SYMBOL
170 2 ▶ if ! $next_symbol->isa('PPI::Structure::List');
Branch: 1 branch
BranchTrueFalseTotal
unless $next_symbol->isa("PPI::Structure::List") 1 1 2
171  
172 # Otherwise, keep the list and continue...
173 1 $subscript .= $next_symbol->content;
174 1 $symbol_source .= $next_symbol->content;
175 }
176 }
177 else {
178 11 last ACCUMULATE_SYMBOL;
179 }
180 }
181 11 my $symbol_name = $symbol->symbol;
182 11 ▶ my $symbol_lookup = $symbol->symbol_type eq '$'
Branch: 1 branch
BranchTrueFalseTotal
$symbol->symbol_type eq '$' ? : 9 2 11
183 ? '${$value_for{q{' . $symbol_name . '}}}'
184 : '$value_for{q{' . $symbol_name . '}}'
185 ;
186  
187 11 ▶ if (length $subscript) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $subscript) 6 5 11
188 6 $subscript =~ s{\A->}{}xms;
189 6 $symbol_lookup .= "->$subscript";
190 }
191  
192 11 push @symbol_names, $symbol_source;
193 11 push @symbol_lookup, $symbol_lookup;
194 }
195  
196 10 my $symlen = max map { length $_ } @symbol_names;
197  
198 # Now report the test...
199 10 local $Test::Builder::Level = $Test::Builder::Level + 1;
200 10 my $builder = Test::Builder->new;
201  
202 10 $builder->no_diag(1);
203 10 $builder->ok($outcome, $desc);
204 10 $builder->no_diag(0);
205  
206 # And report the problem (if any)...
207 10 ▶ if (!$outcome) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $outcome) 6 4 10
208 6 state $VAR_FORMAT = q{ %-*s --> %s};
209 6 $builder->diag(" Failed test at $file line $line");
210 6 ▶ $builder->diag(" $got_code") if defined $got_code;
Branch: 1 branch
BranchTrueFalseTotal
if defined $got_code 5 1 6
211 6 ▶ $builder->diag(" isn't $comparator") if defined $comparator;
Branch: 1 branch
BranchTrueFalseTotal
if defined $comparator 5 1 6
212 6 ▶ if (defined $comparator) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $comparator) { } 5 1 6
213 5 $builder->diag(" $expected_code");
214 5 $builder->diag(" Because:");
215 }
216 else {
217 1 $builder->diag(" Expected true value for: $expected_code");
218 1 $builder->diag(" But was false because:");
219 }
220 6 ▶ if (@symbol_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (@symbol_names) 5 1 6
221 5 for my $symbol ( @symbol_names ) {
222 7 my $symbol_lookup = shift @symbol_lookup;
223 7 ▶ $builder->diag(
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
224 sprintf $VAR_FORMAT, $symlen, $symbol,
225 _tidy_values(eval "package $package; no warnings; $symbol_lookup")
226 );
227 }
228 5 $builder->diag(q{});
229 }
230 6 ▶ if (defined $got_code) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $got_code) 5 1 6
231 5 ▶ my $got_code_value = eval "package $package; no warnings; $got_code";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
232 5 ▶ my $expected_code_value = eval "package $package; no warnings; $expected_code";
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
233 5 ▶ my $symlen = max map { defined $_ ? length $_ : 0 } $got_code, $expected_code;
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 10 0 10
234 5 ▶ if (defined( $got_code_value // $expected_code_value ) && !@symbol_names) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($got_code_value // $expected_code_value) and not @symbol_names) 1 4 5
Condition100%6/6
defined($got_code_value // $expected_code_value) and not @symbol_names
!ll&&!rl&&r
131
$got_code_value // $expected_code_value
l!l&&r!l&&!r
131
MC/DC33%1/3
defined($got_code_value // $expected_code_value) and not @symbol_names
$got_code_value $expected_code_value not @symbol_names
Truth table60%3/5
A // B and not(C)
A= $got_code_valueB= $expected_code_valueC= not @symbol_names
ABCresult
00X0
1X00
0100
1X11
0111
235 1 $builder->diag(" because:");
236 }
237 5 ▶ if (defined $got_code_value && $got_code_value ne $got_code) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $got_code_value and $got_code_value ne $got_code) 1 4 5
Condition66%2/3
defined $got_code_value and $got_code_value ne $got_code
!ll&&!rl&&r
401
MC/DC50%1/2
defined $got_code_value and $got_code_value ne $got_code
defined $got_code_value $got_code_value ne $got_code
Truth table66%2/3
A and B
A= defined $got_code_valueB= $got_code_value ne $got_code
ABresult
0X0
100
111
238 1 $builder->diag( sprintf $VAR_FORMAT, $symlen, $got_code, $got_code_value);
239 }
240 5 ▶ if (defined $expected_code_value && $expected_code_value ne $expected_code) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $expected_code_value and $expected_code_value ne $expected_code) 2 3 5
Condition100%3/3
defined $expected_code_value and $expected_code_value ne $expected_code
!ll&&!rl&&r
122
MC/DC100%2/2
defined $expected_code_value and $expected_code_value ne $expected_code
defined $expected_code_value $expected_code_value ne $expected_code
Truth table100%3/3
A and B
A= defined $expected_code_valueB= $expected_code_value ne $expected_code
ABresult
0X0
100
111
241 2 $builder->diag( sprintf $VAR_FORMAT, $symlen, $expected_code, $expected_code_value);
242 }
243 }
244 }
245 }
246  
247 26 ▶sub _rebuild_code {
Subroutine _rebuild_code: called CC=3 SCAR=11.0
248 26 my $code = join q{}, map { my $content = $_;
249 122 ▶ $content =~ /^\n+/ ? q{}
Branch: 2 branches
BranchTrueFalseTotal
$content =~ /^\n+/u ? : 4 118 122
$content =~ /^\s*$/u ? : 40 78 118
250 : $content =~ /^\s*$/ ? q{ }
251 : $_
252 } @_;
253 26 return $code =~ s{\A\s+|\s+\Z}{}gr;
254 }
255  
256 7 ▶sub _tidy_values {
Subroutine _tidy_values: called CC=4 SCAR=17.1
257 7 my ($ref) = @_;
258  
259 7 my $type = ref($ref);
260  
261 0 ▶ return $type eq 'ARRAY' ? dump @{$ref}
Branch: 2 branches
BranchTrueFalseTotal
$type eq 'HASH' ? : 0 7 7
$type eq 'SCALAR' ? : 0 7 7
262 : $type eq 'HASH' ? dump($ref) =~ s/^{/(/r =~ s/}$/)/r
263 0 ▶ : $type eq 'SCALAR' ? dump ${$ref}
Branch: 1 branch
BranchTrueFalseTotal
$type eq 'ARRAY' ? : 0 7 7
264 : dump $ref;
265 }
266  
267 117 ▶sub _get_symbols {
Subroutine _get_symbols: called CC=2 SCAR=6.9
268 117 my $element = shift;
269 117 ▶ return $element if $element->isa('PPI::Token::Symbol');
Branch: 1 branch
BranchTrueFalseTotal
if $element->isa("PPI::Token::Symbol") 13 104 117
270 104 return map { _get_symbols($_) } eval{ $element->children };
271 }
272  
273 10 ▶sub _uniq {
Subroutine _uniq: called CC=2 SCAR=6.9
274 10 my %seen;
275 10 ▶ return grep { $seen{$_}++ ? () : $_ } @_;
Branch: 1 branch
BranchTrueFalseTotal
$seen{$_}++ ? : 2 11 13
276 }
277  
278 3 1; # Magic true value required at end of module
279 __END__
↑ Summary