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 - @_\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) = @_;
136 my $pass = $got eq $expected;
138 unshift(@mess, "# got "._q($got)."\n",
139 "# expected "._q($expected)."\n");
141 _ok($pass, _where(), $name, @mess);
145 my ($got, $isnt, $name, @mess) = @_;
146 my $pass = $got ne $isnt;
148 unshift(@mess, "# it should not be "._q($got)."\n",
151 _ok($pass, _where(), $name, @mess);
155 my($got, $type, $expected, $name, @mess) = @_;
160 local($@,$!); # don't interfere with $@
161 # eval() sometimes resets $!
162 $pass = eval "\$got $type \$expected";
165 # It seems Irix long doubles can have 2147483648 and 2147483648
166 # that stringify to the same thing but are acutally numerically
167 # different. Display the numbers if $type isn't a string operator,
168 # and the numbers are stringwise the same.
169 # (all string operators have alphabetic names, so tr/a-z// is true)
170 # This will also show numbers for some uneeded cases, but will
171 # definately be helpful for things such as == and <= that fail
172 if ($got eq $expected and $type !~ tr/a-z//) {
173 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
175 unshift(@mess, "# got "._q($got)."\n",
176 "# expected $type "._q($expected)."\n");
178 _ok($pass, _where(), $name, @mess);
181 # Check that $got is within $range of $expected
182 # if $range is 0, then check it's exact
183 # else if $expected is 0, then $range is an absolute value
184 # otherwise $range is a fractional error.
185 # Here $range must be numeric, >= 0
186 # Non numeric ranges might be a useful future extension. (eg %)
188 my ($got, $expected, $range, $name, @mess) = @_;
190 if (!defined $got or !defined $expected or !defined $range) {
191 # This is a fail, but doesn't need extra diagnostics
192 } elsif ($got !~ tr/0-9// or $expected !~ tr/0-9// or $range !~ tr/0-9//) {
194 unshift @mess, "# got, expected and range must be numeric\n";
195 } elsif ($range < 0) {
196 # This is also a fail
197 unshift @mess, "# range must not be negative\n";
198 } elsif ($range == 0) {
200 $pass = $got == $expected;
201 } elsif ($expected == 0) {
202 # If expected is 0, treat range as absolute
203 $pass = ($got <= $range) && ($got >= - $range);
205 my $diff = $got - $expected;
206 $pass = abs ($diff / $expected) < $range;
209 if ($got eq $expected) {
210 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
212 unshift@mess, "# got "._q($got)."\n",
213 "# expected "._q($expected)." (within "._q($range).")\n";
215 _ok($pass, _where(), $name, @mess);
218 # Note: this isn't quite as fancy as Test::More::like().
220 my ($got, $expected, $name, @mess) = @_;
222 if (ref $expected eq 'Regexp') {
223 $pass = $got =~ $expected;
225 unshift(@mess, "# got '$got'\n",
226 "# expected /$expected/\n");
229 $pass = $got =~ /$expected/;
231 unshift(@mess, "# got '$got'\n",
232 "# expected /$expected/\n");
235 _ok($pass, _where(), $name, @mess);
243 _ok(0, _where(), @_);
255 # Note: can't pass multipart messages since we try to
256 # be compatible with Test::More::skip().
259 my $n = @_ ? shift : 1;
261 print STDOUT "ok $test # skip: $why\n";
270 return 0 unless $#$ra == $#$rb;
271 for my $i (0..$#$ra) {
272 return 0 unless $ra->[$i] eq $rb->[$i];
278 my ($orig, $suspect) = @_;
280 while (my ($key, $value) = each %$suspect) {
281 # Force a hash recompute if this perl's internals can cache the hash key.
283 if (exists $orig->{$key}) {
284 if ($orig->{$key} ne $value) {
285 print STDOUT "# key ", _qq($key), " was ", _qq($orig->{$key}),
286 " now ", _qq($value), "\n";
290 print STDOUT "# key ", _qq($key), " is ", _qq($value),
291 ", not in original.\n";
295 foreach (keys %$orig) {
296 # Force a hash recompute if this perl's internals can cache the hash key.
298 next if (exists $suspect->{$_});
299 print STDOUT "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
310 _ok(!$@, _where(), "require $require");
318 _ok(!$@, _where(), "use $use");
321 # runperl - Runs a separate perl interpreter.
323 # switches => [ command-line switches ]
324 # nolib => 1 # don't use -I../lib (included by default)
325 # prog => one-liner (avoid quotes)
326 # progfile => perl script
327 # stdin => string to feed the stdin
328 # stderr => redirect stderr to stdout
329 # args => [ command-line arguments to the perl program ]
330 # verbose => print the command line
332 my $is_mswin = $^O eq 'MSWin32';
333 my $is_netware = $^O eq 'NetWare';
334 my $is_macos = $^O eq 'MacOS';
335 my $is_vms = $^O eq 'VMS';
338 my ($runperl, $args) = @_;
341 # In VMS protect with doublequotes because otherwise
342 # DCL will lowercase -- unless already doublequoted.
343 $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0;
344 $$runperl .= ' ' . $_;
351 if ($args{switches}) {
352 _quote_args(\$runperl, $args{switches});
354 unless ($args{nolib}) {
356 $runperl .= ' -I::lib';
357 # Use UNIX style error messages instead of MPW style.
358 $runperl .= ' -MMac::err=unix' if $args{stderr};
361 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
364 if (defined $args{prog}) {
365 if ($is_mswin || $is_netware || $is_vms) {
366 $runperl .= qq( -e ") . $args{prog} . qq(");
369 $runperl .= qq( -e ') . $args{prog} . qq(');
371 } elsif (defined $args{progfile}) {
372 $runperl .= qq( "$args{progfile}");
374 if (defined $args{stdin}) {
375 # so we don't try to put literal newlines and crs onto the
377 $args{stdin} =~ s/\n/\\n/g;
378 $args{stdin} =~ s/\r/\\r/g;
380 if ($is_mswin || $is_netware || $is_vms) {
381 $runperl = qq{$^X -e "print qq(} .
382 $args{stdin} . q{)" | } . $runperl;
385 $runperl = qq{$^X -e 'print qq(} .
386 $args{stdin} . q{)' | } . $runperl;
389 if (defined $args{args}) {
390 _quote_args(\$runperl, $args{args});
392 $runperl .= ' 2>&1' if $args{stderr} && !$is_macos;
393 $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
394 if ($args{verbose}) {
395 my $runperldisplay = $runperl;
396 $runperldisplay =~ s/\n/\n\#/g;
397 print STDERR "# $runperldisplay\n";
399 my $result = `$runperl`;
400 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
404 *run_perl = \&runperl; # Nice alias.
407 print STDERR "# @_\n";
411 # A somewhat safer version of the sometimes wrong $^X.
414 unless (defined $Perl) {
418 eval "require Config; Config->import";
420 warn "test.pl had problems loading Config: $@";
423 $exe = $Config{_exe};
425 $exe = '' unless defined $exe;
427 # This doesn't absolutize the path: beware of future chdirs().
428 # We could do File::Spec->abs2rel() but that does getcwd()s,
429 # which is a bit heavyweight to do here.
431 if ($Perl =~ /^perl\Q$exe\E$/i) {
432 my $perl = "perl$exe";
433 eval "require File::Spec";
435 warn "test.pl had problems loading File::Spec: $@";
438 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
442 # Build up the name of the executable file from the name of
445 if ($Perl !~ /\Q$exe\E$/i) {
449 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
451 # For subcommands to use.
452 $ENV{PERLEXE} = $Perl;
458 foreach my $file (@_) {
459 1 while unlink $file;
460 print STDERR "# Couldn't unlink '$file': $!\n" if -f $file;
465 my $tmpfile = "misctmp000";
466 1 while -f ++$tmpfile;
467 END { unlink_all $tmpfile }
472 # The $resolve must be a subref that tests the first argument
473 # for success, or returns the definition of success (e.g. the
474 # expected scalar) if given no arguments.
478 my($prog, $resolve, $runperl_args, $name) = @_;
480 $runperl_args ||= {};
481 $runperl_args->{progfile} = $tmpfile;
482 $runperl_args->{stderr} = 1;
484 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
488 $prog =~ s#/dev/null#NL:#;
491 $prog =~ s{if \(-e _ and -f _ and -r _\)}
495 print TEST $prog, "\n";
496 close TEST or die "Cannot close $tmpfile: $!";
498 my $results = runperl(%$runperl_args);
501 # Clean up the results into something a bit more predictable.
502 $results =~ s/\n+$//;
503 $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
504 $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
506 # bison says 'parse error' instead of 'syntax error',
507 # various yaccs may or may not capitalize 'syntax'.
508 $results =~ s/^(syntax|parse) error/syntax error/mig;
511 # some tests will trigger VMS messages that won't be expected
512 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
514 # pipes double these sometimes
515 $results =~ s/\n\n/\n/g;
518 my $pass = $resolve->($results);
520 _diag "# PROG: \n$prog\n";
521 _diag "# EXPECTED:\n", $resolve->(), "\n";
522 _diag "# GOT:\n$results\n";
523 _diag "# STATUS: $status\n";
526 # Use the first line of the program as a name if none was given
528 ($first_line, $name) = $prog =~ /^((.{1,50}).*)/;
529 $name .= '...' if length $first_line > length $name;
532 _ok($pass, _where(), "fresh_perl - $name");
538 # Combination of run_perl() and is().
542 my($prog, $expected, $runperl_args, $name) = @_;
544 sub { @_ ? $_[0] eq $expected : $expected },
545 $runperl_args, $name);
551 # Combination of run_perl() and like().
554 sub fresh_perl_like {
555 my($prog, $expected, $runperl_args, $name) = @_;
558 $_[0] =~ (ref $expected ? $expected : /$expected/) :
560 $runperl_args, $name);