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