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);
151 # Note: this isn't quite as fancy as Test::More::like().
153 my ($got, $expected, $name, @mess) = @_;
155 if (ref $expected eq 'Regexp') {
156 $pass = $got =~ $expected;
158 unshift(@mess, "# got '$got'\n",
159 "# expected /$expected/\n");
162 $pass = $got =~ /$expected/;
164 unshift(@mess, "# got '$got'\n",
165 "# expected /$expected/\n");
168 _ok($pass, _where(), $name, @mess);
176 _ok(0, _where(), @_);
187 # Note: can't pass multipart messages since we try to
188 # be compatible with Test::More::skip().
191 my $n = @_ ? shift : 1;
193 print STDOUT "ok $test # skip: $why\n";
202 return 0 unless $#$ra == $#$rb;
203 for my $i (0..$#$ra) {
204 return 0 unless $ra->[$i] eq $rb->[$i];
210 my ($orig, $suspect) = @_;
212 while (my ($key, $value) = each %$suspect) {
213 # Force a hash recompute if this perl's internals can cache the hash key.
215 if (exists $orig->{$key}) {
216 if ($orig->{$key} ne $value) {
217 print STDOUT "# key ", _qq($key), " was ", _qq($orig->{$key}),
218 " now ", _qq($value), "\n";
222 print STDOUT "# key ", _qq($key), " is ", _qq($value),
223 ", not in original.\n";
227 foreach (keys %$orig) {
228 # Force a hash recompute if this perl's internals can cache the hash key.
230 next if (exists $suspect->{$_});
231 print STDOUT "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
242 _ok(!$@, _where(), "require $require");
250 _ok(!$@, _where(), "use $use");
253 # runperl - Runs a separate perl interpreter.
255 # switches => [ command-line switches ]
256 # nolib => 1 # don't use -I../lib (included by default)
257 # prog => one-liner (avoid quotes)
258 # progfile => perl script
259 # stdin => string to feed the stdin
260 # stderr => redirect stderr to stdout
261 # args => [ command-line arguments to the perl program ]
262 # verbose => print the command line
264 my $is_mswin = $^O eq 'MSWin32';
265 my $is_netware = $^O eq 'NetWare';
266 my $is_macos = $^O eq 'MacOS';
267 my $is_vms = $^O eq 'VMS';
270 my ($runperl, $args) = @_;
273 # In VMS protect with doublequotes because otherwise
274 # DCL will lowercase -- unless already doublequoted.
275 $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0;
276 $$runperl .= ' ' . $_;
283 if ($args{switches}) {
284 _quote_args(\$runperl, $args{switches});
286 unless ($args{nolib}) {
288 $runperl .= ' -I::lib';
289 # Use UNIX style error messages instead of MPW style.
290 $runperl .= ' -MMac::err=unix' if $args{stderr};
293 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
296 if (defined $args{prog}) {
297 if ($is_mswin || $is_netware || $is_vms) {
298 $runperl .= qq( -e ") . $args{prog} . qq(");
301 $runperl .= qq( -e ') . $args{prog} . qq(');
303 } elsif (defined $args{progfile}) {
304 $runperl .= qq( "$args{progfile}");
306 if (defined $args{stdin}) {
307 # so we don't try to put literal newlines and crs onto the
309 $args{stdin} =~ s/\n/\\n/g;
310 $args{stdin} =~ s/\r/\\r/g;
312 if ($is_mswin || $is_netware || $is_vms) {
313 $runperl = qq{$^X -e "print qq(} .
314 $args{stdin} . q{)" | } . $runperl;
317 $runperl = qq{$^X -e 'print qq(} .
318 $args{stdin} . q{)' | } . $runperl;
321 if (defined $args{args}) {
322 _quote_args(\$runperl, $args{args});
324 $runperl .= ' 2>&1' if $args{stderr} && !$is_macos;
325 $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
326 if ($args{verbose}) {
327 my $runperldisplay = $runperl;
328 $runperldisplay =~ s/\n/\n\#/g;
329 print STDERR "# $runperldisplay\n";
331 my $result = `$runperl`;
332 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
338 print STDERR "# @_\n";
342 # A somewhat safer version of the sometimes wrong $^X.
345 unless (defined $Perl) {
349 eval "require Config; Config->import";
351 warn "test.pl had problems loading Config: $@";
354 $exe = $Config{_exe};
356 $exe = '' unless defined $exe;
358 # This doesn't absolutize the path: beware of future chdirs().
359 # We could do File::Spec->abs2rel() but that does getcwd()s,
360 # which is a bit heavyweight to do here.
362 if ($Perl =~ /^perl\Q$exe\E$/i) {
363 my $perl = "perl$exe";
364 eval "require File::Spec";
366 warn "test.pl had problems loading File::Spec: $@";
369 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
373 # Build up the name of the executable file from the name of
376 if ($Perl !~ /\Q$exe\E$/i) {
380 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
382 # For subcommands to use.
383 $ENV{PERLEXE} = $Perl;
389 foreach my $file (@_) {
390 1 while unlink $file;
391 print STDERR "# Couldn't unlink '$file': $!\n" if -f $file;
396 my $tmpfile = "misctmp000";
397 1 while -f ++$tmpfile;
398 END { unlink_all $tmpfile }
403 # The $resolve must be a subref that tests the first argument
404 # for success, or returns the definition of success (e.g. the
405 # expected scalar) if given no arguments.
409 my($prog, $resolve, $runperl_args, $name) = @_;
411 $runperl_args ||= {};
412 $runperl_args->{progfile} = $tmpfile;
413 $runperl_args->{stderr} = 1;
415 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
419 $prog =~ s#/dev/null#NL:#;
422 $prog =~ s{if \(-e _ and -f _ and -r _\)}
426 print TEST $prog, "\n";
427 close TEST or die "Cannot close $tmpfile: $!";
429 my $results = runperl(%$runperl_args);
432 # Clean up the results into something a bit more predictable.
433 $results =~ s/\n+$//;
434 $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
435 $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
437 # bison says 'parse error' instead of 'syntax error',
438 # various yaccs may or may not capitalize 'syntax'.
439 $results =~ s/^(syntax|parse) error/syntax error/mig;
442 # some tests will trigger VMS messages that won't be expected
443 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
445 # pipes double these sometimes
446 $results =~ s/\n\n/\n/g;
449 my $pass = $resolve->($results);
451 print STDERR "# PROG: $switch\n$prog\n";
452 print STDERR "# EXPECTED:\n", $resolve->(), "\n";
453 print STDERR "# GOT:\n$results\n";
454 print STDERR "# STATUS: $status\n";
457 # Use the first line of the program as a name if none was given
459 ($first_line, $name) = $prog =~ /^((.{1,50}).*)/;
460 $name .= '...' if length $first_line > length $name;
463 _ok($pass, _where(), "fresh_perl - $name");
469 # Combination of run_perl() and is().
473 my($prog, $expected, $runperl_args, $name) = @_;
475 sub { @_ ? $_[0] eq $expected : $expected },
476 $runperl_args, $name);
482 # Combination of run_perl() and like().
485 sub fresh_perl_like {
486 my($prog, $expected, $runperl_args, $name) = @_;
489 $_[0] =~ (ref $expected ? $expected : /$expected/) :
491 $runperl_args, $name);