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