2 # t/test.pl - most of Test::More functionality without the fuss
7 # Increment ($x++) has a certain amount of cleverness for things like
10 # $x++; # $x eq 'aaa';
12 # stands more chance of breaking than just a simple
16 # In this file, we use the latter "Baby Perl" approach, and increment
17 # will be worked over by t/op/inc.t
23 my $Perl; # Safer version of $^X set by which_perl()
28 # Use this instead of print to avoid interference while testing globals.
30 local($\, $", $,) = (undef, ' ', '');
35 local($\, $", $,) = (undef, ' ', '');
43 if ($n eq 'no_plan') {
51 _print "1..$n\n" unless $noplan;
58 if (defined $planned && $planned != $ran) {
60 "# Looks like you planned $planned tests but ran $ran.\n";
67 # Use this instead of "print STDERR" when outputing failure diagnostic
71 my @mess = map { /^#/ ? "$_\n" : "# $_\n" }
72 map { split /\n/ } @_;
73 $TODO ? _print(@mess) : _print_stderr(@mess);
82 _print "1..0 # Skip @_\n";
90 my ($pass, $where, $name, @mess) = @_;
91 # Do not try to microoptimize by factoring out the "not ".
95 # escape out '#' or it will interfere with '# skip' and such
97 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
99 $out = $pass ? "ok $test" : "not ok $test";
102 $out = $out . " # TODO $TODO" if $TODO;
106 _diag "# Failed $where\n";
109 # Ensure that the message is properly escaped.
112 $test = $test + 1; # don't use ++
118 my @caller = caller($Level);
119 return "at $caller[1] line $caller[2]";
122 # DON'T use this for matches. Use like() instead.
124 my ($pass, $name, @mess) = @_;
125 _ok($pass, _where(), $name, @mess);
130 return 'undef' unless defined $x;
139 return defined $x ? '"' . display ($x) . '"' : 'undef';
142 # keys are the codes \n etc map to, values are 2 char strings such as \n
143 my %backslash_escape;
144 foreach my $x (split //, 'nrtfa\\\'"') {
145 $backslash_escape{ord eval "\"\\$x\""} = "\\$x";
147 # A way to display scalars containing control characters and Unicode.
148 # Trying to avoid setting $_, or relying on local $_ to work.
152 if (defined $x and not ref $x) {
154 foreach my $c (unpack("U*", $x)) {
156 $y = $y . sprintf "\\x{%x}", $c;
157 } elsif ($backslash_escape{$c}) {
158 $y = $y . $backslash_escape{$c};
160 my $z = chr $c; # Maybe we can get away with a literal...
161 if ($z =~ /[[:^print:]]/) {
163 # Use octal for characters traditionally expressed as
164 # such: the low controls
166 $z = sprintf "\\%03o", $c;
168 $z = sprintf "\\x{%x}", $c;
176 return $x unless wantarray;
183 my ($got, $expected, $name, @mess) = @_;
186 if( !defined $got || !defined $expected ) {
187 # undef only matches undef
188 $pass = !defined $got && !defined $expected;
191 $pass = $got eq $expected;
195 unshift(@mess, "# got "._qq($got)."\n",
196 "# expected "._qq($expected)."\n");
198 _ok($pass, _where(), $name, @mess);
202 my ($got, $isnt, $name, @mess) = @_;
205 if( !defined $got || !defined $isnt ) {
206 # undef only matches undef
207 $pass = defined $got || defined $isnt;
210 $pass = $got ne $isnt;
214 unshift(@mess, "# it should not be "._qq($got)."\n",
217 _ok($pass, _where(), $name, @mess);
221 my($got, $type, $expected, $name, @mess) = @_;
226 local($@,$!); # don't interfere with $@
227 # eval() sometimes resets $!
228 $pass = eval "\$got $type \$expected";
231 # It seems Irix long doubles can have 2147483648 and 2147483648
232 # that stringify to the same thing but are acutally numerically
233 # different. Display the numbers if $type isn't a string operator,
234 # and the numbers are stringwise the same.
235 # (all string operators have alphabetic names, so tr/a-z// is true)
236 # This will also show numbers for some uneeded cases, but will
237 # definately be helpful for things such as == and <= that fail
238 if ($got eq $expected and $type !~ tr/a-z//) {
239 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
241 unshift(@mess, "# got "._qq($got)."\n",
242 "# expected $type "._qq($expected)."\n");
244 _ok($pass, _where(), $name, @mess);
247 # Check that $got is within $range of $expected
248 # if $range is 0, then check it's exact
249 # else if $expected is 0, then $range is an absolute value
250 # otherwise $range is a fractional error.
251 # Here $range must be numeric, >= 0
252 # Non numeric ranges might be a useful future extension. (eg %)
254 my ($got, $expected, $range, $name, @mess) = @_;
256 if (!defined $got or !defined $expected or !defined $range) {
257 # This is a fail, but doesn't need extra diagnostics
258 } elsif ($got !~ tr/0-9// or $expected !~ tr/0-9// or $range !~ tr/0-9//) {
260 unshift @mess, "# got, expected and range must be numeric\n";
261 } elsif ($range < 0) {
262 # This is also a fail
263 unshift @mess, "# range must not be negative\n";
264 } elsif ($range == 0) {
266 $pass = $got == $expected;
267 } elsif ($expected == 0) {
268 # If expected is 0, treat range as absolute
269 $pass = ($got <= $range) && ($got >= - $range);
271 my $diff = $got - $expected;
272 $pass = abs ($diff / $expected) < $range;
275 if ($got eq $expected) {
276 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
278 unshift@mess, "# got "._qq($got)."\n",
279 "# expected "._qq($expected)." (within "._qq($range).")\n";
281 _ok($pass, _where(), $name, @mess);
284 # Note: this isn't quite as fancy as Test::More::like().
286 sub like ($$@) { like_yn (0,@_) }; # 0 for -
287 sub unlike ($$@) { like_yn (1,@_) }; # 1 for un-
290 my ($flip, $got, $expected, $name, @mess) = @_;
292 $pass = $got =~ /$expected/ if !$flip;
293 $pass = $got !~ /$expected/ if $flip;
295 unshift(@mess, "# got '$got'\n",
297 ? "# expected !~ /$expected/\n" : "# expected /$expected/\n");
299 local $Level = $Level + 1;
300 _ok($pass, _where(), $name, @mess);
308 _ok(0, _where(), @_);
318 $test = $test + 1; # don't use ++
322 # Note: can't pass multipart messages since we try to
323 # be compatible with Test::More::skip().
326 my $n = @_ ? shift : 1;
328 _print "ok $test # skip $why\n";
337 my $n = @_ ? shift : 1;
340 _print "not ok $test # TODO & SKIP $why\n";
349 return 0 unless $#$ra == $#$rb;
350 for my $i (0..$#$ra) {
351 next if !defined $ra->[$i] && !defined $rb->[$i];
352 return 0 if !defined $ra->[$i];
353 return 0 if !defined $rb->[$i];
354 return 0 unless $ra->[$i] eq $rb->[$i];
360 my ($orig, $suspect) = @_;
362 while (my ($key, $value) = each %$suspect) {
363 # Force a hash recompute if this perl's internals can cache the hash key.
365 if (exists $orig->{$key}) {
366 if ($orig->{$key} ne $value) {
367 _print "# key ", _qq($key), " was ", _qq($orig->{$key}),
368 " now ", _qq($value), "\n";
372 _print "# key ", _qq($key), " is ", _qq($value),
373 ", not in original.\n";
377 foreach (keys %$orig) {
378 # Force a hash recompute if this perl's internals can cache the hash key.
380 next if (exists $suspect->{$_});
381 _print "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
392 _ok(!$@, _where(), "require $require");
400 _ok(!$@, _where(), "use $use");
403 # runperl - Runs a separate perl interpreter.
405 # switches => [ command-line switches ]
406 # nolib => 1 # don't use -I../lib (included by default)
407 # prog => one-liner (avoid quotes)
408 # progs => [ multi-liner (avoid quotes) ]
409 # progfile => perl script
410 # stdin => string to feed the stdin
411 # stderr => redirect stderr to stdout
412 # args => [ command-line arguments to the perl program ]
413 # verbose => print the command line
415 my $is_mswin = $^O eq 'MSWin32';
416 my $is_netware = $^O eq 'NetWare';
417 my $is_vms = $^O eq 'VMS';
418 my $is_cygwin = $^O eq 'cygwin';
421 my ($runperl, $args) = @_;
424 # In VMS protect with doublequotes because otherwise
425 # DCL will lowercase -- unless already doublequoted.
426 $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0;
427 $runperl = $runperl . ' ' . $_;
432 sub _create_runperl { # Create the string to qx in runperl().
434 my $runperl = which_perl();
435 if ($runperl =~ m/\s/) {
436 $runperl = qq{"$runperl"};
438 #- this allows, for example, to set PERL_RUNPERL_DEBUG=/usr/bin/valgrind
439 if ($ENV{PERL_RUNPERL_DEBUG}) {
440 $runperl = "$ENV{PERL_RUNPERL_DEBUG} $runperl";
442 unless ($args{nolib}) {
443 $runperl = $runperl . ' "-I../lib"'; # doublequotes because of VMS
445 if ($args{switches}) {
447 die "test.pl:runperl(): 'switches' must be an ARRAYREF " . _where()
448 unless ref $args{switches} eq "ARRAY";
449 $runperl = _quote_args($runperl, $args{switches});
451 if (defined $args{prog}) {
452 die "test.pl:runperl(): both 'prog' and 'progs' cannot be used " . _where()
453 if defined $args{progs};
454 $args{progs} = [$args{prog}]
456 if (defined $args{progs}) {
457 die "test.pl:runperl(): 'progs' must be an ARRAYREF " . _where()
458 unless ref $args{progs} eq "ARRAY";
459 foreach my $prog (@{$args{progs}}) {
460 if ($is_mswin || $is_netware || $is_vms) {
461 $runperl = $runperl . qq ( -e "$prog" );
464 $runperl = $runperl . qq ( -e '$prog' );
467 } elsif (defined $args{progfile}) {
468 $runperl = $runperl . qq( "$args{progfile}");
470 # You probaby didn't want to be sucking in from the upstream stdin
471 die "test.pl:runperl(): none of prog, progs, progfile, args, "
472 . " switches or stdin specified"
473 unless defined $args{args} or defined $args{switches}
474 or defined $args{stdin};
476 if (defined $args{stdin}) {
477 # so we don't try to put literal newlines and crs onto the
479 $args{stdin} =~ s/\n/\\n/g;
480 $args{stdin} =~ s/\r/\\r/g;
482 if ($is_mswin || $is_netware || $is_vms) {
483 $runperl = qq{$Perl -e "print qq(} .
484 $args{stdin} . q{)" | } . $runperl;
487 $runperl = qq{$Perl -e 'print qq(} .
488 $args{stdin} . q{)' | } . $runperl;
491 if (defined $args{args}) {
492 $runperl = _quote_args($runperl, $args{args});
494 $runperl = $runperl . ' 2>&1' if $args{stderr};
495 if ($args{verbose}) {
496 my $runperldisplay = $runperl;
497 $runperldisplay =~ s/\n/\n\#/g;
498 _print_stderr "# $runperldisplay\n";
504 die "test.pl:runperl() does not take a hashref"
505 if ref $_[0] and ref $_[0] eq 'HASH';
506 my $runperl = &_create_runperl;
509 my $tainted = ${^TAINT};
511 exists $args{switches} && grep m/^-T$/, @{$args{switches}} and $tainted = $tainted + 1;
514 # We will assume that if you're running under -T, you really mean to
515 # run a fresh perl, so we'll brute force launder everything for you
518 if (! eval 'require Config; 1') {
519 warn "test.pl had problems loading Config: $@";
522 $sep = $Config::Config{path_sep};
525 my @keys = grep {exists $ENV{$_}} qw(CDPATH IFS ENV BASH_ENV);
526 local @ENV{@keys} = ();
527 # Untaint, plus take out . and empty string:
528 local $ENV{'DCL$PATH'} = $1 if $is_vms && exists($ENV{'DCL$PATH'}) && ($ENV{'DCL$PATH'} =~ /(.*)/s);
529 $ENV{PATH} =~ /(.*)/s;
531 join $sep, grep { $_ ne "" and $_ ne "." and -d $_ and
532 ($is_mswin or $is_vms or !(stat && (stat _)[2]&0022)) }
533 split quotemeta ($sep), $1;
534 $ENV{PATH} = $ENV{PATH} . "$sep/bin" if $is_cygwin; # Must have /bin under Cygwin
539 $result = `$runperl`;
541 $result = `$runperl`;
543 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
547 *run_perl = \&runperl; # Nice alias.
550 _print_stderr "# @_\n";
554 # A somewhat safer version of the sometimes wrong $^X.
556 unless (defined $Perl) {
559 # VMS should have 'perl' aliased properly
560 return $Perl if $^O eq 'VMS';
563 if (! eval 'require Config; 1') {
564 warn "test.pl had problems loading Config: $@";
567 $exe = $Config::Config{_exe};
569 $exe = '' unless defined $exe;
571 # This doesn't absolutize the path: beware of future chdirs().
572 # We could do File::Spec->abs2rel() but that does getcwd()s,
573 # which is a bit heavyweight to do here.
575 if ($Perl =~ /^perl\Q$exe\E$/i) {
576 my $perl = "perl$exe";
577 if (! eval 'require File::Spec; 1') {
578 warn "test.pl had problems loading File::Spec: $@";
581 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
585 # Build up the name of the executable file from the name of
588 if ($Perl !~ /\Q$exe\E$/i) {
589 $Perl = $Perl . $exe;
592 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
594 # For subcommands to use.
595 $ENV{PERLEXE} = $Perl;
601 foreach my $file (@_) {
602 1 while unlink $file;
603 _print_stderr "# Couldn't unlink '$file': $!\n" if -f $file;
608 END { unlink_all keys %tmpfiles }
610 # A regexp that matches the tempfile names
611 $::tempfile_regexp = 'tmp\d+[A-Z][A-Z]?';
613 # Avoid ++, avoid ranges, avoid split //
614 my @letters = qw(A B C D E F G H I J K L M N O P Q R S T U V W X Y Z);
621 $try = $try . $letters[$temp % 26];
622 $temp = int ($temp / 26);
624 # Need to note all the file names we allocated, as a second request may
625 # come before the first is created.
626 if (!-e $try && !$tmpfiles{$try}) {
632 } while $count < 26 * 26;
633 die "Can't find temporary file name starting 'tmp$$'";
636 # This is the temporary file for _fresh_perl
637 my $tmpfile = tempfile();
642 # The $resolve must be a subref that tests the first argument
643 # for success, or returns the definition of success (e.g. the
644 # expected scalar) if given no arguments.
648 my($prog, $resolve, $runperl_args, $name) = @_;
650 # Given the choice of the mis-parsable {}
651 # (we want an anon hash, but a borked lexer might think that it's a block)
652 # or relying on taking a reference to a lexical
653 # (\ might be mis-parsed, and the reference counting on the pad may go
655 # it feels like the least-worse thing is to assume that auto-vivification
656 # works. At least, this is only going to be a run-time failure, so won't
657 # affect tests using this file but not this function.
658 $runperl_args->{progfile} = $tmpfile;
659 $runperl_args->{stderr} = 1;
661 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
665 $prog =~ s#/dev/null#NL:#;
668 $prog =~ s{if \(-e _ and -f _ and -r _\)}
673 close TEST or die "Cannot close $tmpfile: $!";
675 my $results = runperl(%$runperl_args);
678 # Clean up the results into something a bit more predictable.
679 $results =~ s/\n+$//;
680 $results =~ s/at\s+$::tempfile_regexp\s+line/at - line/g;
681 $results =~ s/of\s+$::tempfile_regexp\s+aborted/of - aborted/g;
683 # bison says 'parse error' instead of 'syntax error',
684 # various yaccs may or may not capitalize 'syntax'.
685 $results =~ s/^(syntax|parse) error/syntax error/mig;
688 # some tests will trigger VMS messages that won't be expected
689 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
691 # pipes double these sometimes
692 $results =~ s/\n\n/\n/g;
695 my $pass = $resolve->($results);
697 _diag "# PROG: \n$prog\n";
698 _diag "# EXPECTED:\n", $resolve->(), "\n";
699 _diag "# GOT:\n$results\n";
700 _diag "# STATUS: $status\n";
703 # Use the first line of the program as a name if none was given
705 ($first_line, $name) = $prog =~ /^((.{1,50}).*)/;
706 $name = $name . '...' if length $first_line > length $name;
709 _ok($pass, _where(), "fresh_perl - $name");
715 # Combination of run_perl() and is().
719 my($prog, $expected, $runperl_args, $name) = @_;
721 # _fresh_perl() is going to clip the trailing newlines off the result.
722 # This will make it so the test author doesn't have to know that.
723 $expected =~ s/\n+$//;
727 sub { @_ ? $_[0] eq $expected : $expected },
728 $runperl_args, $name);
734 # Combination of run_perl() and like().
737 sub fresh_perl_like {
738 my($prog, $expected, $runperl_args, $name) = @_;
741 sub { @_ ? $_[0] =~ $expected : $expected },
742 $runperl_args, $name);
746 my($proto, @methods) = @_;
747 my $class = ref $proto || $proto;
750 return _ok( 0, _where(), "$class->can(...)" );
754 foreach my $method (@methods) {
755 local($!, $@); # don't interfere with caller's $@
756 # eval sometimes resets $!
757 eval { $proto->can($method) } || push @nok, $method;
761 $name = @methods == 1 ? "$class->can('$methods[0]')"
762 : "$class->can(...)";
764 _ok( !@nok, _where(), $name );
768 my($object, $class, $obj_name) = @_;
771 $obj_name = 'The object' unless defined $obj_name;
772 my $name = "$obj_name isa $class";
773 if( !defined $object ) {
774 $diag = "$obj_name isn't defined";
776 elsif( !ref $object ) {
777 $diag = "$obj_name isn't a reference";
780 # We can't use UNIVERSAL::isa because we want to honor isa() overrides
781 local($@, $!); # eval sometimes resets $!
782 my $rslt = eval { $object->isa($class) };
784 if( $@ =~ /^Can't call method "isa" on unblessed reference/ ) {
785 if( !UNIVERSAL::isa($object, $class) ) {
786 my $ref = ref $object;
787 $diag = "$obj_name isn't a '$class' it's a '$ref'";
791 WHOA! I tried to call ->isa on your object and got some weird error.
792 This should never happen. Please contact the author immediately.
799 my $ref = ref $object;
800 $diag = "$obj_name isn't a '$class' it's a '$ref'";
804 _ok( !$diag, _where(), $name );
807 # Set a watchdog to timeout the entire test file
808 # NOTE: If the test file uses 'threads', then call the watchdog() function
809 # _AFTER_ the 'threads' module is loaded.
813 my $timeout_msg = 'Test process timed out - terminating';
815 my $pid_to_kill = $$; # PID for this process
817 # Don't use a watchdog process if 'threads' is loaded -
818 # use a watchdog thread instead
819 if (! $threads::threads) {
821 # On Windows and VMS, try launching a watchdog process
822 # using system(1, ...) (see perlport.pod)
823 if (($^O eq 'MSWin32') || ($^O eq 'VMS')) {
824 # On Windows, try to get the 'real' PID
825 if ($^O eq 'MSWin32') {
826 eval { require Win32; };
827 if (defined(&Win32::GetCurrentProcessId)) {
828 $pid_to_kill = Win32::GetCurrentProcessId();
832 # If we still have a fake PID, we can't use this method at all
833 return if ($pid_to_kill <= 0);
835 # Launch watchdog process
838 local $SIG{'__WARN__'} = sub {
839 _diag("Watchdog warning: $_[0]");
841 my $sig = $^O eq 'VMS' ? 'TERM' : 'KILL';
842 my $cmd = _create_runperl( prog => "sleep($timeout);" .
843 "warn qq/# $timeout_msg" . '\n/;' .
844 "kill($sig, $pid_to_kill);");
845 $watchdog = system(1, $cmd);
847 if ($@ || ($watchdog <= 0)) {
848 _diag('Failed to start watchdog');
854 # Add END block to parent to terminate and
855 # clean up watchdog process
856 eval "END { local \$! = 0; local \$? = 0;
857 wait() if kill('KILL', $watchdog); };";
861 # Try using fork() to generate a watchdog process
863 eval { $watchdog = fork() };
864 if (defined($watchdog)) {
865 if ($watchdog) { # Parent process
866 # Add END block to parent to terminate and
867 # clean up watchdog process
868 eval "END { local \$! = 0; local \$? = 0;
869 wait() if kill('KILL', $watchdog); };";
873 ### Watchdog process code
875 # Load POSIX if available
876 eval { require POSIX; };
878 # Execute the timeout
879 sleep($timeout - 2) if ($timeout > 2); # Workaround for perlbug #49073
882 # Kill test process if still running
883 if (kill(0, $pid_to_kill)) {
885 kill('KILL', $pid_to_kill);
888 # Don't execute END block (added at beginning of this file)
891 # Terminate ourself (i.e., the watchdog)
892 POSIX::_exit(1) if (defined(&POSIX::_exit));
896 # fork() failed - fall through and try using a thread
899 # Use a watchdog thread because either 'threads' is loaded,
901 if (eval 'require threads; 1') {
902 threads->create(sub {
903 # Load POSIX if available
904 eval { require POSIX; };
906 # Execute the timeout
907 my $time_left = $timeout;
909 $time_left = $time_left - sleep($time_left);
910 } while ($time_left > 0);
912 # Kill the parent (and ourself)
913 select(STDERR); $| = 1;
915 POSIX::_exit(1) if (defined(&POSIX::_exit));
916 my $sig = $^O eq 'VMS' ? 'TERM' : 'KILL';
917 kill($sig, $pid_to_kill);
922 # If everything above fails, then just use an alarm timeout
923 if (eval { alarm($timeout); 1; }) {
924 # Load POSIX if available
925 eval { require POSIX; };
927 # Alarm handler will do the actual 'killing'
929 select(STDERR); $| = 1;
931 POSIX::_exit(1) if (defined(&POSIX::_exit));
932 my $sig = $^O eq 'VMS' ? 'TERM' : 'KILL';
933 kill($sig, $pid_to_kill);