blib/lib/Test/Identity.pm

filter: Statementstmt 97.5% 39 / 40 Branchbran 91.6% 11 / 12 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod 100% 1 / 1 totaltotal 96.6% 57 / 59 CC 7 SCAR 11.1
CC
7
Cov
96%
_describe
4.1
identical
4.0
BEGIN
1.0
CRAP 3.0
↑ Summary
1 # You may distribute under the terms of either the GNU General Public License
2 # or the Artistic License (the same terms as Perl itself)
3 #
4 # (C) Paul Evans, 2010 -- leonerd@leonerd.org.uk
5  
6 package Test::Identity;
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use base qw( Test::Builder::Module );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 2 ▶use Scalar::Util qw( refaddr blessed );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 our $VERSION = '0.01';
15  
16 2 our @EXPORT = qw(
17 identical
18 );
19  
20 =head1 NAME
21  
22 C<Test::Identity> - assert the referential identity of a reference
23  
24 =head1 SYNOPSIS
25  
26 use Test::More tests => 2;
27 use Test::Identity;
28  
29 use Thingy;
30  
31 {
32 my $thingy;
33  
34 sub get_thingy { return $thingy }
35 sub set_thingy { $thingy = shift; }
36 }
37  
38 identical( get_thingy, undef, 'get_thingy is undef' );
39  
40 my $test_thingy = Thingy->new;
41 set_thingy $test_thingy;
42  
43 identical( get_thingy, $thingy, 'get_thingy is now $test_thingy' );
44  
45 =head1 DESCRIPTION
46  
47 This module provides a single testing function, C<identical>. It asserts that
48 a given reference is as expected; that is, it either refers to the same object
49 or is C<undef>. It is similar to C<Test::More::is> except that it uses
50 C<refaddr>, ensuring that it behaves correctly even if the references under
51 test are objects that overload stringification or numification.
52  
53 It also provides better diagnostics if the test fails:
54  
55 $ perl -MTest::More=tests,1 -MTest::Identity -e'identical [], {}'
56 1..1
57 not ok 1
58 # Failed test at -e line 1.
59 # Expected an anonymous HASH ref, got an anonymous ARRAY ref
60 # Looks like you failed 1 test of 1.
61  
62 $ perl -MTest::More=tests,1 -MTest::Identity -e'identical [], []'
63 1..1
64 not ok 1
65 # Failed test at -e line 1.
66 # Expected an anonymous ARRAY ref to the correct object
67 # Looks like you failed 1 test of 1.
68  
69 =cut
70  
71 =head1 FUNCTIONS
72  
73 =cut
74  
75 sub _describe
76 16 ▶{
Subroutine _describe: called CC=4 SCAR=14.0
77 16 my ( $ref ) = @_;
78  
79 16 ▶ if( !defined $ref ) {
Branch: 3 branches
BranchTrueFalseTotal
if (not defined $ref) { } 5 11 16
elsif (not builtin::refaddr($ref)) { } 0 11 11
elsif (builtin::blessed($ref)) { } 4 7 11
80 5 return "undef";
81 }
82 elsif( !refaddr $ref ) {
83 0 return "a non-reference";
84 }
85 elsif( blessed $ref ) {
86 4 return "a reference to a " . ref( $ref );
87 }
88 else {
89 7 return "an anonymous " . ref( $ref ) . " ref";
90 }
91 }
92  
93 =head2 identical( $got, $expected, $name )
94  
95 Asserts that $got refers to the same object as $expected.
96  
97 =cut
98  
99 sub identical($$;$)
100 8 ▶{
Subroutine identical: called CC=4 SCAR=13.9
Pod documented
101 8 my ( $got, $expected, $name ) = @_;
102  
103 8 my $tb = __PACKAGE__->builder;
104  
105 8 my $got_desc = _describe $got;
106 8 my $exp_desc = _describe $expected;
107  
108 # TODO: Consider if undef/undef ought to do this...
109 8 ▶ if( $got_desc ne $exp_desc ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_desc ne $exp_desc) 4 4 8
110 4 $tb->ok( 0, $name );
111 4 $tb->diag( "Expected $exp_desc, got $got_desc" );
112 4 return 0;
113 }
114  
115 4 ▶ if( !defined $got ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $got) 1 3 4
116 # Two undefs
117 1 $tb->ok( 1, $name );
118 1 return 1;
119 }
120  
121 3 my $got_addr = refaddr $got;
122 3 my $exp_addr = refaddr $expected;
123  
124 3 ▶ if( $got_addr != $exp_addr ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($got_addr != $exp_addr) 1 2 3
125 1 $tb->ok( 0, $name );
126 1 $tb->diag( "Expected $exp_desc to the correct object" );
127 1 return 0;
128 }
129  
130 2 $tb->ok( 1, $name );
131 2 return 1;
132 }
133  
134 # Keep perl happy; keep Britain tidy
135 2 1;
136  
137 __END__
↑ Summary