Corvus/ResultsCollector.pm

filter: Statementstmt 87.7% 115 / 131 Branchbran 66.6% 24 / 36 Conditioncond 63.1% 24 / 38 MC/DCmcdc 33.3% 8 / 24 Subroutinesub 88.4% 23 / 26 Podpod 0.0% 0 / 16 totaltotal 71.5% 194 / 271 CC 35 SCAR 15.1
CC
35
Cov
80%
evaluated_boolean_schema
12.0
enumerate_annotations
8.8
_row
7.1
CRAP 4.5
« Corvus.pm ↑ Summary Corvus/Uts46.pm »
1 package JSON::Schema::Corvus::ResultsCollector;
2  
3 # Results collection: a port of the Python package's results.py (Corvus.Text.Json's JsonSchemaResultsCollector and
4 # JsonSchemaAnnotationProducer).
5 #
6 # The evaluator opens a context for each application of a subschema and writes keyword rows into the open context.
7 # Closing a context either commits it (a summary row, then its own rows newest first, after its committed descendants)
8 # or pops it (everything it and its descendants wrote is discarded). The level decides which rows exist and which
9 # carry message text: basic has failures without text, detailed adds text to failures, verbose keeps every row with
10 # text, annotations included.
11  
12 4 ▶use 5.026;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 4 ▶use JSON::Schema::Corvus::Error;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 4 ▶use JSON::Schema::Corvus::Json ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 4 ▶use constant { BASIC => 0, DETAILED => 1, VERBOSE => 2 };
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 4 my %LEVELS = (basic => BASIC, detailed => DETAILED, verbose => VERBOSE);
21  
22 # A collector at a level: basic, detailed (the default) or verbose.
23 6 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod undocumented
24 6 my ($class, $level) = @_;
25 6 ▶ $level //= 'detailed';
Condition100%2/2
$level //= "detailed"
l!l
51
MC/DC100%1/1
$level //= "detailed"
$level
Truth table100%2/2
A
A= $level
Aresult
11
00
26 6 ▶ my $code = $LEVELS{ lc $level } // JSON::Schema::Corvus::Error->throw_usage(
Condition66%2/3
$LEVELS{lc $level} // "JSON::Schema::Corvus::Error"->throw_usage("the level is not basic, detailed or verbose")
l!l&&r!l&&!r
510
MC/DC0%0/2
$LEVELS{lc $level} // "JSON::Schema::Corvus::Error"->throw_usage("the level is not basic, detailed or verbose")
$LEVELS{lc $level} "JSON::Schema::Corvus::Error"->throw_usage("the level is not basic, detailed or verbose")
Truth table33%1/3
A // B
A= $LEVELS{lc $level}B= "JSON::Schema::Corvus::Error"->throw_usage("the level is not basic, detailed or verbose")
ABresult
1X1
011
000
27 'the level is not basic, detailed or verbose');
28 5 return bless { level => $code, committed => [], frames => [], eval_path => '', schema_path => '', doc_path => '' },
29 $class;
30 }
31  
32 0 ▶sub level { (qw(basic detailed verbose))[ $_[0]{level} ] }
Subroutine level: not called CC=1 SCAR=6.9
Pod undocumented
33  
34 # The rows of the last evaluation, in commit order: hashes with is_match, message, evaluation_location,
35 # schema_evaluation_location and document_evaluation_location.
36 3 ▶sub results { [ @{ $_[0]{committed} } ] }
Subroutine results: called CC=1 SCAR=0.0
Pod undocumented
37  
38 3 ▶sub count { scalar @{ $_[0]{committed} } }
Subroutine count: called CC=1 SCAR=0.0
Pod undocumented
39  
40 6 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
41 6 my ($self) = @_;
42 6 @{ $self->{committed} } = ();
43 6 @{ $self->{frames} } = ();
44 6 $self->{eval_path} = $self->{schema_path} = $self->{doc_path} = '';
45 }
46  
47 # --- The evaluator's side ---
48  
49 5 ▶sub _reset { $_[0]->clear }
Subroutine _reset: called CC=1 SCAR=0.0
50  
51 # Opens a child context: the evaluation path is extended by $eval_segment (verbatim), the schema path replaced by
52 # $schema_location, and the document path extended by $doc_segment (already pointer-encoded), each when given.
53 12 ▶sub begin_child_context {
Subroutine begin_child_context: called CC=4 SCAR=13.9
Pod undocumented
54 12 my ($self, $eval_segment, $schema_location, $doc_segment) = @_;
55 12 push @{ $self->{frames} }, [ $self->{eval_path}, $self->{schema_path}, $self->{doc_path},
56 12 scalar @{ $self->{committed} }, [] ];
57 12 ▶ $self->{eval_path} .= "/$eval_segment" if defined $eval_segment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $eval_segment 7 5 12
58 12 ▶ $self->{schema_path} = $schema_location if defined $schema_location;
Branch: 1 branch
BranchTrueFalseTotal
if defined $schema_location 12 0 12
59 12 ▶ $self->{doc_path} .= "/$doc_segment" if defined $doc_segment;
Branch: 1 branch
BranchTrueFalseTotal
if defined $doc_segment 7 5 12
60 }
61  
62 # Closes a child context. When the parent does not need the child's results ($parent_is_match) they are discarded below
63 # verbose; otherwise the context's summary row is written and its rows are committed.
64 12 ▶sub commit_child_context {
Subroutine commit_child_context: called CC=3 SCAR=11.4
Pod undocumented
65 12 my ($self, $parent_is_match, $child_is_match, $message) = @_;
66 12 ▶ if ($parent_is_match && $self->{level} != VERBOSE) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parent_is_match and $self->{"level"} != 2) 0 12 12
Condition66%2/3
$parent_is_match and $self->{"level"} != 2
!ll&&!rl&&r
750
MC/DC0%0/2
$parent_is_match and $self->{"level"} != 2
$parent_is_match $self->{"level"} != 2
Truth table66%2/3
A and B
A= $parent_is_matchB= $self->{"level"} != 2
ABresult
0X0
100
111
67 0 $self->pop_child_context;
68 0 return;
69 }
70 12 my $frame = pop @{ $self->{frames} };
71 12 push @{ $frame->[4] }, $self->_row($child_is_match, $message, $self->{eval_path}, $self->{schema_path},
72 12 $self->{doc_path});
73 12 push @{ $self->{committed} }, reverse @{ $frame->[4] };
74 12 @$self{qw(eval_path schema_path doc_path)} = @$frame[0 .. 2];
75 }
76  
77 # Closes a child context and discards everything it and its descendants wrote.
78 0 ▶sub pop_child_context {
Subroutine pop_child_context: not called CC=1 SCAR=6.9
Pod undocumented
79 0 my ($self) = @_;
80 0 my $frame = pop @{ $self->{frames} };
81 0 splice @{ $self->{committed} }, $frame->[3];
82 0 @$self{qw(eval_path schema_path doc_path)} = @$frame[0 .. 2];
83 }
84  
85  
86 10 ▶sub evaluated_keyword {
Subroutine evaluated_keyword: called CC=3 SCAR=11.0
Pod undocumented
87 10 my ($self, $is_match, $message, $keyword) = @_;
88 10 ▶ return if $is_match && $self->{level} != VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $is_match and $self->{"level"} != 2 2 8 10
Condition100%3/3
$is_match and $self->{"level"} != 2
!ll&&!rl&&r
262
MC/DC100%2/2
$is_match and $self->{"level"} != 2
$is_match $self->{"level"} != 2
Truth table100%3/3
A and B
A= $is_matchB= $self->{"level"} != 2
ABresult
0X0
100
111
89 8 my $k = '/' . _segment($keyword);
90 $self->_write($self->_row($is_match, $message, $self->{eval_path} . $k, $self->{schema_path} . $k,
91 8 $self->{doc_path}));
92 }
93  
94 3 ▶sub evaluated_keyword_for_property {
Subroutine evaluated_keyword_for_property: called CC=3 SCAR=11.3
Pod undocumented
95 3 my ($self, $is_match, $message, $property_name, $keyword) = @_;
96 3 ▶ return if $is_match && $self->{level} != VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $is_match and $self->{"level"} != 2 0 3 3
Condition66%2/3
$is_match and $self->{"level"} != 2
!ll&&!rl&&r
210
MC/DC0%0/2
$is_match and $self->{"level"} != 2
$is_match $self->{"level"} != 2
Truth table66%2/3
A and B
A= $is_matchB= $self->{"level"} != 2
ABresult
0X0
100
111
97 3 my $k = '/' . _segment($keyword);
98 $self->_write($self->_row($is_match, $message, $self->{eval_path} . $k, $self->{schema_path} . $k,
99 3 $self->{doc_path} . '/' . _segment($property_name)));
100 }
101  
102 # An annotation: verbose only; the keyword extends the evaluation path but not the schema path.
103 1 ▶sub ignored_keyword {
Subroutine ignored_keyword: called CC=2 SCAR=7.1
Pod undocumented
104 1 my ($self, $message, $keyword) = @_;
105 1 ▶ return if $self->{level} != VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"level"} != 2 0 1 1
106 $self->_write($self->_row(1, $message, $self->{eval_path} . '/' . _segment($keyword), $self->{schema_path},
107 1 $self->{doc_path}));
108 }
109  
110 0 ▶sub evaluated_boolean_schema {
Subroutine evaluated_boolean_schema: not called CC=3 SCAR=24.8
Pod undocumented
111 0 my ($self, $is_match, $message) = @_;
112 0 ▶ return if $is_match && $self->{level} != VERBOSE;
Branch: 1 branch
BranchTrueFalseTotal
if $is_match and $self->{"level"} != 2 0 0 0
Condition0%0/3
$is_match and $self->{"level"} != 2
!ll&&!rl&&r
000
MC/DC0%0/2
$is_match and $self->{"level"} != 2
$is_match $self->{"level"} != 2
Truth table0%0/3
A and B
A= $is_matchB= $self->{"level"} != 2
ABresult
0X0
100
111
113 0 $self->_write($self->_row($is_match, $message, $self->{eval_path}, $self->{schema_path}, $self->{doc_path}));
114 }
115  
116 15 ▶sub _segment {
Subroutine _segment: called CC=3 SCAR=16.0
117 15 my ($s) = @_;
118 15 ▶ return $s if index($s, '~') < 0 && index($s, '/') < 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($s, "~") == -1 and index($s, "/") == -1 15 0 15
Condition33%1/3
index($s, "~") == -1 and index($s, "/") == -1
!ll&&!rl&&r
0015
MC/DC0%0/2
index($s, "~") == -1 and index($s, "/") == -1
index($s, "~") == -1 index($s, "/") == -1
Truth table33%1/3
A and B
A= index($s, "~") == -1B= index($s, "/") == -1
ABresult
0X0
100
111
119 0 $s =~ s/~/~0/g;
120 0 $s =~ s/\//~1/g;
121 0 return $s;
122 }
123  
124 24 ▶sub _row {
Subroutine _row: called CC=7 SCAR=19.5
125 24 my ($self, $is_match, $message, $evaluation, $schema, $document) = @_;
126 24 ▶ my $with_text = $self->{level} == VERBOSE || (!$is_match && $self->{level} >= DETAILED);
Condition83%5/6
$self->{'level'} == 2 || !$is_match && $self->{'level'} >= 1
l!l&&r!l&&!r
1644
not $is_match and $self->{"level"} >= 1
!ll&&!rl&&r
044
MC/DC66%2/3
$self->{'level'} == 2 || !$is_match && $self->{'level'} >= 1
$self->{'level'} == 2 not $is_match $self->{"level"} >= 1
Truth table75%3/4
A || not(B) and C
A= $self->{'level'} == 2B= not $is_matchC= $self->{"level"} >= 1
ABCresult
1XX1
0111
00X0
0100
127 24 ▶ my $text = $with_text && defined $message ? (ref $message eq 'CODE' ? $message->() : $message) : '';
Branch: 2 branches
BranchTrueFalseTotal
$with_text && defined $message ? : 20 4 24
ref $message eq 'CODE' ? : 3 17 20
Condition66%2/3
$with_text and defined $message
!ll&&!rl&&r
4020
MC/DC50%1/2
$with_text and defined $message
$with_text defined $message
Truth table66%2/3
A and B
A= $with_textB= defined $message
ABresult
0X0
100
111
128 24 ▶ return { is_match => $is_match ? 1 : '', message => $text, evaluation_location => $evaluation,
Branch: 1 branch
BranchTrueFalseTotal
$is_match ? : 16 8 24
129 schema_evaluation_location => $schema, document_evaluation_location => $document };
130 }
131  
132 12 ▶sub _write { push @{ $_[0]{frames}[-1][4] }, $_[1] }
Subroutine _write: called CC=1 SCAR=0.0
133  
134 # --- Annotations ---
135  
136 4 my %JSON_VALUE_START = map { $_ => 1 } split //, '"{[tfn-0123456789';
137  
138 # The annotations in a verbose collector's results (JsonSchemaAnnotationProducer.EnumerateAnnotations):
139 # [instance location, keyword, schema location, JSON text].
140 2 ▶sub enumerate_annotations {
Subroutine enumerate_annotations: called CC=8 SCAR=21.7
Pod undocumented
141 2 my ($self) = @_;
142 2 my @out;
143 2 for my $r (@{ $self->{committed} }) {
144 6 ▶ next if !$r->{is_match} || $r->{message} eq '';
Branch: 1 branch
BranchTrueFalseTotal
if not $r->{"is_match"} or $r->{"message"} eq "" 0 6 6
Condition33%1/3
not $r->{"is_match"} or $r->{"message"} eq ""
l!l&&r!l&&!r
006
MC/DC0%0/2
not $r->{"is_match"} or $r->{"message"} eq ""
not $r->{"is_match"} $r->{"message"} eq ""
Truth table33%1/3
not(A) or B
A= not $r->{"is_match"}B= $r->{"message"} eq ""
ABresult
1X1
011
000
145 6 my $slash = rindex($r->{evaluation_location}, '/');
146 6 ▶ next if $slash < 0 || $r->{evaluation_location} eq $r->{schema_evaluation_location};
Branch: 1 branch
BranchTrueFalseTotal
if $slash < 0 or $r->{"evaluation_location"} eq $r->{"schema_evaluation_location"} 4 2 6
Condition100%3/3
$slash < 0 or $r->{"evaluation_location"} eq $r->{"schema_evaluation_location"}
l!l&&r!l&&!r
222
MC/DC100%2/2
$slash < 0 or $r->{"evaluation_location"} eq $r->{"schema_evaluation_location"}
$slash < 0 $r->{"evaluation_location"} eq $r->{"schema_evaluation_location"}
Truth table100%3/3
A or B
A= $slash < 0B= $r->{"evaluation_location"} eq $r->{"schema_evaluation_location"}
ABresult
1X1
011
000
147 2 my $keyword = substr($r->{evaluation_location}, $slash + 1);
148 2 ▶ next if $keyword eq '' || !$JSON_VALUE_START{ substr($r->{message}, 0, 1) };
Branch: 1 branch
BranchTrueFalseTotal
if $keyword eq "" or not $JSON_VALUE_START{substr $r->{"message"}, 0, 1} 0 2 2
Condition33%1/3
$keyword eq "" or not $JSON_VALUE_START{substr $r->{"message"}, 0, 1}
l!l&&r!l&&!r
002
MC/DC0%0/2
$keyword eq "" or not $JSON_VALUE_START{substr $r->{"message"}, 0, 1}
$keyword eq "" not $JSON_VALUE_START{substr $r->{"message"}, 0, 1}
Truth table33%1/3
A or not(B)
A= $keyword eq ""B= not $JSON_VALUE_START{substr $r->{"message"}, 0, 1}
ABresult
1X1
011
000
149 2 push @out, [ $r->{document_evaluation_location}, $keyword, $r->{schema_evaluation_location}, $r->{message} ];
150 }
151 2 return @out;
152 }
153  
154 # '#' and the schema location, percent-encoded as a URI fragment (upper-case hex, UTF-8).
155 2 ▶sub schema_location_fragment {
Subroutine schema_location_fragment: called CC=1 SCAR=0.0
Pod undocumented
156 2 my ($location) = @_;
157 2 my $bytes = $location;
158 2 utf8::encode($bytes);
159 0 $bytes =~ s/([^A-Za-z0-9\-._~!\$&'()*+,;=:\@\/?])/sprintf('%%%02X', ord $1)/ge;
160 2 return "#$bytes";
161 }
162  
163 # The annotations grouped by instance location, then keyword, then schema location fragment, as Perl data:
164 # { "/name" => { title => { "#/properties/name" => "Name" } } }.
165 1 ▶sub annotations {
Subroutine annotations: called CC=2 SCAR=6.9
Pod undocumented
166 1 my ($self) = @_;
167 1 require JSON::PP;
168 1 my $json = JSON::PP->new->allow_nonref;
169 1 my %out;
170 1 for my $a ($self->enumerate_annotations) {
171 1 $out{ $a->[0] }{ $a->[1] }{ schema_location_fragment($a->[2]) } = $json->decode($a->[3]);
172 }
173 1 return \%out;
174 }
175  
176 # The same as JSON text, with names in the order the results give them, as the C library writes it.
177 1 ▶sub annotations_json {
Subroutine annotations_json: called CC=5 SCAR=16.2
Pod undocumented
178 1 my ($self) = @_;
179 1 my (%out, @locations, %keywords, %schemas);
180 1 for my $a ($self->enumerate_annotations) {
181 1 my ($location, $keyword, $schema, $value) = @$a;
182 1 ▶ push @locations, $location unless exists $out{$location};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $out{$location} 1 0 1
183 1 ▶ push @{ $keywords{$location} }, $keyword unless exists $out{$location}{$keyword};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $out{$location}{$keyword} 1 0 1
184 1 my $fragment = schema_location_fragment($schema);
185 1 ▶ push @{ $schemas{$location}{$keyword} }, $fragment unless exists $out{$location}{$keyword}{$fragment};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $out{$location}{$keyword}{$fragment} 1 0 1
186 1 $out{$location}{$keyword}{$fragment} = $value;
187 }
188 1 my $q = \&JSON::Schema::Corvus::Json::_quote;
189 return '{' . join(',', map {
190 1 my $l = $_;
191 $q->($l) . ':{' . join(',', map {
192 1 my $k = $_;
193 1 $q->($k) . ':{' . join(',', map { $q->($_) . ':' . $out{$l}{$k}{$_} } @{ $schemas{$l}{$k} }) . '}'
194 1 } @{ $keywords{$l} }) . '}'
195 } @locations) . '}';
196 }
197  
198 4 1;
« Corvus.pm ↑ Summary Corvus/Uts46.pm »