blib/lib/Test2/Tools/Ref.pm

filter: Statementstmt 100% 60 / 60 Branchbran 100% 24 / 24 Conditioncond 100% 3 / 3 MC/DCmcdc 100% 2 / 2 Subroutinesub 100% 9 / 9 Podpod 100% 3 / 3 totaltotal 100% 101 / 101 CC 14 SCAR 14.5
CC
14
Cov
100%
ref_is
6.0
ref_is_not
6.0
ref_ok
4.0
CRAP 4.3
« blib/lib/Test2/Util/Guard.pm ↑ Summary blib/lib/Test2/Tools/Target.pm »
1 package Test2::Tools::Ref;
2 162 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 162 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 162 our $VERSION = '1.302226';
6  
7 162 ▶use Scalar::Util qw/reftype refaddr/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 162 ▶use Test2::API qw/context/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 162 ▶use Test2::Util::Ref qw/render_ref/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 162 our @EXPORT = qw/ref_ok ref_is ref_is_not/;
12 162 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 6 ▶sub ref_ok($;$$) {
Subroutine ref_ok: called CC=4 SCAR=13.9
Pod documented
15 6 my ($thing, $wanttype, $name) = @_;
16 6 my $ctx = context();
17  
18 6 my $gotname = render_ref($thing);
19 6 my $gottype = reftype($thing);
20  
21 6 ▶ if (!$gottype) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($gottype) 2 4 6
22 2 $ctx->ok(0, $name, ["'$gotname' is not a reference"]);
23 2 $ctx->release;
24 2 return 0;
25 }
26  
27 4 ▶ if ($wanttype && $gottype ne $wanttype) {
Branch: 1 branch
BranchTrueFalseTotal
if ($wanttype and $gottype ne $wanttype) 1 3 4
Condition100%3/3
$wanttype and $gottype ne $wanttype
!ll&&!rl&&r
121
MC/DC100%2/2
$wanttype and $gottype ne $wanttype
$wanttype $gottype ne $wanttype
Truth table100%3/3
A and B
A= $wanttypeB= $gottype ne $wanttype
ABresult
0X0
100
111
28 1 $ctx->ok(0, $name, ["'$gotname' is not a '$wanttype' reference"]);
29 1 $ctx->release;
30 1 return 0;
31 }
32  
33 3 $ctx->ok(1, $name);
34 3 $ctx->release;
35 3 return 1;
36 }
37  
38 20 ▶sub ref_is($$;$@) {
Subroutine ref_is: called CC=6 SCAR=17.9
Pod documented
39 20 my ($got, $exp, $name, @diag) = @_;
40 20 my $ctx = context();
41  
42 20 ▶ $got = '<undef>' unless defined $got;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got 1 19 20
43 20 ▶ $exp = '<undef>' unless defined $exp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exp 1 19 20
44  
45 20 my $bool = 0;
46 20 ▶ if (!ref($got)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $got) { } 2 18 20
elsif (not ref $exp) { } 2 16 18
47 2 $ctx->ok(0, $name, ["First argument '$got' is not a reference", @diag]);
48 }
49 elsif(!ref($exp)) {
50 2 $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 $bool = refaddr($got) == refaddr($exp);
55 16 $ctx->ok($bool, $name, ["'$got' is not the same reference as '$exp'", @diag]);
56 }
57  
58 20 $ctx->release;
59 20 ▶ return $bool ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$bool ? : 15 5 20
60 }
61  
62 6 ▶sub ref_is_not($$;$) {
Subroutine ref_is_not: called CC=6 SCAR=17.9
Pod documented
63 6 my ($got, $exp, $name, @diag) = @_;
64 6 my $ctx = context();
65  
66 6 ▶ $got = '<undef>' unless defined $got;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $got 1 5 6
67 6 ▶ $exp = '<undef>' unless defined $exp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $exp 1 5 6
68  
69 6 my $bool = 0;
70 6 ▶ if (!ref($got)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $got) { } 2 4 6
elsif (not ref $exp) { } 2 2 4
71 2 $ctx->ok(0, $name, ["First argument '$got' is not a reference", @diag]);
72 }
73 elsif(!ref($exp)) {
74 2 $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 $bool = refaddr($got) != refaddr($exp);
79 2 $ctx->ok($bool, $name, ["'$got' is the same reference as '$exp'", @diag]);
80 }
81  
82 6 $ctx->release;
83 6 ▶ return $bool ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$bool ? : 1 5 6
84 }
85  
86 162 1;
87  
88 __END__
« blib/lib/Test2/Util/Guard.pm ↑ Summary blib/lib/Test2/Tools/Target.pm »