lib/Test/Assertions.pm

filter: Statementstmt 86.8% 192 / 221 Branchbran 68.0% 64 / 94 Conditioncond 53.8% 14 / 26 MC/DCmcdc 31.2% 5 / 16 Subroutinesub 86.2% 25 / 29 Podpod 70.0% 14 / 20 totaltotal 77.3% 314 / 406 CC 59 SCAR 19.8
CC
59
Cov
79%
COMPILES
30.0
ASSESS
12.2
import
10.1
CRAP 7.2
↑ Summary lib/Test/Assertions/TestScript.pm »
1 package Test::Assertions;
2  
3 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 @Test::Assertions::EXPORT = qw(DIED COMPILES EQUAL EQUALS_FILE MATCHES_FILE FILES_EQUAL ASSESS ASSESS_FILE INTERPRET tests READ_FILE WRITE_FILE);
5 11 $Test::Assertions::VERSION = sprintf"%d.%03d", q$Revision: 1.54 $ =~ /: (\d+)\.(\d+)/;
6  
7 #Define constants
8 #(avoid "use constant" to cut compile-time overhead slightly - it *is* measurable)
9 BEGIN
10 11 ▶{
Subroutine BEGIN: called CC=2 SCAR=7.0
11 11 *tests = sub () {1}; # constant to export
12 11 *HAVE_ALARM = sub () {1}; # a flag, so that alarm() is never called if it isn't present (e.g. on Windows)
13 eval
14 11 {
15 11 my $was = alarm 0;
16 11 alarm $was;
17 };
18 11 ▶ undef *HAVE_ALARM, *HAVE_ALARM = sub () {0} if($@); #Change the constant!
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 11 11
19 }
20  
21 # this is the number of the current test, for automatically
22 # numbering the output of ASSERT
23 11 $Test::Assertions::test_no = 0;
24 # this is a flag - true if we are imported in a testing mode
25 11 $Test::Assertions::test_mode = 0;
26  
27 sub import
28 13 ▶{
Subroutine import: called CC=10 SCAR=23.1
29 13 my $pkg = shift;
30 13 my $style = shift;
31 13 my $callpkg = caller(0);
32 11 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
33 13 foreach my $sym (@Test::Assertions::EXPORT) {
34 156 *{"$callpkg\::$sym"} = \&{"$pkg\::$sym"};
35 }
36  
37 #Select implementation of ASSERT
38 13 ▶ if(!$style) {
Branch: 6 branches
BranchTrueFalseTotal
if (not $style) { } 0 13 13
elsif ($style eq 'die') { } 1 12 13
elsif ($style eq 'warn') { } 2 10 12
elsif ($style eq 'confess') { } 1 9 10
elsif ($style eq 'cluck') { } 1 8 9
elsif ($style eq 'test' or $style eq 'test/ok') { } 8 0 8
39 0 ▶ *{"$callpkg\::ASSERT"} = sub {};
Subroutine __ANON__: not called CC=1 SCAR=6.9
40 }
41 elsif($style eq 'die') {
42 1 *{"$callpkg\::ASSERT"} = \&{"$pkg\::ASSERT_die"};
43 }
44 elsif($style eq 'warn') {
45 2 *{"$callpkg\::ASSERT"} = \&{"$pkg\::ASSERT_warn"};
46 }
47 elsif($style eq 'confess') {
48 1 *{"$callpkg\::ASSERT"} = \&{"$pkg\::ASSERT_confess"};
49 }
50 elsif($style eq 'cluck') {
51 1 *{"$callpkg\::ASSERT"} = \&{"$pkg\::ASSERT_cluck"};
52 }
53 ▶ elsif($style eq 'test' || $style eq 'test/ok') {
Condition66%2/3
$style eq "test" or $style eq "test/ok"
l!l&&r!l&&!r
710
MC/DC0%0/2
$style eq "test" or $style eq "test/ok"
$style eq "test" $style eq "test/ok"
Truth table66%2/3
A or B
A= $style eq "test"B= $style eq "test/ok"
ABresult
1X1
011
000
54 8 require File::Spec;
55 8 $Test::Assertions::calling_script = File::Spec->rel2abs($0);
56 8 $Test::Assertions::use_ok = $style eq 'test/ok';
57 8 *{"$callpkg\::ASSERT"} = \&{"$pkg\::ASSERT_test"};
58 8 ▶ *{"$callpkg\::ok"} = \&{"$pkg\::ASSERT_test"} if($style eq 'test/ok');
Branch: 1 branch
BranchTrueFalseTotal
if $style eq "test/ok" 1 7 8
59 8 *{"$callpkg\::plan"} = \&{"$pkg\::plan"};
60 8 *{"$callpkg\::ignore"} = \&{"$pkg\::ignore"};
61 8 *{"$callpkg\::only"} = \&{"$pkg\::only"};
62 8 $Test::Assertions::test_mode = 1;
63 }
64 else {
65 0 croak("Test::Assertions imported with unknown directive: $style");
66 }
67 }
68  
69 #For compatibility with Test::Simple
70 sub plan
71 9 ▶{
Subroutine plan: called CC=3 SCAR=11.0
Pod documented
72 9 shift(); #tests
73 9 my $number = shift();
74 9 ▶ $number = _count_tests($Test::Assertions::calling_script)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $number and $number =~ /^\d+$/ 2 7 9
Condition66%2/3
defined $number and $number =~ /^\d+$/
!ll&&!rl&&r
207
MC/DC50%1/2
defined $number and $number =~ /^\d+$/
defined $number $number =~ /^\d+$/
Truth table66%2/3
A and B
A= defined $numberB= $number =~ /^\d+$/
ABresult
0X0
100
111
75 unless (defined($number) && $number =~ /^\d+$/);
76 9 print "1..$number\n";
77 9 $Test::Assertions::planned_tests = $number;
78 9 return $number;
79 }
80  
81 END
82 11 ▶{
Subroutine END: called CC=4 SCAR=13.9
83 # if we're in test mode and plan() has been called, ensure that the right number of tests have been run
84 11 ▶ if ($Test::Assertions::test_mode && defined($Test::Assertions::planned_tests)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Test::Assertions::test_mode and defined $Test::Assertions::planned_tests) 5 6 11
Condition100%3/3
$Test::Assertions::test_mode and defined $Test::Assertions::planned_tests
!ll&&!rl&&r
515
MC/DC100%2/2
$Test::Assertions::test_mode and defined $Test::Assertions::planned_tests
$Test::Assertions::test_mode defined $Test::Assertions::planned_tests
Truth table100%3/3
A and B
A= $Test::Assertions::test_modeB= defined $Test::Assertions::planned_tests
ABresult
0X0
100
111
85 5 ▶ if ($Test::Assertions::test_no != $Test::Assertions::planned_tests) {
Branch: 1 branch
BranchTrueFalseTotal
if ($Test::Assertions::test_no != $Test::Assertions::planned_tests) 1 4 5
86 1 warn "# Looks like you planned $Test::Assertions::planned_tests tests but actually ran $Test::Assertions::test_no.\n";
87 }
88 }
89 }
90  
91 #Test filtering
92 sub ignore
93 0 ▶{
Subroutine ignore: not called CC=1 SCAR=6.9
Pod documented
94 0 %Test::Assertions::ignore = map {$_ => 1} @_;
95 }
96  
97 sub only
98 0 ▶{
Subroutine only: not called CC=1 SCAR=6.9
Pod documented
99 0 %Test::Assertions::only = map {$_ => 1} @_;
100 }
101  
102  
103 #
104 # Various styles
105 #
106  
107 sub ASSERT_test ($;$)
108 63 ▶{
Subroutine ASSERT_test: called CC=6 SCAR=20.2
Pod undocumented
109 63 my ($test,$msg) = @_;
110 63 my ($pkg, $filename, $line, $sub) = caller(0);
111 63 $Test::Assertions::test_no++;
112 63 ▶ if($Test::Assertions::ignore{$Test::Assertions::test_no} ||
Branch: 1 branch
BranchTrueFalseTotal
if ($Test::Assertions::ignore{$Test::Assertions::test_no} or %Test::Assertions::only and not $Test::Assertions::only{$Test::Assertions::test_no}) { } 0 63 63
Condition33%2/6
$Test::Assertions::ignore{$Test::Assertions::test_no} or %Test::Assertions::only and not $Test::Assertions::only{$Test::Assertions::test_no}
l!l&&r!l&&!r
0063
%Test::Assertions::only and not $Test::Assertions::only{$Test::Assertions::test_no}
!ll&&!rl&&r
6300
MC/DC0%0/3
$Test::Assertions::ignore{$Test::Assertions::test_no} or %Test::Assertions::only and not $Test::Assertions::only{$Test::Assertions::test_no}
$Test::Assertions::ignore{$Test::Assertions::test_no} %Test::Assertions::only not $Test::Assertions::only{$Test::Assertions::test_no}
Truth table25%1/4
A or B and not(C)
A= $Test::Assertions::ignore{$Test::Assertions::test_no}B= %Test::Assertions::onlyC= not $Test::Assertions::only{$Test::Assertions::test_no}
ABCresult
1XX1
0111
00X0
0100
113 %Test::Assertions::only && !$Test::Assertions::only{$Test::Assertions::test_no})
114 {
115 0 print "ok - skipped $Test::Assertions::test_no";
116 }
117 else
118 {
119 63 ▶ print ($test?"ok $Test::Assertions::test_no":"not ok $Test::Assertions::test_no at line $line in $filename");
Branch: 1 branch
BranchTrueFalseTotal
$test ? : 63 0 63
120 }
121 63 ▶ print " ($msg)" if(defined $msg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $msg 60 3 63
122 63 print "\n";
123 }
124  
125 sub ASSERT_die ($;$)
126 1 ▶{
Subroutine ASSERT_die: called CC=3 SCAR=11.3
Pod undocumented
127 1 my $test = shift;
128 1 my $msg = shift;
129 1 ▶ $msg="($msg)" if(defined $msg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $msg 1 0 1
130 1 my ($pkg, $filename, $line, $sub) = caller(0);
131 1 ▶ die("Assertion failure at line $line in $filename $msg\n") unless($test);
Branch: 1 branch
BranchTrueFalseTotal
unless $test 1 0 1
132 }
133  
134 sub ASSERT_warn ($;$)
135 2 ▶{
Subroutine ASSERT_warn: called CC=3 SCAR=11.0
Pod undocumented
136 2 my $test = shift;
137 2 my $msg = shift;
138 2 ▶ $msg="($msg)" if(defined $msg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $msg 2 0 2
139 2 my ($pkg, $filename, $line, $sub) = caller(0);
140 2 ▶ warn("Assertion failure at line $line in $filename $msg\n") unless($test);
Branch: 1 branch
BranchTrueFalseTotal
unless $test 1 1 2
141 }
142  
143 sub ASSERT_confess ($;$)
144 1 ▶{
Subroutine ASSERT_confess: called CC=4 SCAR=14.6
Pod undocumented
145 1 my $test = shift;
146 1 my $msg = shift;
147 1 require Carp;
148 1 ▶ $msg="($msg)" if(defined $msg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $msg 1 0 1
149 1 my ($pkg, $filename, $line, $sub);
150 1 ▶ if (caller(1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (caller 1) { } 1 0 1
151 1 ($pkg, $filename, $line, $sub) = caller(1);
152 } else {
153 0 ($pkg, $filename, $line, $sub) = caller(0);
154 }
155 1 ▶ Carp::confess("Assertion failure at line $line in $filename $msg\n") unless($test);
Branch: 1 branch
BranchTrueFalseTotal
unless $test 1 0 1
156 }
157  
158 sub ASSERT_cluck ($;$)
159 1 ▶{
Subroutine ASSERT_cluck: called CC=4 SCAR=14.6
Pod undocumented
160 1 my $test = shift;
161 1 my $msg = shift;
162 1 require Carp;
163 1 ▶ $msg="($msg)" if(defined $msg);
Branch: 1 branch
BranchTrueFalseTotal
if defined $msg 1 0 1
164 1 my ($pkg, $filename, $line, $sub);
165 1 ▶ if (caller(1)) {
Branch: 1 branch
BranchTrueFalseTotal
if (caller 1) { } 1 0 1
166 1 ($pkg, $filename, $line, $sub) = caller(1);
167 } else {
168 0 ($pkg, $filename, $line, $sub) = caller(0);
169 }
170 1 ▶ Carp::cluck("Assertion failure at line $line in $filename $msg\n") unless($test);
Branch: 1 branch
BranchTrueFalseTotal
unless $test 1 0 1
171 }
172  
173 sub DIED
174 1 ▶{
Subroutine DIED: called CC=1 SCAR=0.0
Pod documented
175 1 my ($coderef) = @_;
176 1 eval {&$coderef};
177 1 my $error = $@;
178 1 TRACE("DIED: " . $error);
179 1 return $error;
180 }
181  
182 sub COMPILES
183 0 ▶{
Subroutine COMPILES: not called CC=5 SCAR=34.0
Pod documented
184 0 my ($file, $strict, $strref) = @_;
185  
186 0 my @args = ($^X);
187 0 ▶ push @args, '-Mstrict', '-w' if $strict;
Branch: 1 branch
BranchTrueFalseTotal
if $strict 0 0 0
188 0 push @args, '-c', $file;
189 0 my $output;
190 0 my $ok = 0;
191 0 ▶ if ($strref && ref($strref) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if ($strref and ref $strref eq 'SCALAR') { } 0 0 0
Condition0%0/3
$strref and ref $strref eq "SCALAR"
!ll&&!rl&&r
000
MC/DC0%0/2
$strref and ref $strref eq "SCALAR"
$strref ref $strref eq "SCALAR"
Truth table0%0/3
A and B
A= $strrefB= ref $strref eq "SCALAR"
ABresult
0X0
100
111
192 0 require IO::CaptureOutput;
193 0 ($output, $$strref) = IO::CaptureOutput::capture_exec(@args);
194 0 $ok = ($$strref =~ /syntax OK/);
195 } else {
196 0 my $command = join ' ', @args;
197 0 $output = `$command 2>&1`;
198 0 $output =~ s/\n$//;
199 0 $ok = ($output =~ /syntax OK/);
200 }
201 0 ▶ return wantarray? ($ok, $output) : $ok;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
202 }
203  
204 sub EQUAL
205 8 ▶{
Subroutine EQUAL: called CC=1 SCAR=0.0
Pod documented
206 8 require Test::More;
207 8 my ($lhs, $rhs) = @_;
208 8 return Test::More::eq_array([$lhs],[$rhs]);
209 }
210  
211 sub FILES_EQUAL
212 4 ▶{
Subroutine FILES_EQUAL: called CC=1 SCAR=0.0
Pod documented
213 4 require File::Compare;
214 4 my ($lhs, $rhs) = @_;
215 4 return File::Compare::compare($lhs,$rhs)==0;
216 }
217  
218 sub EQUALS_FILE
219 3 ▶{
Subroutine EQUALS_FILE: called CC=1 SCAR=0.0
Pod documented
220 3 my ($lhs, $rhs) = @_;
221 3 return($lhs eq _read_file($rhs));
222 }
223  
224 sub MATCHES_FILE
225 4 ▶{
Subroutine MATCHES_FILE: called CC=1 SCAR=0.0
Pod documented
226 4 my ($lhs, $rhs) = @_;
227 4 my $regex = _read_file($rhs);
228 4 return($lhs =~ /^$regex$/);
229 }
230  
231 sub ASSESS_FILE
232 1 ▶{
Subroutine ASSESS_FILE: called CC=6 SCAR=19.0
Pod documented
233 1 my ($file, $verbose, $timeout) = @_;
234 1 ▶ $timeout = 60 unless(defined $timeout);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $timeout 1 0 1
235 1 my @tests;
236 1 local *FH;
237 eval
238 1 {
239 1 alarm $timeout if HAVE_ALARM;
240 1 ▶ open (*FH, "$file |") or die("unable to execute $file - $!");
Branch: 1 branch
BranchTrueFalseTotal
open *FH, "$file |" or die "unable to execute $file - $!" 1 0 1
241 1 @tests = <FH>;
242 1 close FH;
243 };
244 1 alarm 0 if HAVE_ALARM;
245 1 my $rs;
246 1 ▶ if($@) {
Branch: 2 branches
BranchTrueFalseTotal
if ($@) { } 0 1 1
elsif ($?) { } 0 1 1
247 0 $rs = "not ok for $file ($@)\n"
248 } elsif ($?) {
249 0 $rs = "not ok for $file (exit code = $?)\n";
250 } else {
251 1 $rs = ASSESS(\@tests, $file, $verbose);
252 }
253 1 ▶ return wantarray? INTERPRET($rs) : $rs;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
254 }
255  
256 sub ASSESS
257 6 ▶{
Subroutine ASSESS: called CC=12 SCAR=25.0
Pod documented
258 6 my ($tests, $name, $verbose) = @_;
259 6 my $errors = 0;
260 6 my $total = 0;
261 6 my $expected;
262 6 ▶ if (${$tests}[0] =~ m/^1\.\.(\d+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (do { $tests }->[0] =~ /^1\.\.(\d+)$/) { } 2 4 6
263 2 $expected = $1;
264 }
265 else
266 {
267 4 $expected = -1;
268 }
269 6 foreach(@$tests)
270 {
271 11 ▶ if(/^not ok/)
Branch: 2 branches
BranchTrueFalseTotal
if (/^not ok/) { } 3 8 11
elsif (/^ok/) { } 6 2 8
272 {
273 3 $errors++; $total++;
274 3 ▶ if($verbose)
Branch: 1 branch
BranchTrueFalseTotal
if ($verbose) 0 3 3
275 {
276 0 s/\n?$/ in $name\n/;
277 0 print;
278 }
279 }
280 elsif(/^ok/)
281 {
282 6 $total++;
283 }
284 }
285  
286 6 my $rs;
287 6 ▶ if(defined $name) { $name = " for $name"; } else { $name = ''; }
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name) { } 3 3 6
288 6 ▶ if($errors)
Branch: 2 branches
BranchTrueFalseTotal
if ($errors) { } 3 3 6
elsif ($expected != -1 and $total != $expected) { } 1 2 3
289 {
290 3 $rs = "not ok$name ($errors errors in $total tests)\n";
291 }
292 ▶ elsif($expected != -1 && $total != $expected)
Condition66%2/3
$expected != -1 and $total != $expected
!ll&&!rl&&r
201
MC/DC50%1/2
$expected != -1 and $total != $expected
$expected != -1 $total != $expected
Truth table66%2/3
A and B
A= $expected != -1B= $total != $expected
ABresult
0X0
100
111
293 {
294 1 $rs = "not ok$name (Expected $expected tests, ran $total tests)\n";
295 }
296 else
297 {
298 2 ▶ $rs = "ok$name".($verbose?" passed all $total tests ":"")."\n";
Branch: 1 branch
BranchTrueFalseTotal
$verbose ? : 0 2 2
299 }
300 6 ▶ return wantarray? INTERPRET($rs) : $rs;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 4 6
301 }
302  
303 sub INTERPRET
304 2 ▶{
Subroutine INTERPRET: called CC=2 SCAR=6.9
Pod documented
305 2 my $rs = shift;
306 2 my ($status, $desc) = ($rs =~ /^((?:not )?ok)(.*)$/);
307 2 $desc =~ s/^\s+//;
308 2 $desc =~ s/^for //; #ok for x => x
309 2 $desc =~ s/^- //; #ok - x => x
310 2 $desc =~ s/^\((.*)\)/$1/; #ok (x) => x
311 2 ▶ return ($status eq 'ok' || 0, $desc);
Condition100%2/2
$status eq 'ok' || 0
l!l
11
MC/DC100%1/1
$status eq 'ok' || 0
$status eq 'ok'
Truth table100%2/2
A
A= $status eq 'ok'
Aresult
11
00
312 }
313  
314 sub READ_FILE
315 17 ▶{
Subroutine READ_FILE: called CC=1 SCAR=0.0
Pod documented
316 17 my $filename = shift;
317 17 my $contents;
318 17 eval {
319 17 $contents = _read_file($filename);
320 };
321 17 return $contents;
322 }
323  
324 sub WRITE_FILE
325 18 ▶{
Subroutine WRITE_FILE: called CC=1 SCAR=0.0
Pod documented
326 18 my ($filename, $contents) = @_;
327 18 my $success;
328 18 eval {
329 18 _write_file($filename, $contents);
330 18 $success = 1;
331 };
332 18 return $success;
333 }
334  
335 # Misc subroutines
336  
337 sub _count_tests
338 2 ▶{
Subroutine _count_tests: called CC=6 SCAR=18.3
339 2 my $filename = shift;
340 2 my $count = 0;
341 2 local *LI;
342 2 ▶ open (LI, $filename) || die ("Unable to open $filename to count tests - $!");
Branch: 1 branch
BranchTrueFalseTotal
open LI, $filename or die "Unable to open $filename to count tests - $!" 2 0 2
343 2 ▶ while(<LI>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline LI)) 32 2 34
344 {
345 32 s/\#.+//; # ignore commented-out lines
346 32 ▶ $count++ if(/\bASSERT[\s\(]/);
Branch: 1 branch
BranchTrueFalseTotal
if /\bASSERT[\s\(]/ 3 29 32
347 32 ▶ $count++ if($Test::Assertions::use_ok && /\bok[\s\(]/);
Branch: 1 branch
BranchTrueFalseTotal
if $Test::Assertions::use_ok and /\bok[\s\(]/ 0 32 32
Condition33%1/3
$Test::Assertions::use_ok and /\bok[\s\(]/
!ll&&!rl&&r
3200
MC/DC0%0/2
$Test::Assertions::use_ok and /\bok[\s\(]/
$Test::Assertions::use_ok /\bok[\s\(]/
Truth table33%1/3
A and B
A= $Test::Assertions::use_okB= /\bok[\s\(]/
ABresult
0X0
100
111
348 }
349 2 close LI;
350 2 return $count;
351 }
352  
353 sub _read_file
354 24 ▶{
Subroutine _read_file: called CC=2 SCAR=6.9
355 24 my $filename = shift;
356 24 ▶ open (FH, $filename) || die("unable to open $filename - $!");
Branch: 1 branch
BranchTrueFalseTotal
open FH, $filename or die "unable to open $filename - $!" 23 1 24
357 23 local $/ = undef;
358 23 my $data = <FH>;
359 23 close FH;
360 23 return $data;
361 }
362  
363 sub _write_file
364 18 ▶{
Subroutine _write_file: called CC=2 SCAR=7.0
365 18 my ($filename, $data) = @_;
366 18 local *FH;
367 18 ▶ open(FH, ">$filename") or die("Unable to open $filename - $!");
Branch: 1 branch
BranchTrueFalseTotal
open FH, ">$filename" or die "Unable to open $filename - $!" 18 0 18
368 18 binmode FH;
369 18 print FH $data;
370 18 close FH;
371 }
372  
373 #Standard debugging stub - intended to be overridden when
374 #debugging is needed, e.g. by Log::Trace
375 1 ▶sub TRACE {}
Subroutine TRACE: called CC=1 SCAR=0.0
Pod undocumented
376  
377 11 1;
378  
379 __END__
↑ Summary lib/Test/Assertions/TestScript.pm »