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");
153 $pass = $got =~ /$expected/;
155 unshift(@mess, "# got '$got'\n",
156 "# expected /$expected/\n");
159 _ok($pass, _where(), $name, @mess);
167 _ok(0, _where(), @_);
178 # Note: can't pass multipart messages since we try to
179 # be compatible with Test::More::skip().
182 my $n = @_ ? shift : 1;
184 print STDOUT "ok $test # skip: $why\n";
193 return 0 unless $#$ra == $#$rb;
194 for my $i (0..$#$ra) {
195 return 0 unless $ra->[$i] eq $rb->[$i];
201 my ($orig, $suspect) = @_;
203 while (my ($key, $value) = each %$suspect) {
204 # Force a hash recompute if this perl's internals can cache the hash key.
206 if (exists $orig->{$key}) {
207 if ($orig->{$key} ne $value) {
208 print "# key ", _qq($key), " was ", _qq($orig->{$key}),
209 " now ", _qq($value), "\n";
213 print "# key ", _qq($key), " is ", _qq($value), ", not in original.\n";
217 foreach (keys %$orig) {
218 # Force a hash recompute if this perl's internals can cache the hash key.
220 next if (exists $suspect->{$_});
221 print "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
232 _ok(!$@, _where(), "require $require");
240 _ok(!$@, _where(), "use $use");
243 # runperl - Runs a separate perl interpreter.
245 # switches => [ command-line switches ]
246 # nolib => 1 # don't use -I../lib (included by default)
247 # prog => one-liner (avoid quotes)
248 # progfile => perl script
249 # stdin => string to feed the stdin
250 # stderr => redirect stderr to stdout
251 # args => [ command-line arguments to the perl program ]
252 # verbose => print the command line
254 my $is_mswin = $^O eq 'MSWin32';
255 my $is_netware = $^O eq 'NetWare';
256 my $is_macos = $^O eq 'MacOS';
257 my $is_vms = $^O eq 'VMS';
260 my ($runperl, $args) = @_;
263 # In VMS protect with doublequotes because otherwise
264 # DCL will lowercase -- unless already doublequoted.
265 $_ = q(").$_.q(") if $is_vms && !/^\"/;
266 $$runperl .= ' ' . $_;
273 if ($args{switches}) {
274 _quote_args(\$runperl, $args{switches});
276 unless ($args{nolib}) {
278 $runperl .= ' -I::lib';
279 # Use UNIX style error messages instead of MPW style.
280 $runperl .= ' -MMac::err=unix' if $args{stderr};
283 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
286 if (defined $args{prog}) {
287 if ($is_mswin || $is_netware || $is_vms) {
288 $runperl .= qq( -e ") . $args{prog} . qq(");
291 $runperl .= qq( -e ') . $args{prog} . qq(');
293 } elsif (defined $args{progfile}) {
294 $runperl .= qq( "$args{progfile}");
296 if (defined $args{stdin}) {
297 # so we don't try to put literal newlines and crs onto the
299 $args{stdin} =~ s/\n/\\n/g;
300 $args{stdin} =~ s/\r/\\r/g;
302 if ($is_mswin || $is_netware || $is_vms) {
303 $runperl = qq{$^X -e "print qq(} .
304 $args{stdin} . q{)" | } . $runperl;
307 $runperl = qq{$^X -e 'print qq(} .
308 $args{stdin} . q{)' | } . $runperl;
311 if (defined $args{args}) {
312 _quote_args(\$runperl, $args{args});
314 $runperl .= ' 2>&1' if $args{stderr} && !$is_macos;
315 $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
316 if ($args{verbose}) {
317 my $runperldisplay = $runperl;
318 $runperldisplay =~ s/\n/\n\#/g;
319 print STDOUT "# $runperldisplay\n";
321 my $result = `$runperl`;
322 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
328 print STDOUT "Bail out! @_\n";
332 # A somewhat safer version of the sometimes wrong $^X.
335 unless (defined $Perl) {
339 eval "require Config; Config->import";
341 warn "test.pl had problems loading Config: $@";
344 $exe = $Config{_exe};
347 # This doesn't absolutize the path: beware of future chdirs().
348 # We could do File::Spec->abs2rel() but that does getcwd()s,
349 # which is a bit heavyweight to do here.
351 if ($Perl =~ /^perl\Q$exe\E$/i) {
352 my $perl = "perl$exe";
353 eval "require File::Spec";
355 warn "test.pl had problems loading File::Spec: $@";
358 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
362 # Its like this. stat on Cygwin treats 'perl' to mean 'perl.exe'
363 # but open does not. This can get confusing, so to be safe we
364 # always put the .exe on the end on Cygwin.
365 $Perl .= $exe if $^O eq 'cygwin' && $Perl !~ /\Q$exe\E$/;
367 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
369 # For subcommands to use.
370 $ENV{PERLEXE} = $Perl;