source: trunk/essentials/dev-lang/perl/t/test.pl@ 3314

Last change on this file since 3314 was 3181, checked in by bird, 19 years ago

perl 5.8.8

File size: 18.3 KB
Line 
1#
2# t/test.pl - most of Test::More functionality without the fuss
3#
4
5$Level = 1;
6my $test = 1;
7my $planned;
8my $noplan;
9
10$TODO = 0;
11$NO_ENDING = 0;
12
13sub plan {
14 my $n;
15 if (@_ == 1) {
16 $n = shift;
17 if ($n eq 'no_plan') {
18 undef $n;
19 $noplan = 1;
20 }
21 } else {
22 my %plan = @_;
23 $n = $plan{tests};
24 }
25 print STDOUT "1..$n\n" unless $noplan;
26 $planned = $n;
27}
28
29END {
30 my $ran = $test - 1;
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 }
38 }
39}
40
41# Use this instead of "print STDERR" when outputing failure diagnostic
42# messages
43sub _diag {
44 return unless @_;
45 my @mess = map { /^#/ ? "$_\n" : "# $_\n" }
46 map { split /\n/ } @_;
47 my $fh = $TODO ? *STDOUT : *STDERR;
48 print $fh @mess;
49
50}
51
52sub skip_all {
53 if (@_) {
54 print STDOUT "1..0 # Skipped: @_\n";
55 } else {
56 print STDOUT "1..0\n";
57 }
58 exit(0);
59}
60
61sub _ok {
62 my ($pass, $where, $name, @mess) = @_;
63 # Do not try to microoptimize by factoring out the "not ".
64 # VMS will avenge.
65 my $out;
66 if ($name) {
67 # escape out '#' or it will interfere with '# skip' and such
68 $name =~ s/#/\\#/g;
69 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
70 } else {
71 $out = $pass ? "ok $test" : "not ok $test";
72 }
73
74 $out .= " # TODO $TODO" if $TODO;
75 print STDOUT "$out\n";
76
77 unless ($pass) {
78 _diag "# Failed $where\n";
79 }
80
81 # Ensure that the message is properly escaped.
82 _diag @mess;
83
84 $test++;
85
86 return $pass;
87}
88
89sub _where {
90 my @caller = caller($Level);
91 return "at $caller[1] line $caller[2]";
92}
93
94# DON'T use this for matches. Use like() instead.
95sub ok ($@) {
96 my ($pass, $name, @mess) = @_;
97 _ok($pass, _where(), $name, @mess);
98}
99
100sub _q {
101 my $x = shift;
102 return 'undef' unless defined $x;
103 my $q = $x;
104 $q =~ s/\\/\\\\/g;
105 $q =~ s/'/\\'/g;
106 return "'$q'";
107}
108
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
145sub is ($$@) {
146 my ($got, $expected, $name, @mess) = @_;
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
157 unless ($pass) {
158 unshift(@mess, "# got "._q($got)."\n",
159 "# expected "._q($expected)."\n");
160 }
161 _ok($pass, _where(), $name, @mess);
162}
163
164sub isnt ($$@) {
165 my ($got, $isnt, $name, @mess) = @_;
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
176 unless( $pass ) {
177 unshift(@mess, "# it should not be "._q($got)."\n",
178 "# but it is.\n");
179 }
180 _ok($pass, _where(), $name, @mess);
181}
182
183sub cmp_ok ($$$@) {
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 %)
216sub within ($$$@) {
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
247# Note: this isn't quite as fancy as Test::More::like().
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) = @_;
254 my $pass;
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");
260 }
261 local $Level = 2;
262 _ok($pass, _where(), $name, @mess);
263}
264
265sub pass {
266 _ok(1, '', @_);
267}
268
269sub fail {
270 _ok(0, _where(), @_);
271}
272
273sub curr_test {
274 $test = shift if @_;
275 return $test;
276}
277
278sub next_test {
279 $test++;
280}
281
282# Note: can't pass multipart messages since we try to
283# be compatible with Test::More::skip().
284sub skip {
285 my $why = shift;
286 my $n = @_ ? shift : 1;
287 for (1..$n) {
288 print STDOUT "ok $test # skip: $why\n";
289 $test++;
290 }
291 local $^W = 0;
292 last SKIP;
293}
294
295sub todo_skip {
296 my $why = shift;
297 my $n = @_ ? shift : 1;
298
299 for (1..$n) {
300 print STDOUT "not ok $test # TODO & SKIP: $why\n";
301 $test++;
302 }
303 local $^W = 0;
304 last TODO;
305}
306
307sub eq_array {
308 my ($ra, $rb) = @_;
309 return 0 unless $#$ra == $#$rb;
310 for my $i (0..$#$ra) {
311 next if !defined $ra->[$i] && !defined $rb->[$i];
312 return 0 if !defined $ra->[$i];
313 return 0 if !defined $rb->[$i];
314 return 0 unless $ra->[$i] eq $rb->[$i];
315 }
316 return 1;
317}
318
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) {
327 print STDOUT "# key ", _qq($key), " was ", _qq($orig->{$key}),
328 " now ", _qq($value), "\n";
329 $fail = 1;
330 }
331 } else {
332 print STDOUT "# key ", _qq($key), " is ", _qq($value),
333 ", not in original.\n";
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->{$_});
341 print STDOUT "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";
342 $fail = 1;
343 }
344 !$fail;
345}
346
347sub require_ok ($) {
348 my ($require) = @_;
349 eval <<REQUIRE_OK;
350require $require;
351REQUIRE_OK
352 _ok(!$@, _where(), "require $require");
353}
354
355sub use_ok ($) {
356 my ($use) = @_;
357 eval <<USE_OK;
358use $use;
359USE_OK
360 _ok(!$@, _where(), "use $use");
361}
362
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)
368# progs => [ multi-liner (avoid quotes) ]
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 ]
373# verbose => print the command line
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
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.
386 $_ = q(").$_.q(") if $is_vms && !/^\"/ && length($_) > 0;
387 $$runperl .= ' ' . $_;
388 }
389}
390
391sub _create_runperl { # Create the string to qx in runperl().
392 my %args = @_;
393 my $runperl = $^X =~ m/\s/ ? qq{"$^X"} : $^X;
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 }
398 unless ($args{nolib}) {
399 if ($is_macos) {
400 $runperl .= ' -I::lib';
401 # Use UNIX style error messages instead of MPW style.
402 $runperl .= ' -MMac::err=unix' if $args{stderr};
403 }
404 else {
405 $runperl .= ' "-I../lib"'; # doublequotes because of VMS
406 }
407 }
408 if ($args{switches}) {
409 local $Level = 2;
410 die "test.pl:runperl(): 'switches' must be an ARRAYREF " . _where()
411 unless ref $args{switches} eq "ARRAY";
412 _quote_args(\$runperl, $args{switches});
413 }
414 if (defined $args{prog}) {
415 die "test.pl:runperl(): both 'prog' and 'progs' cannot be used " . _where()
416 if defined $args{progs};
417 $args{progs} = [$args{prog}]
418 }
419 if (defined $args{progs}) {
420 die "test.pl:runperl(): 'progs' must be an ARRAYREF " . _where()
421 unless ref $args{progs} eq "ARRAY";
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 }
430 } elsif (defined $args{progfile}) {
431 $runperl .= qq( "$args{progfile}");
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};
438 }
439 if (defined $args{stdin}) {
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;
444
445 if ($is_mswin || $is_netware || $is_vms) {
446 $runperl = qq{$^X -e "print qq(} .
447 $args{stdin} . q{)" | } . $runperl;
448 }
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 }
462 else {
463 $runperl = qq{$^X -e 'print qq(} .
464 $args{stdin} . q{)' | } . $runperl;
465 }
466 }
467 if (defined $args{args}) {
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;
475 print STDERR "# $runperldisplay\n";
476 }
477 return $runperl;
478}
479
480sub runperl {
481 die "test.pl:runperl() does not take a hashref"
482 if ref $_[0] and ref $_[0] eq 'HASH';
483 my $runperl = &_create_runperl;
484 my $result = `$runperl`;
485 $result =~ s/\n\n/\n/ if $is_vms; # XXX pipes sometimes double these
486 return $result;
487}
488
489*run_perl = \&runperl; # Nice alias.
490
491sub DIE {
492 print STDERR "# @_\n";
493 exit 1;
494}
495
496# A somewhat safer version of the sometimes wrong $^X.