2 # t/test.pl - most of Test::More functionality without the fuss
7 # Do not rely on features found only in more modern Perls here, as some CPAN
8 # distributions copy this file and must operate on older Perls. Similarly, keep
9 # things, simple as this may be run under fairly broken circumstances. For
10 # example, increment ($x++) has a certain amount of cleverness for things like
13 # $x++; # $x eq 'aaa';
15 # This stands more chance of breaking than just a simple
19 # In this file, we use the latter "Baby Perl" approach, and increment
20 # will be worked over by t/op/inc.t
28 # This defines ASCII/UTF-8 vs EBCDIC/UTF-EBCDIC
29 $::IS_ASCII = ord 'A' == 65;
30 $::IS_EBCDIC = ord 'A' == 193;
34 $Tests_Are_Passing = 1;
36 # Use this instead of print to avoid interference while testing globals.
38 local($\, $", $,) = (undef, ' ', '') if "$]" >= 5.004;
43 local($\, $", $,) = (undef, ' ', '') if "$]" >= 5.004;
51 if ($n eq 'no_plan') {
57 $plan{skip_all} and skip_all($plan{skip_all});
60 _print "1..$n\n" unless $noplan;
65 # Set the plan at the end. See Test::More::done_testing.
78 if (defined $planned && $planned != $ran) {
80 "# Looks like you planned $planned tests but ran $ran.\n";
89 my @mess = _comment(@_);
90 $TODO ? _print(@mess) : _print_stderr(@mess);
93 # Use this instead of "print STDERR" when outputting failure diagnostic
99 # Use this instead of "print" when outputting informational messages
102 _print( _comment(@_) );
106 return map { /^#/ ? "$_\n" : "# $_\n" }
107 map { split /\n/ } @_;
110 sub _have_dynamic_extension {
111 my $extension = shift;
112 unless (eval {require Config; 1}) {
113 warn "test.pl had problems loading Config: $@";
116 $extension =~ s!::!/!g;
117 return 1 if ($Config::Config{extensions} =~ /\b$extension\b/);
122 _print "1..0 # Skip @_\n";
131 _print("Bail out! $reason\n");
136 my ($pass, $where, $name, @mess) = @_;
137 # Do not try to microoptimize by factoring out the "not ".
141 # escape out '#' or it will interfere with '# skip' and such
143 $out = $pass ? "ok $test - $name" : "not ok $test - $name";
145 $out = $pass ? "ok $test" : "not ok $test";
149 $out = $out . " # TODO $TODO";
151 $Tests_Are_Passing = 0 unless $pass;
157 note @mess; # Ensure that the message is properly escaped.
160 my $msg = "# Failed test $test - ";
161 $msg.= "$name " if $name;
167 $test = $test + 1; # don't use ++
173 my @caller = caller($Level);
174 return "at $caller[1] line $caller[2]";
178 if (@_ > 1) { # ok() really was modern 'is', though limited
179 local $Level = $Level + 1;
182 my ($pass, $name, @mess) = @_;
183 _ok($pass, _where(), $name, @mess);
188 return 'undef' unless defined $x;
197 return defined $x ? '"' . display ($x) . '"' : 'undef';
200 # Support pre-5.10 Perls, for the benefit of CPAN dists that copy this file.
201 # Note that chr(90) exists in both ASCII ("Z") and EBCDIC ("!").
202 my $chars_template = defined(eval { pack "W*", 90 }) ? "W*" : "U*";
203 eval 'sub re::is_regexp { ref($_[0]) eq "Regexp" }'
204 if !defined &re::is_regexp;
206 # keys are the codes \n etc map to, values are 2 char strings such as \n
207 my %backslash_escape;
209 foreach $x (split //, 'nrtfa\\\'"') {
210 $backslash_escape{ord eval "\"\\$x\""} = "\\$x";
212 # A way to display scalars containing control characters and Unicode.
213 # Trying to avoid setting $_, or relying on local $_ to work.
218 if (defined $x and not ref $x) {
221 foreach $c (unpack($chars_template, $x)) {
223 $y = $y . sprintf "\\x{%x}", $c;
224 } elsif ($backslash_escape{$c}) {
225 $y = $y . $backslash_escape{$c};
227 my $z = chr $c; # Maybe we can get away with a literal...
228 my $is_printable = ($::IS_ASCII)
229 ? $c >= ord(" ") && $c <= ord("~")
230 : $z !~ /[^[:^print:][:^ascii:]]/;
231 # /[::]/ was introduced before non-ASCII support
232 # The pattern above is equivalent (by de Morgan's
234 # $z !~ /(?[ [:print:] & [:ascii:] ])/
235 # or, $z is not an ascii printable character
237 unless ($is_printable) {
238 # Use octal for characters with small ordinals that
239 # are traditionally expressed as octal: the controls
240 # below space, which on EBCDIC are almost all the
241 # controls, but on ASCII don't include DEL nor the C1
244 $z = sprintf "\\%03o", $c;
246 $z = sprintf "\\x{%x}", $c;
254 return $x unless wantarray;
261 my ($got, $expected, $name, @mess) = @_;
264 if( !defined $got || !defined $expected ) {
265 # undef only matches undef
266 $pass = !defined $got && !defined $expected;
269 $pass = $got eq $expected;
273 unshift(@mess, "# got "._qq($got)."\n",
274 "# expected "._qq($expected)."\n");
276 _ok($pass, _where(), $name, @mess);
280 my ($got, $isnt, $name, @mess) = @_;
283 if( !defined $got || !defined $isnt ) {
284 # undef only matches undef
285 $pass = defined $got || defined $isnt;
288 $pass = $got ne $isnt;
292 unshift(@mess, "# it should not be "._qq($got)."\n",
295 _ok($pass, _where(), $name, @mess);
299 my($got, $type, $expected, $name, @mess) = @_;
304 local($@,$!); # don't interfere with $@
305 # eval() sometimes resets $!
306 $pass = eval "\$got $type \$expected";
309 # It seems Irix long doubles can have 2147483648 and 2147483648
310 # that stringify to the same thing but are actually numerically
311 # different. Display the numbers if $type isn't a string operator,
312 # and the numbers are stringwise the same.
313 # (all string operators have alphabetic names, so tr/a-z// is true)
314 # This will also show numbers for some unneeded cases, but will
315 # definitely be helpful for things such as == and <= that fail
316 if ($got eq $expected and $type !~ tr/a-z//) {
317 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
319 unshift(@mess, "# got "._qq($got)."\n",
320 "# expected $type "._qq($expected)."\n");
322 _ok($pass, _where(), $name, @mess);
325 # Check that $got is within $range of $expected
326 # if $range is 0, then check it's exact
327 # else if $expected is 0, then $range is an absolute value
328 # otherwise $range is a fractional error.
329 # Here $range must be numeric, >= 0
330 # Non numeric ranges might be a useful future extension. (eg %)
332 my ($got, $expected, $range, $name, @mess) = @_;
334 if (!defined $got or !defined $expected or !defined $range) {
335 # This is a fail, but doesn't need extra diagnostics
336 } elsif ($got !~ tr/0-9// or $expected !~ tr/0-9// or $range !~ tr/0-9//) {
338 unshift @mess, "# got, expected and range must be numeric\n";
339 } elsif ($range < 0) {
340 # This is also a fail
341 unshift @mess, "# range must not be negative\n";
342 } elsif ($range == 0) {
344 $pass = $got == $expected;
345 } elsif ($expected == 0) {
346 # If expected is 0, treat range as absolute
347 $pass = ($got <= $range) && ($got >= - $range);
349 my $diff = $got - $expected;
350 $pass = abs ($diff / $expected) < $range;
353 if ($got eq $expected) {
354 unshift @mess, "# $got - $expected = " . ($got - $expected) . "\n";
356 unshift@mess, "# got "._qq($got)."\n",
357 "# expected "._qq($expected)." (within "._qq($range).")\n";
359 _ok($pass, _where(), $name, @mess);
367 _ok(0, _where(), @_);
377 $test = $test + 1; # don't use ++
381 # Note: can't pass multipart messages since we try to
382 # be compatible with Test::More::skip().
385 my $n = @_ ? shift : 1;
390 $bad_swap = $why > 0 && $n == 0;
391 $both_zero = $why == 0 && $n == 0;
393 if ($bad_swap || $both_zero || @_) {
394 my $arg = "'$why', '$n'";
396 $arg .= join(", ", '', map { qq['$_'] } @_);
398 die qq[$0: expected skip(why, count), got skip($arg)\n];
401 _print "ok $test # skip $why\n";
410 return 0 unless $#$ra == $#$rb;
413 next if !defined $ra->[$i] && !defined $rb->[$i];
414 return 0 if !defined $ra->[$i];
415 return 0 if !defined $rb->[$i];
416 return 0 unless $ra->[$i] eq $rb->[$i];
422 my ($orig, $suspect) = @_;
424 while (my ($key, $value) = each %$suspect) {
425 # Force a hash recompute if this perl's internals can cache the hash key.
427 if (exists $orig->{$key}) {
429 defined $orig->{$key} != defined $value
430 || (defined $value && $orig->{$key} ne $value)
432 _print "# key ", _qq($key), " was ", _qq($orig->{$key}),
433 " now ", _qq($value), "\n";
437 _print "# key ", _qq($key), " is ", _qq($value),
438 ", not in original.\n";
442 foreach (keys %$orig) {
443 # Force a hash recompute if this perl's internals can cache the hash key.
445 next if (exists $suspect->{$_});
446 _print "# key ", _qq($_), " was ", _qq($orig->{$_}), " now missing.\n";