| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Test2::Tools::Ref; | 
| 2 | 155 |  |  | 155 |  | 1142 | use strict; | 
|  | 155 |  |  |  |  | 331 |  | 
|  | 155 |  |  |  |  | 4935 |  | 
| 3 | 155 |  |  | 155 |  | 816 | use warnings; | 
|  | 155 |  |  |  |  | 340 |  | 
|  | 155 |  |  |  |  | 7232 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 |  |  |  |  |  |  | our $VERSION = '0.000156'; | 
| 6 |  |  |  |  |  |  |  | 
| 7 | 155 |  |  | 155 |  | 1105 | use Scalar::Util qw/reftype refaddr/; | 
|  | 155 |  |  |  |  | 379 |  | 
|  | 155 |  |  |  |  | 8841 |  | 
| 8 | 155 |  |  | 155 |  | 988 | use Test2::API qw/context/; | 
|  | 155 |  |  |  |  | 476 |  | 
|  | 155 |  |  |  |  | 8235 |  | 
| 9 | 155 |  |  | 155 |  | 1133 | use Test2::Util::Ref qw/render_ref/; | 
|  | 155 |  |  |  |  | 382 |  | 
|  | 155 |  |  |  |  | 11049 |  | 
| 10 |  |  |  |  |  |  |  | 
| 11 |  |  |  |  |  |  | our @EXPORT = qw/ref_ok ref_is ref_is_not/; | 
| 12 | 155 |  |  | 155 |  | 1544 | use base 'Exporter'; | 
|  | 155 |  |  |  |  | 766 |  | 
|  | 155 |  |  |  |  | 101444 |  | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | sub ref_ok($;$$) { | 
| 15 | 6 |  |  | 6 | 1 | 456 | my ($thing, $wanttype, $name) = @_; | 
| 16 | 6 |  |  |  |  | 15 | my $ctx = context(); | 
| 17 |  |  |  |  |  |  |  | 
| 18 | 6 |  |  |  |  | 514 | my $gotname = render_ref($thing); | 
| 19 | 6 |  |  |  |  | 23 | my $gottype = reftype($thing); | 
| 20 |  |  |  |  |  |  |  | 
| 21 | 6 | 100 |  |  |  | 15 | if (!$gottype) { | 
| 22 | 2 |  |  |  |  | 40 | $ctx->ok(0, $name, ["'$gotname' is not a reference"]); | 
| 23 | 2 |  |  |  |  | 1142 | $ctx->release; | 
| 24 | 2 |  |  |  |  | 60 | return 0; | 
| 25 |  |  |  |  |  |  | } | 
| 26 |  |  |  |  |  |  |  | 
| 27 | 4 | 100 | 100 |  |  | 23 | if ($wanttype && $gottype ne $wanttype) { | 
| 28 | 1 |  |  |  |  | 13 | $ctx->ok(0, $name, ["'$gotname' is not a '$wanttype' reference"]); | 
| 29 | 1 |  |  |  |  | 760 | $ctx->release; | 
| 30 | 1 |  |  |  |  | 28 | return 0; | 
| 31 |  |  |  |  |  |  | } | 
| 32 |  |  |  |  |  |  |  | 
| 33 | 3 |  |  |  |  | 11 | $ctx->ok(1, $name); | 
| 34 | 3 |  |  |  |  | 287 | $ctx->release; | 
| 35 | 3 |  |  |  |  | 77 | return 1; | 
| 36 |  |  |  |  |  |  | } | 
| 37 |  |  |  |  |  |  |  | 
| 38 |  |  |  |  |  |  | sub ref_is($$;$@) { | 
| 39 | 20 |  |  | 20 | 1 | 568 | my ($got, $exp, $name, @diag) = @_; | 
| 40 | 20 |  |  |  |  | 246 | my $ctx = context(); | 
| 41 |  |  |  |  |  |  |  | 
| 42 | 20 | 100 |  |  |  | 1757 | $got = '' unless defined $got; | 
| 43 | 20 | 100 |  |  |  | 54 | $exp = '' unless defined $exp; | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 20 |  |  |  |  | 38 | my $bool = 0; | 
| 46 | 20 | 100 |  |  |  | 92 | if (!ref($got)) { | 
|  |  | 100 |  |  |  |  |  | 
| 47 | 2 |  |  |  |  | 15 | $ctx->ok(0, $name, ["First argument '$got' is not a reference", @diag]); | 
| 48 |  |  |  |  |  |  | } | 
| 49 |  |  |  |  |  |  | elsif(!ref($exp)) { | 
| 50 | 2 |  |  |  |  | 16 | $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 |  |  |  |  | 64 | $bool = refaddr($got) == refaddr($exp); | 
| 55 | 16 |  |  |  |  | 124 | $ctx->ok($bool, $name, ["'$got' is not the same reference as '$exp'", @diag]); | 
| 56 |  |  |  |  |  |  | } | 
| 57 |  |  |  |  |  |  |  | 
| 58 | 20 |  |  |  |  | 5971 | $ctx->release; | 
| 59 | 20 | 100 |  |  |  | 574 | return $bool ? 1 : 0; | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 |  |  |  |  |  |  | sub ref_is_not($$;$) { | 
| 63 | 6 |  |  | 6 | 1 | 90 | my ($got, $exp, $name, @diag) = @_; | 
| 64 | 6 |  |  |  |  | 15 | my $ctx = context(); | 
| 65 |  |  |  |  |  |  |  | 
| 66 | 6 | 100 |  |  |  | 482 | $got = '' unless defined $got; | 
| 67 | 6 | 100 |  |  |  | 14 | $exp = '' unless defined $exp; | 
| 68 |  |  |  |  |  |  |  | 
| 69 | 6 |  |  |  |  | 9 | my $bool = 0; | 
| 70 | 6 | 100 |  |  |  | 23 | if (!ref($got)) { | 
|  |  | 100 |  |  |  |  |  | 
| 71 | 2 |  |  |  |  | 12 | $ctx->ok(0, $name, ["First argument '$got' is not a reference", @diag]); | 
| 72 |  |  |  |  |  |  | } | 
| 73 |  |  |  |  |  |  | elsif(!ref($exp)) { | 
| 74 | 2 |  |  |  |  | 12 | $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 |  |  |  |  | 8 | $bool = refaddr($got) != refaddr($exp); | 
| 79 | 2 |  |  |  |  | 13 | $ctx->ok($bool, $name, ["'$got' is the same reference as '$exp'", @diag]); | 
| 80 |  |  |  |  |  |  | } | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 6 |  |  |  |  | 2837 | $ctx->release; | 
| 83 | 6 | 100 |  |  |  | 161 | return $bool ? 1 : 0; | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | 1; | 
| 87 |  |  |  |  |  |  |  | 
| 88 |  |  |  |  |  |  | __END__ |