assertions.pm

filter: Statementstmt 83.0% 54 / 65 Branchbran 76.6% 46 / 60 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 8 / 8 Subroutinesub 85.7% 6 / 7 Podpod 100% 2 / 2 totaltotal 83.1% 128 / 154 CC 37 SCAR 29.4
CC
37
Cov
82%
_calc_expr
26.0
seen
20.0
enabled
7.2
CRAP 18.9
↑ Summary assertions/activate.pm »
1 package assertions;
2  
3 2 our $VERSION = '0.03';
4  
5 # use strict;
6 # use warnings;
7  
8 2 my $hint=0x01000000;
9 2 my $seen_hint=0x02000000;
10  
11 5 ▶sub _syntax_error ($$) {
Subroutine _syntax_error: called CC=1 SCAR=0.0
12 5 my ($expr, $why)=@_;
13 5 require Carp;
14 5 Carp::croak("syntax error on assertion filter '$expr' ($why)");
15 }
16  
17 2 ▶sub _carp {
Subroutine _carp: called CC=2 SCAR=9.2
18 2 require warnings;
19 2 ▶ if (warnings::enabled('assertions')) {
Branch: 1 branch
BranchTrueFalseTotal
if (warnings::enabled("assertions")) 0 2 2
20 0 require Carp;
21 0 Carp::carp(@_);
22 }
23 }
24  
25 42 ▶sub _calc_expr {
Subroutine _calc_expr: called CC=26 SCAR=32.6
26 42 my $expr=shift;
27 42 my @tokens=split / \s*
28 ( && # and
29 | \|\| # or
30 | \( # parents
31 | \) )
32 \s*
33 | \s+ # spaces out
34 /x, $expr;
35  
36 # print STDERR "tokens: -", join('-',@tokens), "-\n";
37  
38 42 my @now=1;
39 42 my @op='start';
40  
41 42 for my $t (@tokens) {
42 161 ▶ next if (!defined $t or $t eq '');
Branch: 1 branch
BranchTrueFalseTotal
if not defined $t or $t eq "" 24 137 161
Condition100%3/3
not defined $t or $t eq ""
l!l&&r!l&&!r
222137
MC/DC100%2/2
not defined $t or $t eq ""
not defined $t $t eq ""
Truth table100%3/3
not(A) or B
A= not defined $tB= $t eq ""
ABresult
1X1
011
000
43  
44 137 ▶ if ($t eq '(') {
Branch: 1 branch
BranchTrueFalseTotal
if ($t eq '(') { } 13 124 137
45 13 unshift @now, 1;
46 13 unshift @op, 'start';
47 }
48 else {
49 124 ▶ if ($t eq '||') {
Branch: 2 branches
BranchTrueFalseTotal
if ($t eq '||') { } 12 112 124
elsif ($t eq '&&') { } 25 87 112
50 12 ▶ defined $op[0]
Branch: 1 branch
BranchTrueFalseTotal
defined $op[0] and &_syntax_error($expr, "consecutive operators") 1 11 12
51 and _syntax_error $expr, 'consecutive operators';
52 11 $op[0]='||';
53 }
54 elsif ($t eq '&&') {
55 25 ▶ defined $op[0]
Branch: 1 branch
BranchTrueFalseTotal
defined $op[0] and &_syntax_error($expr, "consecutive operators") 1 24 25
56 and _syntax_error $expr, 'consecutive operators';
57 24 $op[0]='&&';
58 }
59 else {
60 87 ▶ if ($t eq ')') {
Branch: 3 branches
BranchTrueFalseTotal
if ($t eq ')') { } 13 74 87
elsif ($t eq '_') { } 8 66 74
elsif ($t ne '0' and $t ne '1') { } 22 58 80
61 13 ▶ @now==1 and
Branch: 1 branch
BranchTrueFalseTotal
@now == 1 and &_syntax_error($expr, "unbalanced parens") 1 12 13
62 _syntax_error $expr, 'unbalanced parens';
63 12 ▶ defined $op[0] and
Branch: 1 branch
BranchTrueFalseTotal
defined $op[0] and &_syntax_error($expr, "key missing after operator '$op[0]'") 0 12 12
64 _syntax_error $expr, "key missing after operator '$op[0]'";
65  
66 12 $t=shift @now;
67 12 shift @op;
68 }
69 elsif ($t eq '_') {
70 8 ▶ unless ($^H & $seen_hint) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($^H & $seen_hint) 2 6 8
71 2 _carp "assertion status '_' referenced but not previously defined";
72 }
73 8 ▶ $t=($^H & $hint) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^H & $hint ? : 4 4 8
74 }
75 ▶ elsif ($t ne '0' and $t ne '1') {
Condition100%3/3
$t ne 0 and $t ne 1
!ll&&!rl&&r
143022
MC/DC100%2/2
$t ne 0 and $t ne 1
$t ne 0 $t ne 1
Truth table100%3/3
A and B
A= $t ne 0B= $t ne 1
ABresult
0X0
100
111
76 22 ▶ $t = ( grep { ref $_ eq 'Regexp'
Branch: 2 branches
BranchTrueFalseTotal
grep({ref $_ eq 'Regexp' ? $t =~ /$_/ : $_->check($t);} @{^ASSERTING}) ? : 15 7 22
ref $_ eq 'Regexp' ? : 92 0 92
77 ? $t=~$_
78 : $_->check($t)
79 } @{^ASSERTING} ) ? 1 : 0;
80 }
81  
82 86 ▶ defined $op[0] or
Branch: 1 branch
BranchTrueFalseTotal
defined $op[0] or &_syntax_error($expr, "operator expected") 86 0 86
83 _syntax_error $expr, 'operator expected';
84  
85 86 ▶ if ($op[0] eq 'start') {
Branch: 2 branches
BranchTrueFalseTotal
if ($op[0] eq 'start') { } 54 32 86
elsif ($op[0] eq '||') { } 11 21 32
86 54 $now[0]=$t;
87 }
88 elsif ($op[0] eq '||') {
89 11 ▶ $now[0]||=$t;
Condition100%3/3
$now[0] ||= $t
l!l&&r!l&&!r
632
MC/DC100%2/2
$now[0] ||= $t
$now[0] $t
Truth table100%3/3
A ||= B
A= $now[0]B= $t
ABresult
1X1
011
000
90 }
91 else {
92 21 ▶ $now[0]&&=$t;
Condition100%3/3
$now[0] &&= $t
!ll&&!rl&&r
3414
MC/DC100%2/2
$now[0] &&= $t
$now[0] $t
Truth table100%3/3
A &&= B
A= $now[0]B= $t
ABresult
0X0
100
111
93 }
94 86 undef $op[0];
95 }
96 }
97 }
98 39 ▶ @now==1 or _syntax_error $expr, 'unbalanced parens';
Branch: 1 branch
BranchTrueFalseTotal
@now == 1 or &_syntax_error($expr, "unbalanced parens") 38 1 39
99 38 ▶ defined $op[0] and _syntax_error $expr, "expression ends on operator '$op[0]'";
Branch: 1 branch
BranchTrueFalseTotal
defined $op[0] and &_syntax_error($expr, "expression ends on operator '$op[0]'") 1 37 38
100  
101 37 return $now[0];
102 }
103  
104  
105 14 ▶sub import {
Subroutine import: called CC=4 SCAR=13.9
106 # print STDERR "\@_=", join("|", @_), "\n";
107 14 shift;
108 14 ▶ @_=(scalar(caller)) unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 13 14
109 14 foreach my $expr (@_) {
110 14 ▶ unless (_calc_expr $expr) {
Branch: 1 branch
BranchTrueFalseTotal
unless (_calc_expr($expr)) 4 10 14
111 # print STDERR "assertions deactived";
112 4 $^H &= ~$hint;
113 4 $^H |= $seen_hint;
114 4 return;
115 }
116 }
117 # print STDERR "assertions actived";
118 10 $^H |= $hint|$seen_hint;
119 }
120  
121 1 ▶sub unimport {
Subroutine unimport: called CC=2 SCAR=7.2
122 1 ▶ @_ > 1
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 and _carp($_[0] . '->unimport arguments are being ignored') 0 1 1
123 and _carp($_[0]."->unimport arguments are being ignored");
124 1 $^H &= ~$hint;
125 }
126  
127 6 ▶sub enabled {
Subroutine enabled: called CC=4 SCAR=19.7
Pod documented
128 6 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 6 6
129 0 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 0 0 0
130 0 $^H |= $hint;
131 }
132 else {
133 0 $^H &= ~$hint;
134 }
135 0 $^H |= $seen_hint;
136 }
137 6 ▶ return $^H & $hint ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^H & $hint ? : 4 2 6
138 }
139  
140 0 ▶sub seen {
Subroutine seen: not called CC=4 SCAR=30.0
Pod documented
141 0 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
142 0 ▶ if ($_[0]) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_[0]) { } 0 0 0
143 0 $^H |= $seen_hint;
144 }
145 else {
146 0 $^H &= ~$seen_hint;
147 }
148 }
149 0 ▶ return $^H & $seen_hint ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$^H & $seen_hint ? : 0 0 0
150 }
151  
152 2 1;
153  
154 __END__
↑ Summary assertions/activate.pm »