blib/lib/JSON/Pointer.pm

filter: Statementstmt 95.3% 165 / 173 Branchbran 77.5% 76 / 98 Conditioncond 75.0% 18 / 24 MC/DCmcdc 68.7% 11 / 16 Subroutinesub 95.8% 23 / 24 Podpod 100% 10 / 10 totaltotal 87.8% 303 / 345 CC 59 SCAR 22.6
CC
59
Cov
88%
test
18.5
remove
12.9
add
10.6
CRAP 9.5
↑ Summary Build »
1 package JSON::Pointer;
2  
3 11 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 11 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 11 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 11 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 11 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 11 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 11 ▶use JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 11 ▶use JSON::Pointer::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 11 ▶use JSON::Pointer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 11 ▶use JSON::Pointer::Syntax qw(is_array_numeric_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 11 ▶use URI::Escape qw(uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 11 our $VERSION = '0.06';
17  
18 sub traverse {
19 141 ▶ my ($class, $document, $pointer, $opts) = @_;
Subroutine traverse: called CC=10 SCAR=23.0
Pod documented
20 $opts = +{
21 strict => 1,
22 inclusive => 0,
23 141 ▶ %{ $opts || +{} }
Branch: 1 branch
BranchTrueFalseTotal
$opts or {} 27 114 141
24 };
25 141 $pointer = uri_unescape($pointer);
26  
27 141 my @tokens = JSON::Pointer::Syntax->tokenize($pointer);
28 141 my $context = JSON::Pointer::Context->new(+{
29 pointer => $pointer,
30 tokens => \@tokens,
31 target => $document,
32 parent => $document,
33 });
34  
35 141 foreach my $token (@tokens) {
36 182 $context->begin($token);
37  
38 182 my $parent = $context->parent;
39 182 my $type = ref $parent;
40  
41 182 ▶ if ($type eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 129 53 182
elsif ($type eq 'ARRAY') { } 53 0 53
42 129 ▶ unless (exists $parent->{$token}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $parent->{$token}) 24 105 129
43 24 return _throw_or_return(ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, $context, $opts->{strict});
44 }
45  
46 105 $context->next($parent->{$token});
47 105 next;
48 }
49 elsif ($type eq "ARRAY") {
50 53 ▶ if ($token eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq "-") 1 52 53
51 1 $token = $#{$parent} + 1;
52 }
53  
54 53 my $max_index = $#{$parent};
55 53 ▶ $max_index++ if $opts->{inclusive};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"inclusive"} 14 39 53
56  
57 53 ▶ if (is_array_numeric_index($token) && $token <= $max_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array_numeric_index($token) and $token <= $max_index) { } 41 12 53
Condition100%3/3
is_array_numeric_index($token) and $token <= $max_index
!ll&&!rl&&r
8441
MC/DC100%2/2
is_array_numeric_index($token) and $token <= $max_index
is_array_numeric_index($token) $token <= $max_index
Truth table100%3/3
A and B
A= is_array_numeric_index($token)B= $token <= $max_index
ABresult
0X0
100
111
58 41 $context->next($parent->[$token]);
59 41 next;
60 }
61 else {
62 12 return _throw_or_return(ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, $context, $opts->{strict});
63 }
64 }
65 else {
66 0 return _throw_or_return(ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, $context, $opts->{strict});
67 }
68 }
69  
70 105 $context->result(1);
71 105 return $context;
72 }
73  
74 sub get {
75 33 ▶ my ($class, $document, $pointer, $strict) = @_;
Subroutine get: called CC=4 SCAR=13.9
Pod documented
76 33 ▶ $strict = 0 unless defined $strict;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $strict 30 3 33
77  
78 33 my $context;
79 33 eval {
80 33 $context = $class->traverse($document, $pointer, +{ strict => $strict });
81 };
82 33 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 3 30 33
83 3 croak $e;
84 }
85  
86 30 ▶ return $context->result ? $context->target : undef;
Branch: 1 branch
BranchTrueFalseTotal
$context->result ? : 28 2 30
87 }
88  
89 sub contains {
90 9 ▶ my ($class, $document, $pointer) = @_;
Subroutine contains: called CC=1 SCAR=0.0
Pod documented
91 9 my $context = $class->traverse($document, $pointer, +{ strict => 0 });
92 9 return $context->result;
93 }
94  
95 sub add {
96 34 ▶ my ($class, $document, $pointer, $value) = @_;
Subroutine add: called CC=10 SCAR=23.6
Pod documented
97  
98 34 my $patched_document = clone($document);
99  
100 34 my $context = $class->traverse($patched_document, $pointer, +{ strict => 0, inclusive => 1 });
101 34 my $parent = $context->parent;
102 34 my $type = ref $parent;
103  
104 34 ▶ if ($type eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 21 13 34
elsif ($type eq 'ARRAY') { } 13 0 13
105 21 ▶ if (!$context->result && @{$context->processed_tokens} < @{$context->tokens} - 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $context->result and @{$context->processed_tokens;} < @{$context->tokens;} - 1) 2 19 21
Condition100%3/3
not $context->result and @{$context->processed_tokens;} < @{$context->tokens;} - 1
!ll&&!rl&&r
2172
MC/DC100%2/2
not $context->result and @{$context->processed_tokens;} < @{$context->tokens;} - 1
not $context->result @{$context->processed_tokens;} < @{$context->tokens;} - 1
Truth table100%3/3
not(A) and B
A= not $context->resultB= @{$context->processed_tokens;} < @{$context->tokens;} - 1
ABresult
0X0
100
111
106 ### Parent isn't object
107 2 JSON::Pointer::Exception->throw(
108 code => ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE,
109 context => $context,
110 );
111 }
112  
113 19 ▶ if (defined $context->last_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->last_token) { } 18 1 19
114 18 $parent->{$context->last_token} = $value;
115 }
116 else {
117 ### pointer is empty string (whole document)
118 1 $patched_document = $value;
119 }
120  
121 19 return $patched_document;
122 }
123 elsif ($type eq "ARRAY") {
124 13 ▶ unless ($context->result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($context->result) 3 10 13
125 3 JSON::Pointer::Exception->throw(
126 code => ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE,
127 context => $context,
128 );
129 }
130  
131 10 ▶ if (defined $context->last_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->last_token) { } 10 0 10
132 10 my $parent_array_length = $#{$parent} + 1;
133 10 ▶ my $target_index = ($context->last_token eq "-") ?
Branch: 1 branch
BranchTrueFalseTotal
$context->last_token eq '-' ? : 1 9 10
134 $parent_array_length : $context->last_token;
135  
136 10 splice(@$parent, $target_index, 0, $value);
137 }
138 else {
139 0 $patched_document = $value;
140 }
141  
142 10 return $patched_document;
143 }
144 else {
145 0 ▶ unless ($context->result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($context->result) 0 0 0
146 0 JSON::Pointer::Exception->throw(
147 code => ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE,
148 context => $context,
149 );
150 }
151  
152 0 return $value;
153 }
154 }
155  
156 sub remove {
157 14 ▶ my ($class, $document, $pointer) = @_;
Subroutine remove: called CC=12 SCAR=25.5
Pod documented
158  
159 14 my $patched_document = clone($document);
160  
161 14 my $context = $class->traverse($patched_document, $pointer);
162 13 my $parent = $context->parent;
163 13 my $type = ref $parent;
164  
165 13 ▶ if ($type eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 8 5 13
elsif ($type eq 'ARRAY') { } 4 1 5
166 8 my $target_member = $context->last_token;
167 8 ▶ if (defined $target_member) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target_member) { } 7 1 8
168 7 my $removed = delete $parent->{$target_member};
169 7 ▶ return wantarray ? ($patched_document, $removed) : $patched_document;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7 0 7
170 }
171 else {
172 ### pointer is empty string (whole document)
173 1 ▶ return wantarray ? (undef, $patched_document) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
174 }
175 }
176 elsif ($type eq "ARRAY") {
177 4 my $target_index = $context->last_token;
178 4 ▶ if (defined $target_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target_index) { } 3 1 4
179 3 my $parent_array_length = $#{$parent} + 1;
180 3 ▶ $target_index = $parent_array_length if ($target_index eq "-");
Branch: 1 branch
BranchTrueFalseTotal
if $target_index eq "-" 0 3 3
181 3 my $removed = splice(@$parent, $target_index, 1);
182 3 ▶ return wantarray ? ($patched_document, $removed) : $patched_document;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
183 }
184 else {
185 ### pointer is empty string (whole document)
186 1 ▶ return wantarray ? (undef, $patched_document) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
187 }
188 }
189 else {
190 1 ▶ unless ($context->result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($context->result) 0 1 1
191 0 JSON::Pointer::Exception->throw(
192 code => ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE,
193 context => $context,
194 );
195 }
196  
197 1 ▶ return wantarray ? (undef, $patched_document) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
198 }
199 }
200  
201 sub replace {
202 12 ▶ my ($class, $document, $pointer, $value) = @_;
Subroutine replace: called CC=9 SCAR=22.2
Pod documented
203  
204 12 my $patched_document = clone($document);
205 12 my $context = $class->traverse($patched_document, $pointer);
206 12 my $parent = $context->parent;
207 12 my $type = ref $parent;
208  
209 12 ▶ if ($type eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'HASH') { } 4 8 12
210 4 my $target_member = $context->last_token;
211 4 ▶ if (defined $target_member) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target_member) { } 3 1 4
212 3 my $replaced = $parent->{$context->last_token};
213 3 $parent->{$context->last_token} = $value;
214 3 ▶ return wantarray ? ($patched_document, $replaced) : $patched_document;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
215 }
216 else {
217 ### pointer is empty string (whole document)
218 1 ▶ return wantarray ? ($value, $patched_document) : $value;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
219 }
220 }
221 else {
222 8 my $target_index = $context->last_token;
223 8 ▶ if (defined $target_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target_index) { } 6 2 8
224 6 my $parent_array_length = $#{$parent} + 1;
225 6 ▶ $target_index = $parent_array_length if ($target_index eq "-");
Branch: 1 branch
BranchTrueFalseTotal
if $target_index eq "-" 0 6 6
226 6 my $replaced = $parent->[$target_index];
227 6 $parent->[$target_index] = $value;
228 6 ▶ return wantarray ? ($patched_document, $replaced) : $patched_document;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 0 6
229 }
230 else {
231 ### pointer is empty string (whole document)
232 2 ▶ return wantarray ? ($value, $patched_document) : $value;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
233 }
234 }
235 }
236  
237 sub set {
238 0 ▶ shift->replace(@_);
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
239 }
240  
241 sub copy {
242 1 ▶ my ($class, $document, $from_pointer, $to_pointer) = @_;
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
243 1 my $context = $class->traverse($document, $from_pointer);
244 1 return $class->add($document, $to_pointer, $context->target);
245 }
246  
247 sub move {
248 5 ▶ my ($class, $document, $from_pointer, $to_pointer) = @_;
Subroutine move: called CC=1 SCAR=0.0
Pod documented
249 5 my ($patched_document, $removed) = $class->remove($document, $from_pointer);
250 5 $class->add($patched_document, $to_pointer, $removed);
251 }
252  
253 sub test {
254 23 ▶ my ($class, $document, $pointer, $value) = @_;
Subroutine test: called CC=16 SCAR=29.2
Pod documented
255  
256 23 my $context = $class->traverse($document, $pointer, +{ strict => 0 });
257  
258 23 ▶ return 0 unless $context->result;
Branch: 1 branch
BranchTrueFalseTotal
unless $context->result 1 22 23
259  
260 22 my $target = $context->target;
261 22 my $target_type = ref $target;
262  
263 22 ▶ if ($target_type eq "HASH" || $target_type eq "ARRAY") {
Branch: 2 branches
BranchTrueFalseTotal
if ($target_type eq 'HASH' or $target_type eq 'ARRAY') { } 4 18 22
elsif (defined $target) { } 17 1 18
Condition100%3/3
$target_type eq "HASH" or $target_type eq "ARRAY"
l!l&&r!l&&!r
3118
MC/DC100%2/2
$target_type eq "HASH" or $target_type eq "ARRAY"
$target_type eq "HASH" $target_type eq "ARRAY"
Truth table100%3/3
A or B
A= $target_type eq "HASH"B= $target_type eq "ARRAY"
ABresult
1X1
011
000
264 4 ▶ return encode_json($target) eq encode_json($value) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&encode_json($target) eq &encode_json($value) ? : 3 1 4
265 }
266 elsif (defined $target) {
267 17 ▶ if (JSON::is_bool($target)) {
Branch: 3 branches
BranchTrueFalseTotal
if (&JSON::is_bool($target)) { } 0 17 17
elsif (_is_iv_or_nv($target) and _is_iv_or_nv($value)) { } 12 5 17
elsif (_is_pv($target) and _is_pv($value)) { } 4 1 5
268 0 ▶ return JSON::is_bool($value) && $target == $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
&JSON::is_bool($value) && $target == $value ? : 0 0 0
Condition0%0/3
&JSON::is_bool($value) and $target == $value
!ll&&!rl&&r
000
MC/DC0%0/2
&JSON::is_bool($value) and $target == $value
&JSON::is_bool($value) $target == $value
Truth table0%0/3
A and B
A= &JSON::is_bool($value)B= $target == $value
ABresult
0X0
100
111
269 }
270 ▶ elsif (_is_iv_or_nv($target) && _is_iv_or_nv($value)) {
Condition100%3/3
_is_iv_or_nv($target) and _is_iv_or_nv($value)
!ll&&!rl&&r
4112
MC/DC100%2/2
_is_iv_or_nv($target) and _is_iv_or_nv($value)
_is_iv_or_nv($target) _is_iv_or_nv($value)
Truth table100%3/3
A and B
A= _is_iv_or_nv($target)B= _is_iv_or_nv($value)
ABresult
0X0
100
111
271 12 ▶ return $target == $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$target == $value ? : 12 0 12
272 }
273 ▶ elsif (_is_pv($target) && _is_pv($value)) {
Condition66%2/3
_is_pv($target) and _is_pv($value)
!ll&&!rl&&r
104
MC/DC50%1/2
_is_pv($target) and _is_pv($value)
_is_pv($target) _is_pv($value)
Truth table66%2/3
A and B
A= _is_pv($target)B= _is_pv($value)
ABresult
0X0
100
111
274 4 ▶ return $target eq $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$target eq $value ? : 3 1 4
275 }
276 else {
277 1 return 0;
278 }
279 }
280 else {
281 ### null
282 1 ▶ return !defined $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!defined($value) ? : 1 0 1
283 }
284 }
285  
286 sub _throw_or_return {
287 36 ▶ my ($code, $context, $strict) = @_;
Subroutine _throw_or_return: called CC=2 SCAR=6.9
288  
289 36 ▶ if ($strict) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strict) { } 4 32 36
290 4 JSON::Pointer::Exception->throw(
291 code => $code,
292 context => $context,
293 );
294 }
295 else {
296 32 $context->last_error($code);
297 32 return $context;
298 }
299 }
300  
301 sub _is_iv_or_nv {
302 30 ▶ my $value = shift;
Subroutine _is_iv_or_nv: called CC=2 SCAR=7.0
303 30 my $flags = B::svref_2object(\$value)->FLAGS;
304 30 ▶ return ( ($flags & ( B::SVp_IOK | B::SVp_NOK )) and !($flags & B::SVp_POK) );
Condition66%2/3
$flags & 12288 && !($flags & 16384)
!ll&&!rl&&r
5025
MC/DC50%1/2
$flags & 12288 && !($flags & 16384)
$flags & 12288 !($flags & 16384)
Truth table66%2/3
A && not(B)
A= $flags & 12288B= !($flags & 16384)
ABresult
0X0
100
111
305 }
306  
307 sub _is_pv {
308 9 ▶ my $value = shift;
Subroutine _is_pv: called CC=2 SCAR=7.0
309 9 my $flags = B::svref_2object(\$value)->FLAGS;
310 9 ▶ return ( !($flags & ( B::SVp_IOK | B::SVp_NOK )) and ($flags & B::SVp_POK) );
Condition66%2/3
!($flags & 12288) && $flags & 16384
!ll&&!rl&&r
108
MC/DC50%1/2
!($flags & 12288) && $flags & 16384
!($flags & 12288) $flags & 16384
Truth table66%2/3
not(A) && B
A= !($flags & 12288)B= $flags & 16384
ABresult
0X0
100
111
311 }
312  
313 11 1;
314  
315 __END__
↑ Summary Build »