Pointer.pm

filter: Statementstmt 95.3% 185 / 194 Branchbran 77.6% 87 / 112 Conditioncond 78.5% 22 / 28 MC/DCmcdc 72.2% 13 / 18 Subroutinesub 96.0% 24 / 25 Podpod 100% 11 / 11 totaltotal 88.1% 342 / 388 CC 68 SCAR 22.6
CC
68
Cov
88%
test
18.5
remove
12.9
add
10.6
CRAP 9.6
↑ Summary Pointer/Syntax.pm »
1 package JSON::Pointer;
2  
3 12 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use B;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use JSON qw(encode_json decode_json);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 ▶use JSON::Pointer::Context;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 12 ▶use JSON::Pointer::Exception qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
13 12 ▶use JSON::Pointer::Syntax qw(is_array_numeric_index);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 12 ▶use URI::Escape qw(uri_unescape);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 12 our $VERSION = '0.07';
17  
18 147 ▶sub traverse {
Subroutine traverse: called CC=10 SCAR=23.0
Pod documented
19 147 my ($class, $document, $pointer, $opts) = @_;
20 $opts = +{
21 strict => 1,
22 inclusive => 0,
23 147 ▶ %{ $opts || +{} }
Condition100%2/2
$opts || {}
l!l
12027
MC/DC100%1/1
$opts || {}
$opts
Truth table100%2/2
A
A= $opts
Aresult
11
00
24 };
25 147 $pointer = uri_unescape($pointer);
26  
27 147 my @tokens = JSON::Pointer::Syntax->tokenize($pointer);
28 147 my $context = JSON::Pointer::Context->new(+{
29 pointer => $pointer,
30 tokens => \@tokens,
31 target => $document,
32 parent => $document,
33 });
34  
35 147 foreach my $token (@tokens) {
36 197 $context->begin($token);
37  
38 197 my $parent = $context->parent;
39 197 my $type = ref $parent;
40  
41 197 ▶ if ($type eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 141 56 197
elsif ($type eq 'ARRAY') { } 56 0 56
42 141 ▶ unless (exists $parent->{$token}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $parent->{$token}) 24 117 141
43 24 return _throw_or_return(ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, $context, $opts->{strict});
44 }
45  
46 117 $context->next($parent->{$token});
47 117 next;
48 }
49 elsif ($type eq "ARRAY") {
50 56 ▶ if ($token eq '-') {
Branch: 1 branch
BranchTrueFalseTotal
if ($token eq "-") 1 55 56
51 1 $token = $#{$parent} + 1;
52 }
53  
54 56 my $max_index = $#{$parent};
55 56 ▶ $max_index++ if $opts->{inclusive};
Branch: 1 branch
BranchTrueFalseTotal
if $opts->{"inclusive"} 14 42 56
56  
57 56 ▶ if (is_array_numeric_index($token) && $token <= $max_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_array_numeric_index($token) and $token <= $max_index) { } 44 12 56
Condition100%3/3
is_array_numeric_index($token) and $token <= $max_index
!ll&&!rl&&r
8444
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 44 $context->next($parent->[$token]);
59 44 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 111 $context->result(1);
71 111 return $context;
72 }
73  
74 33 ▶sub get {
Subroutine get: called CC=4 SCAR=13.9
Pod documented
75 33 my ($class, $document, $pointer, $strict) = @_;
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 12 ▶sub get_relative {
Subroutine get_relative: called CC=10 SCAR=23.1
Pod documented
90 12 my ($class, $document, $current_pointer, $relative_pointer, $strict) = @_;
91 12 ▶ $strict = 0 unless defined $strict;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $strict 10 2 12
92  
93 12 my @current_tokens = JSON::Pointer::Syntax->tokenize($current_pointer);
94  
95 12 my $context = JSON::Pointer::Context->new(+{
96 pointer => $current_pointer,
97 tokens => \@current_tokens,
98 target => $document,
99 parent => $document,
100 });
101  
102 12 my ($steps, $relative_pointer_suffix, $use_index) =
103 ($relative_pointer =~ m{^(0|[1-9]?[0-9]+)([^#]*)(#?)$});
104 12 ▶ $relative_pointer_suffix ||= "";
Condition100%2/2
$relative_pointer_suffix ||= ""
l!l
57
MC/DC100%1/1
$relative_pointer_suffix ||= ""
$relative_pointer_suffix
Truth table100%2/2
A
A= $relative_pointer_suffix
Aresult
11
00
105  
106 12 ▶ unless (defined $steps) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $steps) 1 11 12
107 1 return _throw_or_return(ERROR_INVALID_POINTER_SYNTAX, $context, +{ strict => $strict });
108 }
109  
110 11 ▶ for (my $i = 0; $i < $steps; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $steps) 11 10 21
111 11 ▶ if (@current_tokens == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@current_tokens == 0) 1 10 11
112 1 return _throw_or_return(ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE, $context, +{ strict => $strict });
113 }
114 10 pop(@current_tokens);
115 }
116  
117 10 ▶ if ($use_index) {
Branch: 1 branch
BranchTrueFalseTotal
if ($use_index) 4 6 10
118 4 my @relative_tokens = JSON::Pointer::Syntax->tokenize($relative_pointer_suffix);
119 4 ▶ return (@relative_tokens > 0) ? $relative_tokens[-1] : $current_tokens[-1];
Branch: 1 branch
BranchTrueFalseTotal
@relative_tokens > 0 ? : 0 4 4
120 }
121  
122 6 my $absolute_pointer = JSON::Pointer::Syntax->as_pointer(@current_tokens) . $relative_pointer_suffix;
123  
124 6 eval {
125 6 $context = $class->traverse($document, $absolute_pointer, +{ strict => $strict });
126 };
127 6 ▶ if (my $e = $@) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $e = $@) 0 6 6
128 0 croak $e;
129 }
130  
131 6 ▶ return $context->result ? $context->target : undef;
Branch: 1 branch
BranchTrueFalseTotal
$context->result ? : 6 0 6
132 }
133  
134 9 ▶sub contains {
Subroutine contains: called CC=1 SCAR=0.0
Pod documented
135 9 my ($class, $document, $pointer) = @_;
136 9 my $context = $class->traverse($document, $pointer, +{ strict => 0 });
137 9 return $context->result;
138 }
139  
140 34 ▶sub add {
Subroutine add: called CC=10 SCAR=23.6
Pod documented
141 34 my ($class, $document, $pointer, $value) = @_;
142  
143 34 my $patched_document = clone($document);
144  
145 34 my $context = $class->traverse($patched_document, $pointer, +{ strict => 0, inclusive => 1 });
146 34 my $parent = $context->parent;
147 34 my $type = ref $parent;
148  
149 34 ▶ if ($type eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 21 13 34
elsif ($type eq 'ARRAY') { } 13 0 13
150 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
151 ### Parent isn't object
152 2 JSON::Pointer::Exception->throw(
153 code => ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE,
154 context => $context,
155 );
156 }
157  
158 19 ▶ if (defined $context->last_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->last_token) { } 18 1 19
159 18 $parent->{$context->last_token} = $value;
160 }
161 else {
162 ### pointer is empty string (whole document)
163 1 $patched_document = $value;
164 }
165  
166 19 return $patched_document;
167 }
168 elsif ($type eq "ARRAY") {
169 13 ▶ unless ($context->result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($context->result) 3 10 13
170 3 JSON::Pointer::Exception->throw(
171 code => ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE,
172 context => $context,
173 );
174 }
175  
176 10 ▶ if (defined $context->last_token) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $context->last_token) { } 10 0 10
177 10 my $parent_array_length = $#{$parent} + 1;
178 10 ▶ my $target_index = ($context->last_token eq "-") ?
Branch: 1 branch
BranchTrueFalseTotal
$context->last_token eq '-' ? : 1 9 10
179 $parent_array_length : $context->last_token;
180  
181 10 splice(@$parent, $target_index, 0, $value);
182 }
183 else {
184 0 $patched_document = $value;
185 }
186  
187 10 return $patched_document;
188 }
189 else {
190 0 ▶ unless ($context->result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($context->result) 0 0 0
191 0 JSON::Pointer::Exception->throw(
192 code => ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE,
193 context => $context,
194 );
195 }
196  
197 0 return $value;
198 }
199 }
200  
201 14 ▶sub remove {
Subroutine remove: called CC=12 SCAR=25.5
Pod documented
202 14 my ($class, $document, $pointer) = @_;
203  
204 14 my $patched_document = clone($document);
205  
206 14 my $context = $class->traverse($patched_document, $pointer);
207 13 my $parent = $context->parent;
208 13 my $type = ref $parent;
209  
210 13 ▶ if ($type eq "HASH") {
Branch: 2 branches
BranchTrueFalseTotal
if ($type eq 'HASH') { } 8 5 13
elsif ($type eq 'ARRAY') { } 4 1 5
211 8 my $target_member = $context->last_token;
212 8 ▶ if (defined $target_member) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target_member) { } 7 1 8
213 7 my $removed = delete $parent->{$target_member};
214 7 ▶ return wantarray ? ($patched_document, $removed) : $patched_document;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 7 0 7
215 }
216 else {
217 ### pointer is empty string (whole document)
218 1 ▶ return wantarray ? (undef, $patched_document) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
219 }
220 }
221 elsif ($type eq "ARRAY") {
222 4 my $target_index = $context->last_token;
223 4 ▶ if (defined $target_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target_index) { } 3 1 4
224 3 my $parent_array_length = $#{$parent} + 1;
225 3 ▶ $target_index = $parent_array_length if ($target_index eq "-");
Branch: 1 branch
BranchTrueFalseTotal
if $target_index eq "-" 0 3 3
226 3 my $removed = splice(@$parent, $target_index, 1);
227 3 ▶ return wantarray ? ($patched_document, $removed) : $patched_document;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
228 }
229 else {
230 ### pointer is empty string (whole document)
231 1 ▶ return wantarray ? (undef, $patched_document) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
232 }
233 }
234 else {
235 1 ▶ unless ($context->result) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($context->result) 0 1 1
236 0 JSON::Pointer::Exception->throw(
237 code => ERROR_POINTER_REFERENCES_NON_EXISTENT_VALUE,
238 context => $context,
239 );
240 }
241  
242 1 ▶ return wantarray ? (undef, $patched_document) : undef;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
243 }
244 }
245  
246 12 ▶sub replace {
Subroutine replace: called CC=9 SCAR=22.2
Pod documented
247 12 my ($class, $document, $pointer, $value) = @_;
248  
249 12 my $patched_document = clone($document);
250 12 my $context = $class->traverse($patched_document, $pointer);
251 12 my $parent = $context->parent;
252 12 my $type = ref $parent;
253  
254 12 ▶ if ($type eq "HASH") {
Branch: 1 branch
BranchTrueFalseTotal
if ($type eq 'HASH') { } 4 8 12
255 4 my $target_member = $context->last_token;
256 4 ▶ if (defined $target_member) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target_member) { } 3 1 4
257 3 my $replaced = $parent->{$context->last_token};
258 3 $parent->{$context->last_token} = $value;
259 3 ▶ return wantarray ? ($patched_document, $replaced) : $patched_document;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 3 0 3
260 }
261 else {
262 ### pointer is empty string (whole document)
263 1 ▶ return wantarray ? ($value, $patched_document) : $value;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 0 1
264 }
265 }
266 else {
267 8 my $target_index = $context->last_token;
268 8 ▶ if (defined $target_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $target_index) { } 6 2 8
269 6 my $parent_array_length = $#{$parent} + 1;
270 6 ▶ $target_index = $parent_array_length if ($target_index eq "-");
Branch: 1 branch
BranchTrueFalseTotal
if $target_index eq "-" 0 6 6
271 6 my $replaced = $parent->[$target_index];
272 6 $parent->[$target_index] = $value;
273 6 ▶ return wantarray ? ($patched_document, $replaced) : $patched_document;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 0 6
274 }
275 else {
276 ### pointer is empty string (whole document)
277 2 ▶ return wantarray ? ($value, $patched_document) : $value;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 0 2
278 }
279 }
280 }
281  
282 0 ▶sub set {
Subroutine set: not called CC=1 SCAR=6.9
Pod documented
283 0 shift->replace(@_);
284 }
285  
286 1 ▶sub copy {
Subroutine copy: called CC=1 SCAR=0.0
Pod documented
287 1 my ($class, $document, $from_pointer, $to_pointer) = @_;
288 1 my $context = $class->traverse($document, $from_pointer);
289 1 return $class->add($document, $to_pointer, $context->target);
290 }
291  
292 5 ▶sub move {
Subroutine move: called CC=1 SCAR=0.0
Pod documented
293 5 my ($class, $document, $from_pointer, $to_pointer) = @_;
294 5 my ($patched_document, $removed) = $class->remove($document, $from_pointer);
295 5 $class->add($patched_document, $to_pointer, $removed);
296 }
297  
298 23 ▶sub test {
Subroutine test: called CC=16 SCAR=29.2
Pod documented
299 23 my ($class, $document, $pointer, $value) = @_;
300  
301 23 my $context = $class->traverse($document, $pointer, +{ strict => 0 });
302  
303 23 ▶ return 0 unless $context->result;
Branch: 1 branch
BranchTrueFalseTotal
unless $context->result 1 22 23
304  
305 22 my $target = $context->target;
306 22 my $target_type = ref $target;
307  
308 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
309 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
310 }
311 elsif (defined $target) {
312 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
313 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
314 }
315 ▶ 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
316 12 ▶ return $target == $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$target == $value ? : 12 0 12
317 }
318 ▶ 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
319 4 ▶ return $target eq $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$target eq $value ? : 3 1 4
320 }
321 else {
322 1 return 0;
323 }
324 }
325 else {
326 ### null
327 1 ▶ return !defined $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
!defined($value) ? : 1 0 1
328 }
329 }
330  
331 38 ▶sub _throw_or_return {
Subroutine _throw_or_return: called CC=2 SCAR=6.9
332 38 my ($code, $context, $strict) = @_;
333  
334 38 ▶ if ($strict) {
Branch: 1 branch
BranchTrueFalseTotal
if ($strict) { } 6 32 38
335 6 JSON::Pointer::Exception->throw(
336 code => $code,
337 context => $context,
338 );
339 }
340 else {
341 32 $context->last_error($code);
342 32 return $context;
343 }
344 }
345  
346 30 ▶sub _is_iv_or_nv {
Subroutine _is_iv_or_nv: called CC=2 SCAR=7.0
347 30 my $value = shift;
348 30 my $flags = B::svref_2object(\$value)->FLAGS;
349 30 ▶ return ( ($flags & ( B::SVp_IOK | B::SVp_NOK )) && !($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
350 }
351  
352 9 ▶sub _is_pv {
Subroutine _is_pv: called CC=2 SCAR=7.0
353 9 my $value = shift;
354 9 my $flags = B::svref_2object(\$value)->FLAGS;
355 9 ▶ return ( !($flags & ( B::SVp_IOK | B::SVp_NOK )) && ($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
356 }
357  
358 12 1;
359  
360 __END__
↑ Summary Pointer/Syntax.pm »