blib/lib/Acme/Given/Hash.pm

filter: Statementstmt 98.5% 66 / 67 Branchbran 93.7% 15 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 1 totaltotal 96.9% 96 / 99 CC 10 SCAR 10.1
CC
10
Cov
98%
gvn
5.0
__ANON__
5.0
RUN
2.0
CRAP 2.8
↑ Summary
1 package Acme::Given::Hash;
2 {
3 1 $Acme::Given::Hash::VERSION = '0.007';
4 }
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 require 5.014;
8 1 ▶use List::MoreUtils qw{natatime};
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Exporter qw{import};
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶no if $] >= 5.018, warnings => "experimental::smartmatch";
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 our @EXPORT = qw{gvn};
13  
14 #ABSTRACT: is given() too much typing for you?
15  
16 8 ▶sub gvn ($) {
Subroutine gvn: called CC=5 SCAR=16.1
Pod undocumented
17 8 my $when = shift;
18 # old hashref notation
19 8 ▶ if ( ref($when) eq 'HASH' ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $when eq 'HASH') { } 6 2 8
elsif (ref $when eq 'ARRAY') { } 2 0 2
20 6 return bless {exact => $when, calculate => []}, 'Acme::Given::Hash::Object';
21 }
22 # new arrayref notation
23 elsif ( ref($when) eq 'ARRAY' ) {
24 2 my $input = natatime 2, @{ $_[0] };
25 2 my $self = {exact=>{}, calculate=>[]};
26 2 my $it = natatime 2, @$when;
27 2 ▶ while (my @pairs = $it->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@pairs) = &$it()) 7 2 9
28 7 ▶ if( ref($pairs[0]) eq '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $pairs[0] eq '') { } 1 6 7
29 1 $self->{exact}->{$pairs[0]} = $pairs[1];
30 }
31 else {
32 6 push @{ $self->{calculate} }, {match => $pairs[0], value => $pairs[1]};
33 }
34 }
35 2 return bless $self, 'Acme::Given::Hash::Object';
36 }
37 0 die 'gvn only takes hashrefs and arrayrefs, you have passed soemthing else';
38 }
39  
40 package Acme::Given::Hash::Object;
41 {
42 1 $Acme::Given::Hash::Object::VERSION = '0.007';
43 }
44 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 1 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 1 ▶no if $] >= 5.018, warnings => "experimental::smartmatch";
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 ▶use overload '~~' => sub{
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=5 SCAR=16.1
50 18 my ($self, $key) = @_;
51  
52 # in the case of a sub as a value execute with $key
53 11 ▶ sub RUN($){
Subroutine RUN: called CC=2 SCAR=6.9
54 11 my $ref = shift;
55 11 ▶ return ref($ref) eq 'CODE' ? $ref->($key) : $ref;
Branch: 1 branch
BranchTrueFalseTotal
ref $ref eq 'CODE' ? : 1 10 11
56 }
57  
58 # first check and see if we have an exact match
59 18 ▶ return RUN $self->{exact}->{$key} if exists $self->{exact}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"exact"}{$key} 5 13 18
60  
61 13 local $_ = $key; # allow match subs to just use $_;
62 13 foreach my $pair (@{ $self->{calculate} } ) {
63  
64 23 my $match;
65 # 'string' ~~ [1..10] throws a warning, this disables this just for the check
66 1 ▶ { no warnings qw{numeric};
Subroutine BEGIN: called CC=1 SCAR=0.0
67 # in the case of a sub as a key capture the return value
68 $match = ref($pair->{match}) eq 'CODE'
69 ? $pair->{match}->($key)
70 : $key ~~ $pair->{match}
71 23 ▶ ;
Branch: 1 branch
BranchTrueFalseTotal
ref $pair->{'match'} eq 'CODE' ? : 5 18 23
72 }
73  
74 23 ▶ return RUN $pair->{value} if $match;
Branch: 1 branch
BranchTrueFalseTotal
if $match 6 17 23
75 }
76 7 return undef; # no matches found
77 1 };
78  
79 1 1;
↑ Summary