| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
$wanttype and $gottype ne $wanttype
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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 43 | 20 | ▶ | $exp = '<undef>' unless defined $exp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 20 | my $bool = 0; | |||||||||||||||||||||||||||
| 46 | 20 | ▶ | if (!ref($got)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 67 | 6 | ▶ | $exp = '<undef>' unless defined $exp; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 6 | my $bool = 0; | |||||||||||||||||||||||||||
| 70 | 6 | ▶ | if (!ref($got)) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||
| 85 | |||||||||||||||||||||||||||||
| 86 | 162 | 1; | |||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | __END__ | ||||||||||||||||||||||||||||