blib/lib/Test/Deep/HashRec.pm

filter: Statementstmt 84.1% 53 / 63 Branchbran 50.0% 9 / 18 Conditioncond 50.0% 2 / 4 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 88.8% 8 / 9 Podpod 100% 1 / 1 totaltotal 75.2% 73 / 97 CC 12 SCAR 16.9
CC
12
Cov
75%
descend
9.0
init
6.2
diagnostics
2.0
CRAP 5.4
↑ Summary
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package Test::Deep::HashRec 0.004;
5 # ABSTRACT: test hash entries for required and optional fields
6  
7 #pod =func hashrec
8 #pod
9 #pod cmp_deeply(
10 #pod $got,
11 #pod hashrec({
12 #pod required => { count => any(1,2,3), b => ignore() },
13 #pod optional => { name => { first => ignore(), last => ignore() } },
14 #pod }),
15 #pod "we got a valid record",
16 #pod );
17 #pod
18 #pod C<hashrec> returns a Test::Deep comparator that asserts that:
19 #pod
20 #pod =for :list
21 #pod * all required elements are present
22 #pod * nothing other than required and optional elements are present
23 #pod * all present elements match the comparator given for them
24 #pod
25 #pod If you pass a true C<allow_unknown> argument, then unknown elements will be
26 #pod permitted, and their values ignored.
27 #pod
28 #pod =cut
29  
30 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 1 our @EXPORT = qw(hashrec);
33  
34 1 ▶sub hashrec { Test::Deep::HashRec::Object->new(@_) };
Subroutine hashrec: called CC=1 SCAR=0.0
Pod documented
35  
36 {
37 package
38 Test::Deep::HashRec::Object;
39  
40 1 ▶use Test::Deep::Cmp;
Subroutine BEGIN: called CC=1 SCAR=0.0
41 1 ▶use Test::Deep::HashElements;
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 1 ▶sub init {
Subroutine init: called CC=6 SCAR=18.3
44 1 my ($self, $val) = @_;
45  
46 1 ▶ Carp::confess("argument to hashrec must be a hash reference")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $val eq "HASH" 0 1 1
47 unless ref $val eq 'HASH';
48  
49 1 my %copy = %$val;
50  
51 1 ▶ $self->{required} = delete $copy{required} || {};
Condition50%1/2
delete $copy{'required'} || {}
l!l
10
MC/DC0%0/1
delete $copy{'required'} || {}
delete $copy{'required'}
Truth table50%1/2
A
A= delete $copy{'required'}
Aresult
11
00
52 1 ▶ $self->{optional} = delete $copy{optional} || {};
Condition50%1/2
delete $copy{'optional'} || {}
l!l
10
MC/DC0%0/1
delete $copy{'optional'} || {}
delete $copy{'optional'}
Truth table50%1/2
A
A= delete $copy{'optional'}
Aresult
11
00
53 1 $self->{allow_unknown} = delete $copy{allow_unknown};
54  
55 $self->{is_permitted} = {
56 1 map {; $_ => 1 } (keys %{ $self->{required} }, keys %{ $self->{optional} })
57 };
58  
59 1 $self->{diagnostics} = [];
60  
61 1 ▶ Carp::confess("unknown arguments to hashrec: " . join q{, }, keys %copy)
Branch: 1 branch
BranchTrueFalseTotal
if keys %copy 0 1 1
62 if keys %copy;
63  
64 2 my @dupes = grep {; exists $self->{required}{$_} }
65 1 keys %{ $self->{optional} };
66  
67 1 ▶ Carp::confess("Keys found in both optional and required: @dupes")
Branch: 1 branch
BranchTrueFalseTotal
if @dupes 0 1 1
68 if @dupes;
69  
70 1 return;
71 }
72  
73 0 ▶sub diagnostics {
Subroutine diagnostics: not called CC=1 SCAR=6.9
74 0 my ($self, $where, $last) = @_;
75  
76 0 (my $error = $self->{diag}) =~ s/^/ /gm;
77 0 my $diag = <<EOM;
78 In hash record $where
79 $error
80 EOM
81  
82 0 return $diag;
83 }
84  
85 1 ▶sub descend {
Subroutine descend: called CC=7 SCAR=21.9
86 1 my ($self, $got) = @_;
87  
88 1 undef $self->{diag};
89  
90 1 ▶ unless (ref $got eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $got eq "HASH") 0 1 1
91 0 $self->{diag} = "Didn't get a hash reference";
92 return
93 0 }
94  
95 1 my @keys = keys %$got;
96  
97 1 my @errors;
98  
99 1 ▶ unless ($self->{allow_unknown}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"allow_unknown"}) 1 0 1
100 1 my @unknown = grep {; ! exists $self->{is_permitted}{$_} } @keys;
101 1 ▶ if (@unknown) {
Branch: 1 branch
BranchTrueFalseTotal
if (@unknown) 0 1 1
102 0 push @errors, "Unknown keys found: @unknown";
103 }
104 }
105  
106 1 ▶ if (my @missing = grep {; ! exists $got->{$_} } keys %{ $self->{required}}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@missing) = grep({not exists $got->{$_};} keys %{$$self{"required"};})) 0 1 1
107 0 push @errors, "Missing required keys: @missing";
108 }
109  
110 1 ▶ if (@errors) {
Branch: 1 branch
BranchTrueFalseTotal
if (@errors) 0 1 1
111 0 $self->{diag} = join qq{\n}, @errors;
112 0 return;
113 }
114  
115 my %effective = (
116 map {; $_ => (exists $self->{required}{$_} ? $self->{required}{$_}
117 1 ▶ : $self->{optional}{$_}) }
Branch: 1 branch
BranchTrueFalseTotal
exists $self->{'required'}{$_} ? : 1 0 1
118 1 grep {; $self->{is_permitted}{$_} }
119 keys %$got
120 );
121  
122 1 return Test::Deep::descend(
123 $got,
124 Test::Deep::HashElements->new(\%effective),
125 );
126 }
127  
128 }
129  
130 1 1;
131  
132 __END__
↑ Summary