Commit | Line | Data |
69026470 |
1 | # |
2 | # t/test.pl - most of Test::More functionality without the fuss |
3 | # |
4 | |
dcc7f481 |
5 | $Level = 1; |
69026470 |
6 | my $test = 1; |
7 | my $planned; |
6137113d |
8 | my $noplan; |
69026470 |
9 | |
7d932aad |
10 | $TODO = 0; |
b6345914 |
11 | $NO_ENDING = 0; |
7d932aad |
12 | |
69026470 |
13 | sub plan { |
14 | my $n; |
15 | if (@_ == 1) { |
16 | $n = shift; |
6137113d |
17 | if ($n eq 'no_plan') { |
18 | undef $n; |
19 | $noplan = 1; |
20 | } |
69026470 |
21 | } else { |
22 | my %plan = @_; |
23 | $n = $plan{tests}; |
24 | } |
6137113d |
25 | print STDOUT "1..$n\n" unless $noplan; |
69026470 |
26 | $planned = $n; |
27 | } |
28 | |
29 | END { |
30 | my $ran = $test - 1; |
6137113d |
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 |
38 | } |
39 | } |
40 | |
de522f7a |
41 | # Use this instead of "print STDERR" when outputing failure diagnostic |
42 | # messages |
43 | sub _diag { |
cf8feb78 |
44 | return unless @_; |
45 | my @mess = map { /^#/ ? "$_\n" : "# $_\n" } |
46 | map { split /\n/ } @_; |
de522f7a |
47 | my $fh = $TODO ? *STDOUT : *STDERR; |
cf8feb78 |
48 | print $fh @mess; |
49 | |
de522f7a |
50 | } |
51 | |
69026470 |
52 | sub skip_all { |
53 | if (@_) { |
195d559b |
54 | print STDOUT "1..0 # Skipped: @_\n"; |
69026470 |
55 | } else { |
ad20d923 |
56 | print STDOUT "1..0\n"; |
69026470 |
57 | } |
58 | exit(0); |
59 | } |
60 | |
61 | sub _ok { |
7d932aad |
62 | my ($pass, $where, $name, @mess) = @_; |
69026470 |
63 | # Do not try to microoptimize by factoring out the "not ". |
64 | # VMS will avenge. |
7d932aad |
65 | my $out; |
66 | if ($name) { |
b734d6c9 |
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 |
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 |
80 | |
81 | # Ensure that the message is properly escaped. |
cf8feb78 |
82 | _diag @mess; |
7d932aad |
83 | |
69026470 |
84 | $test++; |
1577bb16 |
85 | |
86 | return $pass; |
69026470 |
87 | } |
88 | |
89 | sub _where { |
dcc7f481 |
90 | my @caller = caller($Level); |
69026470 |
91 | return "at $caller[1] line $caller[2]"; |
92 | } |
93 | |
1d662fb6 |
94 | # DON'T use this for matches. Use like() instead. |
c3029c66 |
95 | sub ok ($@) { |
7d932aad |
96 | my ($pass, $name, @mess) = @_; |
97 | _ok($pass, _where(), $name, @mess); |
69026470 |
98 | } |
99 | |
b3c72391 |
100 | sub _q { |
101 | my $x = shift; |
102 | return 'undef' unless defined $x; |
103 | my $q = $x; |
d279d8f8 |
104 | $q =~ s/\\/\\\\/g; |
105 | $q =~ s/'/\\'/g; |
b3c72391 |
106 | return "'$q'"; |
107 | } |
108 | |
677fb045 |
109 | sub _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 |
115 | my %backslash_escape; |
116 | foreach 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. |
121 | sub 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 |
145 | sub is ($$@) { |
7d932aad |
146 | my ($got, $expected, $name, @mess) = @_; |
c831d34f |
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 |
158 | unshift(@mess, "# got "._q($got)."\n", |
159 | "# expected "._q($expected)."\n"); |
69026470 |
160 | } |
7d932aad |
161 | _ok($pass, _where(), $name, @mess); |
69026470 |
162 | } |
163 | |
c3029c66 |
164 | sub isnt ($$@) { |
3e90d5a3 |
165 | my ($got, $isnt, $name, @mess) = @_; |
c831d34f |
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 |
178 | "# but it is.\n"); |
179 | } |
180 | _ok($pass, _where(), $name, @mess); |
181 | } |
182 | |
c3029c66 |
183 | sub cmp_ok ($$$@) { |
58d76dfd |
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 |
216 | sub within ($$$@) { |
58d76dfd |
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 |
248 | |
249 | sub like ($$@) { like_yn (0,@_) }; # 0 for - |
250 | sub unlike ($$@) { like_yn (1,@_) }; # 1 for un- |
251 | |
252 | sub like_yn ($$$@) { |
253 | my ($flip, $got, $expected, $name, @mess) = @_; |
69026470 |
254 | my $pass; |
724aa791 |
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 | } |
7d932aad |
261 | _ok($pass, _where(), $name, @mess); |
69026470 |
262 | } |
263 | |
264 | sub pass { |
265 | _ok(1, '', @_); |
266 | } |
267 | |
268 | sub fail { |
269 | _ok(0, _where(), @_); |
270 | } |
271 | |
ad20d923 |
272 | sub curr_test { |
cf8feb78 |
273 | $test = shift if @_; |
ad20d923 |
274 | return $test; |
275 | } |
276 | |
3e90d5a3 |
277 | sub next_test { |
cf8feb78 |
278 | $test++; |
3e90d5a3 |
279 | } |
280 | |
69026470 |
281 | # Note: can't pass multipart messages since we try to |
282 | # be compatible with Test::More::skip(). |
283 | sub skip { |
7d932aad |
284 | my $why = shift; |
982b7cb7 |
285 | my $n = @_ ? shift : 1; |
69026470 |
286 | for (1..$n) { |
ad20d923 |
287 | print STDOUT "ok $test # skip: $why\n"; |
e6c299c8 |
288 | $test++; |
69026470 |
289 | } |
290 | local $^W = 0; |
291 | last SKIP; |
292 | } |
293 | |
09f04786 |
294 | sub todo_skip { |
295 | my $why = shift; |
296 | my $n = @_ ? shift : 1; |
297 | |
298 | for (1..$n) { |
299 | print STDOUT "ok $test # TODO & SKIP: $why\n"; |
300 | $test++; |
301 | } |
302 | local $^W = 0; |
303 | last TODO; |
304 | } |
305 | |
69026470 |
306 | sub eq_array { |
307 | my ($ra, $rb) = @_; |
308 | return 0 unless $#$ra == $#$rb; |
309 | for my $i (0..$#$ra) { |
135d199b |
310 | next if !defined $ra->[$i] && !defined $rb->[$i]; |
311 | return 0 if !defined $ra->[$i]; |
312 | return 0 if !defined $rb->[$i]; |
69026470 |
313 | return 0 unless $ra->[$i] eq $rb->[$i]; |
314 | } |
315 | return 1; |
316 | } |
317 | |
677fb045 |
318 | sub eq_hash { |
319 | my ($orig, $suspect) = @_; |
320 | my $fail; |
321 | while (my ($key, $value) = each %$suspect) { |
322 | # Force a hash recompute if this perl's internals can cache the hash key. |
323 | $key = "" . $key; |
324 | if (exists $orig->{$key}) { |
325 | if ($orig->{$key} ne $value) { |
de522f7a |
326 | print STDOUT "# key ", _qq($key), " was ", _qq($orig->{$key}), |
327 | " now ", _qq($value), "\n"; |
677fb045 |
328 | $fail = 1; |
329 | } |
330 | } else { |
de522f7a |
331 | print STDOUT "# key ", _qq($key), " is ", _qq($value), |
75385f53 |
332 | ", not in original.\n"; |
677fb045 |
333 | $fail = 1; |
334 | } |
335 | } |
336 | foreach (keys %$orig) { |
337 | # Force a hash recompute if this perl's internals can cache the hash key. |
338 | $_ = "" . $_; |
339 | next if (exists $suspect->{$_}); |
de522f7a |
340 | print STDOUT "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n"; |
677fb045 |
341 | $fail = 1; |
342 | } |
343 | !$fail; |
344 | } |
345 | |
c3029c66 |
346 | sub require_ok ($) { |
69026470 |
347 | my ($require) = @_; |
348 | eval <<REQUIRE_OK; |
349 | require $require; |
350 | REQUIRE_OK |
1577bb16 |
351 | _ok(!$@, _where(), "require $require"); |
69026470 |
352 | } |
353 | |
c3029c66 |
354 | sub use_ok ($) { |
69026470 |
355 | my ($use) = @_; |
356 | eval <<USE_OK; |
357 | use $use; |
358 | USE_OK |
1577bb16 |
359 | _ok(!$@, _where(), "use $use"); |
69026470 |
360 | } |
361 | |
137352a2 |
362 | # runperl - Runs a separate perl interpreter. |
363 | # Arguments : |
364 | # switches => [ command-line switches ] |
365 | # nolib => 1 # don't use -I../lib (included by default) |
366 | # prog => one-liner (avoid quotes) |
d83945bc |
367 | # progs => [ multi-liner (avoid quotes) ] |
137352a2 |
368 | # progfile => perl script |
369 | # stdin => string to feed the stdin |
370 | # stderr => redirect stderr to stdout |
371 | # args => [ command-line arguments to the perl program ] |
cb9c5e20 |
372 | # verbose => print the command line |
137352a2 |
373 | |
374 | my $is_mswin = $^O eq 'MSWin32'; |
375 | my $is_netware = $^O eq 'NetWare'; |
376 | my $is_macos = $^O eq 'MacOS'; |
377 | my $is_vms = $^O eq 'VMS'; |
378 | |
cb9c5e20 |
379 | sub _quote_args { |
380 | my ($runperl, $args) = @_; |
381 | |
382 | foreach (@$args) { |
383 | # In VMS protect with doublequotes because otherwise |
384 | # DCL will lowercase -- unless already doublequoted. |
ea9ac5ad |
385 | $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0; |
cb9c5e20 |
386 | $$runperl .= ' ' . $_; |
387 | } |
388 | } |
389 | |
4cd2bd1f |
390 | sub _create_runperl { # Create the string to qx in runperl(). |
137352a2 |
391 | my %args = @_; |
44cb023c |
392 | my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X; |
f93a5f07 |
393 | unless ($args{nolib}) { |
394 | if ($is_macos) { |
cb9c5e20 |
395 | $runperl .= ' -I::lib'; |
f93a5f07 |
396 | # Use UNIX style error messages instead of MPW style. |
cb9c5e20 |
397 | $runperl .= ' -MMac::err=unix' if $args{stderr}; |
137352a2 |
398 | } |
399 | else { |
cb9c5e20 |
400 | $runperl .= ' "-I../lib"'; # doublequotes because of VMS |
137352a2 |
401 | } |
402 | } |
d83945bc |
403 | if ($args{switches}) { |
343d4a7b |
404 | local $Level = 2; |
405 | die "test.pl:runperl(): 'switches' must be an ARRAYREF " . _where() |
406 | unless ref $args{switches} eq "ARRAY"; |
d83945bc |
407 | _quote_args(\$runperl, $args{switches}); |
408 | } |
137352a2 |
409 | if (defined $args{prog}) { |
21820af6 |
410 | die "test.pl:runperl(): both 'prog' and 'progs' cannot be used " . _where() |
411 | if defined $args{progs}; |
d83945bc |
412 | $args{progs} = [$args{prog}] |
413 | } |
414 | if (defined $args{progs}) { |
21820af6 |
415 | die "test.pl:runperl(): 'progs' must be an ARRAYREF " . _where() |
416 | unless ref $args{progs} eq "ARRAY"; |
d83945bc |
417 | foreach my $prog (@{$args{progs}}) { |
418 | if ($is_mswin || $is_netware || $is_vms) { |
419 | $runperl .= qq ( -e "$prog" ); |
420 | } |
421 | else { |
422 | $runperl .= qq ( -e '$prog' ); |
423 | } |
424 | } |
137352a2 |
425 | } elsif (defined $args{progfile}) { |
426 | $runperl .= qq( "$args{progfile}"); |
9a731dbd |
427 | } else { |
428 | # You probaby didn't want to be sucking in from the upstream stdin |
429 | die "test.pl:runperl(): none of prog, progs, progfile, args, " |
430 | . " switches or stdin specified" |
431 | unless defined $args{args} or defined $args{switches} |
432 | or defined $args{stdin}; |
137352a2 |
433 | } |
434 | if (defined $args{stdin}) { |
dc459aad |
435 | # so we don't try to put literal newlines and crs onto the |
436 | # command line. |
437 | $args{stdin} =~ s/\n/\\n/g; |
438 | $args{stdin} =~ s/\r/\\r/g; |
5ae09a77 |
439 | |
137352a2 |
440 | if ($is_mswin || $is_netware || $is_vms) { |
f93a5f07 |
441 | $runperl = qq{$^X -e "print qq(} . |
137352a2 |
442 | $args{stdin} . q{)" | } . $runperl; |
443 | } |
dc459aad |
444 | elsif ($is_macos) { |
445 | # MacOS can only do two processes under MPW at once; |
446 | # the test itself is one; we can't do two more, so |
447 | # write to temp file |
448 | my $stdin = qq{$^X -e 'print qq(} . $args{stdin} . qq{)' > teststdin; }; |
449 | if ($args{verbose}) { |
450 | my $stdindisplay = $stdin; |
451 | $stdindisplay =~ s/\n/\n\#/g; |
452 | print STDERR "# $stdindisplay\n"; |
453 | } |
454 | `$stdin`; |
455 | $runperl .= q{ < teststdin }; |
456 | } |
137352a2 |
457 | else { |
f93a5f07 |
458 | $runperl = qq{$^X -e 'print qq(} . |
137352a2 |
459 | $args{stdin} . q{)' | } . $runperl; |
460 | } |
461 | } |
462 | if (defined $args{args}) { |
cb9c5e20 |
463 | _quote_args(\$runperl, $args{args}); |
464 | } |
465 | $runperl .= ' 2>&1' if $args{stderr} && !$is_macos; |
466 | $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos; |
467 | if ($args{verbose}) { |
468 | my $runperldisplay = $runperl; |
469 | $runperldisplay =~ s/\n/\n\#/g; |
75385f53 |
470 | print STDERR "# $runperldisplay\n"; |
137352a2 |
471 | } |
4cd2bd1f |
472 | return $runperl; |
473 | } |
474 | |
475 | sub runperl { |
9a731dbd |
476 | die "test.pl:runperl() does not take a hashref" |
477 | if ref $_[0] and ref $_[0] eq 'HASH'; |
4cd2bd1f |
478 | my $runperl = &_create_runperl; |
137352a2 |
479 | my $result = `$runperl`; |
480 | $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these |
481 | return $result; |
482 | } |
483 | |
d2c6a9c9 |
484 | *run_perl = \&runperl; # Nice alias. |
8799135f |
485 | |
c4fbe247 |
486 | sub DIE { |
75385f53 |
487 | print STDERR "# @_\n"; |
c4fbe247 |
488 | exit 1; |
8799135f |
489 | } |
490 | |
b5fe401b |
491 | # A somewhat safer version of the sometimes wrong $^X. |
17a740d5 |
492 | my $Perl; |
493 | sub which_perl { |
494 | unless (defined $Perl) { |
495 | $Perl = $^X; |
496 | |
73421c4a |
497 | # VMS should have 'perl' aliased properly |
498 | return $Perl if $^O eq 'VMS'; |
499 | |
17a740d5 |
500 | my $exe; |
501 | eval "require Config; Config->import"; |
85363d30 |
502 | if ($@) { |
17a740d5 |
503 | warn "test.pl had problems loading Config: $@"; |
504 | $exe = ''; |
85363d30 |
505 | } else { |
17a740d5 |
506 | $exe = $Config{_exe}; |
85363d30 |
507 | } |
da405c16 |
508 | $exe = '' unless defined $exe; |
17a740d5 |
509 | |
510 | # This doesn't absolutize the path: beware of future chdirs(). |
511 | # We could do File::Spec->abs2rel() but that does getcwd()s, |
512 | # which is a bit heavyweight to do here. |
513 | |
514 | if ($Perl =~ /^perl\Q$exe\E$/i) { |
8db06b02 |
515 | my $perl = "perl$exe"; |
17a740d5 |
516 | eval "require File::Spec"; |
517 | if ($@) { |
518 | warn "test.pl had problems loading File::Spec: $@"; |
8db06b02 |
519 | $Perl = "./$perl"; |
17a740d5 |
520 | } else { |
8db06b02 |
521 | $Perl = File::Spec->catfile(File::Spec->curdir(), $perl); |
17a740d5 |
522 | } |
523 | } |
196918b0 |
524 | |
525 | # Build up the name of the executable file from the name of |
526 | # the command. |
527 | |
528 | if ($Perl !~ /\Q$exe\E$/i) { |
529 | $Perl .= $exe; |
530 | } |
c880be78 |
531 | |
8db06b02 |
532 | warn "which_perl: cannot find $Perl from $^X" unless -f $Perl; |
17a740d5 |
533 | |
534 | # For subcommands to use. |
535 | $ENV{PERLEXE} = $Perl; |
85363d30 |
536 | } |
17a740d5 |
537 | return $Perl; |
b5fe401b |
538 | } |
539 | |
435e7af6 |
540 | sub unlink_all { |
541 | foreach my $file (@_) { |
542 | 1 while unlink $file; |
75385f53 |
543 | print STDERR "# Couldn't unlink '$file': $!\n" if -f $file; |
435e7af6 |
544 | } |
545 | } |
eeabcb2d |
546 | |
547 | |
548 | my $tmpfile = "misctmp000"; |
549 | 1 while -f ++$tmpfile; |
550 | END { unlink_all $tmpfile } |
551 | |
f5cda331 |
552 | # |
553 | # _fresh_perl |
554 | # |
555 | # The $resolve must be a subref that tests the first argument |
556 | # for success, or returns the definition of success (e.g. the |
557 | # expected scalar) if given no arguments. |
558 | # |
559 | |
560 | sub _fresh_perl { |
561 | my($prog, $resolve, $runperl_args, $name) = @_; |
eeabcb2d |
562 | |
563 | $runperl_args ||= {}; |
564 | $runperl_args->{progfile} = $tmpfile; |
565 | $runperl_args->{stderr} = 1; |
566 | |
567 | open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!"; |
568 | |
569 | # VMS adjustments |
570 | if( $^O eq 'VMS' ) { |
571 | $prog =~ s#/dev/null#NL:#; |
572 | |
573 | # VMS file locking |
574 | $prog =~ s{if \(-e _ and -f _ and -r _\)} |
575 | {if (-e _ and -f _)} |
576 | } |
577 | |
578 | print TEST $prog, "\n"; |
579 | close TEST or die "Cannot close $tmpfile: $!"; |
580 | |
581 | my $results = runperl(%$runperl_args); |
582 | my $status = $?; |
583 | |
584 | # Clean up the results into something a bit more predictable. |
585 | $results =~ s/\n+$//; |
586 | $results =~ s/at\s+misctmp\d+\s+line/at - line/g; |
587 | $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g; |
588 | |
589 | # bison says 'parse error' instead of 'syntax error', |
590 | # various yaccs may or may not capitalize 'syntax'. |
591 | $results =~ s/^(syntax|parse) error/syntax error/mig; |
592 | |
593 | if ($^O eq 'VMS') { |
594 | # some tests will trigger VMS messages that won't be expected |
595 | $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//; |
596 | |
597 | # pipes double these sometimes |
598 | $results =~ s/\n\n/\n/g; |
599 | } |
600 | |
f5cda331 |
601 | my $pass = $resolve->($results); |
eeabcb2d |
602 | unless ($pass) { |
cf8feb78 |
603 | _diag "# PROG: \n$prog\n"; |
604 | _diag "# EXPECTED:\n", $resolve->(), "\n"; |
605 | _diag "# GOT:\n$results\n"; |
606 | _diag "# STATUS: $status\n"; |
eeabcb2d |
607 | } |
608 | |
e2c38acd |
609 | # Use the first line of the program as a name if none was given |
610 | unless( $name ) { |
611 | ($first_line, $name) = $prog =~ /^((.{1,50}).*)/; |
612 | $name .= '...' if length $first_line > length $name; |
613 | } |
eeabcb2d |
614 | |
f5cda331 |
615 | _ok($pass, _where(), "fresh_perl - $name"); |
616 | } |
617 | |
618 | # |
141f445b |
619 | # fresh_perl_is |
f5cda331 |
620 | # |
621 | # Combination of run_perl() and is(). |
622 | # |
623 | |
624 | sub fresh_perl_is { |
625 | my($prog, $expected, $runperl_args, $name) = @_; |
dcc7f481 |
626 | local $Level = 2; |
f5cda331 |
627 | _fresh_perl($prog, |
628 | sub { @_ ? $_[0] eq $expected : $expected }, |
629 | $runperl_args, $name); |
630 | } |
631 | |
632 | # |
141f445b |
633 | # fresh_perl_like |
f5cda331 |
634 | # |
635 | # Combination of run_perl() and like(). |
636 | # |
637 | |
638 | sub fresh_perl_like { |
639 | my($prog, $expected, $runperl_args, $name) = @_; |
dcc7f481 |
640 | local $Level = 2; |
f5cda331 |
641 | _fresh_perl($prog, |
642 | sub { @_ ? |
643 | $_[0] =~ (ref $expected ? $expected : /$expected/) : |
644 | $expected }, |
645 | $runperl_args, $name); |
eeabcb2d |
646 | } |
647 | |
69026470 |
648 | 1; |