This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Upgrade to Win32-0.37
[perl5.git] / t / test.pl
CommitLineData
69026470
JH
1#
2# t/test.pl - most of Test::More functionality without the fuss
485f531e
DL
3
4
5# NOTE:
6#
7# Increment ($x++) has a certain amount of cleverness for things like
8#
9# $x = 'zz';
10# $x++; # $x eq 'aaa';
69026470 11#
485f531e
DL
12# stands more chance of breaking than just a simple
13#
14# $x = $x + 1
15#
16# In this file, we use the latter "Baby Perl" approach, and increment
17# will be worked over by t/op/inc.t
69026470 18
dcc7f481 19$Level = 1;
69026470
JH
20my $test = 1;
21my $planned;
6137113d 22my $noplan;
69026470 23
7d932aad 24$TODO = 0;
b6345914 25$NO_ENDING = 0;
7d932aad 26
3d66076a
MS
27# Use this instead of print to avoid interference while testing globals.
28sub _print {
29 local($\, $", $,) = (undef, ' ', '');
30 print STDOUT @_;
31}
32
33sub _print_stderr {
34 local($\, $", $,) = (undef, ' ', '');
35 print STDERR @_;
36}
37
69026470
JH
38sub plan {
39 my $n;
40 if (@_ == 1) {
41 $n = shift;
6137113d
NC
42 if ($n eq 'no_plan') {
43 undef $n;
44 $noplan = 1;
45 }
69026470
JH
46 } else {
47 my %plan = @_;
8210c8d3 48 $n = $plan{tests};
69026470 49 }
3d66076a 50 _print "1..$n\n" unless $noplan;
69026470
JH
51 $planned = $n;
52}
53
54END {
55 my $ran = $test - 1;
6137113d
NC
56 if (!$NO_ENDING) {
57 if (defined $planned && $planned != $ran) {
3d66076a 58 _print_stderr
6137113d
NC
59 "# Looks like you planned $planned tests but ran $ran.\n";
60 } elsif ($noplan) {
3d66076a 61 _print "1..$ran\n";
6137113d 62 }
69026470
JH
63 }
64}
65
8210c8d3 66# Use this instead of "print STDERR" when outputing failure diagnostic
de522f7a
MS
67# messages
68sub _diag {
cf8feb78 69 return unless @_;
8210c8d3 70 my @mess = map { /^#/ ? "$_\n" : "# $_\n" }
cf8feb78 71 map { split /\n/ } @_;
3d66076a
MS
72 my $func = $TODO ? \&_print : \&_print_stderr;
73 $func->(@mess);
cf8feb78 74
de522f7a
MS
75}
76
485f531e
DL
77sub diag {
78 _diag(@_);
79}
80
69026470
JH
81sub skip_all {
82 if (@_) {
3d66076a 83 _print "1..0 # Skipped: @_\n";
69026470 84 } else {
3d66076a 85 _print "1..0\n";
69026470
JH
86 }
87 exit(0);
88}
89
90sub _ok {
7d932aad 91 my ($pass, $where, $name, @mess) = @_;
69026470
JH
92 # Do not try to microoptimize by factoring out the "not ".
93 # VMS will avenge.
7d932aad
MS
94 my $out;
95 if ($name) {
b734d6c9
MS
96 # escape out '#' or it will interfere with '# skip' and such
97 $name =~ s/#/\\#/g;
7d932aad 98 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
69026470 99 } else {
7d932aad 100 $out = $pass ? "ok $test" : "not ok $test";
69026470 101 }
7d932aad
MS
102
103 $out .= " # TODO $TODO" if $TODO;
3d66076a 104 _print "$out\n";
7d932aad 105
69026470 106 unless ($pass) {
de522f7a 107 _diag "# Failed $where\n";
69026470 108 }
7d932aad
MS
109
110 # Ensure that the message is properly escaped.
cf8feb78 111 _diag @mess;
7d932aad 112
485f531e 113 $test = $test + 1; # don't use ++
1577bb16
MS
114
115 return $pass;
69026470
JH
116}
117
118sub _where {
dcc7f481 119 my @caller = caller($Level);
69026470
JH
120 return "at $caller[1] line $caller[2]";
121}
122
1d662fb6 123# DON'T use this for matches. Use like() instead.
c3029c66 124sub ok ($@) {
7d932aad
MS
125 my ($pass, $name, @mess) = @_;
126 _ok($pass, _where(), $name, @mess);
69026470
JH
127}
128
b3c72391
JH
129sub _q {
130 my $x = shift;
131 return 'undef' unless defined $x;
132 my $q = $x;
d279d8f8
NC
133 $q =~ s/\\/\\\\/g;
134 $q =~ s/'/\\'/g;
b3c72391
JH
135 return "'$q'";
136}
137
677fb045
NC
138sub _qq {
139 my $x = shift;
140 return defined $x ? '"' . display ($x) . '"' : 'undef';
141};
142
143# keys are the codes \n etc map to, values are 2 char strings such as \n
144my %backslash_escape;
145foreach my $x (split //, 'nrtfa\\\'"') {
146 $backslash_escape{ord eval "\"\\$x\""} = "\\$x";
147}
148# A way to display scalars containing control characters and Unicode.
149# Trying to avoid setting $_, or relying on local $_ to work.
150sub display {
151 my @result;
152 foreach my $x (@_) {
153 if (defined $x and not ref $x) {
154 my $y = '';
155 foreach my $c (unpack("U*", $x)) {
156 if ($c > 255) {
157 $y .= sprintf "\\x{%x}", $c;
158 } elsif ($backslash_escape{$c}) {
159 $y .= $backslash_escape{$c};
160 } else {
161 my $z = chr $c; # Maybe we can get away with a literal...
162 $z = sprintf "\\%03o", $c if $z =~ /[[:^print:]]/;
163 $y .= $z;
164 }
165 }
166 $x = $y;
167 }
168 return $x unless wantarray;
169 push @result, $x;
170 }
171 return @result;
172}
173
c3029c66 174sub is ($$@) {
7d932aad 175 my ($got, $expected, $name, @mess) = @_;
c831d34f
MS
176
177 my $pass;
178 if( !defined $got || !defined $expected ) {
179 # undef only matches undef
180 $pass = !defined $got && !defined $expected;
181 }
182 else {
183 $pass = $got eq $expected;
184 }
185
69026470 186 unless ($pass) {
b3c72391
JH
187 unshift(@mess, "# got "._q($got)."\n",
188 "# expected "._q($expected)."\n");
69026470 189 }
7d932aad 190 _ok($pass, _where(), $name, @mess);
69026470
JH
191}
192
c3029c66 193sub isnt ($$@) {
3e90d5a3 194 my ($got, $isnt, $name, @mess) = @_;
c831d34f
MS
195
196 my $pass;
197 if( !defined $got || !defined $isnt ) {
198 # undef only matches undef
199 $pass = defined $got || defined $isnt;
200 }
201 else {
202 $pass = $got ne $isnt;
203 }
204
3e90d5a3 205 unless( $pass ) {
b3c72391 206 unshift(@mess, "# it should not be "._q($got)."\n",
3e90d5a3
MS
207 "# but it is.\n");
208 }
209 _ok($pass, _where(), $name, @mess);
210}
211
c3029c66 212sub cmp_ok ($$$@) {
58d76dfd
JH
213 my($got, $type, $expected, $name, @mess) = @_;
214
215 my $pass;
216 {
217 local $^W = 0;
218 local($@,$!); # don't interfere with $@
219 # eval() sometimes resets $!
220 $pass = eval "\$got $type \$expected";
221 }
222 unless ($pass) {
223 # It seems Irix long doubles can have 2147483648 and 2147483648
224 # that stringify to the same thing but are acutally numerically
225 # different. Display the numbers if $type isn't a string operator,
226 # and the numbers are stringwise the same.
227 # (all string operators have alphabetic names, so tr/a-z// is true)
228 # This will also show numbers for some uneeded cases, but will
229 # definately be helpful for things such as == and <= that fail
230 if ($got eq $expected and $type !~ tr/a-z//) {
231 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
232 }
233 unshift(@mess, "# got "._q($got)."\n",
234 "# expected $type "._q($expected)."\n");
235 }
236 _ok($pass, _where(), $name, @mess);
237}
238
239# Check that $got is within $range of $expected
240# if $range is 0, then check it's exact
241# else if $expected is 0, then $range is an absolute value
242# otherwise $range is a fractional error.
243# Here $range must be numeric, >= 0
244# Non numeric ranges might be a useful future extension. (eg %)
c3029c66 245sub within ($$$@) {
58d76dfd
JH
246 my ($got, $expected, $range, $name, @mess) = @_;
247 my $pass;
248 if (!defined $got or !defined $expected or !defined $range) {
249 # This is a fail, but doesn't need extra diagnostics
250 } elsif ($got !~ tr/0-9// or $expected !~ tr/0-9// or $range !~ tr/0-9//) {
251 # This is a fail
252 unshift @mess, "# got, expected and range must be numeric\n";
253 } elsif ($range < 0) {
254 # This is also a fail
255 unshift @mess, "# range must not be negative\n";
256 } elsif ($range == 0) {
257 # Within 0 is ==
258 $pass = $got == $expected;
259 } elsif ($expected == 0) {
260 # If expected is 0, treat range as absolute
261 $pass = ($got <= $range) && ($got >= - $range);
262 } else {
263 my $diff = $got - $expected;
264 $pass = abs ($diff / $expected) < $range;
265 }
266 unless ($pass) {
267 if ($got eq $expected) {
268 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
269 }
270 unshift@mess, "# got "._q($got)."\n",
271 "# expected "._q($expected)." (within "._q($range).")\n";
272 }
273 _ok($pass, _where(), $name, @mess);
274}
275
69026470 276# Note: this isn't quite as fancy as Test::More::like().
724aa791
JC
277
278sub like ($$@) { like_yn (0,@_) }; # 0 for -
279sub unlike ($$@) { like_yn (1,@_) }; # 1 for un-
280
281sub like_yn ($$$@) {
282 my ($flip, $got, $expected, $name, @mess) = @_;
69026470 283 my $pass;
724aa791
JC
284 $pass = $got =~ /$expected/ if !$flip;
285 $pass = $got !~ /$expected/ if $flip;
286 unless ($pass) {
287 unshift(@mess, "# got '$got'\n",
5a4a8c8b
NC
288 $flip
289 ? "# expected !~ /$expected/\n" : "# expected /$expected/\n");
69026470 290 }
5693d826 291 local $Level = $Level + 1;
7d932aad 292 _ok($pass, _where(), $name, @mess);
69026470
JH
293}
294
295sub pass {
296 _ok(1, '', @_);
297}
298
299sub fail {
300 _ok(0, _where(), @_);
301}
302
ad20d923 303sub curr_test {
cf8feb78 304 $test = shift if @_;
ad20d923
MS
305 return $test;
306}
307
3e90d5a3 308sub next_test {
178eff92 309 my $retval = $test;
485f531e 310 $test = $test + 1; # don't use ++
178eff92 311 $retval;
3e90d5a3
MS
312}
313
69026470
JH
314# Note: can't pass multipart messages since we try to
315# be compatible with Test::More::skip().
316sub skip {
7d932aad 317 my $why = shift;
982b7cb7 318 my $n = @_ ? shift : 1;
69026470 319 for (1..$n) {
3d66076a 320 _print "ok $test # skip: $why\n";
485f531e 321 $test = $test + 1;
69026470
JH
322 }
323 local $^W = 0;
324 last SKIP;
325}
326
09f04786
MS
327sub todo_skip {
328 my $why = shift;
329 my $n = @_ ? shift : 1;
330
331 for (1..$n) {
3d66076a 332 _print "not ok $test # TODO & SKIP: $why\n";
485f531e 333 $test = $test + 1;
09f04786
MS
334 }
335 local $^W = 0;
336 last TODO;
337}
338
69026470
JH
339sub eq_array {
340 my ($ra, $rb) = @_;
341 return 0 unless $#$ra == $#$rb;
342 for my $i (0..$#$ra) {
8210c8d3 343 next if !defined $ra->[$i] && !defined $rb->[$i];
135d199b
DM
344 return 0 if !defined $ra->[$i];
345 return 0 if !defined $rb->[$i];
69026470
JH
346 return 0 unless $ra->[$i] eq $rb->[$i];
347 }
348 return 1;
349}
350
677fb045
NC
351sub eq_hash {
352 my ($orig, $suspect) = @_;
353 my $fail;
354 while (my ($key, $value) = each %$suspect) {
355 # Force a hash recompute if this perl's internals can cache the hash key.
356 $key = "" . $key;
357 if (exists $orig->{$key}) {
358 if ($orig->{$key} ne $value) {
3d66076a 359 _print "# key ", _qq($key), " was ", _qq($orig->{$key}),
de522f7a 360 " now ", _qq($value), "\n";
677fb045
NC
361 $fail = 1;
362 }
363 } else {
3d66076a 364 _print "# key ", _qq($key), " is ", _qq($value),
75385f53 365 ", not in original.\n";
677fb045
NC
366 $fail = 1;
367 }
368 }
369 foreach (keys %$orig) {
370 # Force a hash recompute if this perl's internals can cache the hash key.
371 $_ = "" . $_;
372 next if (exists $suspect->{$_});
3d66076a 373 _print "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
677fb045
NC
374 $fail = 1;
375 }
376 !$fail;
377}
378
c3029c66 379sub require_ok ($) {
69026470
JH
380 my ($require) = @_;
381 eval <<REQUIRE_OK;
382require $require;
383REQUIRE_OK
1577bb16 384 _ok(!$@, _where(), "require $require");
69026470
JH
385}
386
c3029c66 387sub use_ok ($) {
69026470
JH
388 my ($use) = @_;
389 eval <<USE_OK;
390use $use;
391USE_OK
1577bb16 392 _ok(!$@, _where(), "use $use");
69026470
JH
393}
394
137352a2
RGS
395# runperl - Runs a separate perl interpreter.
396# Arguments :
397# switches => [ command-line switches ]
398# nolib => 1 # don't use -I../lib (included by default)
399# prog => one-liner (avoid quotes)
d83945bc 400# progs => [ multi-liner (avoid quotes) ]
137352a2
RGS
401# progfile => perl script
402# stdin => string to feed the stdin
403# stderr => redirect stderr to stdout
404# args => [ command-line arguments to the perl program ]
cb9c5e20 405# verbose => print the command line
137352a2
RGS
406
407my $is_mswin = $^O eq 'MSWin32';
408my $is_netware = $^O eq 'NetWare';
409my $is_macos = $^O eq 'MacOS';
410my $is_vms = $^O eq 'VMS';
e67ed694 411my $is_cygwin = $^O eq 'cygwin';
137352a2 412
cb9c5e20
JH
413sub _quote_args {
414 my ($runperl, $args) = @_;
415
416 foreach (@$args) {
417 # In VMS protect with doublequotes because otherwise
418 # DCL will lowercase -- unless already doublequoted.
ea9ac5ad 419 $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0;
cb9c5e20
JH
420 $$runperl .= ' ' . $_;
421 }
422}
423
4cd2bd1f 424sub _create_runperl { # Create the string to qx in runperl().
137352a2 425 my %args = @_;
44cb023c 426 my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X;
6cf707aa
RGS
427 #- this allows, for example, to set PERL_RUNPERL_DEBUG=/usr/bin/valgrind
428 if ($ENV{PERL_RUNPERL_DEBUG}) {
429 $runperl = "$ENV{PERL_RUNPERL_DEBUG} $runperl";
430 }
f93a5f07
JH
431 unless ($args{nolib}) {
432 if ($is_macos) {
cb9c5e20 433 $runperl .= ' -I::lib';
f93a5f07 434 # Use UNIX style error messages instead of MPW style.
cb9c5e20 435 $runperl .= ' -MMac::err=unix' if $args{stderr};
137352a2
RGS
436 }
437 else {
cb9c5e20 438 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
137352a2
RGS
439 }
440 }
d83945bc 441 if ($args{switches}) {
343d4a7b
JH
442 local $Level = 2;
443 die "test.pl:runperl(): 'switches' must be an ARRAYREF " . _where()
444 unless ref $args{switches} eq "ARRAY";
d83945bc
A
445 _quote_args(\$runperl, $args{switches});
446 }
137352a2 447 if (defined $args{prog}) {
21820af6
JH
448 die "test.pl:runperl(): both 'prog' and 'progs' cannot be used " . _where()
449 if defined $args{progs};
d83945bc
A
450 $args{progs} = [$args{prog}]
451 }
452 if (defined $args{progs}) {
21820af6
JH
453 die "test.pl:runperl(): 'progs' must be an ARRAYREF " . _where()
454 unless ref $args{progs} eq "ARRAY";
d83945bc
A
455 foreach my $prog (@{$args{progs}}) {
456 if ($is_mswin || $is_netware || $is_vms) {
457 $runperl .= qq ( -e "$prog" );
458 }
459 else {
460 $runperl .= qq ( -e '$prog' );
461 }
462 }
137352a2
RGS
463 } elsif (defined $args{progfile}) {
464 $runperl .= qq( "$args{progfile}");
9a731dbd
NC
465 } else {
466 # You probaby didn't want to be sucking in from the upstream stdin
467 die "test.pl:runperl(): none of prog, progs, progfile, args, "
468 . " switches or stdin specified"
469 unless defined $args{args} or defined $args{switches}
470 or defined $args{stdin};
137352a2
RGS
471 }
472 if (defined $args{stdin}) {
dc459aad
JH
473 # so we don't try to put literal newlines and crs onto the
474 # command line.
475 $args{stdin} =~ s/\n/\\n/g;
476 $args{stdin} =~ s/\r/\\r/g;
5ae09a77 477
137352a2 478 if ($is_mswin || $is_netware || $is_vms) {
f93a5f07 479 $runperl = qq{$^X -e "print qq(} .
137352a2
RGS
480 $args{stdin} . q{)" | } . $runperl;
481 }
dc459aad
JH
482 elsif ($is_macos) {
483 # MacOS can only do two processes under MPW at once;
484 # the test itself is one; we can't do two more, so
485 # write to temp file
486 my $stdin = qq{$^X -e 'print qq(} . $args{stdin} . qq{)' > teststdin; };
487 if ($args{verbose}) {
488 my $stdindisplay = $stdin;
489 $stdindisplay =~ s/\n/\n\#/g;
3d66076a 490 _print_stderr "# $stdindisplay\n";
dc459aad
JH
491 }
492 `$stdin`;
493 $runperl .= q{ < teststdin };
494 }
137352a2 495 else {
f93a5f07 496 $runperl = qq{$^X -e 'print qq(} .
137352a2
RGS
497 $args{stdin} . q{)' | } . $runperl;
498 }
499 }
500 if (defined $args{args}) {
cb9c5e20
JH
501 _quote_args(\$runperl, $args{args});
502 }
503 $runperl .= ' 2>&1' if $args{stderr} && !$is_macos;
504 $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos;
505 if ($args{verbose}) {
506 my $runperldisplay = $runperl;
507 $runperldisplay =~ s/\n/\n\#/g;
3d66076a 508 _print_stderr "# $runperldisplay\n";
137352a2 509 }
4cd2bd1f
JH
510 return $runperl;
511}
512
513sub runperl {
9a731dbd
NC
514 die "test.pl:runperl() does not take a hashref"
515 if ref $_[0] and ref $_[0] eq 'HASH';
4cd2bd1f 516 my $runperl = &_create_runperl;
613de57f
NC
517 my $result;
518
8210c8d3
MB
519 my $tainted = ${^TAINT};
520 my %args = @_;
485f531e 521 exists $args{switches} && grep m/^-T$/, @{$args{switches}} and $tainted = $tainted + 1;
8210c8d3
MB
522
523 if ($tainted) {
613de57f
NC
524 # We will assume that if you're running under -T, you really mean to
525 # run a fresh perl, so we'll brute force launder everything for you
526 my $sep;
527
528 eval "require Config; Config->import";
529 if ($@) {
530 warn "test.pl had problems loading Config: $@";
531 $sep = ':';
532 } else {
533 $sep = $Config{path_sep};
a70a1627 534 }
613de57f
NC
535
536 my @keys = grep {exists $ENV{$_}} qw(CDPATH IFS ENV BASH_ENV);
537 local @ENV{@keys} = ();
538 # Untaint, plus take out . and empty string:
326b5008 539 local $ENV{'DCL$PATH'} = $1 if $is_vms && ($ENV{'DCL$PATH'} =~ /(.*)/s);
613de57f 540 $ENV{PATH} =~ /(.*)/s;
8210c8d3 541 local $ENV{PATH} =
3b6d8381 542 join $sep, grep { $_ ne "" and $_ ne "." and -d $_ and
326b5008 543 ($is_mswin or $is_vms or !(stat && (stat _)[2]&0022)) }
8210c8d3 544 split quotemeta ($sep), $1;
e67ed694 545 $ENV{PATH} .= "$sep/bin" if $is_cygwin; # Must have /bin under Cygwin
613de57f
NC
546
547 $runperl =~ /(.*)/s;
548 $runperl = $1;
549
550 $result = `$runperl`;
551 } else {
552 $result = `$runperl`;
a70a1627 553 }
137352a2
RGS
554 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
555 return $result;
556}
557
d2c6a9c9 558*run_perl = \&runperl; # Nice alias.
8799135f 559
c4fbe247 560sub DIE {
3d66076a 561 _print_stderr "# @_\n";
c4fbe247 562 exit 1;
8799135f
MS
563}
564
b5fe401b 565# A somewhat safer version of the sometimes wrong $^X.
17a740d5
JH
566my $Perl;
567sub which_perl {
568 unless (defined $Perl) {
569 $Perl = $^X;
8210c8d3 570
73421c4a
CB
571 # VMS should have 'perl' aliased properly
572 return $Perl if $^O eq 'VMS';
573
17a740d5
JH
574 my $exe;
575 eval "require Config; Config->import";
85363d30 576 if ($@) {
17a740d5
JH
577 warn "test.pl had problems loading Config: $@";
578 $exe = '';
85363d30 579 } else {
17a740d5 580 $exe = $Config{_exe};
85363d30 581 }
da405c16 582 $exe = '' unless defined $exe;
8210c8d3 583
17a740d5
JH
584 # This doesn't absolutize the path: beware of future chdirs().
585 # We could do File::Spec->abs2rel() but that does getcwd()s,
586 # which is a bit heavyweight to do here.
8210c8d3 587
17a740d5 588 if ($Perl =~ /^perl\Q$exe\E$/i) {
8db06b02 589 my $perl = "perl$exe";
17a740d5
JH
590 eval "require File::Spec";
591 if ($@) {
592 warn "test.pl had problems loading File::Spec: $@";
8db06b02 593 $Perl = "./$perl";
17a740d5 594 } else {
8db06b02 595 $Perl = File::Spec->catfile(File::Spec->curdir(), $perl);
17a740d5
JH
596 }
597 }
196918b0
PG
598
599 # Build up the name of the executable file from the name of
600 # the command.
601
602 if ($Perl !~ /\Q$exe\E$/i) {
603 $Perl .= $exe;
604 }
c880be78 605
8db06b02 606 warn "which_perl: cannot find $Perl from $^X" unless -f $Perl;
8210c8d3 607
17a740d5
JH
608 # For subcommands to use.
609 $ENV{PERLEXE} = $Perl;
85363d30 610 }
17a740d5 611 return $Perl;
b5fe401b
MS
612}
613
435e7af6
NC
614sub unlink_all {
615 foreach my $file (@_) {
616 1 while unlink $file;
3d66076a 617 _print_stderr "# Couldn't unlink '$file': $!\n" if -f $file;
435e7af6
NC
618 }
619}
eeabcb2d
MS
620
621
622my $tmpfile = "misctmp000";
6231 while -f ++$tmpfile;
624END { unlink_all $tmpfile }
625
f5cda331
JH
626#
627# _fresh_perl
628#
629# The $resolve must be a subref that tests the first argument
630# for success, or returns the definition of success (e.g. the
631# expected scalar) if given no arguments.
632#
633
634sub _fresh_perl {
635 my($prog, $resolve, $runperl_args, $name) = @_;
eeabcb2d
MS
636
637 $runperl_args ||= {};
638 $runperl_args->{progfile} = $tmpfile;
639 $runperl_args->{stderr} = 1;
640
641 open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!";
642
643 # VMS adjustments
644 if( $^O eq 'VMS' ) {
645 $prog =~ s#/dev/null#NL:#;
646
8210c8d3 647 # VMS file locking
eeabcb2d
MS
648 $prog =~ s{if \(-e _ and -f _ and -r _\)}
649 {if (-e _ and -f _)}
650 }
651
0d65d7d5 652 print TEST $prog;
eeabcb2d
MS
653 close TEST or die "Cannot close $tmpfile: $!";
654
655 my $results = runperl(%$runperl_args);
656 my $status = $?;
657
658 # Clean up the results into something a bit more predictable.
659 $results =~ s/\n+$//;
660 $results =~ s/at\s+misctmp\d+\s+line/at - line/g;
661 $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g;
662
663 # bison says 'parse error' instead of 'syntax error',
664 # various yaccs may or may not capitalize 'syntax'.
665 $results =~ s/^(syntax|parse) error/syntax error/mig;
666
667 if ($^O eq 'VMS') {
668 # some tests will trigger VMS messages that won't be expected
669 $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//;
670
671 # pipes double these sometimes
672 $results =~ s/\n\n/\n/g;
673 }
674
f5cda331 675 my $pass = $resolve->($results);
eeabcb2d 676 unless ($pass) {
cf8feb78
MJD
677 _diag "# PROG: \n$prog\n";
678 _diag "# EXPECTED:\n", $resolve->(), "\n";
679 _diag "# GOT:\n$results\n";
680 _diag "# STATUS: $status\n";
eeabcb2d
MS
681 }
682
e2c38acd
JH
683 # Use the first line of the program as a name if none was given
684 unless( $name ) {
685 ($first_line, $name) = $prog =~ /^((.{1,50}).*)/;
686 $name .= '...' if length $first_line > length $name;
687 }
eeabcb2d 688
f5cda331
JH
689 _ok($pass, _where(), "fresh_perl - $name");
690}
691
692#
141f445b 693# fresh_perl_is
f5cda331
JH
694#
695# Combination of run_perl() and is().
696#
697
698sub fresh_perl_is {
699 my($prog, $expected, $runperl_args, $name) = @_;
dcc7f481 700 local $Level = 2;
f5cda331
JH
701 _fresh_perl($prog,
702 sub { @_ ? $_[0] eq $expected : $expected },
703 $runperl_args, $name);
704}
705
706#
141f445b 707# fresh_perl_like
f5cda331
JH
708#
709# Combination of run_perl() and like().
710#
711
712sub fresh_perl_like {
713 my($prog, $expected, $runperl_args, $name) = @_;
dcc7f481 714 local $Level = 2;
f5cda331
JH
715 _fresh_perl($prog,
716 sub { @_ ?
717 $_[0] =~ (ref $expected ? $expected : /$expected/) :
718 $expected },
719 $runperl_args, $name);
eeabcb2d
MS
720}
721
35a60386
RGS
722sub can_ok ($@) {
723 my($proto, @methods) = @_;
724 my $class = ref $proto || $proto;
725
726 unless( @methods ) {
727 return _ok( 0, _where(), "$class->can(...)" );
728 }
729
730 my @nok = ();
731 foreach my $method (@methods) {
732 local($!, $@); # don't interfere with caller's $@
733 # eval sometimes resets $!
734 eval { $proto->can($method) } || push @nok, $method;
735 }
736
737 my $name;
8210c8d3 738 $name = @methods == 1 ? "$class->can('$methods[0]')"
35a60386 739 : "$class->can(...)";
8210c8d3 740
35a60386
RGS
741 _ok( !@nok, _where(), $name );
742}
743
744sub isa_ok ($$;$) {
745 my($object, $class, $obj_name) = @_;
746
747 my $diag;
748 $obj_name = 'The object' unless defined $obj_name;
749 my $name = "$obj_name isa $class";
750 if( !defined $object ) {
751 $diag = "$obj_name isn't defined";
752 }
753 elsif( !ref $object ) {
754 $diag = "$obj_name isn't a reference";
755 }
756 else {
757 # We can't use UNIVERSAL::isa because we want to honor isa() overrides
758 local($@, $!); # eval sometimes resets $!
759 my $rslt = eval { $object->isa($class) };
760 if( $@ ) {
761 if( $@ =~ /^Can't call method "isa" on unblessed reference/ ) {
762 if( !UNIVERSAL::isa($object, $class) ) {
763 my $ref = ref $object;
764 $diag = "$obj_name isn't a '$class' it's a '$ref'";
765 }
766 } else {
767 die <<WHOA;
768WHOA! I tried to call ->isa on your object and got some weird error.
769This should never happen. Please contact the author immediately.
770Here's the error.
771$@
772WHOA
773 }
774 }
775 elsif( !$rslt ) {
776 my $ref = ref $object;
777 $diag = "$obj_name isn't a '$class' it's a '$ref'";
778 }
779 }
780
781 _ok( !$diag, _where(), $name );
782}
783
69026470 7841;