Re: Nearly OK for cygwin@15318
[p5sagit/p5-mst-13.2.git] / t / test.pl
CommitLineData
69026470 1#
2# t/test.pl - most of Test::More functionality without the fuss
3#
4
5my $test = 1;
6my $planned;
7
7d932aad 8$TODO = 0;
b6345914 9$NO_ENDING = 0;
7d932aad 10
69026470 11sub plan {
12 my $n;
13 if (@_ == 1) {
14 $n = shift;
15 } else {
16 my %plan = @_;
17 $n = $plan{tests};
18 }
ad20d923 19 print STDOUT "1..$n\n";
69026470 20 $planned = $n;
21}
22
23END {
24 my $ran = $test - 1;
b6345914 25 if (!$NO_ENDING && defined $planned && $planned != $ran) {
75385f53 26 print STDERR "# Looks like you planned $planned tests but ran $ran.\n";
69026470 27 }
28}
29
de522f7a 30# Use this instead of "print STDERR" when outputing failure diagnostic
31# messages
32sub _diag {
33 my $fh = $TODO ? *STDOUT : *STDERR;
34 print $fh @_;
35}
36
69026470 37sub skip_all {
38 if (@_) {
ad20d923 39 print STDOUT "1..0 - @_\n";
69026470 40 } else {
ad20d923 41 print STDOUT "1..0\n";
69026470 42 }
43 exit(0);
44}
45
46sub _ok {
7d932aad 47 my ($pass, $where, $name, @mess) = @_;
69026470 48 # Do not try to microoptimize by factoring out the "not ".
49 # VMS will avenge.
7d932aad 50 my $out;
51 if ($name) {
b734d6c9 52 # escape out '#' or it will interfere with '# skip' and such
53 $name =~ s/#/\\#/g;
7d932aad 54 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
69026470 55 } else {
7d932aad 56 $out = $pass ? "ok $test" : "not ok $test";
69026470 57 }
7d932aad 58
59 $out .= " # TODO $TODO" if $TODO;
ad20d923 60 print STDOUT "$out\n";
7d932aad 61
69026470 62 unless ($pass) {
de522f7a 63 _diag "# Failed $where\n";
69026470 64 }
7d932aad 65
66 # Ensure that the message is properly escaped.
de522f7a 67 _diag map { /^#/ ? "$_\n" : "# $_\n" }
68 map { split /\n/ } @mess if @mess;
7d932aad 69
69026470 70 $test++;
1577bb16 71
72 return $pass;
69026470 73}
74
75sub _where {
76 my @caller = caller(1);
77 return "at $caller[1] line $caller[2]";
78}
79
1d662fb6 80# DON'T use this for matches. Use like() instead.
69026470 81sub ok {
7d932aad 82 my ($pass, $name, @mess) = @_;
83 _ok($pass, _where(), $name, @mess);
69026470 84}
85
b3c72391 86sub _q {
87 my $x = shift;
88 return 'undef' unless defined $x;
89 my $q = $x;
677fb045 90 $q =~ s/\\/\\\\/;
b3c72391 91 $q =~ s/'/\\'/;
92 return "'$q'";
93}
94
677fb045 95sub _qq {
96 my $x = shift;
97 return defined $x ? '"' . display ($x) . '"' : 'undef';
98};
99
100# keys are the codes \n etc map to, values are 2 char strings such as \n
101my %backslash_escape;
102foreach my $x (split //, 'nrtfa\\\'"') {
103 $backslash_escape{ord eval "\"\\$x\""} = "\\$x";
104}
105# A way to display scalars containing control characters and Unicode.
106# Trying to avoid setting $_, or relying on local $_ to work.
107sub display {
108 my @result;
109 foreach my $x (@_) {
110 if (defined $x and not ref $x) {
111 my $y = '';
112 foreach my $c (unpack("U*", $x)) {
113 if ($c > 255) {
114 $y .= sprintf "\\x{%x}", $c;
115 } elsif ($backslash_escape{$c}) {
116 $y .= $backslash_escape{$c};
117 } else {
118 my $z = chr $c; # Maybe we can get away with a literal...
119 $z = sprintf "\\%03o", $c if $z =~ /[[:^print:]]/;
120 $y .= $z;
121 }
122 }
123 $x = $y;
124 }
125 return $x unless wantarray;
126 push @result, $x;
127 }
128 return @result;
129}
130
69026470 131sub is {
7d932aad 132 my ($got, $expected, $name, @mess) = @_;
69026470 133 my $pass = $got eq $expected;
134 unless ($pass) {
b3c72391 135 unshift(@mess, "# got "._q($got)."\n",
136 "# expected "._q($expected)."\n");
69026470 137 }
7d932aad 138 _ok($pass, _where(), $name, @mess);
69026470 139}
140
3e90d5a3 141sub isnt {
142 my ($got, $isnt, $name, @mess) = @_;
143 my $pass = $got ne $isnt;
144 unless( $pass ) {
b3c72391 145 unshift(@mess, "# it should not be "._q($got)."\n",
3e90d5a3 146 "# but it is.\n");
147 }
148 _ok($pass, _where(), $name, @mess);
149}
150
58d76dfd 151sub cmp_ok {
152 my($got, $type, $expected, $name, @mess) = @_;
153
154 my $pass;
155 {
156 local $^W = 0;
157 local($@,$!); # don't interfere with $@
158 # eval() sometimes resets $!
159 $pass = eval "\$got $type \$expected";
160 }
161 unless ($pass) {
162 # It seems Irix long doubles can have 2147483648 and 2147483648
163 # that stringify to the same thing but are acutally numerically
164 # different. Display the numbers if $type isn't a string operator,
165 # and the numbers are stringwise the same.
166 # (all string operators have alphabetic names, so tr/a-z// is true)
167 # This will also show numbers for some uneeded cases, but will
168 # definately be helpful for things such as == and <= that fail
169 if ($got eq $expected and $type !~ tr/a-z//) {
170 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
171 }
172 unshift(@mess, "# got "._q($got)."\n",
173 "# expected $type "._q($expected)."\n");
174 }
175 _ok($pass, _where(), $name, @mess);
176}
177
178# Check that $got is within $range of $expected
179# if $range is 0, then check it's exact
180# else if $expected is 0, then $range is an absolute value
181# otherwise $range is a fractional error.
182# Here $range must be numeric, >= 0
183# Non numeric ranges might be a useful future extension. (eg %)
184sub within {
185 my ($got, $expected, $range, $name, @mess) = @_;
186 my $pass;
187 if (!defined $got or !defined $expected or !defined $range) {
188 # This is a fail, but doesn't need extra diagnostics
189 } elsif ($got !~ tr/0-9// or $expected !~ tr/0-9// or $range !~ tr/0-9//) {
190 # This is a fail
191 unshift @mess, "# got, expected and range must be numeric\n";
192 } elsif ($range < 0) {
193 # This is also a fail
194 unshift @mess, "# range must not be negative\n";
195 } elsif ($range == 0) {
196 # Within 0 is ==
197 $pass = $got == $expected;
198 } elsif ($expected == 0) {
199 # If expected is 0, treat range as absolute
200 $pass = ($got <= $range) && ($got >= - $range);
201 } else {
202 my $diff = $got - $expected;
203 $pass = abs ($diff / $expected) < $range;
204 }
205 unless ($pass) {
206 if ($got eq $expected) {
207 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
208 }
209 unshift@mess, "# got "._q($got)."\n",
210 "# expected "._q($expected)." (within "._q($range).")\n";
211 }
212 _ok($pass, _where(), $name, @mess);
213}
214
69026470 215# Note: this isn't quite as fancy as Test::More::like().
216sub like {
7d932aad 217 my ($got, $expected, $name, @mess) = @_;
69026470 218 my $pass;
219 if (ref $expected eq 'Regexp') {
220 $pass = $got =~ $expected;
221 unless ($pass) {
435e7af6 222 unshift(@mess, "# got '$got'\n",
223 "# expected /$expected/\n");
69026470 224 }
225 } else {
226 $pass = $got =~ /$expected/;
227 unless ($pass) {
7d932aad 228 unshift(@mess, "# got '$got'\n",
229 "# expected /$expected/\n");
69026470 230 }
231 }
7d932aad 232 _ok($pass, _where(), $name, @mess);
69026470 233}
234
235sub pass {
236 _ok(1, '', @_);
237}
238
239sub fail {
240 _ok(0, _where(), @_);
241}
242
ad20d923 243sub curr_test {
244 return $test;
245}
246
3e90d5a3 247sub next_test {
248 $test++
249}
250
69026470 251# Note: can't pass multipart messages since we try to
252# be compatible with Test::More::skip().
253sub skip {
7d932aad 254 my $why = shift;
982b7cb7 255 my $n = @_ ? shift : 1;
69026470 256 for (1..$n) {
ad20d923 257 print STDOUT "ok $test # skip: $why\n";
e6c299c8 258 $test++;
69026470 259 }
260 local $^W = 0;
261 last SKIP;
262}
263
264sub eq_array {
265 my ($ra, $rb) = @_;
266 return 0 unless $#$ra == $#$rb;
267 for my $i (0..$#$ra) {
268 return 0 unless $ra->[$i] eq $rb->[$i];
269 }
270 return 1;
271}
272
677fb045 273sub eq_hash {
274 my ($orig, $suspect) = @_;
275 my $fail;
276 while (my ($key, $value) = each %$suspect) {
277 # Force a hash recompute if this perl's internals can cache the hash key.
278 $key = "" . $key;
279 if (exists $orig->{$key}) {
280 if ($orig->{$key} ne $value) {
de522f7a 281 print STDOUT "# key ", _qq($key), " was ", _qq($orig->{$key}),
282 " now ", _qq($value), "\n";
677fb045 283 $fail = 1;
284 }
285 } else {
de522f7a 286 print STDOUT "# key ", _qq($key), " is ", _qq($value),
75385f53 287 ", not in original.\n";
677fb045 288 $fail = 1;
289 }
290 }
291 foreach (keys %$orig) {
292 # Force a hash recompute if this perl's internals can cache the hash key.
293 $_ = "" . $_;
294 next if (exists $suspect->{$_});
de522f7a 295 print STDOUT "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
677fb045 296 $fail = 1;
297 }
298 !$fail;
299}
300
69026470 301sub require_ok {
302 my ($require) = @_;
303 eval <<REQUIRE_OK;
304require $require;
305REQUIRE_OK
1577bb16 306 _ok(!$@, _where(), "require $require");
69026470 307}
308
309sub use_ok {
310 my ($use) = @_;
311 eval <<USE_OK;
312use $use;
313USE_OK
1577bb16 314 _ok(!$@, _where(), "use $use");
69026470 315}
316
137352a2 317# runperl - Runs a separate perl interpreter.
318# Arguments :
319# switches => [ command-line switches ]
320# nolib => 1 # don't use -I../lib (included by default)
321# prog => one-liner (avoid quotes)
322# progfile => perl script
323# stdin => string to feed the stdin
324# stderr => redirect stderr to stdout
325# args => [ command-line arguments to the perl program ]
cb9c5e20 326# verbose => print the command line
137352a2 327
328my $is_mswin = $^O eq 'MSWin32';
329my $is_netware = $^O eq 'NetWare';
330my $is_macos = $^O eq 'MacOS';
331my $is_vms = $^O eq 'VMS';
332
cb9c5e20 333sub _quote_args {
334 my ($runperl, $args) = @_;
335
336 foreach (@$args) {
337 # In VMS protect with doublequotes because otherwise
338 # DCL will lowercase -- unless already doublequoted.
ea9ac5ad 339 $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0;
cb9c5e20 340 $$runperl .= ' ' . $_;
341 }
342}
343
137352a2 344sub runperl {
345 my %args = @_;
346 my $runperl = $^X;
f93a5f07 347 if ($args{switches}) {
8da5ed17 348 _quote_args(\$runperl, $args{switches});
137352a2 349 }
f93a5f07 350 unless ($args{nolib}) {
351 if ($is_macos) {
cb9c5e20 352 $runperl .= ' -I::lib';
f93a5f07 353 # Use UNIX style error messages instead of MPW style.
cb9c5e20 354 $runperl .= ' -MMac::err=unix' if $args{stderr};
137352a2 355 }
356 else {
cb9c5e20 357 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
137352a2 358 }
359 }
360 if (defined $args{prog}) {
361 if ($is_mswin || $is_netware || $is_vms) {
362 $runperl .= qq( -e ") . $args{prog} . qq(");
363 }
364 else {
365 $runperl .= qq( -e ') . $args{prog} . qq(');
366 }
367 } elsif (defined $args{progfile}) {
368 $runperl .= qq( "$args{progfile}");
369 }
370 if (defined $args{stdin}) {
5ae09a77 371 # so we don't try to put literal newlines and crs onto the
372 # command line.
373 $args{stdin} =~ s/\n/\\n/g;
374 $args{stdin} =~ s/\r/\\r/g;
375
137352a2 376 if ($is_mswin || $is_netware || $is_vms) {
f93a5f07 377 $runperl = qq{$^X -e "print qq(} .
137352a2 378 $args{stdin} . q{)" | } . $runperl;
379 }
380 else {
f93a5f07 381 $runperl = qq{$^X -e 'print qq(} .
137352a2 382 $args{stdin} . q{)' | } . $runperl;
383 }
384 }
385 if (defined $args{args}) {
cb9c5e20 386 _quote_args(\$runperl, $args{args});
387 }
388 $runperl .= ' 2>&1' if $args{stderr} && !$is_macos;
389 $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
390 if ($args{verbose}) {
391 my $runperldisplay = $runperl;
392 $runperldisplay =~ s/\n/\n\#/g;
75385f53 393 print STDERR "# $runperldisplay\n";
137352a2 394 }
137352a2 395 my $result = `$runperl`;
396 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
397 return $result;
398}
399
8799135f 400
c4fbe247 401sub DIE {
75385f53 402 print STDERR "# @_\n";
c4fbe247 403 exit 1;
8799135f 404}
405
b5fe401b 406# A somewhat safer version of the sometimes wrong $^X.
17a740d5 407my $Perl;
408sub which_perl {
409 unless (defined $Perl) {
410 $Perl = $^X;
411
412 my $exe;
413 eval "require Config; Config->import";
85363d30 414 if ($@) {
17a740d5 415 warn "test.pl had problems loading Config: $@";
416 $exe = '';
85363d30 417 } else {
17a740d5 418 $exe = $Config{_exe};
85363d30 419 }
da405c16 420 $exe = '' unless defined $exe;
17a740d5 421
422 # This doesn't absolutize the path: beware of future chdirs().
423 # We could do File::Spec->abs2rel() but that does getcwd()s,
424 # which is a bit heavyweight to do here.
425
426 if ($Perl =~ /^perl\Q$exe\E$/i) {
8db06b02 427 my $perl = "perl$exe";
17a740d5 428 eval "require File::Spec";
429 if ($@) {
430 warn "test.pl had problems loading File::Spec: $@";
8db06b02 431 $Perl = "./$perl";
17a740d5 432 } else {
8db06b02 433 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
17a740d5 434 }
435 }
196918b0 436
437 # Build up the name of the executable file from the name of
438 # the command.
439
440 if ($Perl !~ /\Q$exe\E$/i) {
441 $Perl .= $exe;
442 }
c880be78 443
8db06b02 444 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
17a740d5 445
446 # For subcommands to use.
447 $ENV{PERLEXE} = $Perl;
85363d30 448 }
17a740d5 449 return $Perl;
b5fe401b 450}
451
435e7af6 452sub unlink_all {
453 foreach my $file (@_) {
454 1 while unlink $file;
75385f53 455 print STDERR "# Couldn't unlink '$file': $!\n" if -f $file;
435e7af6 456 }
457}
eeabcb2d 458
459
460my $tmpfile = "misctmp000";
4611 while -f ++$tmpfile;
462END { unlink_all $tmpfile }
463
f5cda331 464#
465# _fresh_perl
466#
467# The $resolve must be a subref that tests the first argument
468# for success, or returns the definition of success (e.g. the
469# expected scalar) if given no arguments.
470#
471
472sub _fresh_perl {
473 my($prog, $resolve, $runperl_args, $name) = @_;
eeabcb2d 474
475 $runperl_args ||= {};
476 $runperl_args->{progfile} = $tmpfile;
477 $runperl_args->{stderr} = 1;
478
479 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
480
481 # VMS adjustments
482 if( $^O eq 'VMS' ) {
483 $prog =~ s#/dev/null#NL:#;
484
485 # VMS file locking
486 $prog =~ s{if \(-e _ and -f _ and -r _\)}
487 {if (-e _ and -f _)}
488 }
489
490 print TEST $prog, "\n";
491 close TEST or die "Cannot close $tmpfile: $!";
492
493 my $results = runperl(%$runperl_args);
494 my $status = $?;
495
496 # Clean up the results into something a bit more predictable.
497 $results =~ s/\n+$//;
498 $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
499 $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
500
501 # bison says 'parse error' instead of 'syntax error',
502 # various yaccs may or may not capitalize 'syntax'.
503 $results =~ s/^(syntax|parse) error/syntax error/mig;
504
505 if ($^O eq 'VMS') {
506 # some tests will trigger VMS messages that won't be expected
507 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
508
509 # pipes double these sometimes
510 $results =~ s/\n\n/\n/g;
511 }
512
f5cda331 513 my $pass = $resolve->($results);
eeabcb2d 514 unless ($pass) {
515 print STDERR "# PROG: $switch\n$prog\n";
f5cda331 516 print STDERR "# EXPECTED:\n", $resolve->(), "\n";
eeabcb2d 517 print STDERR "# GOT:\n$results\n";
518 print STDERR "# STATUS: $status\n";
519 }
520
e2c38acd 521 # Use the first line of the program as a name if none was given
522 unless( $name ) {
523 ($first_line, $name) = $prog =~ /^((.{1,50}).*)/;
524 $name .= '...' if length $first_line > length $name;
525 }
eeabcb2d 526
f5cda331 527 _ok($pass, _where(), "fresh_perl - $name");
528}
529
530#
531# run_perl_is
532#
533# Combination of run_perl() and is().
534#
535
536sub fresh_perl_is {
537 my($prog, $expected, $runperl_args, $name) = @_;
538 _fresh_perl($prog,
539 sub { @_ ? $_[0] eq $expected : $expected },
540 $runperl_args, $name);
541}
542
543#
544# run_perl_like
545#
546# Combination of run_perl() and like().
547#
548
549sub fresh_perl_like {
550 my($prog, $expected, $runperl_args, $name) = @_;
551 _fresh_perl($prog,
552 sub { @_ ?
553 $_[0] =~ (ref $expected ? $expected : /$expected/) :
554 $expected },
555 $runperl_args, $name);
eeabcb2d 556}
557
69026470 5581;