File Coverage

blib/lib/Test.pm
Criterion Covered Total %
statement 138 211 65.4
branch 70 106 66.0
condition 20 44 45.4
subroutine 13 21 61.9
pod 3 3 100.0
total 244 385 63.3


line stmt bran cond sub pod time code
1              
2             require 5.004;
3             package Test;
4              
5 9     9   6447 use strict;
  9         20  
  9         278  
6              
7 9     9   45 use Carp;
  9         18  
  9         6425  
8 9         39557 use vars (qw($VERSION @ISA @EXPORT @EXPORT_OK $ntest $TestLevel), #public-ish
9             qw($TESTOUT $TESTERR %Program_Lines $told_about_diff
10             $ONFAIL %todo %history $planned @FAILDETAIL) #private-ish
11 9     9   52 );
  9         18  
12              
13             # In case a test is run in a persistent environment.
14             sub _reset_globals {
15 9     9   29 %todo = ();
16 9         24 %history = ();
17 9         18 @FAILDETAIL = ();
18 9         33 $ntest = 1;
19 9         19 $TestLevel = 0; # how many extra stack frames to skip
20 9         19 $planned = 0;
21             }
22              
23             $VERSION = '1.26';
24             require Exporter;
25             @ISA=('Exporter');
26              
27             @EXPORT = qw(&plan &ok &skip);
28             @EXPORT_OK = qw($ntest $TESTOUT $TESTERR);
29              
30             $|=1;
31             $TESTOUT = *STDOUT{IO};
32             $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             $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 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 (in a new F 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, F, and
96             F
97              
98             =head2 Functions
99              
100             This module defines three public functions, C, C,
101             and C. By default, all three are exported by
102             the C statement.
103              
104             =over 4
105              
106             =item C
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 I>
124              
125             The number of tests in your script.
126             This means all ok() and skip() calls.
127              
128             =item C [I<1,5,14>]>
129              
130             A reference to a list of tests which are allowed to fail.
131             See L.
132              
133             =item C sub { ... }>
134              
135             =item C \&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.
139              
140             =back
141              
142             You must call C once and only once. You should call it
143             in a C block, like so:
144              
145             BEGIN { plan tests => 23 }
146              
147             =cut
148              
149             sub plan {
150 9 50   9 1 857 croak "Test::plan(%args): odd number of arguments" if @_ & 1;
151 9 50       39 croak "Test::plan(): should not be called more than once" if $planned;
152              
153 9         51 local($\, $,); # guard against -l and other things that screw with
154             # print
155              
156 9         35 _reset_globals();
157              
158 9         49 _read_program( (caller)[1] );
159              
160 9         19 my $max=0;
161 9         34 while (@_) {
162 13         44 my ($k,$v) = splice(@_, 0, 2);
163 13 100 66     91 if ($k =~ /^test(s)?$/) { $max = $v; }
  9 100       31  
    50          
164             elsif ($k eq 'todo' or
165 3         8 $k eq 'failok') { for (@$v) { $todo{$_}=1; }; }
  8         28  
166             elsif ($k eq 'onfail') {
167 1 50       5 ref $v eq 'CODE' or croak "Test::plan(onfail => $v): must be CODE";
168 1         4 $ONFAIL = $v;
169             }
170 0         0 else { carp "Test::plan(): skipping unrecognized directive '$k'" }
171             }
172 9         56 my @todo = sort { $a <=> $b } keys %todo;
  7         23  
173 9 100       40 if (@todo) {
174 3         580 print $TESTOUT "1..$max todo ".join(' ', @todo).";\n";
175             } else {
176 6         2091 print $TESTOUT "1..$max\n";
177             }
178 9         31 ++$planned;
179 9         1051 print $TESTOUT "# Running under perl version $] for $^O",
180             (chr(65) eq 'A') ? "\n" : " in a non-ASCII world\n";
181              
182 9 50 33     65 print $TESTOUT "# Win32::BuildNumber ", &Win32::BuildNumber(), "\n"
183             if defined(&Win32::BuildNumber) and defined &Win32::BuildNumber();
184              
185 9 50       35 print $TESTOUT "# MacPerl version $MacPerl::Version\n"
186             if defined $MacPerl::Version;
187              
188 9         2749 printf $TESTOUT
189             "# Current time local: %s\n# Current time GMT: %s\n",
190             scalar(localtime($^T)), scalar(gmtime($^T));
191              
192 9         745 print $TESTOUT "# Using Test.pm version $VERSION\n";
193              
194             # Retval never used:
195 9         6253 return undef;
196             }
197              
198             sub _read_program {
199 9     9   25 my($file) = shift;
200 9 50 33     393 return unless defined $file and length $file
      33        
      33        
      33        
201             and -e $file and -f _ and -r _;
202 9 50       317 open(SOURCEFILE, "<$file") || return;
203 9         619 $Program_Lines{$file} = [];
204 9         126 close(SOURCEFILE);
205              
206 9         20 foreach my $x (@{$Program_Lines{$file}})
  9         30  
207 446         570 { $x =~ tr/\cm\cj\n\r//d }
208              
209 9         29 unshift @{$Program_Lines{$file}}, '';
  9         44  
210 9         24 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 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             sub _to_value {
226 124     124   183 my ($v) = @_;
227 124 100       379 return ref $v eq 'CODE' ? $v->() : $v;
228             }
229              
230             sub _quote {
231 26     26   37 my $str = $_[0];
232 26 100       78 return "" unless defined $str;
233 24         35 $str =~ s/\\/\\\\/g;
234 24         36 $str =~ s/"/\\"/g;
235 24         31 $str =~ s/\a/\\a/g;
236 24         45 $str =~ s/[\b]/\\b/g;
237 24         37 $str =~ s/\e/\\e/g;
238 24         28 $str =~ s/\f/\\f/g;
239 24         58 $str =~ s/\n/\\n/g;
240 24         32 $str =~ s/\r/\\r/g;
241 24         47 $str =~ s/\t/\\t/g;
242 24         41 $str =~ s/([\0-\037])(?!\d)/sprintf('\\%o',ord($1))/eg;
  0         0  
243 24         35 $str =~ s/([\0-\037\177-\377])/sprintf('\\x%02X',ord($1))/eg;
  0         0  
244 24         57 $str =~ s/([^\0-\176])/sprintf('\\x{%X}',ord($1))/eg;
  0         0  
245             #if( $_[1] ) {
246             # substr( $str , 218-3 ) = "..."
247             # if length($str) >= 218 and !$ENV{PERL_TEST_NO_TRUNC};
248             #}
249 24         355 return qq("$str");
250             }
251              
252              
253             =end _private
254              
255             =item C
256              
257             ok(1 + 1 == 2);
258             ok($have, $expect);
259             ok($have, $expect, $diagnostics);
260              
261             This function is the reason for C's existence. It's
262             the basic function that
263             handles printing "C" or "C", along with the
264             current test number. (That's what C wants to see.)
265              
266             In its most basic usage, C 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 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, I)> compares the two
297             scalar values to see if they match. They match if both are undefined,
298             or if I is a regex that matches I, or if they compare equal
299             with C.
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 -- the first
336             a coderef, and the second is the number 4. Before C 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 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,I, I)>, where I 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. That is, if you try C, I)>, then
354             C will interpret this as C, I)>, and probably
355             end up testing C eq I> -- and that's not what you want!
356              
357             All of the above special cases can occasionally cause some
358             problems. See L.
359              
360             =cut
361              
362             # A past maintainer of this module said:
363             # <
364             # "feature" that can't be removed due to compatibility.>>
365             #
366              
367             sub ok ($;$$) {
368 65 50   65 1 14513 croak "ok: plan before you test!" if !$planned;
369              
370 65         232 local($\,$,); # guard against -l and other things that screw with
371             # print
372              
373 65         279 my ($pkg,$file,$line) = caller($TestLevel);
374 65         235 my $repetition = ++$history{"$file:$line"};
375 65 100       265 my $context = ("$file at line $line".
376             ($repetition > 1 ? " fail \#$repetition" : ''));
377              
378             # Are we comparing two values?
379 65         77 my $compare = 0;
380              
381 65         74 my $ok=0;
382 65         145 my $result = _to_value(shift);
383 65         102 my ($expected, $isregex, $regex);
384 65 100       138 if (@_ == 0) {
385 17         23 $ok = $result;
386             } else {
387 48         61 $compare = 1;
388 48         84 $expected = _to_value(shift);
389 48 100 100     453 if (!defined $expected) {
    100          
    100          
    100          
390 2         6 $ok = !defined $result;
391             } elsif (!defined $result) {
392 1         2 $ok = 0;
393             } elsif (ref($expected) eq 'Regexp') {
394 1         7 $ok = $result =~ /$expected/;
395 1         4 $regex = $expected;
396             } elsif (($regex) = ($expected =~ m,^ / (.+) / $,sx) or
397             (undef, $regex) = ($expected =~ m,^ m([^\w\s]) (.+) \1 $,sx)) {
398 3         58 $ok = $result =~ /$regex/;
399             } else {
400 41         74 $ok = $result eq $expected;
401             }
402             }
403 65         151 my $todo = $todo{$ntest};
404 65 100 100     211 if ($todo and $ok) {
405 3 50       9 $context .= ' TODO?!' if $todo;
406 3         10 print $TESTOUT "ok $ntest # ($context)\n";
407             } else {
408             # Issuing two seperate prints() causes problems on VMS.
409 62 100       182 if (!$ok) {
410 19         545 print $TESTOUT "not ok $ntest\n";
411             }
412             else {
413 43         7726 print $TESTOUT "ok $ntest\n";
414             }
415              
416 62 100       390 $ok or _complain($result, $expected,
    100          
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         78 ++ $ntest;
427 65         3447 $ok;
428             }
429              
430              
431             sub _complain {
432 19     19   46 my($result, $expected, $detail) = @_;
433 19 100       67 $$detail{expected} = $expected if defined $expected;
434              
435             # Get the user's diagnostic, protecting against multi-line
436             # diagnostics.
437 19         27 my $diag = $$detail{diagnostic};
438 19 100       67 $diag =~ s/\n/\n#/g if defined $diag;
439              
440 19 100       55 $$detail{context} .= ' *TODO*' if $$detail{todo};
441 19 100       56 if (!$$detail{compare}) {
442 6 50       13 if (!$diag) {
443 6         24 print $TESTERR "# Failed test $ntest in $$detail{context}\n";
444             } else {
445 0         0 print $TESTERR "# Failed test $ntest in $$detail{context}: $diag\n";
446             }
447             } else {
448 13         29 my $prefix = "Test $ntest";
449              
450 13         56 print $TESTERR "# $prefix got: " . _quote($result) .
451             " ($$detail{context})\n";
452 13         48 $prefix = ' ' x (length($prefix) - 5);
453 13 50       49 my $expected_quoted = (defined $$detail{regex})
454             ? 'qr{'.($$detail{regex}).'}' : _quote($expected);
455              
456 13 100       387 print $TESTERR "# $prefix Expected: $expected_quoted",
457             $diag ? " ($diag)" : (), "\n";
458              
459 13 100 100     96 _diff_complain( $result, $expected, $detail, $prefix )
460             if defined($expected) and 2 < ($expected =~ tr/\n//);
461             }
462              
463 19 100       75 if(defined $Program_Lines{ $$detail{file} }[ $$detail{line} ]) {
464 18 100       420 print $TESTERR
465             "# $$detail{file} line $$detail{line} is: $Program_Lines{ $$detail{file} }[ $$detail{line} ]\n"
466             if $Program_Lines{ $$detail{file} }[ $$detail{line} ]
467             =~ m/[^\s\#\(\)\{\}\[\]\;]/; # Otherwise it's uninformative
468              
469 18         64 undef $Program_Lines{ $$detail{file} }[ $$detail{line} ];
470             # So we won't repeat it.
471             }
472              
473 19         33 push @FAILDETAIL, $detail;
474 19         36 return;
475             }
476              
477              
478              
479             sub _diff_complain {
480 2     2   4 my($result, $expected, $detail, $prefix) = @_;
481 2 50       8 return _diff_complain_external(@_) if $ENV{PERL_TEST_DIFF};
482             return _diff_complain_algdiff(@_)
483 2 50       3 if eval { require Algorithm::Diff; Algorithm::Diff->VERSION(1.15); 1; };
  2         812  
  0         0  
  0         0  
484              
485 2 100       243 $told_about_diff++ or print $TESTERR <<"EOT";
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 2         7 return;
492             }
493              
494              
495              
496             sub _diff_complain_external {
497 0     0   0 my($result, $expected, $detail, $prefix) = @_;
498 0   0     0 my $diff = $ENV{PERL_TEST_DIFF} || die "WHAAAA?";
499              
500 0         0 require File::Temp;
501 0         0 my($got_fh, $got_filename) = File::Temp::tempfile("test-got-XXXXX");
502 0         0 my($exp_fh, $exp_filename) = File::Temp::tempfile("test-exp-XXXXX");
503 0 0 0     0 unless ($got_fh && $exp_fh) {
504 0         0 warn "Can't get tempfiles";
505 0         0 return;
506             }
507              
508 0         0 print $got_fh $result;
509 0         0 print $exp_fh $expected;
510 0 0 0     0 if (close($got_fh) && close($exp_fh)) {
511 0         0 my $diff_cmd = "$diff $exp_filename $got_filename";
512 0         0 print $TESTERR "#\n# $prefix $diff_cmd\n";
513 0 0       0 if (open(DIFF, "$diff_cmd |")) {
514 0         0 local $_;
515 0         0 while () {
516 0         0 print $TESTERR "# $prefix $_";
517             }
518 0         0 close(DIFF);
519             }
520             else {
521 0         0 warn "Can't run diff: $!";
522             }
523             } else {
524 0         0 warn "Can't write to tempfiles: $!";
525             }
526 0         0 unlink($got_filename);
527 0         0 unlink($exp_filename);
528 0         0 return;
529             }
530              
531              
532              
533             sub _diff_complain_algdiff {
534 0     0   0 my($result, $expected, $detail, $prefix) = @_;
535              
536 0         0 my @got = split(/^/, $result);
537 0         0 my @exp = split(/^/, $expected);
538              
539 0         0 my $diff_kind;
540             my @diff_lines;
541              
542             my $diff_flush = sub {
543 0 0   0   0 return unless $diff_kind;
544              
545 0         0 my $count_lines = @diff_lines;
546 0 0       0 my $s = $count_lines == 1 ? "" : "s";
547 0         0 my $first_line = $diff_lines[0][0] + 1;
548              
549 0         0 print $TESTERR "# $prefix ";
550 0 0       0 if ($diff_kind eq "GOT") {
    0          
    0          
551 0         0 print $TESTERR "Got $count_lines extra line$s at line $first_line:\n";
552 0         0 for my $i (@diff_lines) {
553 0         0 print $TESTERR "# $prefix + " . _quote($got[$i->[0]]) . "\n";
554             }
555             } elsif ($diff_kind eq "EXP") {
556 0 0       0 if ($count_lines > 1) {
557 0         0 my $last_line = $diff_lines[-1][0] + 1;
558 0         0 print $TESTERR "Lines $first_line-$last_line are";
559             }
560             else {
561 0         0 print $TESTERR "Line $first_line is";
562             }
563 0         0 print $TESTERR " missing:\n";
564 0         0 for my $i (@diff_lines) {
565 0         0 print $TESTERR "# $prefix - " . _quote($exp[$i->[1]]) . "\n";
566             }
567             } elsif ($diff_kind eq "CH") {
568 0 0       0 if ($count_lines > 1) {
569 0         0 my $last_line = $diff_lines[-1][0] + 1;
570 0         0 print $TESTERR "Lines $first_line-$last_line are";
571             }
572             else {
573 0         0 print $TESTERR "Line $first_line is";
574             }
575 0         0 print $TESTERR " changed:\n";
576 0         0 for my $i (@diff_lines) {
577 0         0 print $TESTERR "# $prefix - " . _quote($exp[$i->[1]]) . "\n";
578 0         0 print $TESTERR "# $prefix + " . _quote($got[$i->[0]]) . "\n";
579             }
580             }
581              
582             # reset
583 0         0 $diff_kind = undef;
584 0         0 @diff_lines = ();
585 0         0 };
586              
587             my $diff_collect = sub {
588 0     0   0 my $kind = shift;
589 0 0 0     0 &$diff_flush() if $diff_kind && $diff_kind ne $kind;
590 0         0 $diff_kind = $kind;
591 0         0 push(@diff_lines, [@_]);
592 0         0 };
593              
594              
595             Algorithm::Diff::traverse_balanced(
596             \@got, \@exp,
597             {
598 0     0   0 DISCARD_A => sub { &$diff_collect("GOT", @_) },
599 0     0   0 DISCARD_B => sub { &$diff_collect("EXP", @_) },
600 0     0   0 CHANGE => sub { &$diff_collect("CH", @_) },
601 0     0   0 MATCH => sub { &$diff_flush() },
602             },
603 0         0 );
604 0         0 &$diff_flush();
605              
606 0         0 return;
607             }
608              
609              
610              
611              
612             #~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~
613              
614              
615             =item C, I)>
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 emits not just "C>" but
627             actually "C # I>".
628              
629             The arguments after the I are what is fed to C 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 the test, not I 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 is
652             C" or for the second case, "skip unless MSWin...".
653              
654             Also, when your I 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 and C
659             I evaluated -- but as long as the C is true,
660             then we C just tosses out their value (i.e., not
661             bothering to treat them like values to C. But if
662             you need to I 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             sub skip ($;$$$) {
693 7     7 1 2910 local($\, $,); # guard against -l and other things that screw with
694             # print
695              
696 7         55 my $whyskip = _to_value(shift);
697 7 100 66     57 if (!@_ or $whyskip) {
698 6 100       36 $whyskip = '' if $whyskip =~ m/^\d+$/;
699 6         12 $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         15 my $ok = "ok $ntest # skip";
704 6 100       30 $ok .= " $whyskip" if length $whyskip;
705 6         8 $ok .= "\n";
706 6         214 print $TESTOUT $ok;
707 6         13 ++ $ntest;
708 6         20 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 <
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         10 local($TestLevel) = $TestLevel+1; #to ignore this stack frame
719 1         5 return &ok(@_);
720             }
721             }
722              
723             =back
724              
725             =cut
726              
727             END {
728 9 100 66 9   285 $ONFAIL->(\@FAILDETAIL) if @FAILDETAIL && $ONFAIL;
729             }
730              
731             1;
732             __END__