2 # t/test.pl - most of Test::More functionality without the fuss
19 print STDOUT "1..$n\n";
25 if (!$NO_ENDING && defined $planned && $planned != $ran) {
26 print STDERR "# Looks like you planned $planned tests but ran $ran.\n";
30 # Use this instead of "print STDERR" when outputing failure diagnostic
34 my @mess = map { /^#/ ? "$_\n" : "# $_\n" }
35 map { split /\n/ } @_;
36 my $fh = $TODO ? *STDOUT : *STDERR;
43 print STDOUT "1..0 # Skipped: @_\n";
45 print STDOUT "1..0\n";
51 my ($pass, $where, $name, @mess) = @_;
52 # Do not try to microoptimize by factoring out the "not ".
56 # escape out '#' or it will interfere with '# skip' and such
58 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
60 $out = $pass ? "ok $test" : "not ok $test";
63 $out .= " # TODO $TODO" if $TODO;
64 print STDOUT "$out\n";
67 _diag "# Failed $where\n";
70 # Ensure that the message is properly escaped.
79 my @caller = caller(1);
80 return "at $caller[1] line $caller[2]";
83 # DON'T use this for matches. Use like() instead.
85 my ($pass, $name, @mess) = @_;
86 _ok($pass, _where(), $name, @mess);
91 return 'undef' unless defined $x;
100 return defined $x ? '"' . display ($x) . '"' : 'undef';
103 # keys are the codes \n etc map to, values are 2 char strings such as \n
104 my %backslash_escape;
105 foreach my $x (split //, 'nrtfa\\\'"') {
106 $backslash_escape{ord eval "\"\\$x\""} = "\\$x";
108 # A way to display scalars containing control characters and Unicode.
109 # Trying to avoid setting $_, or relying on local $_ to work.
113 if (defined $x and not ref $x) {
115 foreach my $c (unpack("U*", $x)) {
117 $y .= sprintf "\\x{%x}", $c;
118 } elsif ($backslash_escape{$c}) {
119 $y .= $backslash_escape{$c};
121 my $z = chr $c; # Maybe we can get away with a literal...
122 $z = sprintf "\\%03o", $c if $z =~ /[[:^print:]]/;
128 return $x unless wantarray;
135 my ($got, $expected, $name, @mess) = @_;
138 if( !defined $got || !defined $expected ) {
139 # undef only matches undef
140 $pass = !defined $got && !defined $expected;
143 $pass = $got eq $expected;
147 unshift(@mess, "# got "._q($got)."\n",
148 "# expected "._q($expected)."\n");
150 _ok($pass, _where(), $name, @mess);
154 my ($got, $isnt, $name, @mess) = @_;
157 if( !defined $got || !defined $isnt ) {
158 # undef only matches undef
159 $pass = defined $got || defined $isnt;
162 $pass = $got ne $isnt;
166 unshift(@mess, "# it should not be "._q($got)."\n",
169 _ok($pass, _where(), $name, @mess);
173 my($got, $type, $expected, $name, @mess) = @_;
178 local($@,$!); # don't interfere with $@
179 # eval() sometimes resets $!
180 $pass = eval "\$got $type \$expected";
183 # It seems Irix long doubles can have 2147483648 and 2147483648
184 # that stringify to the same thing but are acutally numerically
185 # different. Display the numbers if $type isn't a string operator,
186 # and the numbers are stringwise the same.
187 # (all string operators have alphabetic names, so tr/a-z// is true)
188 # This will also show numbers for some uneeded cases, but will
189 # definately be helpful for things such as == and <= that fail
190 if ($got eq $expected and $type !~ tr/a-z//) {
191 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
193 unshift(@mess, "# got "._q($got)."\n",
194 "# expected $type "._q($expected)."\n");
196 _ok($pass, _where(), $name, @mess);
199 # Check that $got is within $range of $expected
200 # if $range is 0, then check it's exact
201 # else if $expected is 0, then $range is an absolute value
202 # otherwise $range is a fractional error.
203 # Here $range must be numeric, >= 0
204 # Non numeric ranges might be a useful future extension. (eg %)
206 my ($got, $expected, $range, $name, @mess) = @_;
208 if (!defined $got or !defined $expected or !defined $range) {
209 # This is a fail, but doesn't need extra diagnostics
210 } elsif ($got !~ tr/0-9// or $expected !~ tr/0-9// or $range !~ tr/0-9//) {
212 unshift @mess, "# got, expected and range must be numeric\n";
213 } elsif ($range < 0) {
214 # This is also a fail
215 unshift @mess, "# range must not be negative\n";
216 } elsif ($range == 0) {
218 $pass = $got == $expected;
219 } elsif ($expected == 0) {
220 # If expected is 0, treat range as absolute
221 $pass = ($got <= $range) && ($got >= - $range);
223 my $diff = $got - $expected;
224 $pass = abs ($diff / $expected) < $range;
227 if ($got eq $expected) {
228 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
230 unshift@mess, "# got "._q($got)."\n",
231 "# expected "._q($expected)." (within "._q($range).")\n";
233 _ok($pass, _where(), $name, @mess);
236 # Note: this isn't quite as fancy as Test::More::like().
238 my ($got, $expected, $name, @mess) = @_;
240 if (ref $expected eq 'Regexp') {
241 $pass = $got =~ $expected;
243 unshift(@mess, "# got '$got'\n",
244 "# expected /$expected/\n");
247 $pass = $got =~ /$expected/;
249 unshift(@mess, "# got '$got'\n",
250 "# expected /$expected/\n");
253 _ok($pass, _where(), $name, @mess);
261 _ok(0, _where(), @_);
273 # Note: can't pass multipart messages since we try to
274 # be compatible with Test::More::skip().
277 my $n = @_ ? shift : 1;
279 print STDOUT "ok $test # skip: $why\n";
288 return 0 unless $#$ra == $#$rb;
289 for my $i (0..$#$ra) {
290 return 0 unless $ra->[$i] eq $rb->[$i];
296 my ($orig, $suspect) = @_;
298 while (my ($key, $value) = each %$suspect) {
299 # Force a hash recompute if this perl's internals can cache the hash key.
301 if (exists $orig->{$key}) {
302 if ($orig->{$key} ne $value) {
303 print STDOUT "# key ", _qq($key), " was ", _qq($orig->{$key}),
304 " now ", _qq($value), "\n";
308 print STDOUT "# key ", _qq($key), " is ", _qq($value),
309 ", not in original.\n";
313 foreach (keys %$orig) {
314 # Force a hash recompute if this perl's internals can cache the hash key.
316 next if (exists $suspect->{$_});
317 print STDOUT "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
328 _ok(!$@, _where(), "require $require");
336 _ok(!$@, _where(), "use $use");
339 # runperl - Runs a separate perl interpreter.
341 # switches => [ command-line switches ]
342 # nolib => 1 # don't use -I../lib (included by default)
343 # prog => one-liner (avoid quotes)
344 # progs => [ multi-liner (avoid quotes) ]
345 # progfile => perl script
346 # stdin => string to feed the stdin
347 # stderr => redirect stderr to stdout
348 # args => [ command-line arguments to the perl program ]
349 # verbose => print the command line
351 my $is_mswin = $^O eq 'MSWin32';
352 my $is_netware = $^O eq 'NetWare';
353 my $is_macos = $^O eq 'MacOS';
354 my $is_vms = $^O eq 'VMS';
357 my ($runperl, $args) = @_;
360 # In VMS protect with doublequotes because otherwise
361 # DCL will lowercase -- unless already doublequoted.
362 $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0;
363 $$runperl .= ' ' . $_;
367 sub _create_runperl { # Create the string to qx in runperl().
369 my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X;
370 unless ($args{nolib}) {
372 $runperl .= ' -I::lib';
373 # Use UNIX style error messages instead of MPW style.
374 $runperl .= ' -MMac::err=unix' if $args{stderr};
377 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
380 if ($args{switches}) {
381 _quote_args(\$runperl, $args{switches});
383 if (defined $args{prog}) {
384 $args{progs} = [$args{prog}]
386 if (defined $args{progs}) {
387 foreach my $prog (@{$args{progs}}) {
388 if ($is_mswin || $is_netware || $is_vms) {
389 $runperl .= qq ( -e "$prog" );
392 $runperl .= qq ( -e '$prog' );
395 } elsif (defined $args{progfile}) {
396 $runperl .= qq( "$args{progfile}");
398 if (defined $args{stdin}) {
399 # so we don't try to put literal newlines and crs onto the
401 $args{stdin} =~ s/\n/\\n/g;
402 $args{stdin} =~ s/\r/\\r/g;
404 if ($is_mswin || $is_netware || $is_vms) {
405 $runperl = qq{$^X -e "print qq(} .
406 $args{stdin} . q{)" | } . $runperl;
409 # MacOS can only do two processes under MPW at once;
410 # the test itself is one; we can't do two more, so
412 my $stdin = qq{$^X -e 'print qq(} . $args{stdin} . qq{)' > teststdin; };
413 if ($args{verbose}) {
414 my $stdindisplay = $stdin;
415 $stdindisplay =~ s/\n/\n\#/g;
416 print STDERR "# $stdindisplay\n";
419 $runperl .= q{ < teststdin };
422 $runperl = qq{$^X -e 'print qq(} .
423 $args{stdin} . q{)' | } . $runperl;
426 if (defined $args{args}) {
427 _quote_args(\$runperl, $args{args});
429 $runperl .= ' 2>&1' if $args{stderr} && !$is_macos;
430 $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
431 if ($args{verbose}) {
432 my $runperldisplay = $runperl;
433 $runperldisplay =~ s/\n/\n\#/g;
434 print STDERR "# $runperldisplay\n";
440 my $runperl = &_create_runperl;
441 my $result = `$runperl`;
442 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
446 *run_perl = \&runperl; # Nice alias.
449 print STDERR "# @_\n";
453 # A somewhat safer version of the sometimes wrong $^X.
456 unless (defined $Perl) {
459 # VMS should have 'perl' aliased properly
460 return $Perl if $^O eq 'VMS';
463 eval "require Config; Config->import";
465 warn "test.pl had problems loading Config: $@";
468 $exe = $Config{_exe};
470 $exe = '' unless defined $exe;
472 # This doesn't absolutize the path: beware of future chdirs().
473 # We could do File::Spec->abs2rel() but that does getcwd()s,
474 # which is a bit heavyweight to do here.
476 if ($Perl =~ /^perl\Q$exe\E$/i) {
477 my $perl = "perl$exe";
478 eval "require File::Spec";
480 warn "test.pl had problems loading File::Spec: $@";
483 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
487 # Build up the name of the executable file from the name of
490 if ($Perl !~ /\Q$exe\E$/i) {
494 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
496 # For subcommands to use.
497 $ENV{PERLEXE} = $Perl;
503 foreach my $file (@_) {
504 1 while unlink $file;
505 print STDERR "# Couldn't unlink '$file': $!\n" if -f $file;
510 my $tmpfile = "misctmp000";
511 1 while -f ++$tmpfile;
512 END { unlink_all $tmpfile }
517 # The $resolve must be a subref that tests the first argument
518 # for success, or returns the definition of success (e.g. the
519 # expected scalar) if given no arguments.
523 my($prog, $resolve, $runperl_args, $name) = @_;
525 $runperl_args ||= {};
526 $runperl_args->{progfile} = $tmpfile;
527 $runperl_args->{stderr} = 1;
529 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
533 $prog =~ s#/dev/null#NL:#;
536 $prog =~ s{if \(-e _ and -f _ and -r _\)}
540 print TEST $prog, "\n";
541 close TEST or die "Cannot close $tmpfile: $!";
543 my $results = runperl(%$runperl_args);
546 # Clean up the results into something a bit more predictable.
547 $results =~ s/\n+$//;
548 $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
549 $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
551 # bison says 'parse error' instead of 'syntax error',
552 # various yaccs may or may not capitalize 'syntax'.
553 $results =~ s/^(syntax|parse) error/syntax error/mig;
556 # some tests will trigger VMS messages that won't be expected
557 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
559 # pipes double these sometimes
560 $results =~ s/\n\n/\n/g;
563 my $pass = $resolve->($results);
565 _diag "# PROG: \n$prog\n";
566 _diag "# EXPECTED:\n", $resolve->(), "\n";
567 _diag "# GOT:\n$results\n";
568 _diag "# STATUS: $status\n";
571 # Use the first line of the program as a name if none was given
573 ($first_line, $name) = $prog =~ /^((.{1,50}).*)/;
574 $name .= '...' if length $first_line > length $name;
577 _ok($pass, _where(), "fresh_perl - $name");
583 # Combination of run_perl() and is().
587 my($prog, $expected, $runperl_args, $name) = @_;
589 sub { @_ ? $_[0] eq $expected : $expected },
590 $runperl_args, $name);
596 # Combination of run_perl() and like().
599 sub fresh_perl_like {
600 my($prog, $expected, $runperl_args, $name) = @_;
603 $_[0] =~ (ref $expected ? $expected : /$expected/) :
605 $runperl_args, $name);