JSON/Assert.pm

filter: Statementstmt 14.3% 20 / 139 Branchbran 0.0% 0 / 70 Conditioncond 5.0% 1 / 20 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 31.2% 5 / 16 Podpod 100% 8 / 8 totaltotal 12.9% 34 / 262 SCAR 72.6
CC
43
Cov
9%
assert_jpath_values_match
156.0
assert_jpath_value_match
132.0
assert_json_contains
70.4
CRAP 1428.5
↑ Summary Test/JSON/Assert.pm »
1 ## ----------------------------------------------------------------------------
2 # Copyright (C) 2014-2016 NZRS Ltd
3 ## ----------------------------------------------------------------------------
4 package JSON::Assert;
5  
6 7 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use MooX::Types::MooseLike::Base 'Str';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use JSON::Path;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Test::Deep::NoTest;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 $JSON::Path::Safe = 0;
12  
13 7 our $VERSION = '0.07';
14 7 ▶our $VERBOSE = $ENV{JSON_ASSERT_VERBOSE} || 1;
Condition50%1/2
$ENV{'JSON_ASSERT_VERBOSE'} || 1
l!l
07
MC/DC0%0/1
$ENV{'JSON_ASSERT_VERBOSE'} || 1
$ENV{'JSON_ASSERT_VERBOSE'}
Truth table50%1/2
A
A= $ENV{'JSON_ASSERT_VERBOSE'}
Aresult
11
00
15  
16 7 has 'error' =>
17 is => "rw",
18 isa => Str,
19 clearer => "_clear_error",
20 ;
21  
22 sub _self {
23 0 ▶ my $args = shift;
Subroutine _self: not called CC=3 SCAR=24.8
24 0 ▶ if ( ref $args->[0] eq __PACKAGE__ ) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $args->[0] eq __PACKAGE__) { } 0 0 0
elsif ($args->[0] eq __PACKAGE__) { } 0 0 0
25 0 return shift @$args;
26 }
27 elsif ( $args->[0] eq __PACKAGE__ ) {
28 0 return do { shift @$args }->new();
29 }
30 0 return __PACKAGE__->new();
31 }
32  
33 # assert_jpath_count
34 sub assert_jpath_count {
35 0 ▶ my $self = _self(\@_);
Subroutine assert_jpath_count: not called CC=5 SCAR=34.0
Pod documented
36 0 my ($doc, $jpath_str, $count) = @_;
37  
38 0 my $jpath = _parse_jpath($jpath_str);
39  
40 0 my @values = $jpath->values($doc);
41  
42 0 my $found = 0;
43 0 ▶ if (scalar @values != 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @values != 1) { } 0 0 0
elsif (ref $values[0] eq 'ARRAY') { } 0 0 0
44 0 $found = scalar @values;
45 }
46 elsif (ref $values[0] eq 'ARRAY') {
47 0 $found = scalar @{$values[0]};
48 }
49 else {
50 0 $found = 1;
51 }
52  
53 0 ▶ print "assert_jpath_count: Found $found\n" if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $JSON::Assert::VERBOSE 0 0 0
54  
55 0 ▶ unless ( $found == $count ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found == $count) 0 0 0
56 0 die "JPath '$jpath' has $found " . $self->_plural($found, 'value') . ", not $count as expected";
57 }
58  
59 0 return 1;
60 }
61  
62 sub is_jpath_count {
63 0 ▶ my $self = _self(\@_);
Subroutine is_jpath_count: not called CC=2 SCAR=17.9
Pod documented
64 0 my ($doc, $jpath, $count) = @_;
65  
66 0 $self->_clear_error();
67 0 eval { $self->assert_jpath_count($doc, $jpath, $count) };
68 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
69 0 $self->error($@);
70 0 return;
71 }
72 0 return 1;
73 }
74  
75 # assert_jpath_value_match
76 sub assert_jpath_value_match {
77 0 ▶ my $self = _self(\@_);
Subroutine assert_jpath_value_match: not called CC=11 SCAR=48.8
Pod documented
78 0 my ($doc, $jpath_str, $match) = @_;
79  
80 0 my $jpath = _parse_jpath($jpath_str);
81
82 # firstly, check that the node actually exists
83 0 my @values = $jpath->values($doc);
84  
85 0 my $found = 0;
86 0 ▶ if (scalar @values != 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @values != 1) { } 0 0 0
elsif (ref $values[0] eq 'ARRAY') { } 0 0 0
87 0 $found = scalar @values;
88 }
89 elsif (ref $values[0] eq 'ARRAY') {
90 0 $found = scalar @{$values[0]};
91 }
92 else {
93 0 $found = 1;
94 }
95  
96 0 ▶ print "assert_jpath_value_match: Found $found\n" if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $JSON::Assert::VERBOSE 0 0 0
97 0 ▶ unless ( $found == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found == 1) 0 0 0
98 0 die "JPath '$jpath' matched $found values when we expected to match one";
99 }
100  
101 # check the value is what we expect
102 0 my $value = $values[0];
103 0 ▶ print "assert_jpath_value_match: This value's value : " . $value . "\n" if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $JSON::Assert::VERBOSE 0 0 0
104 0 ▶ return 1 if (ref($value) eq ref($match) && ref($value) eq 'HASH' && scalar(keys(%$value)) == 0 && scalar(keys(%$match)) == 0);
Branch: 1 branch
BranchTrueFalseTotal
if ref $value eq ref $match and ref $value eq "HASH" and scalar keys %$value == 0 and scalar keys %$match == 0 0 0 0
Condition0%0/9
ref $value eq ref $match and ref $value eq "HASH" and scalar keys %$value == 0 and scalar keys %$match == 0
!ll&&!rl&&r
000
ref $value eq ref $match and ref $value eq "HASH" and scalar keys %$value == 0
!ll&&!rl&&r
000
ref $value eq ref $match and ref $value eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/4
ref $value eq ref $match and ref $value eq "HASH" and scalar keys %$value == 0 and scalar keys %$match == 0
ref $value eq ref $match ref $value eq "HASH" scalar keys %$value == 0 scalar keys %$match == 0
Truth table0%0/5
A and B and C and D
A= ref $value eq ref $matchB= ref $value eq "HASH"C= scalar keys %$value == 0D= scalar keys %$match == 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
105 0 ▶ unless ( $value =~ $match ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$match/) 0 0 0
106 0 die "JPath '$jpath' doesn't match '$match' as expected, instead it is '" . $value . "'";
107 }
108  
109 0 return 1;
110 }
111  
112 sub does_jpath_value_match {
113 0 ▶ my $self = _self(\@_);
Subroutine does_jpath_value_match: not called CC=2 SCAR=17.9
Pod documented
114 0 my ($doc, $jpath_str, $match) = @_;
115  
116 0 $self->_clear_error();
117 0 eval { $self->assert_jpath_value_match($doc, $jpath_str, $match) };
118 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
119 0 $self->error($@);
120 0 return;
121 }
122 0 return 1;
123 }
124  
125 # assert_jpath_values_match
126 sub assert_jpath_values_match {
127 0 ▶ my $self = _self(\@_);
Subroutine assert_jpath_values_match: not called CC=12 SCAR=50.5
Pod documented
128 0 my ($doc, $jpath_str, $match) = @_;
129  
130 0 my $jpath = _parse_jpath($jpath_str);
131  
132 # firstly, check that the node actually exists
133 0 my @values = $jpath->values($doc);
134  
135 0 my $values;
136 0 ▶ if (scalar @values != 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (scalar @values != 1) { } 0 0 0
elsif (ref $values[0] eq 'ARRAY') { } 0 0 0
137 0 $values = \@values;
138 }
139 elsif (ref $values[0] eq 'ARRAY') {
140 0 $values = $values[0];
141 }
142 else {
143 0 $values = \@values;
144 }
145
146 0 ▶ print 'assert_jpath_values_match: Found ' . (scalar @$values) . "\n" if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $JSON::Assert::VERBOSE 0 0 0
147 0 ▶ unless ( @$values ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@$values) 0 0 0
148 0 die "JPath '$jpath' matched no nodes when we expected to match at least one";
149 }
150  
151 # check the values are what we expect
152 0 my $i = 0;
153 0 foreach my $value ( @$values ) {
154 0 ▶ print "assert_jpath_value_match: This keys's value : " . $value . "\n" if $VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $JSON::Assert::VERBOSE 0 0 0
155 0 ▶ if (ref($value) eq ref($match) && ref($value) eq 'HASH' && scalar(keys(%$value)) == 0 && scalar(keys(%$match)) == 0){
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq ref $match and ref $value eq "HASH" and scalar keys %$value == 0 and scalar keys %$match == 0) 0 0 0
Condition0%0/9
ref $value eq ref $match and ref $value eq "HASH" and scalar keys %$value == 0 and scalar keys %$match == 0
!ll&&!rl&&r
000
ref $value eq ref $match and ref $value eq "HASH" and scalar keys %$value == 0
!ll&&!rl&&r
000
ref $value eq ref $match and ref $value eq "HASH"
!ll&&!rl&&r
000
MC/DC0%0/4
ref $value eq ref $match and ref $value eq "HASH" and scalar keys %$value == 0 and scalar keys %$match == 0
ref $value eq ref $match ref $value eq "HASH" scalar keys %$value == 0 scalar keys %$match == 0
Truth table0%0/5
A and B and C and D
A= ref $value eq ref $matchB= ref $value eq "HASH"C= scalar keys %$value == 0D= scalar keys %$match == 0
ABCDresult
0XXX0
10XX0
110X0
11100
11111
156 0 $i++;
157 0 next;
158 }
159 0 ▶ unless ( $value =~ $match ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($value =~ /$match/) 0 0 0
160 0 die "Item $i of JPath '$jpath' doesn't match '$match' as expected, instead it is '" . $value . "'";
161 }
162 0 $i++;
163 }
164  
165 0 return 1;
166 }
167  
168 sub do_jpath_values_match {
169 0 ▶ my $self = _self(\@_);
Subroutine do_jpath_values_match: not called CC=2 SCAR=17.9
Pod documented
170 0 my ($doc, $jpath_str, $match) = @_;
171  
172 0 $self->_clear_error();
173 0 eval { $self->assert_jpath_values_match($doc, $jpath_str, $match) };
174 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
175 0 $self->error($@);
176 0 return;
177 }
178 0 return 1;
179 }
180  
181 sub assert_json_contains {
182 0 ▶ my $self = _self(\@_);
Subroutine assert_json_contains: not called CC=9 SCAR=42.5
Pod documented
183 0 my ($doc, $jpath_str, $match) = @_;
184  
185 0 my $jpath = _parse_jpath($jpath_str);
186 0 my @values = $jpath->values($doc);
187  
188 0 ▶ if (ref $match eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $match eq 'HASH') { } 0 0 0
elsif (ref $match eq 'ARRAY') { } 0 0 0
189 0 ▶ if (! eq_deeply($values[0], superhashof($match))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (eq_deeply($values[0], superhashof($match))) 0 0 0
190 7 ▶ use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
191 0 ▶ if ($VERBOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($JSON::Assert::VERBOSE) 0 0 0
192 0 print "wanted: " . Dumper($match) . ", got: " . Dumper ($values[0]) . "\n";
193 }
194  
195 }
196 }
197 elsif (ref $match eq 'ARRAY') {
198 0 ▶ if (ref $match->[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $match->[0] eq "HASH") 0 0 0
199 0 my @new_wanted = map { superhashof($_) } @$match;
200  
201 0 ▶ die "JPath '$jpath_str' doesn't match wanted data structure"
Branch: 1 branch
BranchTrueFalseTotal
unless eq_deeply(@values, \@new_wanted) 0 0 0
202 unless eq_deeply(@values, \@new_wanted);
203 }
204  
205 0 ▶ die "JPath '$jpath_str' doesn't match wanted data structure"
Branch: 1 branch
BranchTrueFalseTotal
unless eq_deeply($values[0], $match) 0 0 0
206 unless eq_deeply($values[0], $match);
207 }
208 else {
209 0 ▶ die "JPath '$jpath_str' doesn't match wanted data structure"
Branch: 1 branch
BranchTrueFalseTotal
unless $values[0] eq $match 0 0 0
210 unless $values[0] eq $match;
211 }
212  
213 0 return 1;
214 }
215  
216 sub does_jpath_contains {
217 0 ▶ my $self = _self(\@_);
Subroutine does_jpath_contains: not called CC=2 SCAR=17.9
Pod documented
218 0 my ($doc, $jpath_str, $match) = @_;
219  
220 0 $self->_clear_error();
221 0 eval { $self->assert_json_contains($doc, $jpath_str, $match) };
222 0 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
223 0 $self->error($@);
224 0 return;
225 }
226 0 return 1;
227 }
228  
229  
230 # private functions
231 sub _plural {
232 0 ▶ my ($class, $number, $single, $plural) = @_;
Subroutine _plural: not called CC=3 SCAR=24.8
233  
234 0 ▶ return $number == 1 ? $single : defined $plural ? $plural : "${single}s";
Branch: 2 branches
BranchTrueFalseTotal
$number == 1 ? : 0 0 0
defined $plural ? : 0 0 0
235 }
236  
237 sub _parse_jpath {
238 0 ▶ my ($jpath_str) = @_;
Subroutine _parse_jpath: not called CC=2 SCAR=17.9
239  
240 0 my $jpath = JSON::Path->new($jpath_str);
241 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
242 0 die "Error evaluating json path ($jpath_str): $@";
243 }
244  
245 0 return $jpath;
246 }
247
248  
249 7 1;
250 __END__
↑ Summary Test/JSON/Assert.pm »