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