Commit | Line | Data |
69026470 |
1 | # |
2 | # t/test.pl - most of Test::More functionality without the fuss |
3 | # |
4 | |
5 | my $test = 1; |
6 | my $planned; |
7 | |
7d932aad |
8 | $TODO = 0; |
b6345914 |
9 | $NO_ENDING = 0; |
7d932aad |
10 | |
69026470 |
11 | sub plan { |
12 | my $n; |
13 | if (@_ == 1) { |
14 | $n = shift; |
15 | } else { |
16 | my %plan = @_; |
17 | $n = $plan{tests}; |
18 | } |
ad20d923 |
19 | print STDOUT "1..$n\n"; |
69026470 |
20 | $planned = $n; |
21 | } |
22 | |
23 | END { |
24 | my $ran = $test - 1; |
b6345914 |
25 | if (!$NO_ENDING && defined $planned && $planned != $ran) { |
75385f53 |
26 | print STDERR "# Looks like you planned $planned tests but ran $ran.\n"; |
69026470 |
27 | } |
28 | } |
29 | |
de522f7a |
30 | # Use this instead of "print STDERR" when outputing failure diagnostic |
31 | # messages |
32 | sub _diag { |
33 | my $fh = $TODO ? *STDOUT : *STDERR; |
34 | print $fh @_; |
35 | } |
36 | |
69026470 |
37 | sub skip_all { |
38 | if (@_) { |
ad20d923 |
39 | print STDOUT "1..0 - @_\n"; |
69026470 |
40 | } else { |
ad20d923 |
41 | print STDOUT "1..0\n"; |
69026470 |
42 | } |
43 | exit(0); |
44 | } |
45 | |
46 | sub _ok { |
7d932aad |
47 | my ($pass, $where, $name, @mess) = @_; |
69026470 |
48 | # Do not try to microoptimize by factoring out the "not ". |
49 | # VMS will avenge. |
7d932aad |
50 | my $out; |
51 | if ($name) { |
b734d6c9 |
52 | # escape out '#' or it will interfere with '# skip' and such |
53 | $name =~ s/#/\\#/g; |
7d932aad |
54 | $out = $pass ? "ok $test - $name" : "not ok $test - $name"; |
69026470 |
55 | } else { |
7d932aad |
56 | $out = $pass ? "ok $test" : "not ok $test"; |
69026470 |
57 | } |
7d932aad |
58 | |
59 | $out .= " # TODO $TODO" if $TODO; |
ad20d923 |
60 | print STDOUT "$out\n"; |
7d932aad |
61 | |
69026470 |
62 | unless ($pass) { |
de522f7a |
63 | _diag "# Failed $where\n"; |
69026470 |
64 | } |
7d932aad |
65 | |
66 | # Ensure that the message is properly escaped. |
de522f7a |
67 | _diag map { /^#/ ? "$_\n" : "# $_\n" } |
68 | map { split /\n/ } @mess if @mess; |
7d932aad |
69 | |
69026470 |
70 | $test++; |
1577bb16 |
71 | |
72 | return $pass; |
69026470 |
73 | } |
74 | |
75 | sub _where { |
76 | my @caller = caller(1); |
77 | return "at $caller[1] line $caller[2]"; |
78 | } |
79 | |
1d662fb6 |
80 | # DON'T use this for matches. Use like() instead. |
69026470 |
81 | sub ok { |
7d932aad |
82 | my ($pass, $name, @mess) = @_; |
83 | _ok($pass, _where(), $name, @mess); |
69026470 |
84 | } |
85 | |
b3c72391 |
86 | sub _q { |
87 | my $x = shift; |
88 | return 'undef' unless defined $x; |
89 | my $q = $x; |
677fb045 |
90 | $q =~ s/\\/\\\\/; |
b3c72391 |
91 | $q =~ s/'/\\'/; |
92 | return "'$q'"; |
93 | } |
94 | |
677fb045 |
95 | sub _qq { |
96 | my $x = shift; |
97 | return defined $x ? '"' . display ($x) . '"' : 'undef'; |
98 | }; |
99 | |
100 | # keys are the codes \n etc map to, values are 2 char strings such as \n |
101 | my %backslash_escape; |
102 | foreach my $x (split //, 'nrtfa\\\'"') { |
103 | $backslash_escape{ord eval "\"\\$x\""} = "\\$x"; |
104 | } |
105 | # A way to display scalars containing control characters and Unicode. |
106 | # Trying to avoid setting $_, or relying on local $_ to work. |
107 | sub display { |
108 | my @result; |
109 | foreach my $x (@_) { |
110 | if (defined $x and not ref $x) { |
111 | my $y = ''; |
112 | foreach my $c (unpack("U*", $x)) { |
113 | if ($c > 255) { |
114 | $y .= sprintf "\\x{%x}", $c; |
115 | } elsif ($backslash_escape{$c}) { |
116 | $y .= $backslash_escape{$c}; |
117 | } else { |
118 | my $z = chr $c; # Maybe we can get away with a literal... |
119 | $z = sprintf "\\%03o", $c if $z =~ /[[:^print:]]/; |
120 | $y .= $z; |
121 | } |
122 | } |
123 | $x = $y; |
124 | } |
125 | return $x unless wantarray; |
126 | push @result, $x; |
127 | } |
128 | return @result; |
129 | } |
130 | |
69026470 |
131 | sub is { |
7d932aad |
132 | my ($got, $expected, $name, @mess) = @_; |
69026470 |
133 | my $pass = $got eq $expected; |
134 | unless ($pass) { |
b3c72391 |
135 | unshift(@mess, "# got "._q($got)."\n", |
136 | "# expected "._q($expected)."\n"); |
69026470 |
137 | } |
7d932aad |
138 | _ok($pass, _where(), $name, @mess); |
69026470 |
139 | } |
140 | |
3e90d5a3 |
141 | sub isnt { |
142 | my ($got, $isnt, $name, @mess) = @_; |
143 | my $pass = $got ne $isnt; |
144 | unless( $pass ) { |
b3c72391 |
145 | unshift(@mess, "# it should not be "._q($got)."\n", |
3e90d5a3 |
146 | "# but it is.\n"); |
147 | } |
148 | _ok($pass, _where(), $name, @mess); |
149 | } |
150 | |
69026470 |
151 | # Note: this isn't quite as fancy as Test::More::like(). |
152 | sub like { |
7d932aad |
153 | my ($got, $expected, $name, @mess) = @_; |
69026470 |
154 | my $pass; |
155 | if (ref $expected eq 'Regexp') { |
156 | $pass = $got =~ $expected; |
157 | unless ($pass) { |
435e7af6 |
158 | unshift(@mess, "# got '$got'\n", |
159 | "# expected /$expected/\n"); |
69026470 |
160 | } |
161 | } else { |
162 | $pass = $got =~ /$expected/; |
163 | unless ($pass) { |
7d932aad |
164 | unshift(@mess, "# got '$got'\n", |
165 | "# expected /$expected/\n"); |
69026470 |
166 | } |
167 | } |
7d932aad |
168 | _ok($pass, _where(), $name, @mess); |
69026470 |
169 | } |
170 | |
171 | sub pass { |
172 | _ok(1, '', @_); |
173 | } |
174 | |
175 | sub fail { |
176 | _ok(0, _where(), @_); |
177 | } |
178 | |
ad20d923 |
179 | sub curr_test { |
180 | return $test; |
181 | } |
182 | |
3e90d5a3 |
183 | sub next_test { |
184 | $test++ |
185 | } |
186 | |
69026470 |
187 | # Note: can't pass multipart messages since we try to |
188 | # be compatible with Test::More::skip(). |
189 | sub skip { |
7d932aad |
190 | my $why = shift; |
982b7cb7 |
191 | my $n = @_ ? shift : 1; |
69026470 |
192 | for (1..$n) { |
ad20d923 |
193 | print STDOUT "ok $test # skip: $why\n"; |
e6c299c8 |
194 | $test++; |
69026470 |
195 | } |
196 | local $^W = 0; |
197 | last SKIP; |
198 | } |
199 | |
200 | sub eq_array { |
201 | my ($ra, $rb) = @_; |
202 | return 0 unless $#$ra == $#$rb; |
203 | for my $i (0..$#$ra) { |
204 | return 0 unless $ra->[$i] eq $rb->[$i]; |
205 | } |
206 | return 1; |
207 | } |
208 | |
677fb045 |
209 | sub eq_hash { |
210 | my ($orig, $suspect) = @_; |
211 | my $fail; |
212 | while (my ($key, $value) = each %$suspect) { |
213 | # Force a hash recompute if this perl's internals can cache the hash key. |
214 | $key = "" . $key; |
215 | if (exists $orig->{$key}) { |
216 | if ($orig->{$key} ne $value) { |
de522f7a |
217 | print STDOUT "# key ", _qq($key), " was ", _qq($orig->{$key}), |
218 | " now ", _qq($value), "\n"; |
677fb045 |
219 | $fail = 1; |
220 | } |
221 | } else { |
de522f7a |
222 | print STDOUT "# key ", _qq($key), " is ", _qq($value), |
75385f53 |
223 | ", not in original.\n"; |
677fb045 |
224 | $fail = 1; |
225 | } |
226 | } |
227 | foreach (keys %$orig) { |
228 | # Force a hash recompute if this perl's internals can cache the hash key. |
229 | $_ = "" . $_; |
230 | next if (exists $suspect->{$_}); |
de522f7a |
231 | print STDOUT "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n"; |
677fb045 |
232 | $fail = 1; |
233 | } |
234 | !$fail; |
235 | } |
236 | |
69026470 |
237 | sub require_ok { |
238 | my ($require) = @_; |
239 | eval <<REQUIRE_OK; |
240 | require $require; |
241 | REQUIRE_OK |
1577bb16 |
242 | _ok(!$@, _where(), "require $require"); |
69026470 |
243 | } |
244 | |
245 | sub use_ok { |
246 | my ($use) = @_; |
247 | eval <<USE_OK; |
248 | use $use; |
249 | USE_OK |
1577bb16 |
250 | _ok(!$@, _where(), "use $use"); |
69026470 |
251 | } |
252 | |
137352a2 |
253 | # runperl - Runs a separate perl interpreter. |
254 | # Arguments : |
255 | # switches => [ command-line switches ] |
256 | # nolib => 1 # don't use -I../lib (included by default) |
257 | # prog => one-liner (avoid quotes) |
258 | # progfile => perl script |
259 | # stdin => string to feed the stdin |
260 | # stderr => redirect stderr to stdout |
261 | # args => [ command-line arguments to the perl program ] |
cb9c5e20 |
262 | # verbose => print the command line |
137352a2 |
263 | |
264 | my $is_mswin = $^O eq 'MSWin32'; |
265 | my $is_netware = $^O eq 'NetWare'; |
266 | my $is_macos = $^O eq 'MacOS'; |
267 | my $is_vms = $^O eq 'VMS'; |
268 | |
cb9c5e20 |
269 | sub _quote_args { |
270 | my ($runperl, $args) = @_; |
271 | |
272 | foreach (@$args) { |
273 | # In VMS protect with doublequotes because otherwise |
274 | # DCL will lowercase -- unless already doublequoted. |
ea9ac5ad |
275 | $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0; |
cb9c5e20 |
276 | $$runperl .= ' ' . $_; |
277 | } |
278 | } |
279 | |
137352a2 |
280 | sub runperl { |
281 | my %args = @_; |
282 | my $runperl = $^X; |
f93a5f07 |
283 | if ($args{switches}) { |
8da5ed17 |
284 | _quote_args(\$runperl, $args{switches}); |
137352a2 |
285 | } |
f93a5f07 |
286 | unless ($args{nolib}) { |
287 | if ($is_macos) { |
cb9c5e20 |
288 | $runperl .= ' -I::lib'; |
f93a5f07 |
289 | # Use UNIX style error messages instead of MPW style. |
cb9c5e20 |
290 | $runperl .= ' -MMac::err=unix' if $args{stderr}; |
137352a2 |
291 | } |
292 | else { |
cb9c5e20 |
293 | $runperl .= ' "-I../lib"'; # doublequotes because of VMS |
137352a2 |
294 | } |
295 | } |
296 | if (defined $args{prog}) { |
297 | if ($is_mswin || $is_netware || $is_vms) { |
298 | $runperl .= qq( -e ") . $args{prog} . qq("); |
299 | } |
300 | else { |
301 | $runperl .= qq( -e ') . $args{prog} . qq('); |
302 | } |
303 | } elsif (defined $args{progfile}) { |
304 | $runperl .= qq( "$args{progfile}"); |
305 | } |
306 | if (defined $args{stdin}) { |
5ae09a77 |
307 | # so we don't try to put literal newlines and crs onto the |
308 | # command line. |
309 | $args{stdin} =~ s/\n/\\n/g; |
310 | $args{stdin} =~ s/\r/\\r/g; |
311 | |
137352a2 |
312 | if ($is_mswin || $is_netware || $is_vms) { |
f93a5f07 |
313 | $runperl = qq{$^X -e "print qq(} . |
137352a2 |
314 | $args{stdin} . q{)" | } . $runperl; |
315 | } |
316 | else { |
f93a5f07 |
317 | $runperl = qq{$^X -e 'print qq(} . |
137352a2 |
318 | $args{stdin} . q{)' | } . $runperl; |
319 | } |
320 | } |
321 | if (defined $args{args}) { |
cb9c5e20 |
322 | _quote_args(\$runperl, $args{args}); |
323 | } |
324 | $runperl .= ' 2>&1' if $args{stderr} && !$is_macos; |
325 | $runperl .= " \xB3 Dev:Null" if !$args{stderr} && $is_macos; |
326 | if ($args{verbose}) { |
327 | my $runperldisplay = $runperl; |
328 | $runperldisplay =~ s/\n/\n\#/g; |
75385f53 |
329 | print STDERR "# $runperldisplay\n"; |
137352a2 |
330 | } |
137352a2 |
331 | my $result = `$runperl`; |
332 | $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these |
333 | return $result; |
334 | } |
335 | |
8799135f |
336 | |
c4fbe247 |
337 | sub DIE { |
75385f53 |
338 | print STDERR "# @_\n"; |
c4fbe247 |
339 | exit 1; |
8799135f |
340 | } |
341 | |
b5fe401b |
342 | # A somewhat safer version of the sometimes wrong $^X. |
17a740d5 |
343 | my $Perl; |
344 | sub which_perl { |
345 | unless (defined $Perl) { |
346 | $Perl = $^X; |
347 | |
348 | my $exe; |
349 | eval "require Config; Config->import"; |
85363d30 |
350 | if ($@) { |
17a740d5 |
351 | warn "test.pl had problems loading Config: $@"; |
352 | $exe = ''; |
85363d30 |
353 | } else { |
17a740d5 |
354 | $exe = $Config{_exe}; |
85363d30 |
355 | } |
da405c16 |
356 | $exe = '' unless defined $exe; |
17a740d5 |
357 | |
358 | # This doesn't absolutize the path: beware of future chdirs(). |
359 | # We could do File::Spec->abs2rel() but that does getcwd()s, |
360 | # which is a bit heavyweight to do here. |
361 | |
362 | if ($Perl =~ /^perl\Q$exe\E$/i) { |
8db06b02 |
363 | my $perl = "perl$exe"; |
17a740d5 |
364 | eval "require File::Spec"; |
365 | if ($@) { |
366 | warn "test.pl had problems loading File::Spec: $@"; |
8db06b02 |
367 | $Perl = "./$perl"; |
17a740d5 |
368 | } else { |
8db06b02 |
369 | $Perl = File::Spec->catfile(File::Spec->curdir(), $perl); |
17a740d5 |
370 | } |
371 | } |
196918b0 |
372 | |
373 | # Build up the name of the executable file from the name of |
374 | # the command. |
375 | |
376 | if ($Perl !~ /\Q$exe\E$/i) { |
377 | $Perl .= $exe; |
378 | } |
c880be78 |
379 | |
8db06b02 |
380 | warn "which_perl: cannot find $Perl from $^X" unless -f $Perl; |
17a740d5 |
381 | |
382 | # For subcommands to use. |
383 | $ENV{PERLEXE} = $Perl; |
85363d30 |
384 | } |
17a740d5 |
385 | return $Perl; |
b5fe401b |
386 | } |
387 | |
435e7af6 |
388 | sub unlink_all { |
389 | foreach my $file (@_) { |
390 | 1 while unlink $file; |
75385f53 |
391 | print STDERR "# Couldn't unlink '$file': $!\n" if -f $file; |
435e7af6 |
392 | } |
393 | } |
eeabcb2d |
394 | |
395 | |
396 | my $tmpfile = "misctmp000"; |
397 | 1 while -f ++$tmpfile; |
398 | END { unlink_all $tmpfile } |
399 | |
f5cda331 |
400 | # |
401 | # _fresh_perl |
402 | # |
403 | # The $resolve must be a subref that tests the first argument |
404 | # for success, or returns the definition of success (e.g. the |
405 | # expected scalar) if given no arguments. |
406 | # |
407 | |
408 | sub _fresh_perl { |
409 | my($prog, $resolve, $runperl_args, $name) = @_; |
eeabcb2d |
410 | |
411 | $runperl_args ||= {}; |
412 | $runperl_args->{progfile} = $tmpfile; |
413 | $runperl_args->{stderr} = 1; |
414 | |
415 | open TEST, ">$tmpfile" or die "Cannot open $tmpfile: $!"; |
416 | |
417 | # VMS adjustments |
418 | if( $^O eq 'VMS' ) { |
419 | $prog =~ s#/dev/null#NL:#; |
420 | |
421 | # VMS file locking |
422 | $prog =~ s{if \(-e _ and -f _ and -r _\)} |
423 | {if (-e _ and -f _)} |
424 | } |
425 | |
426 | print TEST $prog, "\n"; |
427 | close TEST or die "Cannot close $tmpfile: $!"; |
428 | |
429 | my $results = runperl(%$runperl_args); |
430 | my $status = $?; |
431 | |
432 | # Clean up the results into something a bit more predictable. |
433 | $results =~ s/\n+$//; |
434 | $results =~ s/at\s+misctmp\d+\s+line/at - line/g; |
435 | $results =~ s/of\s+misctmp\d+\s+aborted/of - aborted/g; |
436 | |
437 | # bison says 'parse error' instead of 'syntax error', |
438 | # various yaccs may or may not capitalize 'syntax'. |
439 | $results =~ s/^(syntax|parse) error/syntax error/mig; |
440 | |
441 | if ($^O eq 'VMS') { |
442 | # some tests will trigger VMS messages that won't be expected |
443 | $results =~ s/\n?%[A-Z]+-[SIWEF]-[A-Z]+,.*//; |
444 | |
445 | # pipes double these sometimes |
446 | $results =~ s/\n\n/\n/g; |
447 | } |
448 | |
f5cda331 |
449 | my $pass = $resolve->($results); |
eeabcb2d |
450 | unless ($pass) { |
451 | print STDERR "# PROG: $switch\n$prog\n"; |
f5cda331 |
452 | print STDERR "# EXPECTED:\n", $resolve->(), "\n"; |
eeabcb2d |
453 | print STDERR "# GOT:\n$results\n"; |
454 | print STDERR "# STATUS: $status\n"; |
455 | } |
456 | |
e2c38acd |
457 | # Use the first line of the program as a name if none was given |
458 | unless( $name ) { |
459 | ($first_line, $name) = $prog =~ /^((.{1,50}).*)/; |
460 | $name .= '...' if length $first_line > length $name; |
461 | } |
eeabcb2d |
462 | |
f5cda331 |
463 | _ok($pass, _where(), "fresh_perl - $name"); |
464 | } |
465 | |
466 | # |
467 | # run_perl_is |
468 | # |
469 | # Combination of run_perl() and is(). |
470 | # |
471 | |
472 | sub fresh_perl_is { |
473 | my($prog, $expected, $runperl_args, $name) = @_; |
474 | _fresh_perl($prog, |
475 | sub { @_ ? $_[0] eq $expected : $expected }, |
476 | $runperl_args, $name); |
477 | } |
478 | |
479 | # |
480 | # run_perl_like |
481 | # |
482 | # Combination of run_perl() and like(). |
483 | # |
484 | |
485 | sub fresh_perl_like { |
486 | my($prog, $expected, $runperl_args, $name) = @_; |
487 | _fresh_perl($prog, |
488 | sub { @_ ? |
489 | $_[0] =~ (ref $expected ? $expected : /$expected/) : |
490 | $expected }, |
491 | $runperl_args, $name); |
eeabcb2d |
492 | } |
493 | |
69026470 |
494 | 1; |