Assert.pm

filter: Statementstmt 87.1% 135 / 155 Branchbran 55.8% 57 / 102 Conditioncond 41.6% 10 / 24 MC/DCmcdc 16.6% 2 / 12 Subroutinesub 100% 16 / 16 Podpod 100% 1 / 1 totaltotal 71.2% 221 / 310 CC 66 SCAR 36.9
CC
66
Cov
72%
assert
79.9
__ANON__
11.1
_normalize_conditions
10.9
CRAP 39.9
↑ Summary Assert/Nothing.pm »
1 package Sub::Assert;
2
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6
7 3 require Exporter;
8
9 3 our @ISA = qw(Exporter);
10
11 3 our @EXPORT = qw(
12 assert
13 );
14 3 our $VERSION = '1.23';
15
16 3 ▶use Carp qw/croak carp/;
Subroutine BEGIN: called CC=1 SCAR=0.0
17
18 6 ▶sub assert {
Subroutine assert: called CC=36 SCAR=43.8
Pod documented
19 6 my %params = @_;
20 6 my $sub = $params{sub};
21 6 ▶ defined $sub or croak("assert missing the subroutine to work with");
Branch: 1 branch
BranchTrueFalseTotal
defined $sub or croak("assert missing the subroutine to work with") 6 0 6
22
23 6 my $package;
24 my $subref;
25 6 ▶ if (ref $sub eq 'CODE') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $sub eq 'CODE') { } 0 6 6
elsif (ref $sub eq '') { } 6 0 6
26 0 $subref = $sub;
27 }
28 elsif (ref $sub eq '') {
29 6 ($package, undef, undef) = caller();
30 6 ▶ defined $package
Branch: 1 branch
BranchTrueFalseTotal
defined $package or croak("assert could not determine caller package") 6 0 6
31 or croak("assert could not determine caller package");
32 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 6 $subref = *{"${package}::$sub"}{CODE};
34 3 ▶ use strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
35 6 ▶ defined $subref and ref($subref) eq 'CODE'
Branch: 1 branch
BranchTrueFalseTotal
defined $subref and ref $subref eq "CODE" or croak("assert finds that there is no '${sub}' subroutine in package '${package}'") 6 0 6
Condition33%1/3
defined $subref and ref $subref eq "CODE"
!ll&&!rl&&r
006
MC/DC0%0/2
defined $subref and ref $subref eq "CODE"
defined $subref ref $subref eq "CODE"
Truth table33%1/3
A and B
A= defined $subrefB= ref $subref eq "CODE"
ABresult
0X0
100
111
36 or croak("assert finds that there is no '$sub' subroutine in package '$package'");
37 }
38 else {
39 0 croak("Subroutine argument to assert is invalid");
40 }
41
42 6 ▶ $params{action} = 'croak' unless defined $params{action};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"action"} 0 6 6
43 6 my $action = $package . '::' . $params{action};
44
45 6 my $precond = _normalize_conditions($params{pre}, 'precondition');
46
47 6 my $postcond = _normalize_conditions($params{post}, 'postcondition');
48
49 6 my $context;
50 6 ▶ if (exists $params{context}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $params{'context'}) { } 2 4 6
51 2 ▶ unless (defined $params{context} and
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $params{"context"} and $params{"context"} eq "list" || $params{"context"} eq "scalar" || $params{"context"} eq "void" || $params{"context"} eq "novoid" || $params{"context"} eq "any") 0 2 2
Condition33%5/15
defined $params{"context"} and $params{"context"} eq "list" || $params{"context"} eq "scalar" || $params{"context"} eq "void" || $params{"context"} eq "novoid" || $params{"context"} eq "any"
!ll&&!rl&&r
002
$params{"context"} eq "list" or $params{"context"} eq "scalar" or $params{"context"} eq "void" or $params{"context"} eq "novoid" or $params{"context"} eq "any"
l!l&&r!l&&!r
200
$params{"context"} eq "list" or $params{"context"} eq "scalar" or $params{"context"} eq "void" or $params{"context"} eq "novoid"
l!l&&r!l&&!r
020
$params{"context"} eq "list" or $params{"context"} eq "scalar" or $params{"context"} eq "void"
l!l&&r!l&&!r
002
$params{"context"} eq "list" or $params{"context"} eq "scalar"
l!l&&r!l&&!r
002
MC/DC0%0/6
defined $params{"context"} and $params{"context"} eq "list" || $params{"context"} eq "scalar" || $params{"context"} eq "void" || $params{"context"} eq "novoid" || $params{"context"} eq "any"
defined $params{"context"} $params{"context"} eq "list" $params{"context"} eq "scalar" $params{"context"} eq "void" $params{"context"} eq "novoid" $params{"context"} eq "any"
Truth table14%1/7
A and B or C or D or E or F
A= defined $params{"context"}B= $params{"context"} eq "list"C= $params{"context"} eq "scalar"D= $params{"context"} eq "void"E= $params{"context"} eq "novoid"F= $params{"context"} eq "any"
ABCDEFresult
0XXXXX0
1000000
11XXXX1
101XXX1
1001XX1
10001X1
1000011
52 $params{context} eq 'list' ||
53 $params{context} eq 'scalar' ||
54 $params{context} eq 'void' ||
55 $params{context} eq 'novoid' ||
56 $params{context} eq 'any'
57 ) {
58 0 croak("Invalid context specified for assertion");
59 }
60 2 $context = $params{context};
61 }
62 else {
63 4 $context = 'any';
64 }
65
66 6 my $new_sub_text = "sub {\nmy \@PARAM = \@_;\n";
67
68 6 ▶ if ($context eq 'list') {
Branch: 4 branches
BranchTrueFalseTotal
if ($context eq 'list') { } 0 6 6
elsif ($context eq 'scalar') { } 0 6 6
elsif ($context eq 'novoid') { } 2 4 6
elsif ($context eq 'void') { } 0 4 4
69 0 ▶ $new_sub_text .= "unless (wantarray()) {\n" .
Branch: 1 branch
BranchTrueFalseTotal
ref $sub eq 'CODE' ? : 0 0 0
70 "my \$context = (defined wantarray() ?\n" .
71 " 'scalar' : 'void');\n" .
72 "$action(\"" .
73 (ref($sub) eq 'CODE' ?
74 'C' :
75 "${package}::$sub c"
76 ) .
77 'alled in $context context.")' .
78 "}\n";
79 }
80 elsif ($context eq 'scalar') {
81 0 ▶ $new_sub_text .= "unless (defined(wantarray()) and not " .
Branch: 1 branch
BranchTrueFalseTotal
ref $sub eq 'CODE' ? : 0 0 0
82 "wantarray()) {\n" .
83 "my \$context = (wantarray() ?\n" .
84 " 'list' : 'void');\n" .
85 "$action(\"" .
86 (ref($sub) eq 'CODE' ?
87 'C' :
88 "${package}::$sub c"
89 ) .
90 'alled in $context context.")' .
91 "}\n";
92 }
93 elsif ($context eq 'novoid') {
94 2 ▶ $new_sub_text .= "unless (defined wantarray()) {\n" .
Branch: 1 branch
BranchTrueFalseTotal
ref $sub eq 'CODE' ? : 0 2 2
95 "$action(\"" .
96 (ref($sub) eq 'CODE' ?
97 'C' :
98 "${package}::$sub c"
99 ) .
100 'alled in void context.")' .
101 "}\n";
102 }
103 elsif ($context eq 'void') {
104 0 ▶ $new_sub_text .= "unless (not defined wantarray()) {\n" .
Branch: 1 branch
BranchTrueFalseTotal
ref $sub eq 'CODE' ? : 0 0 0
105 "my \$context = (wantarray() ?\n" .
106 " 'list' : 'scalar');\n" .
107 "$action(\"" .
108 (ref($sub) eq 'CODE' ?
109 'C' :
110 "${package}::$sub c"
111 ) .
112 'alled in $context context.")' .
113 "}\n";
114 }
115
116 6 foreach my $pre_name (keys %$precond) {
117 6 ▶ if ($pre_name eq '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($pre_name eq '_') { } 3 3 6
118 3 my $pre_array = $precond->{'_'};
119 3 foreach my $pre_no (1..@$pre_array) {
120 3 ▶ $new_sub_text .=
Branch: 1 branch
BranchTrueFalseTotal
ref $sub eq 'CODE' ? : 0 3 3
121 "do{\n".$pre_array->[$pre_no-1]
122 . "\n}\nor $action(\"Precondition "
123 . "$pre_no "
124 . (ref($sub) eq 'CODE' ? '' : "for ${package}::$sub ")
125 . "failed.\");\n\n";
126 }
127 }
128 else {
129 $new_sub_text .=
130 3 ▶ "do{\n".$precond->{$pre_name}
Branch: 1 branch
BranchTrueFalseTotal
ref $sub eq 'CODE' ? : 0 3 3
131 . "\n}\nor $action(\"Precondition "
132 . "'$pre_name' "
133 . (ref($sub) eq 'CODE' ? '' : "for ${package}::$sub ")
134 . "failed.\");\n\n";
135 }
136 }
137 6 $new_sub_text .= <<'HERE';
138 my @RETURN;
139 my $RETURN;
140 my $VOID;
141 if (wantarray()) {
142 @RETURN = $SUBROUTINEREF->(@PARAM);
143 $RETURN = $RETURN[0] if @RETURN;
144 }
145 elsif (defined wantarray()) {
146 $RETURN = $SUBROUTINEREF->(@PARAM);
147 @RETURN = ($RETURN);
148 }
149 else {
150 $VOID = 1;
151 $SUBROUTINEREF->(@PARAM);
152 }
153 HERE
154
155 6 foreach my $post_name (keys %$postcond) {
156 9 ▶ if ($post_name eq '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($post_name eq '_') { } 5 4 9
157 5 my $post_array = $postcond->{'_'};
158 5 foreach my $post_no (1..@$post_array) {
159 8 ▶ $new_sub_text .=
Branch: 1 branch
BranchTrueFalseTotal
ref $sub eq 'CODE' ? : 0 8 8
160 "do{\n".$post_array->[$post_no-1]
161 . "\n}\nor $action(\"Postcondition "
162 . "$post_no "
163 . (ref($sub) eq 'CODE' ? '' : "for ${package}::$sub ")
164 . "failed.\");\n\n";
165 }
166 }
167 else {
168 $new_sub_text .=
169 4 ▶ "do{\n".$postcond->{$post_name}
Branch: 1 branch
BranchTrueFalseTotal
ref $sub eq 'CODE' ? : 0 4 4
170 . "\n}\nor $action(\"Postcondition "
171 . "'$post_name' "
172 . (ref($sub) eq 'CODE' ? '' : "for ${package}::$sub ")
173 . "failed.\");\n\n";
174 }
175 }
176
177 6 ▶ $new_sub_text .= ($context eq 'list' ?
Branch: 1 branch
BranchTrueFalseTotal
$context eq 'list' ? : 0 6 6
178 "return \@RETURN;\n}\n" :
179 "return \$RETURN;\n}\n"
180 );
181 6 my ($new_sub_ref, $error) =
182 _generate_assertion_subroutine($subref, $new_sub_text);
183
184 6 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 0 6 6
185 0 croak("Compilation of assertions failed: $error.\n$new_sub_text");
186 }
187 6 ▶ if (ref($sub) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $sub eq 'CODE') { } 0 6 6
188 0 return $new_sub_ref;
189 }
190 else {
191 3 ▶ no strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
192 3 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
193 6 *{"${package}::$sub"} = $new_sub_ref;
194 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
195 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
196 }
197 6 return $new_sub_ref;
198 }
199
200 6 ▶sub _generate_assertion_subroutine {
Subroutine _generate_assertion_subroutine: called CC=1 SCAR=0.2
201 6 local $@;
202 6 my $SUBROUTINEREF = $_[0];
203 0 ▶ return eval($_[1]), "$@";
Subroutine __ANON__: called CC=8 SCAR=22.7
Subroutine __ANON__: called CC=7 SCAR=21.1
Subroutine __ANON__: called CC=9 SCAR=24.1
Branch: 19 branches
BranchTrueFalseTotal
unless (defined wantarray) 2 4 6
do { $PARAM[0] > 0 } or main::darnedtestmodule("Precondition 1 for main::double failed.") 4 2 6
if (wantarray) { } 0 6 6
elsif (defined wantarray) { } 4 2 6
if @RETURN 0 0 0
do { $VOID or $RETURN > $PARAM[0] } or main::darnedtestmodule("Postcondition 1 for main::double failed.") 4 2 6
do { $PARAM[0] >= 0 } or main::darnedtestmodule("Precondition 1 for main::faultysqrt failed.") 2 0 2
if (wantarray) { } 0 2 2
elsif (defined wantarray) { } 2 0 2
if @RETURN 0 0 0
do { $VOID or $RETURN <= $PARAM[0] } or main::darnedtestmodule("Postcondition 1 for main::faultysqrt failed.") 0 2 2
do { @PARAM == 2 } or main::darnedtestmodule("Precondition 1 for main::anotherfunc failed.") 2 2 4
if (wantarray) { } 0 4 4
elsif (defined wantarray) { } 4 0 4
if @RETURN 0 0 0
do { not $VOID } or main::darnedtestmodule("Postcondition 1 for main::anotherfunc failed.") 4 0 4
do { $RETURN > 0 } or main::darnedtestmodule("Postcondition 2 for main::anotherfunc failed.") 4 0 4
do { $PARAM[0] * $PARAM[1] - "1e-12" < $RETURN } or main::darnedtestmodule("Postcondition 3 for main::anotherfunc failed.") 4 0 4
do { $PARAM[0] * $PARAM[1] + "1e-12" > $RETURN } or main::darnedtestmodule("Postcondition 4 for main::anotherfunc failed.") 4 0 4
Condition66%4/6
$VOID || $RETURN > $PARAM[0]
l!l&&r!l&&!r
222
$VOID || $RETURN <= $PARAM[0]
l!l&&r!l&&!r
002
MC/DC100%2/2
$VOID || $RETURN > $PARAM[0]
$VOID $RETURN > $PARAM[0]
MC/DC0%0/2
$VOID || $RETURN <= $PARAM[0]
$VOID $RETURN <= $PARAM[0]
Truth table100%3/3
A || B
A= $VOIDB= $RETURN > $PARAM[0]
ABresult
1X1
011
000
Truth table33%1/3
A || B
A= $VOIDB= $RETURN <= $PARAM[0]
ABresult
1X1
011
000
204 }
205
206 12 ▶sub _normalize_conditions {
Subroutine _normalize_conditions: called CC=10 SCAR=23.9
207 12 my $conditions = shift;
208 12 my $type = shift;
209
210 12 ▶ if (not defined $conditions) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $conditions) { } 0 12 12
elsif (ref $conditions eq '') { } 5 7 12
elsif (ref $conditions eq 'ARRAY') { } 3 4 7
elsif (ref $conditions eq 'HASH') { } 4 0 4
211 # no conditions
212 0 $conditions = {};
213 }
214 elsif (ref($conditions) eq '') {
215 # a single, unnamed condition
216 5 $conditions = {'_' =>[$conditions]};
217 }
218 elsif (ref($conditions) eq 'ARRAY') {
219 # an array of unnamed conditions
220 3 my $ary = $conditions;
221 3 $conditions = {'_' => [@$ary]};
222 }
223 elsif (ref($conditions) eq 'HASH') {}
224 else {
225 0 croak("Invalid type of $type");
226 }
227
228 12 foreach my $name (keys %$conditions) {
229 15 ▶ if ($name eq '_') {
Branch: 1 branch
BranchTrueFalseTotal
if ($name eq '_') { } 8 7 15
230 8 foreach my $cond (@{$conditions->{'_'}}) {
231 11 ▶ croak("Invalid unnamed $type")
Branch: 1 branch
BranchTrueFalseTotal
if ref $cond ne "" 0 11 11
232 if ref($cond) ne '';
233 }
234 }
235 else {
236 croak("Invalid $type '$name'")
237 7 ▶ if ref($conditions->{$name}) ne '';
Branch: 1 branch
BranchTrueFalseTotal
if ref $conditions->{$name} ne "" 0 7 7
238 }
239 }
240
241 12 return $conditions;
242 }
243
244 3 1;
245 __END__
↑ Summary Assert/Nothing.pm »