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]";
81 my ($pass, $name, @mess) = @_;
82 _ok($pass, _where(), $name, @mess);
87 return 'undef' unless defined $x;
96 return defined $x ? '"' . display ($x) . '"' : 'undef';
99 # keys are the codes \n etc map to, values are 2 char strings such as \n
100 my %backslash_escape;
101 foreach my $x (split //, 'nrtfa\\\'"') {
102 $backslash_escape{ord eval "\"\\$x\""} = "\\$x";
104 # A way to display scalars containing control characters and Unicode.
105 # Trying to avoid setting $_, or relying on local $_ to work.
109 if (defined $x and not ref $x) {
111 foreach my $c (unpack("U*", $x)) {
113 $y .= sprintf "\\x{%x}", $c;
114 } elsif ($backslash_escape{$c}) {
115 $y .= $backslash_escape{$c};
117 my $z = chr $c; # Maybe we can get away with a literal...
118 $z = sprintf "\\%03o", $c if $z =~ /[[:^print:]]/;
124 return $x unless wantarray;
131 my ($got, $expected, $name, @mess) = @_;
132 my $pass = $got eq $expected;
134 unshift(@mess, "# got "._q($got)."\n",
135 "# expected "._q($expected)."\n");
137 _ok($pass, _where(), $name, @mess);
141 my ($got, $isnt, $name, @mess) = @_;
142 my $pass = $got ne $isnt;
144 unshift(@mess, "# it should not be "._q($got)."\n",
147 _ok($pass, _where(), $name, @mess);
150 # Note: this isn't quite as fancy as Test::More::like().
152 my ($got, $expected, $name, @mess) = @_;
154 if (ref $expected eq 'Regexp') {
155 $pass = $got =~ $expected;
157 unshift(@mess, "# got '$got'\n",
158 "# expected /$expected/\n");
161 $pass = $got =~ /$expected/;
163 unshift(@mess, "# got '$got'\n",
164 "# expected /$expected/\n");
167 _ok($pass, _where(), $name, @mess);
175 _ok(0, _where(), @_);
186 # Note: can't pass multipart messages since we try to
187 # be compatible with Test::More::skip().
190 my $n = @_ ? shift : 1;
192 print STDOUT "ok $test # skip: $why\n";
201 return 0 unless $#$ra == $#$rb;
202 for my $i (0..$#$ra) {
203 return 0 unless $ra->[$i] eq $rb->[$i];
209 my ($orig, $suspect) = @_;
211 while (my ($key, $value) = each %$suspect) {
212 # Force a hash recompute if this perl's internals can cache the hash key.
214 if (exists $orig->{$key}) {
215 if ($orig->{$key} ne $value) {
216 print STDOUT "# key ", _qq($key), " was ", _qq($orig->{$key}),
217 " now ", _qq($value), "\n";
221 print STDOUT "# key ", _qq($key), " is ", _qq($value),
222 ", not in original.\n";
226 foreach (keys %$orig) {
227 # Force a hash recompute if this perl's internals can cache the hash key.
229 next if (exists $suspect->{$_});
230 print STDOUT "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
241 _ok(!$@, _where(), "require $require");
249 _ok(!$@, _where(), "use $use");
252 # runperl - Runs a separate perl interpreter.
254 # switches => [ command-line switches ]
255 # nolib => 1 # don't use -I../lib (included by default)
256 # prog => one-liner (avoid quotes)
257 # progfile => perl script
258 # stdin => string to feed the stdin
259 # stderr => redirect stderr to stdout
260 # args => [ command-line arguments to the perl program ]
261 # verbose => print the command line
263 my $is_mswin = $^O eq 'MSWin32';
264 my $is_netware = $^O eq 'NetWare';
265 my $is_macos = $^O eq 'MacOS';
266 my $is_vms = $^O eq 'VMS';
269 my ($runperl, $args) = @_;
272 # In VMS protect with doublequotes because otherwise
273 # DCL will lowercase -- unless already doublequoted.
274 $_ = q(").$_.q(") if $is_vms && !/^\"/;
275 $$runperl .= ' ' . $_;
282 if ($args{switches}) {
283 _quote_args(\$runperl, $args{switches});
285 unless ($args{nolib}) {
287 $runperl .= ' -I::lib';
288 # Use UNIX style error messages instead of MPW style.
289 $runperl .= ' -MMac::err=unix' if $args{stderr};
292 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
295 if (defined $args{prog}) {
296 if ($is_mswin || $is_netware || $is_vms) {
297 $runperl .= qq( -e ") . $args{prog} . qq(");
300 $runperl .= qq( -e ') . $args{prog} . qq(');
302 } elsif (defined $args{progfile}) {
303 $runperl .= qq( "$args{progfile}");
305 if (defined $args{stdin}) {
306 # so we don't try to put literal newlines and crs onto the
308 $args{stdin} =~ s/\n/\\n/g;
309 $args{stdin} =~ s/\r/\\r/g;
311 if ($is_mswin || $is_netware || $is_vms) {
312 $runperl = qq{$^X -e "print qq(} .
313 $args{stdin} . q{)" | } . $runperl;
316 $runperl = qq{$^X -e 'print qq(} .
317 $args{stdin} . q{)' | } . $runperl;
320 if (defined $args{args}) {
321 _quote_args(\$runperl, $args{args});
323 $runperl .= ' 2>&1' if $args{stderr} && !$is_macos;
324 $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
325 if ($args{verbose}) {
326 my $runperldisplay = $runperl;
327 $runperldisplay =~ s/\n/\n\#/g;
328 print STDERR "# $runperldisplay\n";
330 my $result = `$runperl`;
331 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
337 print STDERR "# @_\n";
341 # A somewhat safer version of the sometimes wrong $^X.
344 unless (defined $Perl) {
348 eval "require Config; Config->import";
350 warn "test.pl had problems loading Config: $@";
353 $exe = $Config{_exe};
355 $exe = '' unless defined $exe;
357 # This doesn't absolutize the path: beware of future chdirs().
358 # We could do File::Spec->abs2rel() but that does getcwd()s,
359 # which is a bit heavyweight to do here.
361 if ($Perl =~ /^perl\Q$exe\E$/i) {
362 my $perl = "perl$exe";
363 eval "require File::Spec";
365 warn "test.pl had problems loading File::Spec: $@";
368 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
372 # Its like this. stat on Cygwin treats 'perl' to mean 'perl.exe'
373 # but open does not. This can get confusing, so to be safe we
374 # always put the .exe on the end on Cygwin.
375 $Perl .= $exe if $^O eq 'cygwin' && $Perl !~ /\Q$exe\E$/;
377 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
379 # For subcommands to use.
380 $ENV{PERLEXE} = $Perl;
386 foreach my $file (@_) {
387 1 while unlink $file;
388 print STDERR "# Couldn't unlink '$file': $!\n" if -f $file;