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
33 my $fh = $TODO ? *STDOUT : *STDERR;
39 print STDOUT "1..0 - @_\n";
41 print STDOUT "1..0\n";
47 my ($pass, $where, $name, @mess) = @_;
48 # Do not try to microoptimize by factoring out the "not ".
52 # escape out '#' or it will interfere with '# skip' and such
54 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
56 $out = $pass ? "ok $test" : "not ok $test";
59 $out .= " # TODO $TODO" if $TODO;
60 print STDOUT "$out\n";
63 _diag "# Failed $where\n";
66 # Ensure that the message is properly escaped.
67 _diag map { /^#/ ? "$_\n" : "# $_\n" }
68 map { split /\n/ } @mess if @mess;
76 my @caller = caller(1);
77 return "at $caller[1] line $caller[2]";
80 # DON'T use this for matches. Use like() instead.
82 my ($pass, $name, @mess) = @_;
83 _ok($pass, _where(), $name, @mess);
88 return 'undef' unless defined $x;
97 return defined $x ? '"' . display ($x) . '"' : 'undef';
100 # keys are the codes \n etc map to, values are 2 char strings such as \n
101 my %backslash_escape;
102 foreach my $x (split //, 'nrtfa\\\'"') {
103 $backslash_escape{ord eval "\"\\$x\""} = "\\$x";
105 # A way to display scalars containing control characters and Unicode.
106 # Trying to avoid setting $_, or relying on local $_ to work.
110 if (defined $x and not ref $x) {
112 foreach my $c (unpack("U*", $x)) {
114 $y .= sprintf "\\x{%x}", $c;
115 } elsif ($backslash_escape{$c}) {
116 $y .= $backslash_escape{$c};
118 my $z = chr $c; # Maybe we can get away with a literal...
119 $z = sprintf "\\%03o", $c if $z =~ /[[:^print:]]/;
125 return $x unless wantarray;
132 my ($got, $expected, $name, @mess) = @_;
133 my $pass = $got eq $expected;
135 unshift(@mess, "# got "._q($got)."\n",
136 "# expected "._q($expected)."\n");
138 _ok($pass, _where(), $name, @mess);
142 my ($got, $isnt, $name, @mess) = @_;
143 my $pass = $got ne $isnt;
145 unshift(@mess, "# it should not be "._q($got)."\n",
148 _ok($pass, _where(), $name, @mess);
152 my($got, $type, $expected, $name, @mess) = @_;
157 local($@,$!); # don't interfere with $@
158 # eval() sometimes resets $!
159 $pass = eval "\$got $type \$expected";
162 # It seems Irix long doubles can have 2147483648 and 2147483648
163 # that stringify to the same thing but are acutally numerically
164 # different. Display the numbers if $type isn't a string operator,
165 # and the numbers are stringwise the same.
166 # (all string operators have alphabetic names, so tr/a-z// is true)
167 # This will also show numbers for some uneeded cases, but will
168 # definately be helpful for things such as == and <= that fail
169 if ($got eq $expected and $type !~ tr/a-z//) {
170 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
172 unshift(@mess, "# got "._q($got)."\n",
173 "# expected $type "._q($expected)."\n");
175 _ok($pass, _where(), $name, @mess);
178 # Check that $got is within $range of $expected
179 # if $range is 0, then check it's exact
180 # else if $expected is 0, then $range is an absolute value
181 # otherwise $range is a fractional error.
182 # Here $range must be numeric, >= 0
183 # Non numeric ranges might be a useful future extension. (eg %)
185 my ($got, $expected, $range, $name, @mess) = @_;
187 if (!defined $got or !defined $expected or !defined $range) {
188 # This is a fail, but doesn't need extra diagnostics
189 } elsif ($got !~ tr/0-9// or $expected !~ tr/0-9// or $range !~ tr/0-9//) {
191 unshift @mess, "# got, expected and range must be numeric\n";
192 } elsif ($range < 0) {
193 # This is also a fail
194 unshift @mess, "# range must not be negative\n";
195 } elsif ($range == 0) {
197 $pass = $got == $expected;
198 } elsif ($expected == 0) {
199 # If expected is 0, treat range as absolute
200 $pass = ($got <= $range) && ($got >= - $range);
202 my $diff = $got - $expected;
203 $pass = abs ($diff / $expected) < $range;
206 if ($got eq $expected) {
207 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
209 unshift@mess, "# got "._q($got)."\n",
210 "# expected "._q($expected)." (within "._q($range).")\n";
212 _ok($pass, _where(), $name, @mess);
215 # Note: this isn't quite as fancy as Test::More::like().
217 my ($got, $expected, $name, @mess) = @_;
219 if (ref $expected eq 'Regexp') {
220 $pass = $got =~ $expected;
222 unshift(@mess, "# got '$got'\n",
223 "# expected /$expected/\n");
226 $pass = $got =~ /$expected/;
228 unshift(@mess, "# got '$got'\n",
229 "# expected /$expected/\n");
232 _ok($pass, _where(), $name, @mess);
240 _ok(0, _where(), @_);
251 # Note: can't pass multipart messages since we try to
252 # be compatible with Test::More::skip().
255 my $n = @_ ? shift : 1;
257 print STDOUT "ok $test # skip: $why\n";
266 return 0 unless $#$ra == $#$rb;
267 for my $i (0..$#$ra) {
268 return 0 unless $ra->[$i] eq $rb->[$i];
274 my ($orig, $suspect) = @_;
276 while (my ($key, $value) = each %$suspect) {
277 # Force a hash recompute if this perl's internals can cache the hash key.
279 if (exists $orig->{$key}) {
280 if ($orig->{$key} ne $value) {
281 print STDOUT "# key ", _qq($key), " was ", _qq($orig->{$key}),
282 " now ", _qq($value), "\n";
286 print STDOUT "# key ", _qq($key), " is ", _qq($value),
287 ", not in original.\n";
291 foreach (keys %$orig) {
292 # Force a hash recompute if this perl's internals can cache the hash key.
294 next if (exists $suspect->{$_});
295 print STDOUT "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
306 _ok(!$@, _where(), "require $require");
314 _ok(!$@, _where(), "use $use");
317 # runperl - Runs a separate perl interpreter.
319 # switches => [ command-line switches ]
320 # nolib => 1 # don't use -I../lib (included by default)
321 # prog => one-liner (avoid quotes)
322 # progfile => perl script
323 # stdin => string to feed the stdin
324 # stderr => redirect stderr to stdout
325 # args => [ command-line arguments to the perl program ]
326 # verbose => print the command line
328 my $is_mswin = $^O eq 'MSWin32';
329 my $is_netware = $^O eq 'NetWare';
330 my $is_macos = $^O eq 'MacOS';
331 my $is_vms = $^O eq 'VMS';
334 my ($runperl, $args) = @_;
337 # In VMS protect with doublequotes because otherwise
338 # DCL will lowercase -- unless already doublequoted.
339 $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0;
340 $$runperl .= ' ' . $_;
347 if ($args{switches}) {
348 _quote_args(\$runperl, $args{switches});
350 unless ($args{nolib}) {
352 $runperl .= ' -I::lib';
353 # Use UNIX style error messages instead of MPW style.
354 $runperl .= ' -MMac::err=unix' if $args{stderr};
357 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
360 if (defined $args{prog}) {
361 if ($is_mswin || $is_netware || $is_vms) {
362 $runperl .= qq( -e ") . $args{prog} . qq(");
365 $runperl .= qq( -e ') . $args{prog} . qq(');
367 } elsif (defined $args{progfile}) {
368 $runperl .= qq( "$args{progfile}");
370 if (defined $args{stdin}) {
371 # so we don't try to put literal newlines and crs onto the
373 $args{stdin} =~ s/\n/\\n/g;
374 $args{stdin} =~ s/\r/\\r/g;
376 if ($is_mswin || $is_netware || $is_vms) {
377 $runperl = qq{$^X -e "print qq(} .
378 $args{stdin} . q{)" | } . $runperl;
381 $runperl = qq{$^X -e 'print qq(} .
382 $args{stdin} . q{)' | } . $runperl;
385 if (defined $args{args}) {
386 _quote_args(\$runperl, $args{args});
388 $runperl .= ' 2>&1' if $args{stderr} && !$is_macos;
389 $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
390 if ($args{verbose}) {
391 my $runperldisplay = $runperl;
392 $runperldisplay =~ s/\n/\n\#/g;
393 print STDERR "# $runperldisplay\n";
395 my $result = `$runperl`;
396 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
402 print STDERR "# @_\n";
406 # A somewhat safer version of the sometimes wrong $^X.
409 unless (defined $Perl) {
413 eval "require Config; Config->import";
415 warn "test.pl had problems loading Config: $@";
418 $exe = $Config{_exe};
420 $exe = '' unless defined $exe;
422 # This doesn't absolutize the path: beware of future chdirs().
423 # We could do File::Spec->abs2rel() but that does getcwd()s,
424 # which is a bit heavyweight to do here.
426 if ($Perl =~ /^perl\Q$exe\E$/i) {
427 my $perl = "perl$exe";
428 eval "require File::Spec";
430 warn "test.pl had problems loading File::Spec: $@";
433 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
437 # Build up the name of the executable file from the name of
440 if ($Perl !~ /\Q$exe\E$/i) {
444 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
446 # For subcommands to use.
447 $ENV{PERLEXE} = $Perl;
453 foreach my $file (@_) {
454 1 while unlink $file;
455 print STDERR "# Couldn't unlink '$file': $!\n" if -f $file;
460 my $tmpfile = "misctmp000";
461 1 while -f ++$tmpfile;
462 END { unlink_all $tmpfile }
467 # The $resolve must be a subref that tests the first argument
468 # for success, or returns the definition of success (e.g. the
469 # expected scalar) if given no arguments.
473 my($prog, $resolve, $runperl_args, $name) = @_;
475 $runperl_args ||= {};
476 $runperl_args->{progfile} = $tmpfile;
477 $runperl_args->{stderr} = 1;
479 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
483 $prog =~ s#/dev/null#NL:#;
486 $prog =~ s{if \(-e _ and -f _ and -r _\)}
490 print TEST $prog, "\n";
491 close TEST or die "Cannot close $tmpfile: $!";
493 my $results = runperl(%$runperl_args);
496 # Clean up the results into something a bit more predictable.
497 $results =~ s/\n+$//;
498 $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
499 $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
501 # bison says 'parse error' instead of 'syntax error',
502 # various yaccs may or may not capitalize 'syntax'.
503 $results =~ s/^(syntax|parse) error/syntax error/mig;
506 # some tests will trigger VMS messages that won't be expected
507 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
509 # pipes double these sometimes
510 $results =~ s/\n\n/\n/g;
513 my $pass = $resolve->($results);
515 print STDERR "# PROG: $switch\n$prog\n";
516 print STDERR "# EXPECTED:\n", $resolve->(), "\n";
517 print STDERR "# GOT:\n$results\n";
518 print STDERR "# STATUS: $status\n";
521 # Use the first line of the program as a name if none was given
523 ($first_line, $name) = $prog =~ /^((.{1,50}).*)/;
524 $name .= '...' if length $first_line > length $name;
527 _ok($pass, _where(), "fresh_perl - $name");
533 # Combination of run_perl() and is().
537 my($prog, $expected, $runperl_args, $name) = @_;
539 sub { @_ ? $_[0] eq $expected : $expected },
540 $runperl_args, $name);
546 # Combination of run_perl() and like().
549 sub fresh_perl_like {
550 my($prog, $expected, $runperl_args, $name) = @_;
553 $_[0] =~ (ref $expected ? $expected : /$expected/) :
555 $runperl_args, $name);