Assert the referential identity of a reference
use Test::More tests => 2; use Test::Identity; use Thingy; { my $thingy; sub get_thingy { return $thingy } sub set_thingy { $thingy = shift; } } identical( get_thingy, undef, 'get_thingy is undef' ); my $test_thingy = Thingy->new; set_thingy $test_thingy; identical( get_thingy, $thingy, 'get_thingy is now $test_thingy' );
This module provides a single testing function, \*(C`identical\*(C'. It asserts that a given reference is as expected; that is, it either refers to the same object or is \*(C`undef\*(C'. It is similar to \*(C`Test::More::is\*(C' except that it uses \*(C`refaddr\*(C', ensuring that it behaves correctly even if the references under test are objects that overload stringification or numification.
It also provides better diagnostics if the test fails:
$ perl -MTest::More=tests,1 -MTest::Identity -e'identical [], {}' 1..1 not ok 1 # Failed test at -e line 1. # Expected an anonymous HASH ref, got an anonymous ARRAY ref # Looks like you failed 1 test of 1.
$ perl -MTest::More=tests,1 -MTest::Identity -e'identical [], []' 1..1 not ok 1 # Failed test at -e line 1. # Expected an anonymous ARRAY ref to the correct object # Looks like you failed 1 test of 1.
Asserts that $got refers to the same object as $expected.
Paul Evans <[email protected]>