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