File Coverage

blib/lib/Test2/Tools/Ref.pm
Criterion Covered Total %
statement 57 57 100.0
branch 24 24 100.0
condition 3 3 100.0
subroutine 9 9 100.0
pod 3 3 100.0
total 96 96 100.0


line stmt bran cond sub pod time code
1             package Test2::Tools::Ref;
2 155     155   1129 use strict;
  155         337  
  155         4405  
3 155     155   806 use warnings;
  155         358  
  155         6362  
4              
5             our $VERSION = '0.000155';
6              
7 155     155   933 use Scalar::Util qw/reftype refaddr/;
  155         374  
  155         8677  
8 155     155   1069 use Test2::API qw/context/;
  155         387  
  155         7034  
9 155     155   1216 use Test2::Util::Ref qw/render_ref/;
  155         351  
  155         11417  
10              
11             our @EXPORT = qw/ref_ok ref_is ref_is_not/;
12 155     155   1115 use base 'Exporter';
  155         452  
  155         97608  
13              
14             sub ref_ok($;$$) {
15 6     6 1 516 my ($thing, $wanttype, $name) = @_;
16 6         18 my $ctx = context();
17              
18 6         483 my $gotname = render_ref($thing);
19 6         17 my $gottype = reftype($thing);
20              
21 6 100       12 if (!$gottype) {
22 2         13 $ctx->ok(0, $name, ["'$gotname' is not a reference"]);
23 2         1126 $ctx->release;
24 2         55 return 0;
25             }
26              
27 4 100 100     21 if ($wanttype && $gottype ne $wanttype) {
28 1         13 $ctx->ok(0, $name, ["'$gotname' is not a '$wanttype' reference"]);
29 1         785 $ctx->release;
30 1         28 return 0;
31             }
32              
33 3         11 $ctx->ok(1, $name);
34 3         254 $ctx->release;
35 3         78 return 1;
36             }
37              
38             sub ref_is($$;$@) {
39 20     20 1 546 my ($got, $exp, $name, @diag) = @_;
40 20         92 my $ctx = context();
41              
42 20 100       1916 $got = '' unless defined $got;
43 20 100       63 $exp = '' unless defined $exp;
44              
45 20         42 my $bool = 0;
46 20 100       82 if (!ref($got)) {
    100          
47 2         11 $ctx->ok(0, $name, ["First argument '$got' is not a reference", @diag]);
48             }
49             elsif(!ref($exp)) {
50 2         17 $ctx->ok(0, $name, ["Second argument '$exp' is not a reference", @diag]);
51             }
52             else {
53             # Don't let overloading mess with us.
54 16         57 $bool = refaddr($got) == refaddr($exp);
55 16         108 $ctx->ok($bool, $name, ["'$got' is not the same reference as '$exp'", @diag]);
56             }
57              
58 20         4933 $ctx->release;
59 20 100       580 return $bool ? 1 : 0;
60             }
61              
62             sub ref_is_not($$;$) {
63 6     6 1 90 my ($got, $exp, $name, @diag) = @_;
64 6         18 my $ctx = context();
65              
66 6 100       478 $got = '' unless defined $got;
67 6 100       15 $exp = '' unless defined $exp;
68              
69 6         10 my $bool = 0;
70 6 100       34 if (!ref($got)) {
    100          
71 2         21 $ctx->ok(0, $name, ["First argument '$got' is not a reference", @diag]);
72             }
73             elsif(!ref($exp)) {
74 2         13 $ctx->ok(0, $name, ["Second argument '$exp' is not a reference", @diag]);
75             }
76             else {
77             # Don't let overloading mess with us.
78 2         12 $bool = refaddr($got) != refaddr($exp);
79 2         14 $ctx->ok($bool, $name, ["'$got' is the same reference as '$exp'", @diag]);
80             }
81              
82 6         2688 $ctx->release;
83 6 100       153 return $bool ? 1 : 0;
84             }
85              
86             1;
87              
88             __END__