1b8851fccSafresh1 2b8851fccSafresh1require 5.004; 3b8851fccSafresh1package Test; 4b8851fccSafresh1 5b8851fccSafresh1use strict; 6b8851fccSafresh1 7b8851fccSafresh1use Carp; 8*5759b3d2Safresh1our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, $ntest, $TestLevel); #public-is 9*5759b3d2Safresh1our ($TESTOUT, $TESTERR, %Program_Lines, $told_about_diff, 10*5759b3d2Safresh1 $ONFAIL, %todo, %history, $planned, @FAILDETAIL); #private-ish 11b8851fccSafresh1 12b8851fccSafresh1# In case a test is run in a persistent environment. 13b8851fccSafresh1sub _reset_globals { 14b8851fccSafresh1 %todo = (); 15b8851fccSafresh1 %history = (); 16b8851fccSafresh1 @FAILDETAIL = (); 17b8851fccSafresh1 $ntest = 1; 18b8851fccSafresh1 $TestLevel = 0; # how many extra stack frames to skip 19b8851fccSafresh1 $planned = 0; 20b8851fccSafresh1} 21b8851fccSafresh1 22*5759b3d2Safresh1$VERSION = '1.31'; 23b8851fccSafresh1require Exporter; 24b8851fccSafresh1@ISA=('Exporter'); 25b8851fccSafresh1 26b8851fccSafresh1@EXPORT = qw(&plan &ok &skip); 27b8851fccSafresh1@EXPORT_OK = qw($ntest $TESTOUT $TESTERR); 28b8851fccSafresh1 29b8851fccSafresh1$|=1; 30b8851fccSafresh1$TESTOUT = *STDOUT{IO}; 31b8851fccSafresh1$TESTERR = *STDERR{IO}; 32b8851fccSafresh1 33b8851fccSafresh1# Use of this variable is strongly discouraged. It is set mainly to 34b8851fccSafresh1# help test coverage analyzers know which test is running. 35b8851fccSafresh1$ENV{REGRESSION_TEST} = $0; 36b8851fccSafresh1 37b8851fccSafresh1 38b8851fccSafresh1=head1 NAME 39b8851fccSafresh1 40b8851fccSafresh1Test - provides a simple framework for writing test scripts 41b8851fccSafresh1 42b8851fccSafresh1=head1 SYNOPSIS 43b8851fccSafresh1 44b8851fccSafresh1 use strict; 45b8851fccSafresh1 use Test; 46b8851fccSafresh1 47b8851fccSafresh1 # use a BEGIN block so we print our plan before MyModule is loaded 48b8851fccSafresh1 BEGIN { plan tests => 14, todo => [3,4] } 49b8851fccSafresh1 50b8851fccSafresh1 # load your module... 51b8851fccSafresh1 use MyModule; 52b8851fccSafresh1 53b8851fccSafresh1 # Helpful notes. All note-lines must start with a "#". 54b8851fccSafresh1 print "# I'm testing MyModule version $MyModule::VERSION\n"; 55b8851fccSafresh1 56b8851fccSafresh1 ok(0); # failure 57b8851fccSafresh1 ok(1); # success 58b8851fccSafresh1 59b8851fccSafresh1 ok(0); # ok, expected failure (see todo list, above) 60b8851fccSafresh1 ok(1); # surprise success! 61b8851fccSafresh1 62b8851fccSafresh1 ok(0,1); # failure: '0' ne '1' 63b8851fccSafresh1 ok('broke','fixed'); # failure: 'broke' ne 'fixed' 64b8851fccSafresh1 ok('fixed','fixed'); # success: 'fixed' eq 'fixed' 65b8851fccSafresh1 ok('fixed',qr/x/); # success: 'fixed' =~ qr/x/ 66b8851fccSafresh1 67b8851fccSafresh1 ok(sub { 1+1 }, 2); # success: '2' eq '2' 68b8851fccSafresh1 ok(sub { 1+1 }, 3); # failure: '2' ne '3' 69b8851fccSafresh1 70b8851fccSafresh1 my @list = (0,0); 71b8851fccSafresh1 ok @list, 3, "\@list=".join(',',@list); #extra notes 72b8851fccSafresh1 ok 'segmentation fault', '/(?i)success/'; #regex match 73b8851fccSafresh1 74b8851fccSafresh1 skip( 75b8851fccSafresh1 $^O =~ m/MSWin/ ? "Skip if MSWin" : 0, # whether to skip 76b8851fccSafresh1 $foo, $bar # arguments just like for ok(...) 77b8851fccSafresh1 ); 78b8851fccSafresh1 skip( 79b8851fccSafresh1 $^O =~ m/MSWin/ ? 0 : "Skip unless MSWin", # whether to skip 80b8851fccSafresh1 $foo, $bar # arguments just like for ok(...) 81b8851fccSafresh1 ); 82b8851fccSafresh1 83b8851fccSafresh1=head1 DESCRIPTION 84b8851fccSafresh1 85b8851fccSafresh1This module simplifies the task of writing test files for Perl modules, 86b8851fccSafresh1such that their output is in the format that 87b8851fccSafresh1L<Test::Harness|Test::Harness> expects to see. 88b8851fccSafresh1 89b8851fccSafresh1=head1 QUICK START GUIDE 90b8851fccSafresh1 91b8851fccSafresh1To write a test for your new (and probably not even done) module, create 92b8851fccSafresh1a new file called F<t/test.t> (in a new F<t> directory). If you have 93b8851fccSafresh1multiple test files, to test the "foo", "bar", and "baz" feature sets, 94b8851fccSafresh1then feel free to call your files F<t/foo.t>, F<t/bar.t>, and 95b8851fccSafresh1F<t/baz.t> 96b8851fccSafresh1 97b8851fccSafresh1=head2 Functions 98b8851fccSafresh1 99b8851fccSafresh1This module defines three public functions, C<plan(...)>, C<ok(...)>, 100b8851fccSafresh1and C<skip(...)>. By default, all three are exported by 101b8851fccSafresh1the C<use Test;> statement. 102b8851fccSafresh1 103b8851fccSafresh1=over 4 104b8851fccSafresh1 105b8851fccSafresh1=item C<plan(...)> 106b8851fccSafresh1 107b8851fccSafresh1 BEGIN { plan %theplan; } 108b8851fccSafresh1 109b8851fccSafresh1This should be the first thing you call in your test script. It 110b8851fccSafresh1declares your testing plan, how many there will be, if any of them 111b8851fccSafresh1should be allowed to fail, and so on. 112b8851fccSafresh1 113b8851fccSafresh1Typical usage is just: 114b8851fccSafresh1 115b8851fccSafresh1 use Test; 116b8851fccSafresh1 BEGIN { plan tests => 23 } 117b8851fccSafresh1 118b8851fccSafresh1These are the things that you can put in the parameters to plan: 119b8851fccSafresh1 120b8851fccSafresh1=over 121b8851fccSafresh1 122b8851fccSafresh1=item C<tests =E<gt> I<number>> 123b8851fccSafresh1 124b8851fccSafresh1The number of tests in your script. 125b8851fccSafresh1This means all ok() and skip() calls. 126b8851fccSafresh1 127b8851fccSafresh1=item C<todo =E<gt> [I<1,5,14>]> 128b8851fccSafresh1 129b8851fccSafresh1A reference to a list of tests which are allowed to fail. 130b8851fccSafresh1See L</TODO TESTS>. 131b8851fccSafresh1 132b8851fccSafresh1=item C<onfail =E<gt> sub { ... }> 133b8851fccSafresh1 134b8851fccSafresh1=item C<onfail =E<gt> \&some_sub> 135b8851fccSafresh1 136b8851fccSafresh1A subroutine reference to be run at the end of the test script, if 137b8851fccSafresh1any of the tests fail. See L</ONFAIL>. 138b8851fccSafresh1 139b8851fccSafresh1=back 140b8851fccSafresh1 141b8851fccSafresh1You must call C<plan(...)> once and only once. You should call it 142b8851fccSafresh1in a C<BEGIN {...}> block, like so: 143b8851fccSafresh1 144b8851fccSafresh1 BEGIN { plan tests => 23 } 145b8851fccSafresh1 146b8851fccSafresh1=cut 147b8851fccSafresh1 148b8851fccSafresh1sub plan { 149b8851fccSafresh1 croak "Test::plan(%args): odd number of arguments" if @_ & 1; 150b8851fccSafresh1 croak "Test::plan(): should not be called more than once" if $planned; 151b8851fccSafresh1 152b8851fccSafresh1 local($\, $,); # guard against -l and other things that screw with 153b8851fccSafresh1 # print 154b8851fccSafresh1 155b8851fccSafresh1 _reset_globals(); 156b8851fccSafresh1 157b8851fccSafresh1 _read_program( (caller)[1] ); 158b8851fccSafresh1 159b8851fccSafresh1 my $max=0; 160b8851fccSafresh1 while (@_) { 161b8851fccSafresh1 my ($k,$v) = splice(@_, 0, 2); 162b8851fccSafresh1 if ($k =~ /^test(s)?$/) { $max = $v; } 163b8851fccSafresh1 elsif ($k eq 'todo' or 164b8851fccSafresh1 $k eq 'failok') { for (@$v) { $todo{$_}=1; }; } 165b8851fccSafresh1 elsif ($k eq 'onfail') { 166b8851fccSafresh1 ref $v eq 'CODE' or croak "Test::plan(onfail => $v): must be CODE"; 167b8851fccSafresh1 $ONFAIL = $v; 168b8851fccSafresh1 } 169b8851fccSafresh1 else { carp "Test::plan(): skipping unrecognized directive '$k'" } 170b8851fccSafresh1 } 171b8851fccSafresh1 my @todo = sort { $a <=> $b } keys %todo; 172b8851fccSafresh1 if (@todo) { 173b8851fccSafresh1 print $TESTOUT "1..$max todo ".join(' ', @todo).";\n"; 174b8851fccSafresh1 } else { 175b8851fccSafresh1 print $TESTOUT "1..$max\n"; 176b8851fccSafresh1 } 177b8851fccSafresh1 ++$planned; 178b8851fccSafresh1 print $TESTOUT "# Running under perl version $] for $^O", 179b8851fccSafresh1 (chr(65) eq 'A') ? "\n" : " in a non-ASCII world\n"; 180b8851fccSafresh1 181b8851fccSafresh1 print $TESTOUT "# Win32::BuildNumber ", &Win32::BuildNumber(), "\n" 182b8851fccSafresh1 if defined(&Win32::BuildNumber) and defined &Win32::BuildNumber(); 183b8851fccSafresh1 184b8851fccSafresh1 print $TESTOUT "# MacPerl version $MacPerl::Version\n" 185b8851fccSafresh1 if defined $MacPerl::Version; 186b8851fccSafresh1 187b8851fccSafresh1 printf $TESTOUT 188b8851fccSafresh1 "# Current time local: %s\n# Current time GMT: %s\n", 189b8851fccSafresh1 scalar(localtime($^T)), scalar(gmtime($^T)); 190b8851fccSafresh1 191b8851fccSafresh1 print $TESTOUT "# Using Test.pm version $VERSION\n"; 192b8851fccSafresh1 193b8851fccSafresh1 # Retval never used: 194b8851fccSafresh1 return undef; 195b8851fccSafresh1} 196b8851fccSafresh1 197b8851fccSafresh1sub _read_program { 198b8851fccSafresh1 my($file) = shift; 199b8851fccSafresh1 return unless defined $file and length $file 200b8851fccSafresh1 and -e $file and -f _ and -r _; 201*5759b3d2Safresh1 open(SOURCEFILE, '<', $file) || return; 202b8851fccSafresh1 $Program_Lines{$file} = [<SOURCEFILE>]; 203b8851fccSafresh1 close(SOURCEFILE); 204b8851fccSafresh1 205b8851fccSafresh1 foreach my $x (@{$Program_Lines{$file}}) 206b8851fccSafresh1 { $x =~ tr/\cm\cj\n\r//d } 207b8851fccSafresh1 208b8851fccSafresh1 unshift @{$Program_Lines{$file}}, ''; 209b8851fccSafresh1 return 1; 210b8851fccSafresh1} 211b8851fccSafresh1 212b8851fccSafresh1=begin _private 213b8851fccSafresh1 214b8851fccSafresh1=item B<_to_value> 215b8851fccSafresh1 216b8851fccSafresh1 my $value = _to_value($input); 217b8851fccSafresh1 218b8851fccSafresh1Converts an C<ok> parameter to its value. Typically this just means 219b8851fccSafresh1running it, if it's a code reference. You should run all inputted 220b8851fccSafresh1values through this. 221b8851fccSafresh1 222b8851fccSafresh1=cut 223b8851fccSafresh1 224b8851fccSafresh1sub _to_value { 225b8851fccSafresh1 my ($v) = @_; 226b8851fccSafresh1 return ref $v eq 'CODE' ? $v->() : $v; 227b8851fccSafresh1} 228b8851fccSafresh1 229b8851fccSafresh1sub _quote { 230b8851fccSafresh1 my $str = $_[0]; 231b8851fccSafresh1 return "<UNDEF>" unless defined $str; 232b8851fccSafresh1 $str =~ s/\\/\\\\/g; 233b8851fccSafresh1 $str =~ s/"/\\"/g; 234b8851fccSafresh1 $str =~ s/\a/\\a/g; 235b8851fccSafresh1 $str =~ s/[\b]/\\b/g; 236b8851fccSafresh1 $str =~ s/\e/\\e/g; 237b8851fccSafresh1 $str =~ s/\f/\\f/g; 238b8851fccSafresh1 $str =~ s/\n/\\n/g; 239b8851fccSafresh1 $str =~ s/\r/\\r/g; 240b8851fccSafresh1 $str =~ s/\t/\\t/g; 241b8851fccSafresh1 if (defined $^V && $^V ge v5.6) { 242b8851fccSafresh1 $str =~ s/([[:cntrl:]])(?!\d)/sprintf('\\%o',ord($1))/eg; 243b8851fccSafresh1 $str =~ s/([[:^print:]])/sprintf('\\x%02X',ord($1))/eg; 244b8851fccSafresh1 $str =~ s/([[:^ascii:]])/sprintf('\\x{%X}',ord($1))/eg; 245b8851fccSafresh1 } 246b8851fccSafresh1 elsif (ord("A") == 65) { 247b8851fccSafresh1 $str =~ s/([\0-\037])(?!\d)/sprintf('\\%o',ord($1))/eg; 248b8851fccSafresh1 $str =~ s/([\0-\037\177-\377])/sprintf('\\x%02X',ord($1))/eg; 249b8851fccSafresh1 $str =~ s/([^\0-\176])/sprintf('\\x{%X}',ord($1))/eg; 250b8851fccSafresh1 } 251b8851fccSafresh1 else { # Assuming EBCDIC on this ancient Perl 252b8851fccSafresh1 253b8851fccSafresh1 # The controls except for one are 0-\077, so almost all controls on 254b8851fccSafresh1 # EBCDIC platforms will be expressed in octal, instead of just the C0 255b8851fccSafresh1 # ones. 256b8851fccSafresh1 $str =~ s/([\0-\077])(?!\d)/sprintf('\\%o',ord($1))/eg; 257b8851fccSafresh1 $str =~ s/([\0-\077])/sprintf('\\x%02X',ord($1))/eg; 258b8851fccSafresh1 259b8851fccSafresh1 $str =~ s/([^\0-\xFF])/sprintf('\\x{%X}',ord($1))/eg; 260b8851fccSafresh1 261b8851fccSafresh1 # What remains to be escaped are the non-ASCII-range characters, 262b8851fccSafresh1 # including the one control that isn't in the 0-077 range. 263b8851fccSafresh1 # (We don't escape further any ASCII printables.) 264b8851fccSafresh1 $str =~ s<[^ !"\$\%#'()*+,\-./0123456789:;\<=\>?\@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~]><sprintf('\\x%02X',ord($1))>eg; 265b8851fccSafresh1 } 266b8851fccSafresh1 #if( $_[1] ) { 267b8851fccSafresh1 # substr( $str , 218-3 ) = "..." 268b8851fccSafresh1 # if length($str) >= 218 and !$ENV{PERL_TEST_NO_TRUNC}; 269b8851fccSafresh1 #} 270b8851fccSafresh1 return qq("$str"); 271b8851fccSafresh1} 272b8851fccSafresh1 273b8851fccSafresh1 274b8851fccSafresh1=end _private 275b8851fccSafresh1 276b8851fccSafresh1=item C<ok(...)> 277b8851fccSafresh1 278b8851fccSafresh1 ok(1 + 1 == 2); 279b8851fccSafresh1 ok($have, $expect); 280b8851fccSafresh1 ok($have, $expect, $diagnostics); 281b8851fccSafresh1 282b8851fccSafresh1This function is the reason for C<Test>'s existence. It's 283b8851fccSafresh1the basic function that 284b8851fccSafresh1handles printing "C<ok>" or "C<not ok>", along with the 285b8851fccSafresh1current test number. (That's what C<Test::Harness> wants to see.) 286b8851fccSafresh1 287b8851fccSafresh1In its most basic usage, C<ok(...)> simply takes a single scalar 288b8851fccSafresh1expression. If its value is true, the test passes; if false, 289b8851fccSafresh1the test fails. Examples: 290b8851fccSafresh1 291b8851fccSafresh1 # Examples of ok(scalar) 292b8851fccSafresh1 293b8851fccSafresh1 ok( 1 + 1 == 2 ); # ok if 1 + 1 == 2 294b8851fccSafresh1 ok( $foo =~ /bar/ ); # ok if $foo contains 'bar' 295b8851fccSafresh1 ok( baz($x + $y) eq 'Armondo' ); # ok if baz($x + $y) returns 296b8851fccSafresh1 # 'Armondo' 297b8851fccSafresh1 ok( @a == @b ); # ok if @a and @b are the same 298b8851fccSafresh1 # length 299b8851fccSafresh1 300b8851fccSafresh1The expression is evaluated in scalar context. So the following will 301b8851fccSafresh1work: 302b8851fccSafresh1 303b8851fccSafresh1 ok( @stuff ); # ok if @stuff has any 304b8851fccSafresh1 # elements 305b8851fccSafresh1 ok( !grep !defined $_, @stuff ); # ok if everything in @stuff 306b8851fccSafresh1 # is defined. 307b8851fccSafresh1 308b8851fccSafresh1A special case is if the expression is a subroutine reference (in either 309b8851fccSafresh1C<sub {...}> syntax or C<\&foo> syntax). In 310b8851fccSafresh1that case, it is executed and its value (true or false) determines if 311b8851fccSafresh1the test passes or fails. For example, 312b8851fccSafresh1 313b8851fccSafresh1 ok( sub { # See whether sleep works at least passably 314b8851fccSafresh1 my $start_time = time; 315b8851fccSafresh1 sleep 5; 316b8851fccSafresh1 time() - $start_time >= 4 317b8851fccSafresh1 }); 318b8851fccSafresh1 319b8851fccSafresh1In its two-argument form, C<ok(I<arg1>, I<arg2>)> compares the two 320b8851fccSafresh1scalar values to see if they match. They match if both are undefined, 321b8851fccSafresh1or if I<arg2> is a regex that matches I<arg1>, or if they compare equal 322b8851fccSafresh1with C<eq>. 323b8851fccSafresh1 324b8851fccSafresh1 # Example of ok(scalar, scalar) 325b8851fccSafresh1 326b8851fccSafresh1 ok( "this", "that" ); # not ok, 'this' ne 'that' 327b8851fccSafresh1 ok( "", undef ); # not ok, "" is defined 328b8851fccSafresh1 329b8851fccSafresh1The second argument is considered a regex if it is either a regex 330b8851fccSafresh1object or a string that looks like a regex. Regex objects are 331b8851fccSafresh1constructed with the qr// operator in recent versions of perl. A 332b8851fccSafresh1string is considered to look like a regex if its first and last 333b8851fccSafresh1characters are "/", or if the first character is "m" 334b8851fccSafresh1and its second and last characters are both the 335b8851fccSafresh1same non-alphanumeric non-whitespace character. These regexp 336b8851fccSafresh1 337b8851fccSafresh1Regex examples: 338b8851fccSafresh1 339b8851fccSafresh1 ok( 'JaffO', '/Jaff/' ); # ok, 'JaffO' =~ /Jaff/ 340b8851fccSafresh1 ok( 'JaffO', 'm|Jaff|' ); # ok, 'JaffO' =~ m|Jaff| 341b8851fccSafresh1 ok( 'JaffO', qr/Jaff/ ); # ok, 'JaffO' =~ qr/Jaff/; 342b8851fccSafresh1 ok( 'JaffO', '/(?i)jaff/ ); # ok, 'JaffO' =~ /jaff/i; 343b8851fccSafresh1 344b8851fccSafresh1If either (or both!) is a subroutine reference, it is run and used 345b8851fccSafresh1as the value for comparing. For example: 346b8851fccSafresh1 347b8851fccSafresh1 ok sub { 348*5759b3d2Safresh1 open(OUT, '>', 'x.dat') || die $!; 349b8851fccSafresh1 print OUT "\x{e000}"; 350b8851fccSafresh1 close OUT; 351b8851fccSafresh1 my $bytecount = -s 'x.dat'; 352b8851fccSafresh1 unlink 'x.dat' or warn "Can't unlink : $!"; 353b8851fccSafresh1 return $bytecount; 354b8851fccSafresh1 }, 355b8851fccSafresh1 4 356b8851fccSafresh1 ; 357b8851fccSafresh1 358b8851fccSafresh1The above test passes two values to C<ok(arg1, arg2)> -- the first 359b8851fccSafresh1a coderef, and the second is the number 4. Before C<ok> compares them, 360b8851fccSafresh1it calls the coderef, and uses its return value as the real value of 361b8851fccSafresh1this parameter. Assuming that C<$bytecount> returns 4, C<ok> ends up 362b8851fccSafresh1testing C<4 eq 4>. Since that's true, this test passes. 363b8851fccSafresh1 364b8851fccSafresh1Finally, you can append an optional third argument, in 365b8851fccSafresh1C<ok(I<arg1>,I<arg2>, I<note>)>, where I<note> is a string value that 366b8851fccSafresh1will be printed if the test fails. This should be some useful 367b8851fccSafresh1information about the test, pertaining to why it failed, and/or 368b8851fccSafresh1a description of the test. For example: 369b8851fccSafresh1 370b8851fccSafresh1 ok( grep($_ eq 'something unique', @stuff), 1, 371b8851fccSafresh1 "Something that should be unique isn't!\n". 372b8851fccSafresh1 '@stuff = '.join ', ', @stuff 373b8851fccSafresh1 ); 374b8851fccSafresh1 375b8851fccSafresh1Unfortunately, a note cannot be used with the single argument 376b8851fccSafresh1style of C<ok()>. That is, if you try C<ok(I<arg1>, I<note>)>, then 377b8851fccSafresh1C<Test> will interpret this as C<ok(I<arg1>, I<arg2>)>, and probably 378b8851fccSafresh1end up testing C<I<arg1> eq I<arg2>> -- and that's not what you want! 379b8851fccSafresh1 380b8851fccSafresh1All of the above special cases can occasionally cause some 381b8851fccSafresh1problems. See L</BUGS and CAVEATS>. 382b8851fccSafresh1 383b8851fccSafresh1=cut 384b8851fccSafresh1 385b8851fccSafresh1# A past maintainer of this module said: 386b8851fccSafresh1# <<ok(...)'s special handling of subroutine references is an unfortunate 387b8851fccSafresh1# "feature" that can't be removed due to compatibility.>> 388b8851fccSafresh1# 389b8851fccSafresh1 390b8851fccSafresh1sub ok ($;$$) { 391b8851fccSafresh1 croak "ok: plan before you test!" if !$planned; 392b8851fccSafresh1 393b8851fccSafresh1 local($\,$,); # guard against -l and other things that screw with 394b8851fccSafresh1 # print 395b8851fccSafresh1 396b8851fccSafresh1 my ($pkg,$file,$line) = caller($TestLevel); 397b8851fccSafresh1 my $repetition = ++$history{"$file:$line"}; 398b8851fccSafresh1 my $context = ("$file at line $line". 399b8851fccSafresh1 ($repetition > 1 ? " fail \#$repetition" : '')); 400b8851fccSafresh1 401b8851fccSafresh1 # Are we comparing two values? 402b8851fccSafresh1 my $compare = 0; 403b8851fccSafresh1 404b8851fccSafresh1 my $ok=0; 405b8851fccSafresh1 my $result = _to_value(shift); 406b8851fccSafresh1 my ($expected, $isregex, $regex); 407b8851fccSafresh1 if (@_ == 0) { 408b8851fccSafresh1 $ok = $result; 409b8851fccSafresh1 } else { 410b8851fccSafresh1 $compare = 1; 411b8851fccSafresh1 $expected = _to_value(shift); 412b8851fccSafresh1 if (!defined $expected) { 413b8851fccSafresh1 $ok = !defined $result; 414b8851fccSafresh1 } elsif (!defined $result) { 415b8851fccSafresh1 $ok = 0; 416b8851fccSafresh1 } elsif (ref($expected) eq 'Regexp') { 417b8851fccSafresh1 $ok = $result =~ /$expected/; 418b8851fccSafresh1 $regex = $expected; 419b8851fccSafresh1 } elsif (($regex) = ($expected =~ m,^ / (.+) / $,sx) or 420b8851fccSafresh1 (undef, $regex) = ($expected =~ m,^ m([^\w\s]) (.+) \1 $,sx)) { 421b8851fccSafresh1 $ok = $result =~ /$regex/; 422b8851fccSafresh1 } else { 423b8851fccSafresh1 $ok = $result eq $expected; 424b8851fccSafresh1 } 425b8851fccSafresh1 } 426b8851fccSafresh1 my $todo = $todo{$ntest}; 427b8851fccSafresh1 if ($todo and $ok) { 428b8851fccSafresh1 $context .= ' TODO?!' if $todo; 429b8851fccSafresh1 print $TESTOUT "ok $ntest # ($context)\n"; 430b8851fccSafresh1 } else { 431b8851fccSafresh1 # Issuing two seperate prints() causes problems on VMS. 432b8851fccSafresh1 if (!$ok) { 433b8851fccSafresh1 print $TESTOUT "not ok $ntest\n"; 434b8851fccSafresh1 } 435b8851fccSafresh1 else { 436b8851fccSafresh1 print $TESTOUT "ok $ntest\n"; 437b8851fccSafresh1 } 438b8851fccSafresh1 439b8851fccSafresh1 $ok or _complain($result, $expected, 440b8851fccSafresh1 { 441b8851fccSafresh1 'repetition' => $repetition, 'package' => $pkg, 442b8851fccSafresh1 'result' => $result, 'todo' => $todo, 443b8851fccSafresh1 'file' => $file, 'line' => $line, 444b8851fccSafresh1 'context' => $context, 'compare' => $compare, 445b8851fccSafresh1 @_ ? ('diagnostic' => _to_value(shift)) : (), 446b8851fccSafresh1 }); 447b8851fccSafresh1 448b8851fccSafresh1 } 449b8851fccSafresh1 ++ $ntest; 450b8851fccSafresh1 $ok; 451b8851fccSafresh1} 452b8851fccSafresh1 453b8851fccSafresh1 454b8851fccSafresh1sub _complain { 455b8851fccSafresh1 my($result, $expected, $detail) = @_; 456b8851fccSafresh1 $$detail{expected} = $expected if defined $expected; 457b8851fccSafresh1 458b8851fccSafresh1 # Get the user's diagnostic, protecting against multi-line 459b8851fccSafresh1 # diagnostics. 460b8851fccSafresh1 my $diag = $$detail{diagnostic}; 461b8851fccSafresh1 $diag =~ s/\n/\n#/g if defined $diag; 462b8851fccSafresh1 463b8851fccSafresh1 my $out = $$detail{todo} ? $TESTOUT : $TESTERR; 464b8851fccSafresh1 $$detail{context} .= ' *TODO*' if $$detail{todo}; 465b8851fccSafresh1 if (!$$detail{compare}) { 466b8851fccSafresh1 if (!$diag) { 467b8851fccSafresh1 print $out "# Failed test $ntest in $$detail{context}\n"; 468b8851fccSafresh1 } else { 469b8851fccSafresh1 print $out "# Failed test $ntest in $$detail{context}: $diag\n"; 470b8851fccSafresh1 } 471b8851fccSafresh1 } else { 472b8851fccSafresh1 my $prefix = "Test $ntest"; 473b8851fccSafresh1 474b8851fccSafresh1 print $out "# $prefix got: " . _quote($result) . 475b8851fccSafresh1 " ($$detail{context})\n"; 476b8851fccSafresh1 $prefix = ' ' x (length($prefix) - 5); 477b8851fccSafresh1 my $expected_quoted = (defined $$detail{regex}) 478b8851fccSafresh1 ? 'qr{'.($$detail{regex}).'}' : _quote($expected); 479b8851fccSafresh1 480b8851fccSafresh1 print $out "# $prefix Expected: $expected_quoted", 481b8851fccSafresh1 $diag ? " ($diag)" : (), "\n"; 482b8851fccSafresh1 483b8851fccSafresh1 _diff_complain( $result, $expected, $detail, $prefix ) 484b8851fccSafresh1 if defined($expected) and 2 < ($expected =~ tr/\n//); 485b8851fccSafresh1 } 486b8851fccSafresh1 487b8851fccSafresh1 if(defined $Program_Lines{ $$detail{file} }[ $$detail{line} ]) { 488b8851fccSafresh1 print $out 489b8851fccSafresh1 "# $$detail{file} line $$detail{line} is: $Program_Lines{ $$detail{file} }[ $$detail{line} ]\n" 490b8851fccSafresh1 if $Program_Lines{ $$detail{file} }[ $$detail{line} ] 491b8851fccSafresh1 =~ m/[^\s\#\(\)\{\}\[\]\;]/; # Otherwise it's uninformative 492b8851fccSafresh1 493b8851fccSafresh1 undef $Program_Lines{ $$detail{file} }[ $$detail{line} ]; 494b8851fccSafresh1 # So we won't repeat it. 495b8851fccSafresh1 } 496b8851fccSafresh1 497b8851fccSafresh1 push @FAILDETAIL, $detail; 498b8851fccSafresh1 return; 499b8851fccSafresh1} 500b8851fccSafresh1 501b8851fccSafresh1 502b8851fccSafresh1 503b8851fccSafresh1sub _diff_complain { 504b8851fccSafresh1 my($result, $expected, $detail, $prefix) = @_; 505b8851fccSafresh1 return _diff_complain_external(@_) if $ENV{PERL_TEST_DIFF}; 506b8851fccSafresh1 return _diff_complain_algdiff(@_) 507b8851fccSafresh1 if eval { 508b8851fccSafresh1 local @INC = @INC; 509b8851fccSafresh1 pop @INC if $INC[-1] eq '.'; 510b8851fccSafresh1 require Algorithm::Diff; Algorithm::Diff->VERSION(1.15); 511b8851fccSafresh1 1; 512b8851fccSafresh1 }; 513b8851fccSafresh1 514b8851fccSafresh1 $told_about_diff++ or print $TESTERR <<"EOT"; 515b8851fccSafresh1# $prefix (Install the Algorithm::Diff module to have differences in multiline 516b8851fccSafresh1# $prefix output explained. You might also set the PERL_TEST_DIFF environment 517b8851fccSafresh1# $prefix variable to run a diff program on the output.) 518b8851fccSafresh1EOT 519b8851fccSafresh1 ; 520b8851fccSafresh1 return; 521b8851fccSafresh1} 522b8851fccSafresh1 523b8851fccSafresh1 524b8851fccSafresh1 525b8851fccSafresh1sub _diff_complain_external { 526b8851fccSafresh1 my($result, $expected, $detail, $prefix) = @_; 527b8851fccSafresh1 my $diff = $ENV{PERL_TEST_DIFF} || die "WHAAAA?"; 528b8851fccSafresh1 529b8851fccSafresh1 require File::Temp; 530b8851fccSafresh1 my($got_fh, $got_filename) = File::Temp::tempfile("test-got-XXXXX"); 531b8851fccSafresh1 my($exp_fh, $exp_filename) = File::Temp::tempfile("test-exp-XXXXX"); 532b8851fccSafresh1 unless ($got_fh && $exp_fh) { 533b8851fccSafresh1 warn "Can't get tempfiles"; 534b8851fccSafresh1 return; 535b8851fccSafresh1 } 536b8851fccSafresh1 537b8851fccSafresh1 print $got_fh $result; 538b8851fccSafresh1 print $exp_fh $expected; 539b8851fccSafresh1 if (close($got_fh) && close($exp_fh)) { 540b8851fccSafresh1 my $diff_cmd = "$diff $exp_filename $got_filename"; 541b8851fccSafresh1 print $TESTERR "#\n# $prefix $diff_cmd\n"; 542*5759b3d2Safresh1 if (open(DIFF, '-|', $diff_cmd)) { 543b8851fccSafresh1 local $_; 544b8851fccSafresh1 while (<DIFF>) { 545b8851fccSafresh1 print $TESTERR "# $prefix $_"; 546b8851fccSafresh1 } 547b8851fccSafresh1 close(DIFF); 548b8851fccSafresh1 } 549b8851fccSafresh1 else { 550b8851fccSafresh1 warn "Can't run diff: $!"; 551b8851fccSafresh1 } 552b8851fccSafresh1 } else { 553b8851fccSafresh1 warn "Can't write to tempfiles: $!"; 554b8851fccSafresh1 } 555b8851fccSafresh1 unlink($got_filename); 556b8851fccSafresh1 unlink($exp_filename); 557b8851fccSafresh1 return; 558b8851fccSafresh1} 559b8851fccSafresh1 560b8851fccSafresh1 561b8851fccSafresh1 562b8851fccSafresh1sub _diff_complain_algdiff { 563b8851fccSafresh1 my($result, $expected, $detail, $prefix) = @_; 564b8851fccSafresh1 565b8851fccSafresh1 my @got = split(/^/, $result); 566b8851fccSafresh1 my @exp = split(/^/, $expected); 567b8851fccSafresh1 568b8851fccSafresh1 my $diff_kind; 569b8851fccSafresh1 my @diff_lines; 570b8851fccSafresh1 571b8851fccSafresh1 my $diff_flush = sub { 572b8851fccSafresh1 return unless $diff_kind; 573b8851fccSafresh1 574b8851fccSafresh1 my $count_lines = @diff_lines; 575b8851fccSafresh1 my $s = $count_lines == 1 ? "" : "s"; 576b8851fccSafresh1 my $first_line = $diff_lines[0][0] + 1; 577b8851fccSafresh1 578b8851fccSafresh1 print $TESTERR "# $prefix "; 579b8851fccSafresh1 if ($diff_kind eq "GOT") { 580b8851fccSafresh1 print $TESTERR "Got $count_lines extra line$s at line $first_line:\n"; 581b8851fccSafresh1 for my $i (@diff_lines) { 582b8851fccSafresh1 print $TESTERR "# $prefix + " . _quote($got[$i->[0]]) . "\n"; 583b8851fccSafresh1 } 584b8851fccSafresh1 } elsif ($diff_kind eq "EXP") { 585b8851fccSafresh1 if ($count_lines > 1) { 586b8851fccSafresh1 my $last_line = $diff_lines[-1][0] + 1; 587b8851fccSafresh1 print $TESTERR "Lines $first_line-$last_line are"; 588b8851fccSafresh1 } 589b8851fccSafresh1 else { 590b8851fccSafresh1 print $TESTERR "Line $first_line is"; 591b8851fccSafresh1 } 592b8851fccSafresh1 print $TESTERR " missing:\n"; 593b8851fccSafresh1 for my $i (@diff_lines) { 594b8851fccSafresh1 print $TESTERR "# $prefix - " . _quote($exp[$i->[1]]) . "\n"; 595b8851fccSafresh1 } 596b8851fccSafresh1 } elsif ($diff_kind eq "CH") { 597b8851fccSafresh1 if ($count_lines > 1) { 598b8851fccSafresh1 my $last_line = $diff_lines[-1][0] + 1; 599b8851fccSafresh1 print $TESTERR "Lines $first_line-$last_line are"; 600b8851fccSafresh1 } 601b8851fccSafresh1 else { 602b8851fccSafresh1 print $TESTERR "Line $first_line is"; 603b8851fccSafresh1 } 604b8851fccSafresh1 print $TESTERR " changed:\n"; 605b8851fccSafresh1 for my $i (@diff_lines) { 606b8851fccSafresh1 print $TESTERR "# $prefix - " . _quote($exp[$i->[1]]) . "\n"; 607b8851fccSafresh1 print $TESTERR "# $prefix + " . _quote($got[$i->[0]]) . "\n"; 608b8851fccSafresh1 } 609b8851fccSafresh1 } 610b8851fccSafresh1 611b8851fccSafresh1 # reset 612b8851fccSafresh1 $diff_kind = undef; 613b8851fccSafresh1 @diff_lines = (); 614b8851fccSafresh1 }; 615b8851fccSafresh1 616b8851fccSafresh1 my $diff_collect = sub { 617b8851fccSafresh1 my $kind = shift; 618b8851fccSafresh1 &$diff_flush() if $diff_kind && $diff_kind ne $kind; 619b8851fccSafresh1 $diff_kind = $kind; 620b8851fccSafresh1 push(@diff_lines, [@_]); 621b8851fccSafresh1 }; 622b8851fccSafresh1 623b8851fccSafresh1 624b8851fccSafresh1 Algorithm::Diff::traverse_balanced( 625b8851fccSafresh1 \@got, \@exp, 626b8851fccSafresh1 { 627b8851fccSafresh1 DISCARD_A => sub { &$diff_collect("GOT", @_) }, 628b8851fccSafresh1 DISCARD_B => sub { &$diff_collect("EXP", @_) }, 629b8851fccSafresh1 CHANGE => sub { &$diff_collect("CH", @_) }, 630b8851fccSafresh1 MATCH => sub { &$diff_flush() }, 631b8851fccSafresh1 }, 632b8851fccSafresh1 ); 633b8851fccSafresh1 &$diff_flush(); 634b8851fccSafresh1 635b8851fccSafresh1 return; 636b8851fccSafresh1} 637b8851fccSafresh1 638b8851fccSafresh1 639b8851fccSafresh1 640b8851fccSafresh1 641b8851fccSafresh1#~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~`~ 642b8851fccSafresh1 643b8851fccSafresh1 644b8851fccSafresh1=item C<skip(I<skip_if_true>, I<args...>)> 645b8851fccSafresh1 646b8851fccSafresh1This is used for tests that under some conditions can be skipped. It's 647b8851fccSafresh1basically equivalent to: 648b8851fccSafresh1 649b8851fccSafresh1 if( $skip_if_true ) { 650b8851fccSafresh1 ok(1); 651b8851fccSafresh1 } else { 652b8851fccSafresh1 ok( args... ); 653b8851fccSafresh1 } 654b8851fccSafresh1 655b8851fccSafresh1...except that the C<ok(1)> emits not just "C<ok I<testnum>>" but 656b8851fccSafresh1actually "C<ok I<testnum> # I<skip_if_true_value>>". 657b8851fccSafresh1 658b8851fccSafresh1The arguments after the I<skip_if_true> are what is fed to C<ok(...)> if 659b8851fccSafresh1this test isn't skipped. 660b8851fccSafresh1 661b8851fccSafresh1Example usage: 662b8851fccSafresh1 663b8851fccSafresh1 my $if_MSWin = 664b8851fccSafresh1 $^O =~ m/MSWin/ ? 'Skip if under MSWin' : ''; 665b8851fccSafresh1 666b8851fccSafresh1 # A test to be skipped if under MSWin (i.e., run except under 667b8851fccSafresh1 # MSWin) 668b8851fccSafresh1 skip($if_MSWin, thing($foo), thing($bar) ); 669b8851fccSafresh1 670b8851fccSafresh1Or, going the other way: 671b8851fccSafresh1 672b8851fccSafresh1 my $unless_MSWin = 673b8851fccSafresh1 $^O =~ m/MSWin/ ? '' : 'Skip unless under MSWin'; 674b8851fccSafresh1 675b8851fccSafresh1 # A test to be skipped unless under MSWin (i.e., run only under 676b8851fccSafresh1 # MSWin) 677b8851fccSafresh1 skip($unless_MSWin, thing($foo), thing($bar) ); 678b8851fccSafresh1 679b8851fccSafresh1The tricky thing to remember is that the first parameter is true if 680b8851fccSafresh1you want to I<skip> the test, not I<run> it; and it also doubles as a 681b8851fccSafresh1note about why it's being skipped. So in the first codeblock above, read 682b8851fccSafresh1the code as "skip if MSWin -- (otherwise) test whether C<thing($foo)> is 683b8851fccSafresh1C<thing($bar)>" or for the second case, "skip unless MSWin...". 684b8851fccSafresh1 685b8851fccSafresh1Also, when your I<skip_if_reason> string is true, it really should (for 686b8851fccSafresh1backwards compatibility with older Test.pm versions) start with the 687b8851fccSafresh1string "Skip", as shown in the above examples. 688b8851fccSafresh1 689b8851fccSafresh1Note that in the above cases, C<thing($foo)> and C<thing($bar)> 690b8851fccSafresh1I<are> evaluated -- but as long as the C<skip_if_true> is true, 691b8851fccSafresh1then we C<skip(...)> just tosses out their value (i.e., not 692b8851fccSafresh1bothering to treat them like values to C<ok(...)>. But if 693b8851fccSafresh1you need to I<not> eval the arguments when skipping the 694b8851fccSafresh1test, use 695b8851fccSafresh1this format: 696b8851fccSafresh1 697b8851fccSafresh1 skip( $unless_MSWin, 698b8851fccSafresh1 sub { 699b8851fccSafresh1 # This code returns true if the test passes. 700b8851fccSafresh1 # (But it doesn't even get called if the test is skipped.) 701b8851fccSafresh1 thing($foo) eq thing($bar) 702b8851fccSafresh1 } 703b8851fccSafresh1 ); 704b8851fccSafresh1 705b8851fccSafresh1or even this, which is basically equivalent: 706b8851fccSafresh1 707b8851fccSafresh1 skip( $unless_MSWin, 708b8851fccSafresh1 sub { thing($foo) }, sub { thing($bar) } 709b8851fccSafresh1 ); 710b8851fccSafresh1 711b8851fccSafresh1That is, both are like this: 712b8851fccSafresh1 713b8851fccSafresh1 if( $unless_MSWin ) { 714b8851fccSafresh1 ok(1); # but it actually appends "# $unless_MSWin" 715b8851fccSafresh1 # so that Test::Harness can tell it's a skip 716b8851fccSafresh1 } else { 717b8851fccSafresh1 # Not skipping, so actually call and evaluate... 718b8851fccSafresh1 ok( sub { thing($foo) }, sub { thing($bar) } ); 719b8851fccSafresh1 } 720b8851fccSafresh1 721b8851fccSafresh1=cut 722b8851fccSafresh1 723b8851fccSafresh1sub skip ($;$$$) { 724b8851fccSafresh1 local($\, $,); # guard against -l and other things that screw with 725b8851fccSafresh1 # print 726b8851fccSafresh1 727b8851fccSafresh1 my $whyskip = _to_value(shift); 728b8851fccSafresh1 if (!@_ or $whyskip) { 729b8851fccSafresh1 $whyskip = '' if $whyskip =~ m/^\d+$/; 730b8851fccSafresh1 $whyskip =~ s/^[Ss]kip(?:\s+|$)//; # backwards compatibility, old 731b8851fccSafresh1 # versions required the reason 732b8851fccSafresh1 # to start with 'skip' 733b8851fccSafresh1 # We print in one shot for VMSy reasons. 734b8851fccSafresh1 my $ok = "ok $ntest # skip"; 735b8851fccSafresh1 $ok .= " $whyskip" if length $whyskip; 736b8851fccSafresh1 $ok .= "\n"; 737b8851fccSafresh1 print $TESTOUT $ok; 738b8851fccSafresh1 ++ $ntest; 739b8851fccSafresh1 return 1; 740b8851fccSafresh1 } else { 741b8851fccSafresh1 # backwards compatibility (I think). skip() used to be 742b8851fccSafresh1 # called like ok(), which is weird. I haven't decided what to do with 743b8851fccSafresh1 # this yet. 744b8851fccSafresh1# warn <<WARN if $^W; 745b8851fccSafresh1#This looks like a skip() using the very old interface. Please upgrade to 746b8851fccSafresh1#the documented interface as this has been deprecated. 747b8851fccSafresh1#WARN 748b8851fccSafresh1 749b8851fccSafresh1 local($TestLevel) = $TestLevel+1; #to ignore this stack frame 750b8851fccSafresh1 return &ok(@_); 751b8851fccSafresh1 } 752b8851fccSafresh1} 753b8851fccSafresh1 754b8851fccSafresh1=back 755b8851fccSafresh1 756b8851fccSafresh1=cut 757b8851fccSafresh1 758b8851fccSafresh1END { 759b8851fccSafresh1 $ONFAIL->(\@FAILDETAIL) if @FAILDETAIL && $ONFAIL; 760b8851fccSafresh1} 761b8851fccSafresh1 762b8851fccSafresh11; 763b8851fccSafresh1__END__ 764b8851fccSafresh1 765b8851fccSafresh1=head1 TEST TYPES 766b8851fccSafresh1 767b8851fccSafresh1=over 4 768b8851fccSafresh1 769b8851fccSafresh1=item * NORMAL TESTS 770b8851fccSafresh1 771b8851fccSafresh1These tests are expected to succeed. Usually, most or all of your tests 772b8851fccSafresh1are in this category. If a normal test doesn't succeed, then that 773b8851fccSafresh1means that something is I<wrong>. 774b8851fccSafresh1 775b8851fccSafresh1=item * SKIPPED TESTS 776b8851fccSafresh1 777b8851fccSafresh1The C<skip(...)> function is for tests that might or might not be 778b8851fccSafresh1possible to run, depending 779b8851fccSafresh1on the availability of platform-specific features. The first argument 780b8851fccSafresh1should evaluate to true (think "yes, please skip") if the required 781b8851fccSafresh1feature is I<not> available. After the first argument, C<skip(...)> works 782b8851fccSafresh1exactly the same way as C<ok(...)> does. 783b8851fccSafresh1 784b8851fccSafresh1=item * TODO TESTS 785b8851fccSafresh1 786b8851fccSafresh1TODO tests are designed for maintaining an B<executable TODO list>. 787b8851fccSafresh1These tests are I<expected to fail.> If a TODO test does succeed, 788b8851fccSafresh1then the feature in question shouldn't be on the TODO list, now 789b8851fccSafresh1should it? 790b8851fccSafresh1 791b8851fccSafresh1Packages should NOT be released with succeeding TODO tests. As soon 792b8851fccSafresh1as a TODO test starts working, it should be promoted to a normal test, 793b8851fccSafresh1and the newly working feature should be documented in the release 794b8851fccSafresh1notes or in the change log. 795b8851fccSafresh1 796b8851fccSafresh1=back 797b8851fccSafresh1 798b8851fccSafresh1=head1 ONFAIL 799b8851fccSafresh1 800b8851fccSafresh1 BEGIN { plan test => 4, onfail => sub { warn "CALL 911!" } } 801b8851fccSafresh1 802b8851fccSafresh1Although test failures should be enough, extra diagnostics can be 803b8851fccSafresh1triggered at the end of a test run. C<onfail> is passed an array ref 804b8851fccSafresh1of hash refs that describe each test failure. Each hash will contain 805b8851fccSafresh1at least the following fields: C<package>, C<repetition>, and 806b8851fccSafresh1C<result>. (You shouldn't rely on any other fields being present.) If the test 807b8851fccSafresh1had an expected value or a diagnostic (or "note") string, these will also be 808b8851fccSafresh1included. 809b8851fccSafresh1 810b8851fccSafresh1The I<optional> C<onfail> hook might be used simply to print out the 811b8851fccSafresh1version of your package and/or how to report problems. It might also 812b8851fccSafresh1be used to generate extremely sophisticated diagnostics for a 813b8851fccSafresh1particularly bizarre test failure. However it's not a panacea. Core 814b8851fccSafresh1dumps or other unrecoverable errors prevent the C<onfail> hook from 815b8851fccSafresh1running. (It is run inside an C<END> block.) Besides, C<onfail> is 816b8851fccSafresh1probably over-kill in most cases. (Your test code should be simpler 817b8851fccSafresh1than the code it is testing, yes?) 818b8851fccSafresh1 819b8851fccSafresh1 820b8851fccSafresh1=head1 BUGS and CAVEATS 821b8851fccSafresh1 822b8851fccSafresh1=over 823b8851fccSafresh1 824b8851fccSafresh1=item * 825b8851fccSafresh1 826b8851fccSafresh1C<ok(...)>'s special handing of strings which look like they might be 827b8851fccSafresh1regexes can also cause unexpected behavior. An innocent: 828b8851fccSafresh1 829b8851fccSafresh1 ok( $fileglob, '/path/to/some/*stuff/' ); 830b8851fccSafresh1 831b8851fccSafresh1will fail, since Test.pm considers the second argument to be a regex! 832b8851fccSafresh1The best bet is to use the one-argument form: 833b8851fccSafresh1 834b8851fccSafresh1 ok( $fileglob eq '/path/to/some/*stuff/' ); 835b8851fccSafresh1 836b8851fccSafresh1=item * 837b8851fccSafresh1 838b8851fccSafresh1C<ok(...)>'s use of string C<eq> can sometimes cause odd problems 839b8851fccSafresh1when comparing 840b8851fccSafresh1numbers, especially if you're casting a string to a number: 841b8851fccSafresh1 842b8851fccSafresh1 $foo = "1.0"; 843b8851fccSafresh1 ok( $foo, 1 ); # not ok, "1.0" ne 1 844b8851fccSafresh1 845b8851fccSafresh1Your best bet is to use the single argument form: 846b8851fccSafresh1 847b8851fccSafresh1 ok( $foo == 1 ); # ok "1.0" == 1 848b8851fccSafresh1 849b8851fccSafresh1=item * 850b8851fccSafresh1 851b8851fccSafresh1As you may have inferred from the above documentation and examples, 852b8851fccSafresh1C<ok>'s prototype is C<($;$$)> (and, incidentally, C<skip>'s is 853b8851fccSafresh1C<($;$$$)>). This means, for example, that you can do C<ok @foo, @bar> 854b8851fccSafresh1to compare the I<size> of the two arrays. But don't be fooled into 855b8851fccSafresh1thinking that C<ok @foo, @bar> means a comparison of the contents of two 856b8851fccSafresh1arrays -- you're comparing I<just> the number of elements of each. It's 857b8851fccSafresh1so easy to make that mistake in reading C<ok @foo, @bar> that you might 858b8851fccSafresh1want to be very explicit about it, and instead write C<ok scalar(@foo), 859b8851fccSafresh1scalar(@bar)>. 860b8851fccSafresh1 861b8851fccSafresh1=item * 862b8851fccSafresh1 863b8851fccSafresh1This almost definitely doesn't do what you expect: 864b8851fccSafresh1 865b8851fccSafresh1 ok $thingy->can('some_method'); 866b8851fccSafresh1 867b8851fccSafresh1Why? Because C<can> returns a coderef to mean "yes it can (and the 868b8851fccSafresh1method is this...)", and then C<ok> sees a coderef and thinks you're 869b8851fccSafresh1passing a function that you want it to call and consider the truth of 870b8851fccSafresh1the result of! I.e., just like: 871b8851fccSafresh1 872b8851fccSafresh1 ok $thingy->can('some_method')->(); 873b8851fccSafresh1 874b8851fccSafresh1What you probably want instead is this: 875b8851fccSafresh1 876b8851fccSafresh1 ok $thingy->can('some_method') && 1; 877b8851fccSafresh1 878b8851fccSafresh1If the C<can> returns false, then that is passed to C<ok>. If it 879b8851fccSafresh1returns true, then the larger expression S<< C<< 880b8851fccSafresh1$thingy->can('some_method') && 1 >> >> returns 1, which C<ok> sees as 881b8851fccSafresh1a simple signal of success, as you would expect. 882b8851fccSafresh1 883b8851fccSafresh1 884b8851fccSafresh1=item * 885b8851fccSafresh1 886b8851fccSafresh1The syntax for C<skip> is about the only way it can be, but it's still 887b8851fccSafresh1quite confusing. Just start with the above examples and you'll 888b8851fccSafresh1be okay. 889b8851fccSafresh1 890b8851fccSafresh1Moreover, users may expect this: 891b8851fccSafresh1 892b8851fccSafresh1 skip $unless_mswin, foo($bar), baz($quux); 893b8851fccSafresh1 894b8851fccSafresh1to not evaluate C<foo($bar)> and C<baz($quux)> when the test is being 895b8851fccSafresh1skipped. But in reality, they I<are> evaluated, but C<skip> just won't 896b8851fccSafresh1bother comparing them if C<$unless_mswin> is true. 897b8851fccSafresh1 898b8851fccSafresh1You could do this: 899b8851fccSafresh1 900b8851fccSafresh1 skip $unless_mswin, sub{foo($bar)}, sub{baz($quux)}; 901b8851fccSafresh1 902b8851fccSafresh1But that's not terribly pretty. You may find it simpler or clearer in 903b8851fccSafresh1the long run to just do things like this: 904b8851fccSafresh1 905b8851fccSafresh1 if( $^O =~ m/MSWin/ ) { 906b8851fccSafresh1 print "# Yay, we're under $^O\n"; 907b8851fccSafresh1 ok foo($bar), baz($quux); 908b8851fccSafresh1 ok thing($whatever), baz($stuff); 909b8851fccSafresh1 ok blorp($quux, $whatever); 910b8851fccSafresh1 ok foo($barzbarz), thang($quux); 911b8851fccSafresh1 } else { 912b8851fccSafresh1 print "# Feh, we're under $^O. Watch me skip some tests...\n"; 913b8851fccSafresh1 for(1 .. 4) { skip "Skip unless under MSWin" } 914b8851fccSafresh1 } 915b8851fccSafresh1 916b8851fccSafresh1But be quite sure that C<ok> is called exactly as many times in the 917b8851fccSafresh1first block as C<skip> is called in the second block. 918b8851fccSafresh1 919b8851fccSafresh1=back 920b8851fccSafresh1 921b8851fccSafresh1 922b8851fccSafresh1=head1 ENVIRONMENT 923b8851fccSafresh1 924b8851fccSafresh1If C<PERL_TEST_DIFF> environment variable is set, it will be used as a 925b8851fccSafresh1command for comparing unexpected multiline results. If you have GNU 926b8851fccSafresh1diff installed, you might want to set C<PERL_TEST_DIFF> to C<diff -u>. 927b8851fccSafresh1If you don't have a suitable program, you might install the 928b8851fccSafresh1C<Text::Diff> module and then set C<PERL_TEST_DIFF> to be C<perl 929b8851fccSafresh1-MText::Diff -e 'print diff(@ARGV)'>. If C<PERL_TEST_DIFF> isn't set 930b8851fccSafresh1but the C<Algorithm::Diff> module is available, then it will be used 931b8851fccSafresh1to show the differences in multiline results. 932b8851fccSafresh1 933b8851fccSafresh1=for comment 934b8851fccSafresh1If C<PERL_TEST_NO_TRUNC> is set, then the initial "Got 'something' but 935b8851fccSafresh1expected 'something_else'" readings for long multiline output values aren't 936b8851fccSafresh1truncated at about the 230th column, as they normally could be in some 937b8851fccSafresh1cases. Normally you won't need to use this, unless you were carefully 938b8851fccSafresh1parsing the output of your test programs. 939b8851fccSafresh1 940b8851fccSafresh1 941b8851fccSafresh1=head1 NOTE 942b8851fccSafresh1 943b8851fccSafresh1A past developer of this module once said that it was no longer being 944b8851fccSafresh1actively developed. However, rumors of its demise were greatly 945b8851fccSafresh1exaggerated. Feedback and suggestions are quite welcome. 946b8851fccSafresh1 947b8851fccSafresh1Be aware that the main value of this module is its simplicity. Note 948b8851fccSafresh1that there are already more ambitious modules out there, such as 949b8851fccSafresh1L<Test::More> and L<Test::Unit>. 950b8851fccSafresh1 951b8851fccSafresh1Some earlier versions of this module had docs with some confusing 952b8851fccSafresh1typos in the description of C<skip(...)>. 953b8851fccSafresh1 954b8851fccSafresh1 955b8851fccSafresh1=head1 SEE ALSO 956b8851fccSafresh1 957b8851fccSafresh1L<Test::Harness> 958b8851fccSafresh1 959b8851fccSafresh1L<Test::Simple>, L<Test::More>, L<Devel::Cover> 960b8851fccSafresh1 961b8851fccSafresh1L<Test::Builder> for building your own testing library. 962b8851fccSafresh1 963b8851fccSafresh1L<Test::Unit> is an interesting XUnit-style testing library. 964b8851fccSafresh1 965b8851fccSafresh1L<Test::Inline> lets you embed tests in code. 966b8851fccSafresh1 967b8851fccSafresh1 968b8851fccSafresh1=head1 AUTHOR 969b8851fccSafresh1 970b8851fccSafresh1Copyright (c) 1998-2000 Joshua Nathaniel Pritikin. 971b8851fccSafresh1 972b8851fccSafresh1Copyright (c) 2001-2002 Michael G. Schwern. 973b8851fccSafresh1 974b8851fccSafresh1Copyright (c) 2002-2004 Sean M. Burke. 975b8851fccSafresh1 976b8851fccSafresh1Current maintainer: Jesse Vincent. E<lt>jesse@bestpractical.comE<gt> 977b8851fccSafresh1 978b8851fccSafresh1This package is free software and is provided "as is" without express 979b8851fccSafresh1or implied warranty. It may be used, redistributed and/or modified 980b8851fccSafresh1under the same terms as Perl itself. 981b8851fccSafresh1 982b8851fccSafresh1=cut 983b8851fccSafresh1 984b8851fccSafresh1# "Your mistake was a hidden intention." 985b8851fccSafresh1# -- /Oblique Strategies/, Brian Eno and Peter Schmidt 986