/usr/share/perl5/Test/Unit/Assert.pm is in libtest-unit-perl 0.25-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 | package Test::Unit::Assert;
use strict;
use Test::Unit::Debug qw(debug);
use Test::Unit::Failure;
use Test::Unit::Error;
use Test::Unit::Exception;
use Test::Unit::Assertion::CodeRef;
use Error qw/:try/;
use Carp;
sub assert {
my $self = shift;
my $assertion = $self->normalize_assertion(shift);
$self->do_assertion($assertion, (caller($Error::Depth))[0 .. 2], @_);
}
sub normalize_assertion {
my $self = shift;
my $assertion = shift;
if (!ref($assertion) || ref($assertion) =~ 'ARRAY') {
debug((defined $assertion ? $assertion : '_undef_') .
" normalized as boolean\n");
require Test::Unit::Assertion::Boolean;
return Test::Unit::Assertion::Boolean->new($assertion);
}
# If we're this far, we must have a reference.
if (eval {$assertion->isa('Regexp')}) {
debug("$assertion normalized as Regexp\n");
require Test::Unit::Assertion::Regexp;
return Test::Unit::Assertion::Regexp->new($assertion);
}
if (ref($assertion) eq 'CODE') {
debug("$assertion normalized as coderef\n");
require Test::Unit::Assertion::CodeRef;
return Test::Unit::Assertion::CodeRef->new($assertion);
}
# if (ref($assertion) eq 'SCALAR') {
# debug("$assertion normalized as scalar ref\n");
# require Test::Unit::Assertion::Scalar;
# return Test::Unit::Assertion::Scalar->new($assertion);
# }
if (ref($assertion) !~ /^(GLOB|LVALUE|REF|SCALAR)$/) {
debug("$assertion already an object\n");
require Test::Unit::Assertion::Boolean;
return $assertion->can('do_assertion') ? $assertion :
Test::Unit::Assertion::Boolean->new($assertion);
}
else {
die "Don't know how to normalize $assertion (ref ", ref($assertion), ")\n";
}
}
sub assert_raises {
my $self = shift;
require Test::Unit::Assertion::Exception;
my $assertion = Test::Unit::Assertion::Exception->new(shift);
my ($asserter, $file, $line) = caller($Error::Depth);
my $exception =
$self->do_assertion($assertion, (caller($Error::Depth))[0 .. 2], @_);
}
sub do_assertion {
my $self = shift;
my $assertion = shift;
my $asserter = shift;
my $file = shift;
my $line = shift;
debug("Asserting [$assertion] from $asserter in $file line $line\n");
my @args = @_;
try { $assertion->do_assertion(@args) }
catch Test::Unit::Exception with {
my $e = shift;
debug(" Caught $e, rethrowing from $asserter, $file line $line\n");
$e->throw_new(-package => $asserter,
-file => $file,
-line => $line,
-object => $self);
}
}
sub multi_assert {
my $self = shift;
my ($assertion, @argsets) = @_;
my ($asserter, $file, $line) = caller($Error::Depth);
foreach my $argset (@argsets) {
try {
$self->assert($assertion, @$argset);
}
catch Test::Unit::Exception with {
my $e = shift;
debug(" Caught $e, rethrowing from $asserter, $file line $line\n");
$e->throw_new(-package => $asserter,
-file => $file,
-line => $line,
-object => $self);
}
}
}
sub is_numeric {
my $str = shift;
local $^W;
return defined $str && ! ($str == 0 && $str !~ /^\s*[+-]?0(e0)?\s*$/i);
}
# First argument determines the comparison type.
sub assert_equals {
my $self = shift;
my($asserter, $file, $line) = caller($Error::Depth);
my @args = @_;
try {
if (! defined($args[0]) and ! defined($args[1])) {
# pass
}
elsif (defined($args[0]) xor defined($args[1])) {
$self->fail('one arg was not defined');
}
elsif (is_numeric($args[0])) {
$self->assert_num_equals(@args);
}
elsif (eval {ref($args[0]) && $args[0]->isa('UNIVERSAL')}) {
require overload;
if (overload::Method($args[0], '==')) {
$self->assert_num_equals(@args);
}
else {
$self->assert_str_equals(@args);
}
}
else {
$self->assert_str_equals(@args);
}
}
catch Test::Unit::Exception with {
my $e = shift;
$e->throw_new(-package => $asserter,
-file => $file,
-line => $line,
-object => $self);
}
}
sub ok { # To make porting from Test easier
my $self = shift;
my @args = @_;
local $Error::Depth = $Error::Depth + 1;
if (@args == 1) {
$self->assert($args[0]); # boolean assertion
}
elsif (@args >= 2 && @args <= 3) {
if (ref($args[0]) eq 'CODE') {
my $code = shift @args;
my $expected = shift @args;
$self->assert_equals($expected, $code->(), @args);
}
elsif (eval {$args[1]->isa('Regexp')}) {
my $got = shift @args;
my $re = shift @args;
$self->assert($re, $got, @args);
}
else {
my $got = shift @args;
my $expected = shift @args;
$self->assert_equals($expected, $got, @args);
}
}
else {
$self->error('ok() called with wrong number of args');
}
}
sub assert_not_equals {
my $self = shift;
my($asserter,$file,$line) = caller($Error::Depth);
my @args = @_;
try {
if (! defined($args[0]) && ! defined($args[1])) {
my $first = shift @args;
my $second = shift @args;
$self->fail(@args ? join('', @args) : 'both args were undefined');
}
elsif (defined($args[0]) xor defined($args[1])) {
# succeed
}
elsif (is_numeric($args[0])) {
$self->assert_num_not_equals(@args);
}
elsif (eval {ref($args[0]) && $args[0]->isa('UNIVERSAL')}) {
require overload;
if (overload::Method($args[0], '==')) {
$self->assert_num_not_equals(@args);
}
else {
$self->assert_str_not_equals(@args);
}
}
else {
$self->assert_str_not_equals(@args);
}
}
catch Test::Unit::Exception with {
my $e = shift;
$e->throw_new(-package => $asserter,
-file => $file,
-line => $line,
-object => $self,);
};
}
# Shamelessly pinched from Test::More and adapted to Test::Unit.
our %Seen_Refs = ();
our @Data_Stack;
my $DNE = bless [], 'Does::Not::Exist';
sub assert_deep_equals {
my $self = shift;
my $this = shift;
my $that = shift;
local $Error::Depth = $Error::Depth + 1;
if (! ref $this || ! ref $that) {
Test::Unit::Failure->throw(
-text => @_ ? join('', @_)
: 'Both arguments were not references'
);
}
local @Data_Stack = ();
local %Seen_Refs = ();
if (! $self->_deep_check($this, $that)) {
Test::Unit::Failure->throw(
-text => @_ ? join('', @_)
: $self->_format_stack(@Data_Stack)
);
}
}
sub _deep_check {
my $self = shift;
my ($e1, $e2) = @_;
if ( ! defined $e1 || ! defined $e2 ) {
return 1 if !defined $e1 && !defined $e2;
push @Data_Stack, { vals => [$e1, $e2] };
return 0;
}
return 0 if ( (defined $e1 && $e1 eq $DNE)
|| (defined $e2 && $e2 eq $DNE ));
return 1 if $e1 eq $e2;
if ( ref $e1 && ref $e2 ) {
my $e2_ref = "$e2";
return 1 if defined $Seen_Refs{$e1} && $Seen_Refs{$e1} eq $e2_ref;
$Seen_Refs{$e1} = $e2_ref;
}
if (UNIVERSAL::isa($e1, 'ARRAY') and UNIVERSAL::isa($e2, 'ARRAY')) {
return $self->_eq_array($e1, $e2);
}
elsif (UNIVERSAL::isa($e1, 'HASH') and UNIVERSAL::isa($e2, 'HASH')) {
return $self->_eq_hash($e1, $e2);
}
elsif (UNIVERSAL::isa($e1, 'REF') and UNIVERSAL::isa($e2, 'REF')) {
push @Data_Stack, { type => 'REF', vals => [$e1, $e2] };
my $ok = $self->_deep_check($$e1, $$e2);
pop @Data_Stack if $ok;
return $ok;
}
elsif (UNIVERSAL::isa($e1, 'SCALAR') and UNIVERSAL::isa($e2, 'SCALAR')) {
push @Data_Stack, { type => 'REF', vals => [$e1, $e2] };
return $self->_deep_check($$e1, $$e2);
}
else {
push @Data_Stack, { vals => [$e1, $e2] };
return 0;
}
}
sub _eq_array {
my $self = shift;
my($a1, $a2) = @_;
return 1 if $a1 eq $a2;
my $ok = 1;
my $max = $#$a1 > $#$a2 ? $#$a1 : $#$a2;
for (0..$max) {
my $e1 = $_ > $#$a1 ? $DNE : $a1->[$_];
my $e2 = $_ > $#$a2 ? $DNE : $a2->[$_];
push @Data_Stack, { type => 'ARRAY', idx => $_, vals => [$e1, $e2] };
$ok = $self->_deep_check($e1,$e2);
pop @Data_Stack if $ok;
last unless $ok;
}
return $ok;
}
sub _eq_hash {
my $self = shift;
my($a1, $a2) = @_;
return 1 if $a1 eq $a2;
my $ok = 1;
my $bigger = keys %$a1 > keys %$a2 ? $a1 : $a2;
foreach my $k (keys %$bigger) {
my $e1 = exists $a1->{$k} ? $a1->{$k} : $DNE;
my $e2 = exists $a2->{$k} ? $a2->{$k} : $DNE;
push @Data_Stack, { type => 'HASH', idx => $k, vals => [$e1, $e2] };
$ok = $self->_deep_check($e1, $e2);
pop @Data_Stack if $ok;
last unless $ok;
}
return $ok;
}
sub _format_stack {
my $self = shift;
my @Stack = @_;
my $var = '$FOO';
my $did_arrow = 0;
foreach my $entry (@Stack) {
my $type = $entry->{type} || '';
my $idx = $entry->{'idx'};
if( $type eq 'HASH' ) {
$var .= "->" unless $did_arrow++;
$var .= "{$idx}";
}
elsif( $type eq 'ARRAY' ) {
$var .= "->" unless $did_arrow++;
$var .= "[$idx]";
}
elsif( $type eq 'REF' ) {
$var = "\${$var}";
}
}
my @vals = @{$Stack[-1]{vals}}[0,1];
my @vars = ();
($vars[0] = $var) =~ s/\$FOO/ \$a/;
($vars[1] = $var) =~ s/\$FOO/ \$b/;
my $out = "Structures begin differing at:\n";
foreach my $idx (0..$#vals) {
my $val = $vals[$idx];
$vals[$idx] = !defined $val ? 'undef' :
$val eq $DNE ? "Does not exist"
: "'$val'";
}
$out .= "$vars[0] = $vals[0]\n";
$out .= "$vars[1] = $vals[1]\n";
return $out;
}
{
my %assert_subs = (
str_equals => sub {
my $str1 = shift;
my $str2 = shift;
defined $str1 or
Test::Unit::Failure->throw(
-text => @_ ? join('',@_) :
"expected value was undef; should be using assert_null?"
);
defined $str2 or
Test::Unit::Failure->throw(
-text => @_ ? join('',@_) : "expected '$str1', got undef"
);
$str1 eq $str2 or
Test::Unit::Failure->throw(
-text => @_ ? join('',@_) : "expected '$str1', got '$str2'"
);
},
str_not_equals => sub {
my $str1 = shift;
my $str2 = shift;
defined $str1 or
Test::Unit::Failure->throw(
-text => @_ ? join('',@_) :
"expected value was undef; should be using assert_not_null?"
);
defined $str2 or
Test::Unit::Failure->throw(
-text => @_ ? join('',@_) :
"expected a string ne '$str1', got undef"
);
$str1 ne $str2 or
Test::Unit::Failure->throw(
-text => @_ ? join('',@_) : "'$str1' and '$str2' should differ"
);
},
num_equals => sub {
my $num1 = shift;
my $num2 = shift;
defined $num1 or
Test::Unit::Failure->throw(
-text => @_ ? join('',@_) :
"expected value was undef; should be using assert_null?"
);
defined $num2 or
Test::Unit::Failure->throw(
-text => @_ ? join('',@_) : "expected '$num1', got undef"
);
# silence `Argument "" isn't numeric in numeric eq (==)' warnings
local $^W;
$num1 == $num2 or
Test::Unit::Failure->throw(
-text => @_ ? join('', @_) : "expected $num1, got $num2"
);
},
num_not_equals => sub {
my $num1 = shift;
my $num2 = shift;
defined $num1 or
Test::Unit::Failure->throw(
-text => @_ ? join('',@_) :
"expected value was undef; should be using assert_not_null?"
);
defined $num2 or
Test::Unit::Failure->throw(
-text => @_ ? join('',@_) :
"expected a number != '$num1', got undef"
);
# silence `Argument "" isn't numeric in numeric ne (!=)' warnings
local $^W;
$num1 != $num2 or
Test::Unit::Failure->throw(
-text => @_ ? join('', @_) : "$num1 and $num2 should differ"
);
},
matches => sub {
my $regexp = shift;
eval { $regexp->isa('Regexp') } or
Test::Unit::Error->throw(
-text => "arg 1 to assert_matches() must be a regexp"
);
my $string = shift;
$string =~ $regexp or
Test::Unit::Failure->throw
(-text => @_ ? join('', @_) :
"$string didn't match /$regexp/");
},
does_not_match => sub {
my $regexp = shift;
eval { $regexp->isa('Regexp') } or
Test::Unit::Error->throw(
-text => "arg 1 to assert_does_not_match() must be a regexp"
);
my $string = shift;
$string !~ $regexp or
Test::Unit::Failure->throw
(-text => @_ ? join('', @_) :
"$string matched /$regexp/");
},
null => sub {
my $arg = shift;
!defined($arg) or
Test::Unit::Failure->throw
(-text => @_ ? join('',@_) : "$arg is defined");
},
not_null => sub {
my $arg = shift;
defined($arg) or
Test::Unit::Failure->throw
(-text => @_ ? join('', @_) : "<undef> unexpected");
},
);
foreach my $type (keys %assert_subs) {
my $assertion = Test::Unit::Assertion::CodeRef->new($assert_subs{$type});
no strict 'refs';
*{"Test::Unit::Assert::assert_$type"} =
sub {
local $Error::Depth = $Error::Depth + 3;
my $self = shift;
$assertion->do_assertion(@_);
};
}
}
sub fail {
my $self = shift;
debug(ref($self) . "::fail() called\n");
my($asserter,$file,$line) = caller($Error::Depth);
my $message = join '', @_;
Test::Unit::Failure->throw(-text => $message,
-object => $self,
-file => $file,
-line => $line);
}
sub error {
my $self = shift;
debug(ref($self) . "::error() called\n");
my($asserter,$file,$line) = caller($Error::Depth);
my $message = join '', @_;
Test::Unit::Error->throw(-text => $message,
-object => $self,
-file => $file,
-line => $line);
}
sub quell_backtrace {
my $self = shift;
carp "quell_backtrace deprecated";
}
sub get_backtrace_on_fail {
my $self = shift;
carp "get_backtrace_on_fail deprecated";
}
1;
__END__
=head1 NAME
Test::Unit::Assert - unit testing framework assertion class
=head1 SYNOPSIS
# this class is not intended to be used directly,
# normally you get the functionality by subclassing from
# Test::Unit::TestCase
use Test::Unit::TestCase;
# more code here ...
$self->assert($your_condition_here, $your_optional_message_here);
# or, for regular expression comparisons:
$self->assert(qr/some_pattern/, $result);
# or, for functional style coderef tests:
$self->assert(sub {
$_[0] == $_[1]
or $self->fail("Expected $_[0], got $_[1]");
}, 1, 2);
# or, for old style regular expression comparisons
# (strongly deprecated; see warning below)
$self->assert(scalar("foo" =~ /bar/), $your_optional_message_here);
# Or, if you don't mind us guessing
$self->assert_equals('expected', $actual [, $optional_message]);
$self->assert_equals(1,$actual);
$self->assert_not_equals('not expected', $actual [, $optional_message]);
$self->assert_not_equals(0,1);
# Or, if you want to force the comparator
$self->assert_num_equals(1,1);
$self->assert_num_not_equals(1,0);
$self->assert_str_equals('string','string');
$self->assert_str_not_equals('stringA', 'stringB');
# assert defined/undefined status
$self->assert_null(undef);
$self->assert_not_null('');
=head1 DESCRIPTION
This class contains the various standard assertions used within the
framework. With the exception of the C<assert(CODEREF, @ARGS)>, all
the assertion methods take an optional message after the mandatory
fields. The message can either be a single string, or a list, which
will get concatenated.
Although you can specify a message, it is hoped that the default error
messages generated when an assertion fails will be good enough for
most cases.
=head2 Methods
=over 4
=item assert_equals(EXPECTED, ACTUAL [, MESSAGE])
=item assert_not_equals(NOTEXPECTED, ACTUAL [, MESSAGE])
The catch all assertions of (in)equality. We make a guess about
whether to test for numeric or string (in)equality based on the first
argument. If it looks like a number then we do a numeric test, if it
looks like a string, we do a string test.
If the first argument is an object, we check to see if the C<'=='>
operator has been overloaded and use that if it has, otherwise we do
the string test.
=item assert_num_equals
=item assert_num_not_equals
Force numeric comparison with these two.
=item assert_str_equals
=item assert_str_not_equals
Force string comparison
=item assert_matches(qr/PATTERN/, STRING [, MESSAGE])
=item assert_does_not_match(qr/PATTERN/, STRING [, MESSAGE])
Assert that STRING does or does not match the PATTERN regex.
=item assert_deep_equals(A, B [, MESSAGE ])
Assert that reference A is a deep copy of reference B. The references
can be complex, deep structures. If they are different, the default
message will display the place where they start differing.
B<NOTE> This is NOT well-tested on circular references. Nor am I
quite sure what will happen with filehandles.
=item assert_null(ARG [, MESSAGE])
=item assert_not_null(ARG [, MESSAGE])
Assert that ARG is defined or not defined.
=item assert(BOOLEAN [, MESSAGE])
Checks if the BOOLEAN expression returns a true value that is neither
a CODE ref nor a REGEXP. Note that MESSAGE is almost non optional in
this case, otherwise all the assertion has to go on is the truth or
otherwise of the boolean.
If you want to use the "old" style for testing regular expression
matching, please be aware of this: the arguments to assert() are
evaluated in list context, e.g. making a failing regex "pull" the
message into the place of the first argument. Since this is usually
just plain wrong, please use scalar() to force the regex comparison
to yield a useful boolean value.
=item assert(qr/PATTERN/, ACTUAL [, MESSAGE])
Matches ACTUAL against the PATTERN regex. If you omit MESSAGE, you
should get a sensible error message.
=item assert(CODEREF, @ARGS)
Calls CODEREF->(@ARGS). Assertion fails if this returns false (or
throws Test::Unit::Failure)
=item assert_raises(EXCEPTION_CLASS, CODEREF [, MESSAGE])
Calls CODEREF->(). Assertion fails unless an exception of class
EXCEPTION_CLASS is raised.
=item multi_assert(ASSERTION, @ARGSETS)
Calls $self->assert(ASSERTION, @$ARGSET) for each $ARGSET in @ARGSETS.
=item ok(@ARGS)
Simulates the behaviour of the L<Test|Test> module. B<Deprecated.>
=back
=head1 AUTHOR
Copyright (c) 2000-2002, 2005 the PerlUnit Development Team
(see L<Test::Unit> or the F<AUTHORS> file included in this
distribution).
All rights reserved. This program is free software; you can
redistribute it and/or modify it under the same terms as Perl itself.
=head1 SEE ALSO
=over 4
=item *
L<Test::Unit::Assertion>
=item *
L<Test::Unit::Assertion::Regexp>
=item *
L<Test::Unit::Assertion::CodeRef>
=item *
L<Test::Unit::Assertion::Boolean>
=item *
L<Test::Unit::TestCase>
=item *
L<Test::Unit::Exception>
=item *
The framework self-testing suite
(L<t::tlib::AllTests|t::tlib::AllTests>)
=back
=cut
|