JSV/Context.pm

filter: Statementstmt 92.3% 97 / 105 Branchbran 82.0% 41 / 50 Conditioncond 50.0% 3 / 6 MC/DCmcdc 25.0% 1 / 4 Subroutinesub 100% 15 / 15 Podpod 0.0% 0 / 6 totaltotal 84.4% 157 / 186 CC 34 SCAR 27.4
CC
34
Cov
88%
validate
26.4
log_error
6.0
apply_keyword
3.5
CRAP 15.5
↑ Summary JSV/Keyword/Draft4/Properties.pm »
1 package JSV::Context;
2  
3 47 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 47 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 47 ▶use Class::Accessor::Lite (
Subroutine BEGIN: called CC=1 SCAR=0.0
7 47 new => 1,
8 rw => [qw/
9 keywords
10 reference
11 original_schema
12 throw_error
13 throw_immediate
14 enable_history
15 enable_format
16 formats
17 history
18 json
19 loose_type
20 /],
21 ro => [qw/
22 errors
23 current_type
24 current_keyword
25 current_pointer
26 current_instance
27 current_schema
28 current_schema_pointer
29 schema_pointer_history
30 /],
31 47 );
32  
33 47 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
34 47 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 47 ▶use JSV::Keyword qw(:constants);
Subroutine BEGIN: called CC=1 SCAR=0.0
36 47 ▶use JSV::Util::Type qw(detect_instance_type detect_instance_type_loose);
Subroutine BEGIN: called CC=1 SCAR=0.0
37 47 ▶use JSV::Result;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 47 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 1421 ▶sub validate {
Subroutine validate: called CC=26 SCAR=32.7
Pod undocumented
41 1421 my ($self, $schema, $instance) = @_;
42  
43 1421 ▶ local $self->{current_type} = !$self->loose_type ? detect_instance_type($instance)
Branch: 1 branch
BranchTrueFalseTotal
!$self->loose_type ? : 783 638 1421
44 : detect_instance_type_loose($instance);
45  
46 1421 local $self->{cleanup_callbacks} = [];
47  
48 1421 my $rv;
49 1421 eval {
50 1421 for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_ANY()} }) {
51 11368 ▶ next unless exists $schema->{$keyword->keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$keyword->keyword} 10144 1224 11368
52 1224 $self->apply_keyword($keyword, $schema, $instance);
53 }
54  
55 1421 ▶ if ($self->is_matched_types(qw/integer_or_string number_or_string/)) {
Branch: 5 branches
BranchTrueFalseTotal
if ($self->is_matched_types('integer_or_string', 'number_or_string')) { } 212 1209 1421
elsif ($self->is_matched_types('integer', 'number')) { } 237 972 1209
elsif ($self->is_matched_types('string')) { } 439 533 972
elsif ($self->current_type eq 'array') { } 157 376 533
elsif ($self->current_type eq 'object') { } 328 48 376
56 212 for my $keyword (
57 212 @{ $self->keywords->{INSTANCE_TYPE_NUMERIC()} },
58 212 @{ $self->keywords->{INSTANCE_TYPE_STRING()} }
59 ) {
60 1484 ▶ next unless exists $schema->{$keyword->keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$keyword->keyword} 1399 85 1484
61 85 $self->apply_keyword($keyword, $schema, $instance);
62 }
63 }
64 elsif ($self->is_matched_types(qw/integer number/)) {
65 237 for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_NUMERIC()} }) {
66 711 ▶ next unless exists $schema->{$keyword->keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$keyword->keyword} 649 62 711
67 62 $self->apply_keyword($keyword, $schema, $instance);
68 }
69 }
70 elsif ($self->is_matched_types(qw/string/)) {
71 439 for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_STRING()} }) {
72 1756 ▶ next unless exists $schema->{$keyword->keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$keyword->keyword} 1648 108 1756
73 108 $self->apply_keyword($keyword, $schema, $instance);
74 }
75 }
76 elsif ($self->current_type eq "array") {
77 157 for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_ARRAY()} }) {
78 628 ▶ next unless exists $schema->{$keyword->keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $$schema{$keyword->keyword} 489 139 628
79 139 $self->apply_keyword($keyword, $schema, $instance);
80 }
81 }
82 elsif ($self->current_type eq "object") {
83 328 for my $keyword (@{ $self->keywords->{INSTANCE_TYPE_OBJECT()} }) {
84 ### for addtionalProperties, patternProperties keyword without properties keyword
85 1640 ▶ next unless ( ( grep { defined $_ && exists $schema->{$_} } ($keyword->keyword, @{$keyword->additional_keywords}) ) > 0 );
Branch: 1 branch
BranchTrueFalseTotal
unless grep({defined $_ and exists $schema->{$_};} $keyword->keyword, @{$keyword->additional_keywords;}) > 0 1222 418 1640
Condition66%2/3
defined $_ && exists $schema->{$_}
!ll&&!rl&&r
01811485
MC/DC50%1/2
defined $_ && exists $schema->{$_}
defined $_ exists $schema->{$_}
Truth table66%2/3
A && B
A= defined $_B= exists $schema->{$_}
ABresult
0X0
100
111
86 # next unless exists $schema->{$_->keyword};
87 418 $self->apply_keyword($keyword, $schema, $instance);
88 }
89 }
90  
91 1421 ▶ $rv = JSV::Result->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->enable_history ? : 0 1421 1421
92 ($self->enable_history ? (history => $self->history) : ()),
93 );
94 };
95 1421 ▶ if ( my $e = $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 1421 1421
96 0 $self->log_error(sprintf("Unexpected error: %s", $e));
97 }
98  
99 1421 ▶ while (my $cb = pop @{ $self->{cleanup_callbacks} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cb = pop @{$$self{"cleanup_callbacks"};}) 121 1421 1542
100 121 $cb->();
101 }
102  
103 1421 ▶ if ( scalar @{ $self->errors } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @{$self->errors;}) 581 840 1421
104 581 ▶ $rv = JSV::Result->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->enable_history ? : 0 581 581
105 errors => $self->errors,
106 ($self->enable_history ? (history => $self->history) : ()),
107 );
108 581 ▶ if ( $self->throw_error ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->throw_error) 0 581 581
109 0 croak $rv;
110 }
111 }
112  
113 1421 return $rv;
114 }
115  
116 2036 ▶sub apply_keyword {
Subroutine apply_keyword: called CC=3 SCAR=12.6
Pod undocumented
117 2036 my ($self, $keyword, $schema, $instance) = @_;
118  
119 2036 local $self->{current_keyword} = $keyword->keyword;
120 2036 local $self->{current_schema} = $schema;
121 2036 local $self->{current_instance} = $instance;
122  
123 2036 $keyword->validate($self, $schema, $instance);
124  
125 2036 ▶ if ( $ENV{JSV_DEBUG} || $self->enable_history ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"JSV_DEBUG"} or $self->enable_history) 0 2036 2036
Condition33%1/3
$ENV{"JSV_DEBUG"} or $self->enable_history
l!l&&r!l&&!r
002036
MC/DC0%0/2
$ENV{"JSV_DEBUG"} or $self->enable_history
$ENV{"JSV_DEBUG"} $self->enable_history
Truth table33%1/3
A or B
A= $ENV{"JSV_DEBUG"}B= $self->enable_history
ABresult
1X1
011
000
126 0 push @{ $self->history }, +{
127 keyword => $self->current_keyword,
128 pointer => $self->current_pointer,
129 schema => $self->current_schema,
130 instance => $self->resolve_current_instance,
131 };
132 }
133 }
134  
135 422 ▶sub log_error {
Subroutine log_error: called CC=4 SCAR=17.9
Pod undocumented
136 422 my ($self, $message) = @_;
137  
138 422 my $error = +{
139 keyword => $self->current_keyword,
140 pointer => $self->current_pointer,
141 schema => $self->current_schema,
142 instance => $self->resolve_current_instance,
143 schema_pointer => $self->current_schema_pointer,
144 schema_pointer_history => clone($self->schema_pointer_history),
145 message => $message,
146 };
147  
148 422 ▶ if ( $ENV{JSV_DEBUG} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ENV{"JSV_DEBUG"}) 0 422 422
149 0 require Data::Dump;
150 0 warn "history = " . Data::Dump::dump($self->history);
151 0 warn "error = " . Data::Dump::dump($error);
152 }
153  
154 422 ▶ if ( $self->throw_immediate ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->throw_immediate) { } 0 422 422
155 0 ▶ croak JSV::Result->new(
Branch: 1 branch
BranchTrueFalseTotal
$self->enable_history ? : 0 0 0
156 error => $error,
157 ($self->enable_history ? (history => $self->history) : ()),
158 );
159 }
160 else {
161 422 push @{ $self->{errors} }, $error;
162 }
163 }
164  
165 422 ▶sub resolve_current_instance {
Subroutine resolve_current_instance: called CC=3 SCAR=11.0
Pod undocumented
166 422 my $self = shift;
167  
168 422 my $instance;
169 422 ▶ if ( JSON::is_bool($self->current_instance) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (&JSON::is_bool(scalar $self->current_instance)) { } 11 411 422
170 11 ▶ if ( $self->current_instance == JSON::true ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->current_instance == JSON::true()) { } 9 2 11
171 9 $instance = "true";
172 }
173 else {
174 2 $instance = "false";
175 }
176 }
177 else {
178 411 $instance = $self->current_instance;
179 }
180  
181 422 return $instance;
182 }
183  
184 3602 ▶sub is_matched_types {
Subroutine is_matched_types: called CC=2 SCAR=6.9
Pod undocumented
185 3602 my ($self, @types) = @_;
186  
187 3602 ▶ return (grep { $self->{current_type} eq $_ } @types) > 0 ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
grep({$self->{'current_type'} eq $_;} @types) > 0 ? : 888 2714 3602
188 }
189  
190 121 ▶sub register_cleanup_callback {
Subroutine register_cleanup_callback: called CC=1 SCAR=0.0
Pod undocumented
191 121 my ($self, $cb) = @_;
192  
193 121 push @{ $self->{cleanup_callbacks} }, $cb;
194 }
195  
196 47 1;
↑ Summary JSV/Keyword/Draft4/Properties.pm »