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";
32 print STDOUT "1..0 - @_\n";
34 print STDOUT "1..0\n";
40 my ($pass, $where, $name, @mess) = @_;
41 # Do not try to microoptimize by factoring out the "not ".
45 # escape out '#' or it will interfere with '# skip' and such
47 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
49 $out = $pass ? "ok $test" : "not ok $test";
52 $out .= " # TODO $TODO" if $TODO;
53 print STDOUT "$out\n";
56 print STDERR "# Failed $where\n";
59 # Ensure that the message is properly escaped.
60 print STDERR map { /^#/ ? "$_\n" : "# $_\n" }
61 map { split /\n/ } @mess if @mess;
69 my @caller = caller(1);
70 return "at $caller[1] line $caller[2]";
74 my ($pass, $name, @mess) = @_;
75 _ok($pass, _where(), $name, @mess);
80 return 'undef' unless defined $x;
89 return defined $x ? '"' . display ($x) . '"' : 'undef';
92 # keys are the codes \n etc map to, values are 2 char strings such as \n
94 foreach my $x (split //, 'nrtfa\\\'"') {
95 $backslash_escape{ord eval "\"\\$x\""} = "\\$x";
97 # A way to display scalars containing control characters and Unicode.
98 # Trying to avoid setting $_, or relying on local $_ to work.
102 if (defined $x and not ref $x) {
104 foreach my $c (unpack("U*", $x)) {
106 $y .= sprintf "\\x{%x}", $c;
107 } elsif ($backslash_escape{$c}) {
108 $y .= $backslash_escape{$c};
110 my $z = chr $c; # Maybe we can get away with a literal...
111 $z = sprintf "\\%03o", $c if $z =~ /[[:^print:]]/;
117 return $x unless wantarray;
124 my ($got, $expected, $name, @mess) = @_;
125 my $pass = $got eq $expected;
127 unshift(@mess, "# got "._q($got)."\n",
128 "# expected "._q($expected)."\n");
130 _ok($pass, _where(), $name, @mess);
134 my ($got, $isnt, $name, @mess) = @_;
135 my $pass = $got ne $isnt;
137 unshift(@mess, "# it should not be "._q($got)."\n",
140 _ok($pass, _where(), $name, @mess);
143 # Note: this isn't quite as fancy as Test::More::like().
145 my ($got, $expected, $name, @mess) = @_;
147 if (ref $expected eq 'Regexp') {
148 $pass = $got =~ $expected;
150 unshift(@mess, "# got '$got'\n",
151 "# expected /$expected/\n");
154 $pass = $got =~ /$expected/;
156 unshift(@mess, "# got '$got'\n",
157 "# expected /$expected/\n");
160 _ok($pass, _where(), $name, @mess);
168 _ok(0, _where(), @_);
179 # Note: can't pass multipart messages since we try to
180 # be compatible with Test::More::skip().
183 my $n = @_ ? shift : 1;
185 print STDOUT "ok $test # skip: $why\n";
194 return 0 unless $#$ra == $#$rb;
195 for my $i (0..$#$ra) {
196 return 0 unless $ra->[$i] eq $rb->[$i];
202 my ($orig, $suspect) = @_;
204 while (my ($key, $value) = each %$suspect) {
205 # Force a hash recompute if this perl's internals can cache the hash key.
207 if (exists $orig->{$key}) {
208 if ($orig->{$key} ne $value) {
209 print STDERR "# key ", _qq($key), " was ", _qq($orig->{$key}),
210 " now ", _qq($value), "\n";
214 print STDERR "# key ", _qq($key), " is ", _qq($value),
215 ", not in original.\n";
219 foreach (keys %$orig) {
220 # Force a hash recompute if this perl's internals can cache the hash key.
222 next if (exists $suspect->{$_});
223 print STDERR "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
234 _ok(!$@, _where(), "require $require");
242 _ok(!$@, _where(), "use $use");
245 # runperl - Runs a separate perl interpreter.
247 # switches => [ command-line switches ]
248 # nolib => 1 # don't use -I../lib (included by default)
249 # prog => one-liner (avoid quotes)
250 # progfile => perl script
251 # stdin => string to feed the stdin
252 # stderr => redirect stderr to stdout
253 # args => [ command-line arguments to the perl program ]
254 # verbose => print the command line
256 my $is_mswin = $^O eq 'MSWin32';
257 my $is_netware = $^O eq 'NetWare';
258 my $is_macos = $^O eq 'MacOS';
259 my $is_vms = $^O eq 'VMS';
262 my ($runperl, $args) = @_;
265 # In VMS protect with doublequotes because otherwise
266 # DCL will lowercase -- unless already doublequoted.
267 $_ = q(").$_.q(") if $is_vms && !/^\"/;
268 $$runperl .= ' ' . $_;
275 if ($args{switches}) {
276 _quote_args(\$runperl, $args{switches});
278 unless ($args{nolib}) {
280 $runperl .= ' -I::lib';
281 # Use UNIX style error messages instead of MPW style.
282 $runperl .= ' -MMac::err=unix' if $args{stderr};
285 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
288 if (defined $args{prog}) {
289 if ($is_mswin || $is_netware || $is_vms) {
290 $runperl .= qq( -e ") . $args{prog} . qq(");
293 $runperl .= qq( -e ') . $args{prog} . qq(');
295 } elsif (defined $args{progfile}) {
296 $runperl .= qq( "$args{progfile}");
298 if (defined $args{stdin}) {
299 # so we don't try to put literal newlines and crs onto the
301 $args{stdin} =~ s/\n/\\n/g;
302 $args{stdin} =~ s/\r/\\r/g;
304 if ($is_mswin || $is_netware || $is_vms) {
305 $runperl = qq{$^X -e "print qq(} .
306 $args{stdin} . q{)" | } . $runperl;
309 $runperl = qq{$^X -e 'print qq(} .
310 $args{stdin} . q{)' | } . $runperl;
313 if (defined $args{args}) {
314 _quote_args(\$runperl, $args{args});
316 $runperl .= ' 2>&1' if $args{stderr} && !$is_macos;
317 $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
318 if ($args{verbose}) {
319 my $runperldisplay = $runperl;
320 $runperldisplay =~ s/\n/\n\#/g;
321 print STDERR "# $runperldisplay\n";
323 my $result = `$runperl`;
324 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
330 print STDERR "# @_\n";
334 # A somewhat safer version of the sometimes wrong $^X.
337 unless (defined $Perl) {
341 eval "require Config; Config->import";
343 warn "test.pl had problems loading Config: $@";
346 $exe = $Config{_exe};
348 $exe = '' unless defined $exe;
350 # This doesn't absolutize the path: beware of future chdirs().
351 # We could do File::Spec->abs2rel() but that does getcwd()s,
352 # which is a bit heavyweight to do here.
354 if ($Perl =~ /^perl\Q$exe\E$/i) {
355 my $perl = "perl$exe";
356 eval "require File::Spec";
358 warn "test.pl had problems loading File::Spec: $@";
361 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
365 # Its like this. stat on Cygwin treats 'perl' to mean 'perl.exe'
366 # but open does not. This can get confusing, so to be safe we
367 # always put the .exe on the end on Cygwin.
368 $Perl .= $exe if $^O eq 'cygwin' && $Perl !~ /\Q$exe\E$/;
370 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
372 # For subcommands to use.
373 $ENV{PERLEXE} = $Perl;
379 foreach my $file (@_) {
380 1 while unlink $file;
381 print STDERR "# Couldn't unlink '$file': $!\n" if -f $file;