blib/lib/Test/Expr.pm

filter: Statementstmt 65.8% 102 / 155 Branchbran 50.0% 16 / 32 Conditioncond 46.8% 15 / 32 MC/DCmcdc 38.8% 7 / 18 Subroutinesub 87.5% 14 / 16 Podpod n/a totaltotal 60.8% 154 / 253 CC 29 SCAR 30.1
CC
29
Cov
61%
__ANON__
32.2
__ANON__
32.2
import
7.5
CRAP 20.2
↑ Summary
1 package Test::Expr;
2 4 our $VERSION = '0.000011';
3  
4 4 ▶use 5.012; use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Keyword::Declare;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Data::Dump;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use List::Util 'max';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 4 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 4 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 our @EXPORT = @Test::More::EXPORT;
11  
12 45 ▶sub _trim {
Subroutine _trim: called CC=1 SCAR=0.0
13 45 my $str = shift;
14 45 $str =~ s{\A\s*|\s*\Z}{}g;
15 45 return $str;
16 }
17  
18 4 my $PERL_MATCHABLE = qr{ ^ (?&PerlOWS)
19 (?>
20 (?&PerlMatch)
21 | (?&PerlSubstitution)
22 | (?&PerlTransliteration)
23 )
24 $PPR::GRAMMAR
25 }xms;
26  
27 4 ▶use re 'eval';
Subroutine BEGIN: called CC=1 SCAR=0.0
28 my $PERL_FIND_VARS = qr{
29 ^ (?&PerlExpression)
30  
31 (?(DEFINE)
32 (?<PerlVariable>
33 (
34 (?= [\$\@%] )
35 (?>
36 (?&PerlScalarAccess)
37 | (?&PerlHashAccess)
38 | (?&PerlArrayAccess)
39 )
40 )
41 (?{ $Test::Expr::vars{$^N} = 1 })
42 )
43  
44 (?<PerlScalarAccessNoSpace>
45 (
46 (?>(?&PerlVariableScalarNoSpace))
47 (?:
48 (?:
49 (?:
50 ->
51 (?&PerlParenthesesList)
52 |
53 (?: -> )?+
54 (?> \$\* | (?&PerlArrayIndexer) | (?&PerlHashIndexer) )
55 )
56 (?:
57 (?: -> )?+
58 (?> \$\* | (?&PerlArrayIndexer) | (?&PerlHashIndexer) | (?&PerlParenthesesList) )
59 )*+
60 )?+
61 (?:
62 ->
63 [\@%]
64 (?> \* | (?&PerlArrayIndexer) | (?&PerlHashIndexer) )
65 )?+
66 )?+
67 )
68 (?{ $Test::Expr::vars{$^N} = 1 })
69 ) # End of rule
70  
71  
72 (?<PerlArrayAccessNoSpace>
73 (
74 (?>(?&PerlVariableArrayNoSpace))
75 (?:
76 (?: -> )?+
77 (?> \$\* | (?&PerlArrayIndexer) | (?&PerlHashIndexer) | (?&PerlParenthesesList) )
78 )*+
79 (?:
80 ->
81 [\@%]
82 (?> \* | (?&PerlArrayIndexer) | (?&PerlHashIndexer) )
83 )?+
84 )
85 (?{ $Test::Expr::vars{$^N} = 1 })
86 ) # End of rule
87 )
88  
89 $PPR::GRAMMAR
90 4 }xms;
91  
92 4 my $PERL_LITERAL = qr{^ (?> (?&PerlString)
93 | (?&PerlQuotelikeQR)
94 | (?&PerlQuotelikeQW)
95 | (?&PerlNumber) )
96 $
97 $PPR::GRAMMAR
98 }xms;
99  
100 4 my $PERL_EXPR = qr{
101 ^ (?>(?&PerlOWS)) (?<lhs> (?>(?&PerlHighBinaryExpression)) )
102 (?>(?&PerlOWS)) (?<op> (?> [<>]=?|[=!]=|<=>|[~=!]~|\b(?:[lg]t|[lgn]e|eq|cmp)\b))
103 (?>(?&PerlOWS)) (?<rhs> .*)
104  
105 (?(DEFINE)
106  
107 (?<PerlHighBinaryExpression>
108 (?>(?&PerlPrefixPostfixTerm))
109 (?: (?>(?&PerlOWS)) (?>(?&PerlInfixHighBinaryOperator))
110 (?>(?&PerlOWS)) (?&PerlPrefixPostfixTerm) )*+
111 )
112  
113 (?<PerlInfixHighBinaryOperator>
114 (?> [+] (?! [+=] )
115 | - (?! [-=] )
116 | [.]{2,3}+
117 | [.%x] (?! [=] )
118 | [&|^][.] (?! [=] )
119 | [*&|/]{1,2}+ (?! [=] )
120 | [<>]{2} (?! [=] )
121 | \^ (?! [=] )
122 )
123 ) # End of rule
124 )
125  
126 $PPR::GRAMMAR
127  
128 }mxs;
129  
130 3 ▶sub import {
Subroutine import: called CC=5 SCAR=20.1
131 3 my ($package) = @_;
132 3 $package->export_to_level(1, @_);
133 3  
134 0 ▶ keyword ok (Expr $test) {{{
Subroutine __ANON__: called CC=1 SCAR=0.8
Subroutine __ANON__: not called CC=1 SCAR=0.8
Branch: 1 branch
BranchTrueFalseTotal
if (((caller 0)[3] // "") =~ /\b(?:un)?import\Z/u) 3 0 3
Condition50%1/2
(caller 0)[3] // ""
l!l
30
MC/DC0%0/1
(caller 0)[3] // ""
(caller 0)[3]
Truth table50%1/2
A
A= (caller 0)[3]
Aresult
11
00
135 0 ok do{«$test»}, q{«$test»};
136 0 }}}
137 3  
138 0 ▶ keyword ok (ListElem $test, Comma, ListElem $desc) {
Subroutine __ANON__: called CC=13 SCAR=34.7
Subroutine __ANON__: not called CC=13 SCAR=34.7
Branch: 1 branch
BranchTrueFalseTotal
if (((caller 0)[3] // "") =~ /\b(?:un)?import\Z/u) 3 0 3
Condition50%1/2
(caller 0)[3] // ""
l!l
30
MC/DC0%0/1
(caller 0)[3] // ""
(caller 0)[3]
Truth table50%1/2
A
A= (caller 0)[3]
Aresult
11
00
139 0 # Work out what values to report if there's a problem...
140 0 (my $test_code = $test) =~ s/^\s*do\s*\{(.*)\}\s*$/$1/;
141 3 ▶ 
Subroutine BEGIN: called CC=1 SCAR=0.5
142 0 # Handle low-precedence prefix not...
143 0 (my $pos_test_code = $test_code) =~ s/^\s*(not)\b//;
144 0 my $negative = $pos_test_code eq $test_code ? q{} : 'not';
145  
146 0 # Extract components of test, if possible...
147 $pos_test_code =~ $PERL_EXPR;
148 0 my ($lhs, $op, $rhs) = @+{qw<lhs op rhs>};
149  
150 # These hold rearranged code to capture and test component values (if possible)...
151 0 my $test_setup;
152 0 ▶ my $test_expr;
Branch: 2 branches
BranchTrueFalseTotal
$pos_test_code eq $test_code ? : 20 2 22
$pos_test_code eq $test_code ? : 0 0 0
153  
154 # Don't try to capture explicit regex matches on the rhs...
155 0 if ($op && $op =~ /[=!~]~/ && $rhs =~ $PERL_MATCHABLE) {
156 0 $test_setup = qq{ my \$_____l_h_s_____ = $lhs; };
157 $test_expr = qq{ $negative \$_____l_h_s_____ $op $rhs };
158 $rhs = "";
159 0 }
160  
161 # Catch lhs and rhs values for other comparison operators...
162 elsif ($op) {
163 0 ▶ $test_setup = qq{ my \$_____l_h_s_____ = $lhs; my \$_____r_h_s_____ = $rhs; };
Branch: 4 branches
BranchTrueFalseTotal
if ($op and $op =~ /[=!~]~/u and $rhs =~ /$PERL_MATCHABLE/u) { } 6 16 22
elsif ($op) { } 13 3 16
if ($op and $op =~ /[=!~]~/u and $rhs =~ /$PERL_MATCHABLE/u) { } 0 0 0
elsif ($op) { } 0 0 0
Condition50%6/12
$op and $op =~ /[=!~]~/u and $rhs =~ /$PERL_MATCHABLE/u
!ll&&!rl&&r
1426
$op and $op =~ /[=!~]~/u
!ll&&!rl&&r
3118
$op and $op =~ /[=!~]~/u and $rhs =~ /$PERL_MATCHABLE/u
!ll&&!rl&&r
000
$op and $op =~ /[=!~]~/u
!ll&&!rl&&r
000
MC/DC100%3/3
$op and $op =~ /[=!~]~/u and $rhs =~ /$PERL_MATCHABLE/u
$op $op =~ /[=!~]~/u $rhs =~ /$PERL_MATCHABLE/u
MC/DC0%0/3
$op and $op =~ /[=!~]~/u and $rhs =~ /$PERL_MATCHABLE/u
$op $op =~ /[=!~]~/u $rhs =~ /$PERL_MATCHABLE/u
Truth table100%4/4
A and B and C
A= $opB= $op =~ /[=!~]~/uC= $rhs =~ /$PERL_MATCHABLE/u
ABCresult
0XX0
10X0
1100
1111
Truth table0%0/4
A and B and C
A= $opB= $op =~ /[=!~]~/uC= $rhs =~ /$PERL_MATCHABLE/u
ABCresult
0XX0
10X0
1100
1111
164 0 $test_expr = qq{ $negative \$_____l_h_s_____ $op \$_____r_h_s_____ };
165 0 }
166 0  
167 # Otherwise just execute the text verbatim...
168 else {
169 $lhs = "";
170 $rhs = "";
171 0 $test_setup = qq{my \$_____l_h_s_____ = $test_code;};
172 0 $test_expr = q{$_____l_h_s_____};
173 }
174  
175 # Extract and tidy all variables in the test expression...
176 %Test::Expr::vars = ();
177 0 $test_code =~ $PERL_FIND_VARS;
178 0 my @vars = grep {defined} keys %Test::Expr::vars;
179 0 ($lhs, $rhs, @vars) = map {_trim $_} $lhs, $rhs, @vars;
180 0  
181 # Find maximum width of any reported value, so we can align them...
182 my $var_len = max map {length} $lhs, $rhs, @vars;
183  
184 0 # Generate diagnostics (reporting each distinct value only once)...
185 0 my %seen = ( $rhs => 1, $lhs => 1 );
186 0 my @diagnostics = (
187 0 ( $lhs && $lhs !~ $PERL_LITERAL
188 ? qq{diag sprintf(q{ %${var_len}s --> }, q{$lhs}),
189 Data::Dump::dump(\$_____l_h_s_____);}
190 0 : ()
191 ),
192 ( $rhs && $rhs !~ $PERL_LITERAL
193 0 ? qq{diag sprintf(q{ %${var_len}s --> }, q{$rhs}),
194 Data::Dump::dump(\$_____r_h_s_____);}
195 : ()
196 ),
197 map {qq{diag sprintf(q{ %${var_len}s --> }, q{$_}), Data::Dump::dump($_);}}
198 grep { !$seen{$_}++ }
199 @vars
200 );
201  
202 # Build the diagnostic code...
203 unshift @diagnostics, qq{diag q{ because:};}
204 if @diagnostics;
205 0 unshift @diagnostics, qq{diag q{}; diag q{ ($test_code) was false};}
206 0 ▶ if (eval($desc)//'') ne qq{$test_code};
Branch: 4 branches
BranchTrueFalseTotal
$lhs && !($lhs =~ /$PERL_LITERAL/u) ? : 16 6 22
$rhs && !($rhs =~ /$PERL_LITERAL/u) ? : 7 15 22
$lhs && !($lhs =~ /$PERL_LITERAL/u) ? : 0 0 0
$rhs && !($rhs =~ /$PERL_LITERAL/u) ? : 0 0 0
Condition50%6/12
$lhs and not $lhs =~ /$PERL_LITERAL/u
!ll&&!rl&&r
3316
$rhs and not $rhs =~ /$PERL_LITERAL/u
!ll&&!rl&&r
967
$lhs and not $lhs =~ /$PERL_LITERAL/u
!ll&&!rl&&r
000
$rhs and not $rhs =~ /$PERL_LITERAL/u
!ll&&!rl&&r
000
MC/DC100%2/2
$lhs and not $lhs =~ /$PERL_LITERAL/u
$lhs not $lhs =~ /$PERL_LITERAL/u
MC/DC100%2/2
$rhs and not $rhs =~ /$PERL_LITERAL/u
$rhs not $rhs =~ /$PERL_LITERAL/u
MC/DC0%0/2
$lhs and not $lhs =~ /$PERL_LITERAL/u
$lhs not $lhs =~ /$PERL_LITERAL/u
MC/DC0%0/2
$rhs and not $rhs =~ /$PERL_LITERAL/u
$rhs not $rhs =~ /$PERL_LITERAL/u
Truth table100%3/3
A and not(B)
A= $lhsB= not $lhs =~ /$PERL_LITERAL/u
ABresult
0X0
100
111
Truth table100%3/3
A and not(B)
A= $rhsB= not $rhs =~ /$PERL_LITERAL/u
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $lhsB= not $lhs =~ /$PERL_LITERAL/u
ABresult
0X0
100
111
Truth table0%0/3
A and not(B)
A= $rhsB= not $rhs =~ /$PERL_LITERAL/u
ABresult
0X0
100
111
207  
208 # Build the test code...
209 my $new_test_code = qq{
210 {
211 0 ▶ $test_setup
Branch: 2 branches
BranchTrueFalseTotal
if @diagnostics 16 6 22
if @diagnostics 0 0 0
212 if ($test_expr) {
213 0 ▶ Test::More::ok(1, $desc);
Branch: 2 branches
BranchTrueFalseTotal
if (eval $desc // "") ne "$test_code" 1 21 22
if (eval $desc // "") ne "$test_code" 0 0 0
Condition25%1/4
eval $desc // ""
l!l
220
eval $desc // ""
l!l
00
MC/DC0%0/1
eval $desc // ""
eval $desc
MC/DC0%0/1
eval $desc // ""
eval $desc
Truth table50%1/2
A
A= eval $desc
Aresult
11
00
Truth table0%0/2
A
A= eval $desc
Aresult
11
00
214 }
215 else {
216 fail($desc); @diagnostics diag q{};
217 0 }
218 }
219 };
220 $new_test_code =~ tr/\n/ /;
221 return $new_test_code;
222 }
223 }
224  
225 4 1; # Magic true value required at end of module
226 __END__
↑ Summary