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 STDOUT "# 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 STDOUT "# Failed $where\n";
59 # Ensure that the message is properly escaped.
60 print STDOUT 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 "# key ", _qq($key), " was ", _qq($orig->{$key}),
210 " now ", _qq($value), "\n";
214 print "# key ", _qq($key), " is ", _qq($value), ", not in original.\n";
218 foreach (keys %$orig) {
219 # Force a hash recompute if this perl's internals can cache the hash key.
221 next if (exists $suspect->{$_});
222 print "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
233 _ok(!$@, _where(), "require $require");
241 _ok(!$@, _where(), "use $use");
244 # runperl - Runs a separate perl interpreter.
246 # switches => [ command-line switches ]
247 # nolib => 1 # don't use -I../lib (included by default)
248 # prog => one-liner (avoid quotes)
249 # progfile => perl script
250 # stdin => string to feed the stdin
251 # stderr => redirect stderr to stdout
252 # args => [ command-line arguments to the perl program ]
253 # verbose => print the command line
255 my $is_mswin = $^O eq 'MSWin32';
256 my $is_netware = $^O eq 'NetWare';
257 my $is_macos = $^O eq 'MacOS';
258 my $is_vms = $^O eq 'VMS';
261 my ($runperl, $args) = @_;
264 # In VMS protect with doublequotes because otherwise
265 # DCL will lowercase -- unless already doublequoted.
266 $_ = q(").$_.q(") if $is_vms && !/^\"/;
267 $$runperl .= ' ' . $_;
274 if ($args{switches}) {
275 _quote_args(\$runperl, $args{switches});
277 unless ($args{nolib}) {
279 $runperl .= ' -I::lib';
280 # Use UNIX style error messages instead of MPW style.
281 $runperl .= ' -MMac::err=unix' if $args{stderr};
284 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
287 if (defined $args{prog}) {
288 if ($is_mswin || $is_netware || $is_vms) {
289 $runperl .= qq( -e ") . $args{prog} . qq(");
292 $runperl .= qq( -e ') . $args{prog} . qq(');
294 } elsif (defined $args{progfile}) {
295 $runperl .= qq( "$args{progfile}");
297 if (defined $args{stdin}) {
298 # so we don't try to put literal newlines and crs onto the
300 $args{stdin} =~ s/\n/\\n/g;
301 $args{stdin} =~ s/\r/\\r/g;
303 if ($is_mswin || $is_netware || $is_vms) {
304 $runperl = qq{$^X -e "print qq(} .
305 $args{stdin} . q{)" | } . $runperl;
308 $runperl = qq{$^X -e 'print qq(} .
309 $args{stdin} . q{)' | } . $runperl;
312 if (defined $args{args}) {
313 _quote_args(\$runperl, $args{args});
315 $runperl .= ' 2>&1' if $args{stderr} && !$is_macos;
316 $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
317 if ($args{verbose}) {
318 my $runperldisplay = $runperl;
319 $runperldisplay =~ s/\n/\n\#/g;
320 print STDOUT "# $runperldisplay\n";
322 my $result = `$runperl`;
323 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
329 print STDOUT "# @_\n";
333 # A somewhat safer version of the sometimes wrong $^X.
336 unless (defined $Perl) {
340 eval "require Config; Config->import";
342 warn "test.pl had problems loading Config: $@";
345 $exe = $Config{_exe};
347 $exe = '' unless defined $exe;
349 # This doesn't absolutize the path: beware of future chdirs().
350 # We could do File::Spec->abs2rel() but that does getcwd()s,
351 # which is a bit heavyweight to do here.
353 if ($Perl =~ /^perl\Q$exe\E$/i) {
354 my $perl = "perl$exe";
355 eval "require File::Spec";
357 warn "test.pl had problems loading File::Spec: $@";
360 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
364 # Its like this. stat on Cygwin treats 'perl' to mean 'perl.exe'
365 # but open does not. This can get confusing, so to be safe we
366 # always put the .exe on the end on Cygwin.
367 $Perl .= $exe if $^O eq 'cygwin' && $Perl !~ /\Q$exe\E$/;
369 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
371 # For subcommands to use.
372 $ENV{PERLEXE} = $Perl;
378 foreach my $file (@_) {
379 1 while unlink $file;
380 print "# Couldn't unlink '$file': $!\n" if -f $file;