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