blib/lib/Test.pm

filter: Statementstmt 81.0% 180 / 222 Branchbran 72.7% 80 / 110 Conditioncond 52.2% 23 / 44 MC/DCmcdc 38.4% 10 / 26 Subroutinesub 85.7% 18 / 21 Podpod 100% 3 / 3 totaltotal 73.7% 314 / 426 CC 76 SCAR 27.7
CC
76
Cov
75%
_diff_complain_external
72.0
__ANON__
17.4
plan
15.3
CRAP 15.9
↑ Summary
1  
2 9 require 5.004;
3 package Test;
4  
5 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use vars (qw($VERSION @ISA @EXPORT @EXPORT_OK $ntest $TestLevel), #public-ish
Subroutine BEGIN: called CC=1 SCAR=0.0
9 qw($TESTOUT $TESTERR %Program_Lines $told_about_diff
10 $ONFAIL %todo %history $planned @FAILDETAIL) #private-ish
11 9 );
12  
13 # In case a test is run in a persistent environment.
14 9 ▶sub _reset_globals {
Subroutine _reset_globals: called CC=1 SCAR=0.0
15 9 %todo = ();
16 9 %history = ();
17 9 @FAILDETAIL = ();
18 9 $ntest = 1;
19 9 $TestLevel = 0; # how many extra stack frames to skip
20 9 $planned = 0;
21 }
22  
23 9 $VERSION = '1.26';
24 9 require Exporter;
25 9 @ISA=('Exporter');
26  
27 9 @EXPORT = qw(&plan &ok &skip);
28 9 @EXPORT_OK = qw($ntest $TESTOUT $TESTERR);
29  
30 9 $|=1;
31 9 $TESTOUT = *STDOUT{IO};
32 9 $TESTERR = *STDERR{IO};
33  
34 # Use of this variable is strongly discouraged. It is set mainly to
35 # help test coverage analyzers know which test is running.
36 9 $ENV{REGRESSION_TEST} = $0;
37  
38  
39 =head1 NAME
40  
41 Test - provides a simple framework for writing test scripts
42  
43 =head1 SYNOPSIS
44  
45 use strict;
46 use Test;
47  
48 # use a BEGIN block so we print our plan before MyModule is loaded
49 BEGIN { plan tests => 14, todo => [3,4] }
50  
51 # load your module...
52 use MyModule;
53  
54 # Helpful notes. All note-lines must start with a "#".
55 print "# I'm testing MyModule version $MyModule::VERSION\n";
56  
57 ok(0); # failure
58 ok(1); # success
59  
60 ok(0); # ok, expected failure (see todo list, above)
61 ok(1); # surprise success!
62  
63 ok(0,1); # failure: '0' ne '1'
64 ok('broke','fixed'); # failure: 'broke' ne 'fixed'
65 ok('fixed','fixed'); # success: 'fixed' eq 'fixed'
66 ok('fixed',qr/x/); # success: 'fixed' =~ qr/x/
67  
68 ok(sub { 1+1 }, 2); # success: '2' eq '2'
69 ok(sub { 1+1 }, 3); # failure: '2' ne '3'
70  
71 my @list = (0,0);
72 ok @list, 3, "\@list=".join(',',@list); #extra notes
73 ok 'segmentation fault', '/(?i)success/'; #regex match
74  
75 skip(
76 $^O =~ m/MSWin/ ? "Skip if MSWin" : 0, # whether to skip
77 $foo, $bar # arguments just like for ok(...)
78 );
79 skip(
80 $^O =~ m/MSWin/ ? 0 : "Skip unless MSWin", # whether to skip
81 $foo, $bar # arguments just like for ok(...)
82 );
83  
84 =head1 DESCRIPTION
85  
86 This module simplifies the task of writing test files for Perl modules,
87 such that their output is in the format that
88 L<Test::Harness|Test::Harness> expects to see.
89  
90 =head1 QUICK START GUIDE
91  
92 To write a test for your new (and probably not even done) module, create
93 a new file called F<t/test.t> (in a new F<t> directory). If you have
94 multiple test files, to test the "foo", "bar", and "baz" feature sets,
95 then feel free to call your files F<t/foo.t>, F<t/bar.t>, and
96 F<t/baz.t>
97  
98 =head2 Functions
99  
100 This module defines three public functions, C<plan(...)>, C<ok(...)>,
101 and C<skip(...)>. By default, all three are exported by
102 the C<use Test;> statement.
103  
104 =over 4
105  
106 =item C<plan(...)>
107  
108 BEGIN { plan %theplan; }
109  
110 This should be the first thing you call in your test script. It
111 declares your testing plan, how many there will be, if any of them
112 should be allowed to fail, and so on.
113  
114 Typical usage is just:
115  
116 use Test;
117 BEGIN { plan tests => 23 }
118  
119 These are the things that you can put in the parameters to plan:
120  
121 =over
122  
123 =item C<tests =E<gt> I<number>>
124  
125 The number of tests in your script.
126 This means all ok() and skip() calls.
127  
128 =item C<todo =E<gt> [I<1,5,14>]>
129  
130 A reference to a list of tests which are allowed to fail.
131 See L</TODO TESTS>.
132  
133 =item C<onfail =E<gt> sub { ... }>
134  
135 =item C<onfail =E<gt> \&some_sub>
136  
137 A subroutine reference to be run at the end of the test script, if
138 any of the tests fail. See L</ONFAIL>.
139  
140 =back
141  
142 You must call C<plan(...)> once and only once. You should call it
143 in a C<BEGIN {...}> block, like so:
144  
145 BEGIN { plan tests => 23 }
146  
147 =cut
148  
149 9 ▶sub plan {
Subroutine plan: called CC=14 SCAR=27.3
Pod documented
150 9 ▶ croak "Test::plan(%args): odd number of arguments" if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 9 9
151 9 ▶ croak "Test::plan(): should not be called more than once" if $planned;
Branch: 1 branch
BranchTrueFalseTotal
if $Test::planned 0 9 9
152  
153 9 local($\, $,); # guard against -l and other things that screw with
154 # print
155  
156 9 _reset_globals();
157  
158 9 _read_program( (caller)[1] );
159  
160 9 my $max=0;
161 9 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 13 9 22
162 13 my ($k,$v) = splice(@_, 0, 2);
163 13 ▶ if ($k =~ /^test(s)?$/) { $max = $v; }
Branch: 3 branches
BranchTrueFalseTotal
if ($k =~ /^test(s)?$/) { } 9 4 13
elsif ($k eq 'todo' or $k eq 'failok') { } 3 1 4
elsif ($k eq 'onfail') { } 1 0 1
164 ▶ elsif ($k eq 'todo' or
Condition66%2/3
$k eq "todo" or $k eq "failok"
l!l&&r!l&&!r
301
MC/DC50%1/2
$k eq "todo" or $k eq "failok"
$k eq "todo" $k eq "failok"
Truth table66%2/3
A or B
A= $k eq "todo"B= $k eq "failok"
ABresult
1X1
011
000
165 3 $k eq 'failok') { for (@$v) { $todo{$_}=1; }; }
166 elsif ($k eq 'onfail') {
167 1 ▶ ref $v eq 'CODE' or croak "Test::plan(onfail => $v): must be CODE";
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq "CODE" or croak("Test::plan(onfail => $v): must be CODE") 1 0 1
168 1 $ONFAIL = $v;
169 }
170 0 else { carp "Test::plan(): skipping unrecognized directive '$k'" }
171 }
172 9 my @todo = sort { $a <=> $b } keys %todo;
173 9 ▶ if (@todo) {
Branch: 1 branch
BranchTrueFalseTotal
if (@todo) { } 3 6 9
174 3 print $TESTOUT "1..$max todo ".join(' ', @todo).";\n";
175 } else {
176 6 print $TESTOUT "1..$max\n";
177 }
178 9 ++$planned;
179 9 print $TESTOUT "# Running under perl version $] for $^O",
180 (chr(65) eq 'A') ? "\n" : " in a non-ASCII world\n";
181  
182 9 ▶ print $TESTOUT "# Win32::BuildNumber ", &Win32::BuildNumber(), "\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined &Win32::BuildNumber and defined &Win32::BuildNumber() 0 9 9
Condition33%1/3
defined &Win32::BuildNumber and defined &Win32::BuildNumber()
!ll&&!rl&&r
900
MC/DC0%0/2
defined &Win32::BuildNumber and defined &Win32::BuildNumber()
defined &Win32::BuildNumber defined &Win32::BuildNumber()
Truth table33%1/3
A and B
A= defined &Win32::BuildNumberB= defined &Win32::BuildNumber()
ABresult
0X0
100
111
183 if defined(&Win32::BuildNumber) and defined &Win32::BuildNumber();
184  
185 9 ▶ print $TESTOUT "# MacPerl version $MacPerl::Version\n"
Branch: 1 branch
BranchTrueFalseTotal
if defined $MacPerl::Version 0 9 9
186 if defined $MacPerl::Version;
187  
188 9 printf $TESTOUT
189 "# Current time local: %s\n# Current time GMT: %s\n",
190 scalar(localtime($^T)), scalar(gmtime($^T));
191  
192 9 print $TESTOUT "# Using Test.pm version $VERSION\n";
193  
194 # Retval never used:
195 9 return undef;
196 }
197  
198 9 ▶sub _read_program {
Subroutine _read_program: called CC=8 SCAR=24.2
199 9 my($file) = shift;
200 9 ▶ return unless defined $file and length $file
Branch: 1 branch
BranchTrueFalseTotal
unless defined $file and length $file and -e $file and -f _ and -r _ 0 9 9
Condition33%4/12
defined $file and length $file and -e $file and -f _ and -r _
!ll&&!rl&&r
009
defined $file and length $file and -e $file and -f _
!ll&&!rl&&r
009
defined $file and length $file and -e $file
!ll&&!rl&&r
009
defined $file and length $file
!ll&&!rl&&r
009
MC/DC0%0/5
defined $file and length $file and -e $file and -f _ and -r _
defined $file length $file -e $file -f _ -r _
Truth table16%1/6
A and B and C and D and E
A= defined $fileB= length $fileC= -e $fileD= -f _E= -r _
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
201 and -e $file and -f _ and -r _;
202 9 ▶ open(SOURCEFILE, "<$file") || return;
Branch: 1 branch
BranchTrueFalseTotal
open SOURCEFILE, "<$file" or return 9 0 9
203 9 $Program_Lines{$file} = [<SOURCEFILE>];
204 9 close(SOURCEFILE);
205  
206 9 foreach my $x (@{$Program_Lines{$file}})
207 446 { $x =~ tr/\cm\cj\n\r//d }
208  
209 9 unshift @{$Program_Lines{$file}}, '';
210 9 return 1;
211 }
212  
213 =begin _private
214  
215 =item B<_to_value>
216  
217 my $value = _to_value($input);
218  
219 Converts an C<ok> parameter to its value. Typically this just means
220 running it, if it's a code reference. You should run all inputted
221 values through this.
222  
223 =cut
224  
225 124 ▶sub _to_value {
Subroutine _to_value: called CC=2 SCAR=6.9
226 124 my ($v) = @_;
227 124 ▶ return ref $v eq 'CODE' ? $v->() : $v;
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq 'CODE' ? : 6 118 124
228 }
229  
230 50 ▶sub _quote {
Subroutine _quote: called CC=2 SCAR=7.0
231 50 my $str = $_[0];
232 50 ▶ return "<UNDEF>" unless defined $str;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str 2 48 50
233 48 $str =~ s/\\/\\\\/g;
234 48 $str =~ s/"/\\"/g;
235 48 $str =~ s/\a/\\a/g;
236 48 $str =~ s/[\b]/\\b/g;
237 48 $str =~ s/\e/\\e/g;
238 48 $str =~ s/\f/\\f/g;
239 48 $str =~ s/\n/\\n/g;
240 48 $str =~ s/\r/\\r/g;
241 48 $str =~ s/\t/\\t/g;
242 0 $str =~ s/([\0-\037])(?!\d)/sprintf('\\%o',ord($1))/eg;
243 0 $str =~ s/([\0-\037\177-\377])/sprintf('\\x%02X',ord($1))/eg;
244 0 $str =~ s/([^\0-\176])/sprintf('\\x{%X}',ord($1))/eg;
245 #if( $_[1] ) {
246 # substr( $str , 218-3 ) = "..."
247 # if length($str) >= 218 and !$ENV{PERL_TEST_NO_TRUNC};
248 #}
249 48 return qq("$str");
250 }
251  
252  
253 =end _private
254  
255 =item C<ok(...)>
256  
257 ok(1 + 1 == 2);
258 ok($have, $expect);
259 ok($have, $expect, $diagnostics);
260  
261 This function is the reason for C<Test>'s existence. It's
262 the basic function that
263 handles printing "C<ok>" or "C<not ok>", along with the
264 current test number. (That's what C<Test::Harness> wants to see.)
265  
266 In its most basic usage, C<ok(...)> simply takes a single scalar
267 expression. If its value is true, the test passes; if false,
268 the test fails. Examples:
269  
270 # Examples of ok(scalar)
271  
272 ok( 1 + 1 == 2 ); # ok if 1 + 1 == 2
273 ok( $foo =~ /bar/ ); # ok if $foo contains 'bar'
274 ok( baz($x + $y) eq 'Armondo' ); # ok if baz($x + $y) returns
275 # 'Armondo'
276 ok( @a == @b ); # ok if @a and @b are the same length
277  
278 The expression is evaluated in scalar context. So the following will
279 work:
280  
281 ok( @stuff ); # ok if @stuff has any elements
282 ok( !grep !defined $_, @stuff ); # ok if everything in @stuff is
283 # defined.
284  
285 A special case is if the expression is a subroutine reference (in either
286 C<sub {...}> syntax or C<\&foo> syntax). In
287 that case, it is executed and its value (true or false) determines if
288 the test passes or fails. For example,
289  
290 ok( sub { # See whether sleep works at least passably
291 my $start_time = time;
292 sleep 5;
293 time() - $start_time >= 4
294 });
295  
296 In its two-argument form, C<ok(I<arg1>, I<arg2>)> compares the two
297 scalar values to see if they match. They match if both are undefined,
298 or if I<arg2> is a regex that matches I<arg1>, or if they compare equal
299 with C<eq>.
300  
301 # Example of ok(scalar, scalar)
302  
303 ok( "this", "that" ); # not ok, 'this' ne 'that'
304 ok( "", undef ); # not ok, "" is defined
305  
306 The second argument is considered a regex if it is either a regex
307 object or a string that looks like a regex. Regex objects are
308 constructed with the qr// operator in recent versions of perl. A
309 string is considered to look like a regex if its first and last
310 characters are "/", or if the first character is "m"
311 and its second and last characters are both the
312 same non-alphanumeric non-whitespace character. These regexp
313  
314 Regex examples:
315  
316 ok( 'JaffO', '/Jaff/' ); # ok, 'JaffO' =~ /Jaff/
317 ok( 'JaffO', 'm|Jaff|' ); # ok, 'JaffO' =~ m|Jaff|
318 ok( 'JaffO', qr/Jaff/ ); # ok, 'JaffO' =~ qr/Jaff/;
319 ok( 'JaffO', '/(?i)jaff/ ); # ok, 'JaffO' =~ /jaff/i;
320  
321 If either (or both!) is a subroutine reference, it is run and used
322 as the value for comparing. For example:
323  
324 ok sub {
325 open(OUT, ">x.dat") || die $!;
326 print OUT "\x{e000}";
327 close OUT;
328 my $bytecount = -s 'x.dat';
329 unlink 'x.dat' or warn "Can't unlink : $!";
330 return $bytecount;
331 },
332 4
333 ;
334  
335 The above test passes two values to C<ok(arg1, arg2)> -- the first
336 a coderef, and the second is the number 4. Before C<ok> compares them,
337 it calls the coderef, and uses its return value as the real value of
338 this parameter. Assuming that C<$bytecount> returns 4, C<ok> ends up
339 testing C<4 eq 4>. Since that's true, this test passes.
340  
341 Finally, you can append an optional third argument, in
342 C<ok(I<arg1>,I<arg2>, I<note>)>, where I<note> is a string value that
343 will be printed if the test fails. This should be some useful
344 information about the test, pertaining to why it failed, and/or
345 a description of the test. For example:
346  
347 ok( grep($_ eq 'something unique', @stuff), 1,
348 "Something that should be unique isn't!\n".
349 '@stuff = '.join ', ', @stuff
350 );
351  
352 Unfortunately, a note cannot be used with the single argument
353 style of C<ok()>. That is, if you try C<ok(I<arg1>, I<note>)>, then
354 C<Test> will interpret this as C<ok(I<arg1>, I<arg2>)>, and probably
355 end up testing C<I<arg1> eq I<arg2>> -- and that's not what you want!
356  
357 All of the above special cases can occasionally cause some
358 problems. See L</BUGS and CAVEATS>.
359  
360 =cut
361  
362 # A past maintainer of this module said:
363 # <<ok(...)'s special handling of subroutine references is an unfortunate
364 # "feature" that can't be removed due to compatibility.>>
365 #
366  
367 65 ▶sub ok ($;$$) {
Subroutine ok: called CC=15 SCAR=27.1
Pod documented
368 65 ▶ croak "ok: plan before you test!" if !$planned;
Branch: 1 branch
BranchTrueFalseTotal
unless $Test::planned 0 65 65
369  
370 65 local($\,$,); # guard against -l and other things that screw with
371 # print
372  
373 65 my ($pkg,$file,$line) = caller($TestLevel);
374 65 my $repetition = ++$history{"$file:$line"};
375 65 ▶ my $context = ("$file at line $line".
Branch: 1 branch
BranchTrueFalseTotal
$repetition > 1 ? : 20 45 65
376 ($repetition > 1 ? " fail \#$repetition" : ''));
377  
378 # Are we comparing two values?
379 65 my $compare = 0;
380  
381 65 my $ok=0;
382 65 my $result = _to_value(shift);
383 65 my ($expected, $isregex, $regex);
384 65 ▶ if (@_ == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 0) { } 17 48 65
385 17 $ok = $result;
386 } else {
387 48 $compare = 1;
388 48 $expected = _to_value(shift);
389 48 ▶ if (!defined $expected) {
Branch: 4 branches
BranchTrueFalseTotal
if (not defined $expected) { } 2 46 48
elsif (not defined $result) { } 1 45 46
elsif (ref $expected eq 'Regexp') { } 1 44 45
elsif (($regex) = $expected =~ m[^ / (.+) / $]sx or (undef, $regex) = $expected =~ /^ m([^\w\s]) (.+) \1 $/sx) { } 3 41 44
390 2 $ok = !defined $result;
391 } elsif (!defined $result) {
392 1 $ok = 0;
393 } elsif (ref($expected) eq 'Regexp') {
394 1 $ok = $result =~ /$expected/;
395 1 $regex = $expected;
396 ▶ } elsif (($regex) = ($expected =~ m,^ / (.+) / $,sx) or
Condition100%3/3
($regex) = $expected =~ m[^ / (.+) / $]sx or (undef, $regex) = $expected =~ /^ m([^\w\s]) (.+) \1 $/sx
l!l&&r!l&&!r
2141
MC/DC100%2/2
($regex) = $expected =~ m[^ / (.+) / $]sx or (undef, $regex) = $expected =~ /^ m([^\w\s]) (.+) \1 $/sx
($regex) = $expected =~ m[^ / (.+) / $]sx (undef, $regex) = $expected =~ /^ m([^\w\s]) (.+) \1 $/sx
Truth table100%3/3
A or B
A= ($regex) = $expected =~ m[^ / (.+) / $]sxB= (undef, $regex) = $expected =~ /^ m([^\w\s]) (.+) \1 $/sx
ABresult
1X1
011
000
397 (undef, $regex) = ($expected =~ m,^ m([^\w\s]) (.+) \1 $,sx)) {
398 3 $ok = $result =~ /$regex/;
399 } else {
400 41 $ok = $result eq $expected;
401 }
402 }
403 65 my $todo = $todo{$ntest};
404 65 ▶ if ($todo and $ok) {
Branch: 1 branch
BranchTrueFalseTotal
if ($todo and $ok) { } 3 62 65
Condition100%3/3
$todo and $ok
!ll&&!rl&&r
5753
MC/DC100%2/2
$todo and $ok
$todo $ok
Truth table100%3/3
A and B
A= $todoB= $ok
ABresult
0X0
100
111
405 3 ▶ $context .= ' TODO?!' if $todo;
Branch: 1 branch
BranchTrueFalseTotal
if $todo 3 0 3
406 3 print $TESTOUT "ok $ntest # ($context)\n";
407 } else {
408 # Issuing two seperate prints() causes problems on VMS.
409 62 ▶ if (!$ok) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok) { } 19 43 62
410 19 print $TESTOUT "not ok $ntest\n";
411 }
412 else {
413 43 print $TESTOUT "ok $ntest\n";
414 }
415  
416 62 ▶ $ok or _complain($result, $expected,
Branch: 2 branches
BranchTrueFalseTotal
$ok or _complain($result, $expected, {"repetition", $repetition, "package", $pkg, "result", $result, "todo", $todo, "file", $file, "line", $line, "context", $context, "compare", $compare, @_ ? ("diagnostic", _to_value(shift())) : ()}) 43 19 62
@_ ? : 4 15 19
417 {
418 'repetition' => $repetition, 'package' => $pkg,
419 'result' => $result, 'todo' => $todo,
420 'file' => $file, 'line' => $line,
421 'context' => $context, 'compare' => $compare,
422 @_ ? ('diagnostic' => _to_value(shift)) : (),
423 });
424  
425 }
426 65 ++ $ntest;
427 65 $ok;
428 }
429  
430  
431 19 ▶sub _complain {
Subroutine _complain: called CC=12 SCAR=24.9
432 19 my($result, $expected, $detail) = @_;
433 19 ▶ $$detail{expected} = $expected if defined $expected;
Branch: 1 branch
BranchTrueFalseTotal
if defined $expected 12 7 19
434  
435 # Get the user's diagnostic, protecting against multi-line
436 # diagnostics.
437 19 my $diag = $$detail{diagnostic};
438 19 ▶ $diag =~ s/\n/\n#/g if defined $diag;
Branch: 1 branch
BranchTrueFalseTotal
if defined $diag 4 15 19
439  
440 19 ▶ $$detail{context} .= ' *TODO*' if $$detail{todo};
Branch: 1 branch
BranchTrueFalseTotal
if $detail->{"todo"} 5 14 19
441 19 ▶ if (!$$detail{compare}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $detail->{'compare'}) { } 6 13 19
442 6 ▶ if (!$diag) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $diag) { } 6 0 6
443 6 print $TESTERR "# Failed test $ntest in $$detail{context}\n";
444 } else {
445 0 print $TESTERR "# Failed test $ntest in $$detail{context}: $diag\n";
446 }
447 } else {
448 13 my $prefix = "Test $ntest";
449  
450 13 print $TESTERR "# $prefix got: " . _quote($result) .
451 " ($$detail{context})\n";
452 13 $prefix = ' ' x (length($prefix) - 5);
453 my $expected_quoted = (defined $$detail{regex})
454 13 ▶ ? 'qr{'.($$detail{regex}).'}' : _quote($expected);
Branch: 1 branch
BranchTrueFalseTotal
defined $detail->{'regex'} ? : 0 13 13
455  
456 13 ▶ print $TESTERR "# $prefix Expected: $expected_quoted",
Branch: 1 branch
BranchTrueFalseTotal
$diag ? : 4 9 13
457 $diag ? " ($diag)" : (), "\n";
458  
459 13 ▶ _diff_complain( $result, $expected, $detail, $prefix )
Branch: 1 branch
BranchTrueFalseTotal
if defined $expected and 2 < $expected =~ tr/\n// 2 11 13
Condition100%3/3
defined $expected and 2 < $expected =~ tr/\n//
!ll&&!rl&&r
1102
MC/DC100%2/2
defined $expected and 2 < $expected =~ tr/\n//
defined $expected 2 < $expected =~ tr/\n//
Truth table100%3/3
A and B
A= defined $expectedB= 2 < $expected =~ tr/\n//
ABresult
0X0
100
111
460 if defined($expected) and 2 < ($expected =~ tr/\n//);
461 }
462  
463 19 ▶ if(defined $Program_Lines{ $$detail{file} }[ $$detail{line} ]) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $Test::Program_Lines{$detail->{"file"}}[$detail->{"line"}]) 18 1 19
464 print $TESTERR
465 "# $$detail{file} line $$detail{line} is: $Program_Lines{ $$detail{file} }[ $$detail{line} ]\n"
466 18 ▶ if $Program_Lines{ $$detail{file} }[ $$detail{line} ]
Branch: 1 branch
BranchTrueFalseTotal
if $Test::Program_Lines{$detail->{"file"}}[$detail->{"line"}] =~ /[^\s\#\(\)\{\}\[\]\;]/ 17 1 18
467 =~ m/[^\s\#\(\)\{\}\[\]\;]/; # Otherwise it's uninformative
468  
469 18 undef $Program_Lines{ $$detail{file} }[ $$detail{line} ];
470 # So we won't repeat it.
471 }
472  
473 19 push @FAILDETAIL, $detail;
474 19 return;
475 }
476  
477  
478  
479 2 ▶sub _diff_complain {
Subroutine _diff_complain: called CC=4 SCAR=16.6
480 2 my($result, $expected, $detail, $prefix) = @_;
481 2 ▶ return _diff_complain_external(@_) if $ENV{PERL_TEST_DIFF};
Branch: 1 branch
BranchTrueFalseTotal
if $ENV{"PERL_TEST_DIFF"} 0 2 2
482 return _diff_complain_algdiff(@_)
483 2 ▶ if eval { require Algorithm::Diff; Algorithm::Diff->VERSION(1.15); 1; };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { require Algorithm::Diff; "Algorithm::Diff"->VERSION("1.15"); 1 } } 2 0 2
484  
485 0 ▶ $told_about_diff++ or print $TESTERR <<"EOT";
Branch: 1 branch
BranchTrueFalseTotal
$Test::told_about_diff++ or print $Test::TESTERR "# $prefix (Install the Algorithm::Diff module to have differences in multiline\n# $prefix output explained. You might also set the PERL_TEST_DIFF environment\n# $prefix variable to run a diff program on the output.)\n" 0 0 0
486 # $prefix (Install the Algorithm::Diff module to have differences in multiline
487 # $prefix output explained. You might also set the PERL_TEST_DIFF environment
488 # $prefix variable to run a diff program on the output.)
489 EOT
490 ;
491 0 return;
492 }
493  
494  
495  
496 0 ▶sub _diff_complain_external {
Subroutine _diff_complain_external: not called CC=8 SCAR=42.8
497 0 my($result, $expected, $detail, $prefix) = @_;
498 0 ▶ my $diff = $ENV{PERL_TEST_DIFF} || die "WHAAAA?";
Condition0%0/2
$ENV{'PERL_TEST_DIFF'} || die('WHAAAA?')
l!l
00
MC/DC0%0/1
$ENV{'PERL_TEST_DIFF'} || die('WHAAAA?')
$ENV{'PERL_TEST_DIFF'}
Truth table0%0/2
A
A= $ENV{'PERL_TEST_DIFF'}
Aresult
11
00
499  
500 0 require File::Temp;
501 0 my($got_fh, $got_filename) = File::Temp::tempfile("test-got-XXXXX");
502 0 my($exp_fh, $exp_filename) = File::Temp::tempfile("test-exp-XXXXX");
503 0 ▶ unless ($got_fh && $exp_fh) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($got_fh and $exp_fh) 0 0 0
Condition0%0/3
$got_fh and $exp_fh
!ll&&!rl&&r
000
MC/DC0%0/2
$got_fh and $exp_fh
$got_fh $exp_fh
Truth table0%0/3
A and B
A= $got_fhB= $exp_fh
ABresult
0X0
100
111
504 0 warn "Can't get tempfiles";
505 0 return;
506 }
507  
508 0 print $got_fh $result;
509 0 print $exp_fh $expected;
510 0 ▶ if (close($got_fh) && close($exp_fh)) {
Branch: 1 branch
BranchTrueFalseTotal
if (close $got_fh and close $exp_fh) { } 0 0 0
Condition0%0/3
close $got_fh and close $exp_fh
!ll&&!rl&&r
000
MC/DC0%0/2
close $got_fh and close $exp_fh
close $got_fh close $exp_fh
Truth table0%0/3
A and B
A= close $got_fhB= close $exp_fh
ABresult
0X0
100
111
511 0 my $diff_cmd = "$diff $exp_filename $got_filename";
512 0 print $TESTERR "#\n# $prefix $diff_cmd\n";
513 0 ▶ if (open(DIFF, "$diff_cmd |")) {
Branch: 1 branch
BranchTrueFalseTotal
if (open DIFF, "$diff_cmd |") { } 0 0 0
514 0 local $_;
515 0 ▶ while (<DIFF>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline DIFF)) 0 0 0
516 0 print $TESTERR "# $prefix $_";
517 }
518 0 close(DIFF);
519 }
520 else {
521 0 warn "Can't run diff: $!";
522 }
523 } else {
524 0 warn "Can't write to tempfiles: $!";
525 }
526 0 unlink($got_filename);
527 0 unlink($exp_filename);
528 0 return;
529 }
530  
531  
532  
533 2 ▶sub _diff_complain_algdiff {
Subroutine _diff_complain_algdiff: called CC=1 SCAR=0.3
534 2 my($result, $expected, $detail, $prefix) = @_;
535  
536 2 my @got = split(/^/, $result);
537 2 my @exp = split(/^/, $expected);
538  
539 2 my $diff_kind;
540 my @diff_lines;
541  
542 9 ▶ my $diff_flush = sub {
Subroutine __ANON__: called CC=11 SCAR=28.6
543 9 ▶ return unless $diff_kind;
Branch: 1 branch
BranchTrueFalseTotal
unless $diff_kind 4 5 9
544  
545 5 my $count_lines = @diff_lines;
546 5 ▶ my $s = $count_lines == 1 ? "" : "s";
Branch: 1 branch
BranchTrueFalseTotal
$count_lines == 1 ? : 2 3 5
547 5 my $first_line = $diff_lines[0][0] + 1;
548  
549 5 print $TESTERR "# $prefix ";
550 5 ▶ if ($diff_kind eq "GOT") {
Branch: 3 branches
BranchTrueFalseTotal
if ($diff_kind eq 'GOT') { } 0 5 5
elsif ($diff_kind eq 'EXP') { } 0 5 5
elsif ($diff_kind eq 'CH') { } 5 0 5
551 0 print $TESTERR "Got $count_lines extra line$s at line $first_line:\n";
552 0 for my $i (@diff_lines) {
553 0 print $TESTERR "# $prefix + " . _quote($got[$i->[0]]) . "\n";
554 }
555 } elsif ($diff_kind eq "EXP") {
556 0 ▶ if ($count_lines > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count_lines > 1) { } 0 0 0
557 0 my $last_line = $diff_lines[-1][0] + 1;
558 0 print $TESTERR "Lines $first_line-$last_line are";
559 }
560 else {
561 0 print $TESTERR "Line $first_line is";
562 }
563 0 print $TESTERR " missing:\n";
564 0 for my $i (@diff_lines) {
565 0 print $TESTERR "# $prefix - " . _quote($exp[$i->[1]]) . "\n";
566 }
567 } elsif ($diff_kind eq "CH") {
568 5 ▶ if ($count_lines > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($count_lines > 1) { } 3 2 5
569 3 my $last_line = $diff_lines[-1][0] + 1;
570 3 print $TESTERR "Lines $first_line-$last_line are";
571 }
572 else {
573 2 print $TESTERR "Line $first_line is";
574 }
575 5 print $TESTERR " changed:\n";
576 5 for my $i (@diff_lines) {
577 12 print $TESTERR "# $prefix - " . _quote($exp[$i->[1]]) . "\n";
578 12 print $TESTERR "# $prefix + " . _quote($got[$i->[0]]) . "\n";
579 }
580 }
581  
582 # reset
583 5 $diff_kind = undef;
584 5 @diff_lines = ();
585 2 };
586  
587 12 ▶ my $diff_collect = sub {
Subroutine __ANON__: called CC=3 SCAR=11.3
588 12 my $kind = shift;
589 12 ▶ &$diff_flush() if $diff_kind && $diff_kind ne $kind;
Branch: 1 branch
BranchTrueFalseTotal
if $diff_kind and $diff_kind ne $kind 0 12 12
Condition66%2/3
$diff_kind and $diff_kind ne $kind
!ll&&!rl&&r
570
MC/DC0%0/2
$diff_kind and $diff_kind ne $kind
$diff_kind $diff_kind ne $kind
Truth table66%2/3
A and B
A= $diff_kindB= $diff_kind ne $kind
ABresult
0X0
100
111
590 12 $diff_kind = $kind;
591 12 push(@diff_lines, [@_]);
592 2 };
593  
594  
595 Algorithm::Diff::traverse_balanced(
596 \@got, \@exp,
597 {
598 0 ▶ DISCARD_A => sub { &$diff_collect("GOT", @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
599 0 ▶ DISCARD_B => sub { &$diff_collect("EXP", @_) },
Subroutine __ANON__: not called CC=1 SCAR=6.9
600 12 ▶ CHANGE => sub { &$diff_collect("CH", @_) },
Subroutine __ANON__: called CC=1 SCAR=0.0
601 7 ▶ MATCH => sub { &$diff_flush() },
Subroutine __ANON__: called CC=1 SCAR=0.0
602 },
603 2 );
604 2 &$diff_flush();
605  
606 2 return;
607 }
608  
609  
610  
611  
612 #~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~
613  
614  
615 =item C<skip(I<skip_if_true>, I<args...>)>
616  
617 This is used for tests that under some conditions can be skipped. It's
618 basically equivalent to:
619  
620 if( $skip_if_true ) {
621 ok(1);
622 } else {
623 ok( args... );
624 }
625  
626 ...except that the C<ok(1)> emits not just "C<ok I<testnum>>" but
627 actually "C<ok I<testnum> # I<skip_if_true_value>>".
628  
629 The arguments after the I<skip_if_true> are what is fed to C<ok(...)> if
630 this test isn't skipped.
631  
632 Example usage:
633  
634 my $if_MSWin =
635 $^O =~ m/MSWin/ ? 'Skip if under MSWin' : '';
636  
637 # A test to be skipped if under MSWin (i.e., run except under MSWin)
638 skip($if_MSWin, thing($foo), thing($bar) );
639  
640 Or, going the other way:
641  
642 my $unless_MSWin =
643 $^O =~ m/MSWin/ ? '' : 'Skip unless under MSWin';
644  
645 # A test to be skipped unless under MSWin (i.e., run only under MSWin)
646 skip($unless_MSWin, thing($foo), thing($bar) );
647  
648 The tricky thing to remember is that the first parameter is true if
649 you want to I<skip> the test, not I<run> it; and it also doubles as a
650 note about why it's being skipped. So in the first codeblock above, read
651 the code as "skip if MSWin -- (otherwise) test whether C<thing($foo)> is
652 C<thing($bar)>" or for the second case, "skip unless MSWin...".
653  
654 Also, when your I<skip_if_reason> string is true, it really should (for
655 backwards compatibility with older Test.pm versions) start with the
656 string "Skip", as shown in the above examples.
657  
658 Note that in the above cases, C<thing($foo)> and C<thing($bar)>
659 I<are> evaluated -- but as long as the C<skip_if_true> is true,
660 then we C<skip(...)> just tosses out their value (i.e., not
661 bothering to treat them like values to C<ok(...)>. But if
662 you need to I<not> eval the arguments when skipping the
663 test, use
664 this format:
665  
666 skip( $unless_MSWin,
667 sub {
668 # This code returns true if the test passes.
669 # (But it doesn't even get called if the test is skipped.)
670 thing($foo) eq thing($bar)
671 }
672 );
673  
674 or even this, which is basically equivalent:
675  
676 skip( $unless_MSWin,
677 sub { thing($foo) }, sub { thing($bar) }
678 );
679  
680 That is, both are like this:
681  
682 if( $unless_MSWin ) {
683 ok(1); # but it actually appends "# $unless_MSWin"
684 # so that Test::Harness can tell it's a skip
685 } else {
686 # Not skipping, so actually call and evaluate...
687 ok( sub { thing($foo) }, sub { thing($bar) } );
688 }
689  
690 =cut
691  
692 7 ▶sub skip ($;$$$) {
Subroutine skip: called CC=5 SCAR=16.1
Pod documented
693 7 local($\, $,); # guard against -l and other things that screw with
694 # print
695  
696 7 my $whyskip = _to_value(shift);
697 7 ▶ if (!@_ or $whyskip) {
Branch: 1 branch
BranchTrueFalseTotal
if (not @_ or $whyskip) { } 6 1 7
Condition66%2/3
not @_ or $whyskip
l!l&&r!l&&!r
061
MC/DC50%1/2
not @_ or $whyskip
not @_ $whyskip
Truth table66%2/3
not(A) or B
A= not @_B= $whyskip
ABresult
1X1
011
000
698 6 ▶ $whyskip = '' if $whyskip =~ m/^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
if $whyskip =~ /^\d+$/ 2 4 6
699 6 $whyskip =~ s/^[Ss]kip(?:\s+|$)//; # backwards compatibility, old
700 # versions required the reason
701 # to start with 'skip'
702 # We print in one shot for VMSy reasons.
703 6 my $ok = "ok $ntest # skip";
704 6 ▶ $ok .= " $whyskip" if length $whyskip;
Branch: 1 branch
BranchTrueFalseTotal
if length $whyskip 4 2 6
705 6 $ok .= "\n";
706 6 print $TESTOUT $ok;
707 6 ++ $ntest;
708 6 return 1;
709 } else {
710 # backwards compatibility (I think). skip() used to be
711 # called like ok(), which is weird. I haven't decided what to do with
712 # this yet.
713 # warn <<WARN if $^W;
714 #This looks like a skip() using the very old interface. Please upgrade to
715 #the documented interface as this has been deprecated.
716 #WARN
717  
718 1 local($TestLevel) = $TestLevel+1; #to ignore this stack frame
719 1 return &ok(@_);
720 }
721 }
722  
723 =back
724  
725 =cut
726  
727 9 ▶END {
Subroutine END: called CC=3 SCAR=11.0
728 9 ▶ $ONFAIL->(\@FAILDETAIL) if @FAILDETAIL && $ONFAIL;
Branch: 1 branch
BranchTrueFalseTotal
if @Test::FAILDETAIL and $Test::ONFAIL 1 8 9
Condition100%3/3
@Test::FAILDETAIL and $Test::ONFAIL
!ll&&!rl&&r
441
MC/DC100%2/2
@Test::FAILDETAIL and $Test::ONFAIL
@Test::FAILDETAIL $Test::ONFAIL
Truth table100%3/3
A and B
A= @Test::FAILDETAILB= $Test::ONFAIL
ABresult
0X0
100
111
729 }
730  
731 9 1;
732 __END__
↑ Summary