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