2 # t/test.pl - most of Test::More functionality without the fuss
17 if ($n eq 'no_plan') {
25 print STDOUT "1..$n\n" unless $noplan;
32 if (defined $planned && $planned != $ran) {
34 "# Looks like you planned $planned tests but ran $ran.\n";
41 # Use this instead of "print STDERR" when outputing failure diagnostic
45 my @mess = map { /^#/ ? "$_\n" : "# $_\n" }
46 map { split /\n/ } @_;
47 my $fh = $TODO ? *STDOUT : *STDERR;
54 print STDOUT "1..0 # Skipped: @_\n";
56 print STDOUT "1..0\n";
62 my ($pass, $where, $name, @mess) = @_;
63 # Do not try to microoptimize by factoring out the "not ".
67 # escape out '#' or it will interfere with '# skip' and such
69 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
71 $out = $pass ? "ok $test" : "not ok $test";
74 $out .= " # TODO $TODO" if $TODO;
75 print STDOUT "$out\n";
78 _diag "# Failed $where\n";
81 # Ensure that the message is properly escaped.
90 my @caller = caller($Level);
91 return "at $caller[1] line $caller[2]";
94 # DON'T use this for matches. Use like() instead.
96 my ($pass, $name, @mess) = @_;
97 _ok($pass, _where(), $name, @mess);
102 return 'undef' unless defined $x;
111 return defined $x ? '"' . display ($x) . '"' : 'undef';
114 # keys are the codes \n etc map to, values are 2 char strings such as \n
115 my %backslash_escape;
116 foreach my $x (split //, 'nrtfa\\\'"') {
117 $backslash_escape{ord eval "\"\\$x\""} = "\\$x";
119 # A way to display scalars containing control characters and Unicode.
120 # Trying to avoid setting $_, or relying on local $_ to work.
124 if (defined $x and not ref $x) {
126 foreach my $c (unpack("U*", $x)) {
128 $y .= sprintf "\\x{%x}", $c;
129 } elsif ($backslash_escape{$c}) {
130 $y .= $backslash_escape{$c};
132 my $z = chr $c; # Maybe we can get away with a literal...
133 $z = sprintf "\\%03o", $c if $z =~ /[[:^print:]]/;
139 return $x unless wantarray;
146 my ($got, $expected, $name, @mess) = @_;
149 if( !defined $got || !defined $expected ) {
150 # undef only matches undef
151 $pass = !defined $got && !defined $expected;
154 $pass = $got eq $expected;
158 unshift(@mess, "# got "._q($got)."\n",
159 "# expected "._q($expected)."\n");
161 _ok($pass, _where(), $name, @mess);
165 my ($got, $isnt, $name, @mess) = @_;
168 if( !defined $got || !defined $isnt ) {
169 # undef only matches undef
170 $pass = defined $got || defined $isnt;
173 $pass = $got ne $isnt;
177 unshift(@mess, "# it should not be "._q($got)."\n",
180 _ok($pass, _where(), $name, @mess);
184 my($got, $type, $expected, $name, @mess) = @_;
189 local($@,$!); # don't interfere with $@
190 # eval() sometimes resets $!
191 $pass = eval "\$got $type \$expected";
194 # It seems Irix long doubles can have 2147483648 and 2147483648
195 # that stringify to the same thing but are acutally numerically
196 # different. Display the numbers if $type isn't a string operator,
197 # and the numbers are stringwise the same.
198 # (all string operators have alphabetic names, so tr/a-z// is true)
199 # This will also show numbers for some uneeded cases, but will
200 # definately be helpful for things such as == and <= that fail
201 if ($got eq $expected and $type !~ tr/a-z//) {
202 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
204 unshift(@mess, "# got "._q($got)."\n",
205 "# expected $type "._q($expected)."\n");
207 _ok($pass, _where(), $name, @mess);
210 # Check that $got is within $range of $expected
211 # if $range is 0, then check it's exact
212 # else if $expected is 0, then $range is an absolute value
213 # otherwise $range is a fractional error.
214 # Here $range must be numeric, >= 0
215 # Non numeric ranges might be a useful future extension. (eg %)
217 my ($got, $expected, $range, $name, @mess) = @_;
219 if (!defined $got or !defined $expected or !defined $range) {
220 # This is a fail, but doesn't need extra diagnostics
221 } elsif ($got !~ tr/0-9// or $expected !~ tr/0-9// or $range !~ tr/0-9//) {
223 unshift @mess, "# got, expected and range must be numeric\n";
224 } elsif ($range < 0) {
225 # This is also a fail
226 unshift @mess, "# range must not be negative\n";
227 } elsif ($range == 0) {
229 $pass = $got == $expected;
230 } elsif ($expected == 0) {
231 # If expected is 0, treat range as absolute
232 $pass = ($got <= $range) && ($got >= - $range);
234 my $diff = $got - $expected;
235 $pass = abs ($diff / $expected) < $range;
238 if ($got eq $expected) {
239 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
241 unshift@mess, "# got "._q($got)."\n",
242 "# expected "._q($expected)." (within "._q($range).")\n";
244 _ok($pass, _where(), $name, @mess);
247 # Note: this isn't quite as fancy as Test::More::like().
249 sub like ($$@) { like_yn (0,@_) }; # 0 for -
250 sub unlike ($$@) { like_yn (1,@_) }; # 1 for un-
253 my ($flip, $got, $expected, $name, @mess) = @_;
255 $pass = $got =~ /$expected/ if !$flip;
256 $pass = $got !~ /$expected/ if $flip;
258 unshift(@mess, "# got '$got'\n",
259 "# expected /$expected/\n");
261 _ok($pass, _where(), $name, @mess);
269 _ok(0, _where(), @_);
281 # Note: can't pass multipart messages since we try to
282 # be compatible with Test::More::skip().
285 my $n = @_ ? shift : 1;
287 print STDOUT "ok $test # skip: $why\n";
296 my $n = @_ ? shift : 1;
299 print STDOUT "ok $test # TODO & SKIP: $why\n";
308 return 0 unless $#$ra == $#$rb;
309 for my $i (0..$#$ra) {
310 next if !defined $ra->[$i] && !defined $rb->[$i];
311 return 0 if !defined $ra->[$i];
312 return 0 if !defined $rb->[$i];
313 return 0 unless $ra->[$i] eq $rb->[$i];
319 my ($orig, $suspect) = @_;
321 while (my ($key, $value) = each %$suspect) {
322 # Force a hash recompute if this perl's internals can cache the hash key.
324 if (exists $orig->{$key}) {
325 if ($orig->{$key} ne $value) {
326 print STDOUT "# key ", _qq($key), " was ", _qq($orig->{$key}),
327 " now ", _qq($value), "\n";
331 print STDOUT "# key ", _qq($key), " is ", _qq($value),
332 ", not in original.\n";
336 foreach (keys %$orig) {
337 # Force a hash recompute if this perl's internals can cache the hash key.
339 next if (exists $suspect->{$_});
340 print STDOUT "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
351 _ok(!$@, _where(), "require $require");
359 _ok(!$@, _where(), "use $use");
362 # runperl - Runs a separate perl interpreter.
364 # switches => [ command-line switches ]
365 # nolib => 1 # don't use -I../lib (included by default)
366 # prog => one-liner (avoid quotes)
367 # progs => [ multi-liner (avoid quotes) ]
368 # progfile => perl script
369 # stdin => string to feed the stdin
370 # stderr => redirect stderr to stdout
371 # args => [ command-line arguments to the perl program ]
372 # verbose => print the command line
374 my $is_mswin = $^O eq 'MSWin32';
375 my $is_netware = $^O eq 'NetWare';
376 my $is_macos = $^O eq 'MacOS';
377 my $is_vms = $^O eq 'VMS';
380 my ($runperl, $args) = @_;
383 # In VMS protect with doublequotes because otherwise
384 # DCL will lowercase -- unless already doublequoted.
385 $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0;
386 $$runperl .= ' ' . $_;
390 sub _create_runperl { # Create the string to qx in runperl().
392 my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X;
393 #- this allows, for example, to set PERL_RUNPERL_DEBUG=/usr/bin/valgrind
394 if ($ENV{PERL_RUNPERL_DEBUG}) {
395 $runperl = "$ENV{PERL_RUNPERL_DEBUG} $runperl";
397 unless ($args{nolib}) {
399 $runperl .= ' -I::lib';
400 # Use UNIX style error messages instead of MPW style.
401 $runperl .= ' -MMac::err=unix' if $args{stderr};
404 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
407 if ($args{switches}) {
409 die "test.pl:runperl(): 'switches' must be an ARRAYREF " . _where()
410 unless ref $args{switches} eq "ARRAY";
411 _quote_args(\$runperl, $args{switches});
413 if (defined $args{prog}) {
414 die "test.pl:runperl(): both 'prog' and 'progs' cannot be used " . _where()
415 if defined $args{progs};
416 $args{progs} = [$args{prog}]
418 if (defined $args{progs}) {
419 die "test.pl:runperl(): 'progs' must be an ARRAYREF " . _where()
420 unless ref $args{progs} eq "ARRAY";
421 foreach my $prog (@{$args{progs}}) {
422 if ($is_mswin || $is_netware || $is_vms) {
423 $runperl .= qq ( -e "$prog" );
426 $runperl .= qq ( -e '$prog' );
429 } elsif (defined $args{progfile}) {
430 $runperl .= qq( "$args{progfile}");
432 # You probaby didn't want to be sucking in from the upstream stdin
433 die "test.pl:runperl(): none of prog, progs, progfile, args, "
434 . " switches or stdin specified"
435 unless defined $args{args} or defined $args{switches}
436 or defined $args{stdin};
438 if (defined $args{stdin}) {
439 # so we don't try to put literal newlines and crs onto the
441 $args{stdin} =~ s/\n/\\n/g;
442 $args{stdin} =~ s/\r/\\r/g;
444 if ($is_mswin || $is_netware || $is_vms) {
445 $runperl = qq{$^X -e "print qq(} .
446 $args{stdin} . q{)" | } . $runperl;
449 # MacOS can only do two processes under MPW at once;
450 # the test itself is one; we can't do two more, so
452 my $stdin = qq{$^X -e 'print qq(} . $args{stdin} . qq{)' > teststdin; };
453 if ($args{verbose}) {
454 my $stdindisplay = $stdin;
455 $stdindisplay =~ s/\n/\n\#/g;
456 print STDERR "# $stdindisplay\n";
459 $runperl .= q{ < teststdin };
462 $runperl = qq{$^X -e 'print qq(} .
463 $args{stdin} . q{)' | } . $runperl;
466 if (defined $args{args}) {
467 _quote_args(\$runperl, $args{args});
469 $runperl .= ' 2>&1' if $args{stderr} && !$is_macos;
470 $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
471 if ($args{verbose}) {
472 my $runperldisplay = $runperl;
473 $runperldisplay =~ s/\n/\n\#/g;
474 print STDERR "# $runperldisplay\n";
480 die "test.pl:runperl() does not take a hashref"
481 if ref $_[0] and ref $_[0] eq 'HASH';
482 my $runperl = &_create_runperl;
483 my $result = `$runperl`;
484 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
488 *run_perl = \&runperl; # Nice alias.
491 print STDERR "# @_\n";
495 # A somewhat safer version of the sometimes wrong $^X.
498 unless (defined $Perl) {
501 # VMS should have 'perl' aliased properly
502 return $Perl if $^O eq 'VMS';
505 eval "require Config; Config->import";
507 warn "test.pl had problems loading Config: $@";
510 $exe = $Config{_exe};
512 $exe = '' unless defined $exe;
514 # This doesn't absolutize the path: beware of future chdirs().
515 # We could do File::Spec->abs2rel() but that does getcwd()s,
516 # which is a bit heavyweight to do here.
518 if ($Perl =~ /^perl\Q$exe\E$/i) {
519 my $perl = "perl$exe";
520 eval "require File::Spec";
522 warn "test.pl had problems loading File::Spec: $@";
525 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
529 # Build up the name of the executable file from the name of
532 if ($Perl !~ /\Q$exe\E$/i) {
536 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
538 # For subcommands to use.
539 $ENV{PERLEXE} = $Perl;
545 foreach my $file (@_) {
546 1 while unlink $file;
547 print STDERR "# Couldn't unlink '$file': $!\n" if -f $file;
552 my $tmpfile = "misctmp000";
553 1 while -f ++$tmpfile;
554 END { unlink_all $tmpfile }
559 # The $resolve must be a subref that tests the first argument
560 # for success, or returns the definition of success (e.g. the
561 # expected scalar) if given no arguments.
565 my($prog, $resolve, $runperl_args, $name) = @_;
567 $runperl_args ||= {};
568 $runperl_args->{progfile} = $tmpfile;
569 $runperl_args->{stderr} = 1;
571 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
575 $prog =~ s#/dev/null#NL:#;
578 $prog =~ s{if \(-e _ and -f _ and -r _\)}
583 close TEST or die "Cannot close $tmpfile: $!";
585 my $results = runperl(%$runperl_args);
588 # Clean up the results into something a bit more predictable.
589 $results =~ s/\n+$//;
590 $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
591 $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
593 # bison says 'parse error' instead of 'syntax error',
594 # various yaccs may or may not capitalize 'syntax'.
595 $results =~ s/^(syntax|parse) error/syntax error/mig;
598 # some tests will trigger VMS messages that won't be expected
599 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
601 # pipes double these sometimes
602 $results =~ s/\n\n/\n/g;
605 my $pass = $resolve->($results);
607 _diag "# PROG: \n$prog\n";
608 _diag "# EXPECTED:\n", $resolve->(), "\n";
609 _diag "# GOT:\n$results\n";
610 _diag "# STATUS: $status\n";
613 # Use the first line of the program as a name if none was given
615 ($first_line, $name) = $prog =~ /^((.{1,50}).*)/;
616 $name .= '...' if length $first_line > length $name;
619 _ok($pass, _where(), "fresh_perl - $name");
625 # Combination of run_perl() and is().
629 my($prog, $expected, $runperl_args, $name) = @_;
632 sub { @_ ? $_[0] eq $expected : $expected },
633 $runperl_args, $name);
639 # Combination of run_perl() and like().
642 sub fresh_perl_like {
643 my($prog, $expected, $runperl_args, $name) = @_;
647 $_[0] =~ (ref $expected ? $expected : /$expected/) :
649 $runperl_args, $name);
653 my($proto, @methods) = @_;
654 my $class = ref $proto || $proto;
657 return _ok( 0, _where(), "$class->can(...)" );
661 foreach my $method (@methods) {
662 local($!, $@); # don't interfere with caller's $@
663 # eval sometimes resets $!
664 eval { $proto->can($method) } || push @nok, $method;
668 $name = @methods == 1 ? "$class->can('$methods[0]')"
669 : "$class->can(...)";
671 _ok( !@nok, _where(), $name );
675 my($object, $class, $obj_name) = @_;
678 $obj_name = 'The object' unless defined $obj_name;
679 my $name = "$obj_name isa $class";
680 if( !defined $object ) {
681 $diag = "$obj_name isn't defined";
683 elsif( !ref $object ) {
684 $diag = "$obj_name isn't a reference";
687 # We can't use UNIVERSAL::isa because we want to honor isa() overrides
688 local($@, $!); # eval sometimes resets $!
689 my $rslt = eval { $object->isa($class) };
691 if( $@ =~ /^Can't call method "isa" on unblessed reference/ ) {
692 if( !UNIVERSAL::isa($object, $class) ) {
693 my $ref = ref $object;
694 $diag = "$obj_name isn't a '$class' it's a '$ref'";
698 WHOA! I tried to call ->isa on your object and got some weird error.
699 This should never happen. Please contact the author immediately.
706 my $ref = ref $object;
707 $diag = "$obj_name isn't a '$class' it's a '$ref'";
711 _ok( !$diag, _where(), $name );