| 1 | package Test2::Compare::JSON::Pointer; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 4 | ▶ | use Test2::Util::HashBase qw( pointer input json ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 4 | ▶ | use JSON::Pointer; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | 4 | ▶ | use Encode (); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 4 | ▶ | use parent 'Test2::Compare::Base'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | |||||||||||||||
| 10 | # ABSTRACT: Representation of a hash or array reference pointed to by a JSON pointer during deep comparison. | ||||||||||||||
| 11 | 4 | our $VERSION = '0.02'; # VERSION | |||||||||||||
| 12 | |||||||||||||||
| 13 | |||||||||||||||
| 14 | 3 | ▶ | sub operator { 'JSON PTR' } | ||||||||||||
|
Subroutine
operator: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 15 | |||||||||||||||
| 16 | sub name | ||||||||||||||
| 17 | 3 | ▶ | { | ||||||||||||
|
Subroutine
name: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 18 | 3 | my($self) = @_; | |||||||||||||
| 19 | 3 | my($input, $pointer) = ($self->{+INPUT}, $self->{+POINTER}); | |||||||||||||
| 20 | 3 | ▶ | $pointer eq '' ? "$input" : "$pointer $input"; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 21 | } | ||||||||||||||
| 22 | |||||||||||||||
| 23 | sub verify | ||||||||||||||
| 24 | 12 | ▶ | { | ||||||||||||
|
Subroutine
verify: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 25 | 12 | my($self, %params) = @_; | |||||||||||||
| 26 | 12 | my($got, $exists) = @params{'got','exists'}; | |||||||||||||
| 27 | |||||||||||||||
| 28 | 12 | ▶ | return 0 unless $exists; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 29 | 12 | return 1; | |||||||||||||
| 30 | } | ||||||||||||||
| 31 | |||||||||||||||
| 32 | sub _convert_got | ||||||||||||||
| 33 | 12 | ▶ | { | ||||||||||||
|
Subroutine
_convert_got: called CC=4 SCAR=13.9
| |||||||||||||||
| 34 | 12 | my(undef, $got) = @_; | |||||||||||||
| 35 | |||||||||||||||
| 36 | 12 | ▶ | if(ref $got) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 37 | { | ||||||||||||||
| 38 | 4 | ▶ | if(eval { $got->isa('Path::Tiny') }) | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 39 | { | ||||||||||||||
| 40 | 2 | return $got->slurp_raw; | |||||||||||||
| 41 | } | ||||||||||||||
| 42 | 2 | elsif(eval { $got->isa('Path::Class::File') }) | |||||||||||||
| 43 | { | ||||||||||||||
| 44 | 2 | return $got->slurp(iomode => '<:unix'); | |||||||||||||
| 45 | } | ||||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 8 | return Encode::encode("UTF-8", $got); | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | |||||||||||||||
| 51 | sub deltas | ||||||||||||||
| 52 | 12 | ▶ | { | ||||||||||||
|
Subroutine
deltas: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||
| 53 | 12 | my($self, %p) = @_; | |||||||||||||
| 54 | 12 | my($got, $convert) = @p{'got','convert','seen'}; | |||||||||||||
| 55 | |||||||||||||||
| 56 | 12 | my $check = $convert->($self->{+INPUT}); | |||||||||||||
| 57 | |||||||||||||||
| 58 | 12 | my $got_root_ref = eval { | |||||||||||||
| 59 | 12 | $self->{+JSON}->decode($self->_convert_got($got)); | |||||||||||||
| 60 | }; | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 12 | my $pointer = $self->{+POINTER}; | |||||||||||||
| 63 | 12 | ▶ | my $id = [ META => $pointer eq '' ? 'JSON' : "JSON $pointer" ]; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 64 | |||||||||||||||
| 65 | 12 | ▶ | if(my $error = "$@") | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 66 | { | ||||||||||||||
| 67 | 1 | my $check = $convert->('valid json'); | |||||||||||||
| 68 | |||||||||||||||
| 69 | 1 | $error =~ s/ at \S+ line [0-9]+\.//; | |||||||||||||
| 70 | 1 | return $check->delta_class->new( | |||||||||||||
| 71 | verified => undef, | ||||||||||||||
| 72 | id => $id, | ||||||||||||||
| 73 | got => undef, | ||||||||||||||
| 74 | check => $check, | ||||||||||||||
| 75 | exception => "invalid json: $error", | ||||||||||||||
| 76 | ); | ||||||||||||||
| 77 | } | ||||||||||||||
| 78 | |||||||||||||||
| 79 | 11 | my $got_ref; | |||||||||||||
| 80 | my $exists; | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 11 | ▶ | if(JSON::Pointer->contains($got_root_ref, $pointer)) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | { | ||||||||||||||
| 84 | 11 | $exists = 1; | |||||||||||||
| 85 | 11 | $got_ref = JSON::Pointer->get($got_root_ref, $pointer); | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | else | ||||||||||||||
| 88 | { | ||||||||||||||
| 89 | 0 | $exists = 0; | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | 11 | my $delta = $check->run( | |||||||||||||
| 93 | id => $id, | ||||||||||||||
| 94 | got => $got_ref, | ||||||||||||||
| 95 | exists => $exists, | ||||||||||||||
| 96 | convert => $convert, | ||||||||||||||
| 97 | seen => {}, | ||||||||||||||
| 98 | ); | ||||||||||||||
| 99 | |||||||||||||||
| 100 | 11 | ▶ | $delta ? $delta : (); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | } | ||||||||||||||
| 102 | |||||||||||||||
| 103 | 4 | 1; | |||||||||||||
| 104 | |||||||||||||||
| 105 | __END__ | ||||||||||||||