Rx/CoreType/rec.pm

filter: Statementstmt 100% 50 / 50 Branchbran 95.4% 21 / 22 Conditioncond 100% 12 / 12 MC/DCmcdc 100% 8 / 8 Subroutinesub 100% 7 / 7 Podpod 0.0% 0 / 3 totaltotal 96.0% 98 / 102 CC 19 SCAR 21.9
CC
19
Cov
99%
assert_valid
13.0
guts_from_arg
7.0
BEGIN
1.0
CRAP 8.9
« Rx/FailureSet.pm ↑ Summary Rx/CoreType/num.pm »
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 package Data::Rx::CoreType::rec;
4 # ABSTRACT: the Rx //rec type
5 1 $Data::Rx::CoreType::rec::VERSION = '0.200007';
6 1 ▶use parent 'Data::Rx::CoreType';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 54 ▶sub subname { 'rec' }
Subroutine subname: called CC=1 SCAR=0.0
Pod undocumented
11  
12 sub guts_from_arg {
13 15 ▶ my ($class, $arg, $rx, $type) = @_;
Subroutine guts_from_arg: called CC=7 SCAR=19.5
Pod undocumented
14  
15 15 ▶ Carp::croak("unknown arguments to new") unless
Branch: 1 branch
BranchTrueFalseTotal
unless "Data::Rx::Util"->_x_subset_keys_y($arg, {"rest", 1, "required", 1, "optional", 1}) 2 13 15
16 Data::Rx::Util->_x_subset_keys_y($arg, {
17 rest => 1,
18 required => 1,
19 optional => 1,
20 });
21  
22 13 my $guts = {};
23  
24 13 my $content_schema = {};
25  
26 13 ▶ $guts->{rest_schema} = $rx->make_schema($arg->{rest}) if $arg->{rest};
Branch: 1 branch
BranchTrueFalseTotal
if $arg->{"rest"} 5 8 13
27  
28 13 TYPE: for my $type (qw(required optional)) {
29 26 ▶ next TYPE unless my $entries = $arg->{$type};
Branch: 1 branch
BranchTrueFalseTotal
unless my $entries = $arg->{$type} 5 21 26
30  
31 21 for my $entry (keys %$entries) {
32 Carp::croak("$entry appears in both required and optional")
33 25 ▶ if $content_schema->{ $entry };
Branch: 1 branch
BranchTrueFalseTotal
if $content_schema->{$entry} 1 24 25
34  
35 $content_schema->{ $entry } = {
36 optional => $type eq 'optional',
37 24 schema => $rx->make_schema($entries->{ $entry }),
38 };
39 }
40 };
41  
42 12 $guts->{content_schema} = $content_schema;
43 12 return $guts;
44 }
45  
46 sub assert_valid {
47 406 ▶ my ($self, $value) = @_;
Subroutine assert_valid: called CC=13 SCAR=25.7
Pod undocumented
48  
49 406 ▶ unless (! Scalar::Util::blessed($value) and ref $value eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
unless (not builtin::blessed($value) and ref $value eq "HASH") 344 62 406
Condition100%3/3
not builtin::blessed($value) and ref $value eq "HASH"
!ll&&!rl&&r
1632862
MC/DC100%2/2
not builtin::blessed($value) and ref $value eq "HASH"
not builtin::blessed($value) ref $value eq "HASH"
Truth table100%3/3
not(A) and B
A= not builtin::blessed($value)B= ref $value eq "HASH"
ABresult
0X0
100
111
50 344 $self->fail({
51 error => [ qw(type) ],
52 message => "value is not a hashref",
53 value => $value,
54 });
55 }
56  
57 62 my $c_schema = $self->{content_schema};
58  
59 62 my @subchecks;
60  
61 62 my @rest_keys = grep { ! exists $c_schema->{$_} } keys %$value;
62 62 ▶ if (@rest_keys and not $self->{rest_schema}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest_keys and not $self->{"rest_schema"}) 9 53 62
Condition100%3/3
@rest_keys and not $self->{"rest_schema"}
!ll&&!rl&&r
32219
MC/DC100%2/2
@rest_keys and not $self->{"rest_schema"}
@rest_keys not $self->{"rest_schema"}
Truth table100%3/3
A and not(B)
A= @rest_keysB= not $self->{"rest_schema"}
ABresult
0X0
100
111
63 9 @rest_keys = sort @rest_keys;
64 9 push @subchecks,
65 $self->new_fail({
66 error => [ qw(unexpected) ],
67 keys => [@rest_keys],
68 message => "found unexpected entries: @rest_keys",
69 value => $value,
70 });
71 }
72  
73 62 ▶ for my $key ($self->rx->sort_keys ? sort keys %$c_schema : keys %$c_schema) {
Branch: 1 branch
BranchTrueFalseTotal
$self->rx->sort_keys ? : 62 0 62
74 99 my $check = $c_schema->{$key};
75  
76 99 ▶ if (not $check->{optional} and not exists $value->{ $key }) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($check->{"optional"} or exists $value->{$key}) 23 76 99
Condition100%3/3
$check->{"optional"} or exists $value->{$key}
l!l&&r!l&&!r
235323
MC/DC100%2/2
$check->{"optional"} or exists $value->{$key}
$check->{"optional"} exists $value->{$key}
Truth table100%3/3
A or B
A= $check->{"optional"}B= exists $value->{$key}
ABresult
1X1
011
000
77 23 push @subchecks,
78 $self->new_fail({
79 error => [ qw(missing) ],
80 keys => [$key],
81 message => "no value given for required entry $key",
82 value => $value,
83 });
84 23 next;
85 }
86  
87 76 ▶ if (exists $value->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value->{$key}) 59 17 76
88 push @subchecks, [
89 $value->{$key},
90 $check->{schema},
91 { data_path => [ [$key, 'key' ] ],
92 check_path => [
93 59 ▶ [ $check->{optional} ? 'optional' : 'required', 'key' ],
Branch: 1 branch
BranchTrueFalseTotal
$check->{'optional'} ? : 6 53 59
94 [ $key, 'key' ],
95 ],
96 },
97 ];
98 }
99 }
100  
101 62 ▶ if (@rest_keys && $self->{rest_schema}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@rest_keys and $self->{"rest_schema"}) 21 41 62
Condition100%3/3
@rest_keys and $self->{"rest_schema"}
!ll&&!rl&&r
32921
MC/DC100%2/2
@rest_keys and $self->{"rest_schema"}
@rest_keys $self->{"rest_schema"}
Truth table100%3/3
A and B
A= @rest_keysB= $self->{"rest_schema"}
ABresult
0X0
100
111
102 21 my %rest = map { $_ => $value->{$_} } @rest_keys;
103  
104 push @subchecks, [
105 \%rest,
106 $self->{rest_schema},
107 21 { check_path => [ ['rest', 'key' ] ],
108 },
109 ];
110 }
111  
112 62 $self->perform_subchecks(\@subchecks);
113  
114 25 return 1;
115 }
116  
117 1 1;
118  
119 __END__
« Rx/FailureSet.pm ↑ Summary Rx/CoreType/num.pm »