Tester/Role/SentenceCollection.pm

filter: Statementstmt 91.2% 94 / 103 Branchbran 68.7% 22 / 32 Conditioncond 100% 6 / 6 MC/DCmcdc 100% 4 / 4 Subroutinesub 90.9% 20 / 22 Podpod 92.8% 13 / 14 totaltotal 87.8% 159 / 181 CC 26 SCAR 15.4
CC
26
Cov
87%
assert_n_sentences
20.0
_index_setup
6.1
sentence_named
5.0
CRAP 4.6
« Tester.pm ↑ Summary Tester/Role/Result.pm »
1 2 ▶use v5.20.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 package JMAP::Tester::Role::SentenceCollection 0.113;
3  
4 2 ▶use Moo::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use experimental 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 requires 'sentence_broker';
9  
10 2 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=6.9
11 2 for my $m (qw(
12 client_ids_for_items
13 sentence_for_item
14 paragraph_for_items
15  
16 strip_json_types
17 )) {
18 164 ▶ my $sub = sub ($self, @rest) {
Subroutine __ANON__: called CC=1 SCAR=0.0
19 164 $self->sentence_broker->$m(@rest);
20 8 };
21 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 8 *$m = $sub;
23 }
24 }
25  
26 2 requires 'items';
27 2 requires 'add_items';
28  
29 0 ▶after add_items => sub ($self, @) { $self->_index_setup };
Subroutine __ANON__: not called CC=1 SCAR=1.2
30  
31 29 ▶sub BUILD ($self, @) {
Subroutine BUILD: called CC=1 SCAR=0.0
Pod undocumented
32 29 $self->_index_setup;
33 }
34  
35 29 ▶sub _index_setup ($self) {
Subroutine _index_setup: called CC=6 SCAR=18.1
36 29 my @cids = $self->client_ids_for_items([ $self->items ]);
37  
38 29 my $prev_cid;
39 29 my $next_para_idx = 0;
40  
41 29 my %cid_indices;
42 my @para_indices;
43  
44 29 for my $i (0 .. $#cids) {
45 40 my $cid = $cids[$i];
46 40 ▶ unless (defined $cid) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $cid) 0 40 40
47 0 Carp::cluck("undefined client_id in position $i");
48 0 next;
49 }
50  
51 40 ▶ if (defined $prev_cid && $prev_cid ne $cid) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $prev_cid and $prev_cid ne $cid) 8 32 40
Condition100%3/3
defined $prev_cid and $prev_cid ne $cid
!ll&&!rl&&r
2938
MC/DC100%2/2
defined $prev_cid and $prev_cid ne $cid
defined $prev_cid $prev_cid ne $cid
Truth table100%3/3
A and B
A= defined $prev_cidB= $prev_cid ne $cid
ABresult
0X0
100
111
52 # We're transition from cid1 to cid2. -- rjbs, 2016-04-08
53 $self->abort("client_id <$cid> appears in non-contiguous positions")
54 8 ▶ if $cid_indices{$cid};
Branch: 1 branch
BranchTrueFalseTotal
if $cid_indices{$cid} 0 8 8
55  
56 8 $next_para_idx++;
57 }
58  
59 40 push @{ $cid_indices{$cid} }, $i;
60 40 push @{ $para_indices[ $next_para_idx ] }, $i;
61  
62 40 $prev_cid = $cid;
63 }
64  
65 29 $self->_cid_indices(\%cid_indices);
66 29 $self->_para_indices(\@para_indices);
67 }
68  
69 # The reason we don't have cid-to-para and para-to-lines is that in the event
70 # that one cid appears in non-contiguous positions, we want to allow it, even
71 # though it's garbage. -- rjbs, 2016-04-11
72 2 has cid_indices => (is => 'bare', accessor => '_cid_indices');
73 2 has para_indices => (is => 'bare', accessor => '_para_indices');
74  
75 #pod =method sentence
76 #pod
77 #pod my $sentence = $response->sentence($n);
78 #pod
79 #pod This method returns the I<n>th L<Sentence|JMAP::Tester::Response::Sentence> of
80 #pod the response.
81 #pod
82 #pod =cut
83  
84 25 ▶sub sentence ($self, $n) {
Subroutine sentence: called CC=2 SCAR=6.9
Pod documented
85 25 my @items = $self->items;
86 25 ▶ $self->abort("there is no sentence for index $n")
Branch: 1 branch
BranchTrueFalseTotal
unless my $item = $items[$n] 1 24 25
87 unless my $item = $items[$n];
88  
89 24 return $self->sentence_for_item($item);
90 }
91  
92 #pod =method sentences
93 #pod
94 #pod my @sentences = $response->sentences;
95 #pod
96 #pod This method returns a list of all sentences in the response.
97 #pod
98 #pod =cut
99  
100 19 ▶sub sentences ($self) {
Subroutine sentences: called CC=1 SCAR=0.0
Pod documented
101 19 my @sentences = map {; $self->sentence_for_item($_) }
102 $self->items;
103  
104 19 return @sentences;
105 }
106  
107 #pod =method single_sentence
108 #pod
109 #pod my $sentence = $response->single_sentence;
110 #pod my $sentence = $response->single_sentence($name);
111 #pod
112 #pod This method returns the only L<Sentence|JMAP::Tester::Response::Sentence> of
113 #pod the response, raising an exception if there's more than one Sentence. If
114 #pod C<$name> is given, an exception is raised if the Sentence's name doesn't match
115 #pod the given name.
116 #pod
117 #pod =cut
118  
119 13 ▶sub single_sentence {
Subroutine single_sentence: called CC=4 SCAR=13.9
Pod documented
120 13 my ($self, $name) = @_;
121  
122 13 my @items = $self->items;
123 13 ▶ unless (@items == 1) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@items == 1) 1 12 13
124 1 $self->abort(
125 sprintf("single_sentence called but there are %i sentences", 0+@items)
126 );
127 }
128  
129 12 my $sentence = $self->sentence_for_item($items[0]);
130  
131 12 my $have = $sentence->name;
132 12 ▶ if (defined $name && $have ne $name) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $name and $have ne $name) 2 10 12
Condition100%3/3
defined $name and $have ne $name
!ll&&!rl&&r
282
MC/DC100%2/2
defined $name and $have ne $name
defined $name $have ne $name
Truth table100%3/3
A and B
A= defined $nameB= $have ne $name
ABresult
0X0
100
111
133 2 $self->abort(qq{single sentence has name "$have" not "$name"});
134 }
135  
136 10 return $sentence;
137 }
138  
139 #pod =method sentence_named
140 #pod
141 #pod my $sentence = $response->sentence_named($name);
142 #pod
143 #pod This method returns the sentence with the given name. If no such sentence
144 #pod exists, or if two sentences with the name exist, the tester will abort.
145 #pod
146 #pod =cut
147  
148 5 ▶sub sentence_named ($self, $name = undef) {
Subroutine sentence_named: called CC=5 SCAR=16.1
Pod documented
149 5 ▶ Carp::confess("no name given") unless defined $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name 0 5 5
150  
151 5 my @sentences = grep {; $_->name eq $name } $self->sentences;
152  
153 5 ▶ unless (@sentences) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@sentences) 1 4 5
154 1 $self->abort(qq{no sentence found with name "$name"});
155 }
156  
157 4 ▶ if (@sentences > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@sentences > 1) 1 3 4
158 1 $self->abort(qq{found more than one sentence with name "$name"});
159 }
160  
161 3 return $sentences[0];
162 }
163  
164 #pod =method assert_n_sentences
165 #pod
166 #pod my ($s1, $s2, ...) = $response->assert_n_sentences($n);
167 #pod
168 #pod This method returns all the sentences in the response, as long as there are
169 #pod exactly C<$n>. Otherwise, it aborts.
170 #pod
171 #pod =cut
172  
173 0 ▶sub assert_n_sentences ($self, $n = undef) {
Subroutine assert_n_sentences: not called CC=4 SCAR=30.0
Pod documented
174 0 ▶ Carp::confess("no sentence count given") unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 0 0
175  
176 0 my @sentences = $self->sentences;
177  
178 0 ▶ unless (@sentences == $n) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@sentences == $n) 0 0 0
179 0 $self->abort("expected $n sentences but got " . @sentences)
180 }
181  
182 0 return @sentences;
183 }
184  
185 #pod =method paragraph
186 #pod
187 #pod my $para = $response->paragraph($n);
188 #pod
189 #pod This method returns the I<n>th L<Paragraph|JMAP::Tester::Response::Paragraph>
190 #pod of the response.
191 #pod
192 #pod =cut
193  
194 13 ▶sub paragraph ($self, $n) {
Subroutine paragraph: called CC=2 SCAR=6.9
Pod documented
195 13 ▶ $self->abort("there is no paragraph for index $n")
Branch: 1 branch
BranchTrueFalseTotal
unless my $indices = $self->_para_indices->[$n] 1 12 13
196 unless my $indices = $self->_para_indices->[$n];
197  
198 12 my @items = $self->items;
199 12 my @selected = @items[ @$indices ];
200  
201 12 $self->paragraph_for_items(\@selected);
202 }
203  
204 #pod =method paragraphs
205 #pod
206 #pod my @paragraphs = $response->paragraphs;
207 #pod
208 #pod This method returns a list of all paragraphs in the response.
209 #pod
210 #pod =cut
211  
212 2 ▶sub paragraphs ($self) {
Subroutine paragraphs: called CC=2 SCAR=6.9
Pod documented
213 2 my @para_indices = @{ $self->_para_indices };
214 2 my @items = $self->items;
215  
216 2 my @paragraphs;
217 2 for my $i_set (@para_indices) {
218 6 push @paragraphs, $self->paragraph_for_items(
219 [ @items[ @$i_set ] ]
220 );
221 }
222  
223 2 return @paragraphs;
224 }
225  
226 #pod =method assert_n_paragraphs
227 #pod
228 #pod my ($p1, $p2, ...) = $response->assert_n_paragraphs($n);
229 #pod
230 #pod This method returns all the paragraphs in the response, as long as there are
231 #pod exactly C<$n>. Otherwise, it aborts.
232 #pod
233 #pod =cut
234  
235 2 ▶sub assert_n_paragraphs ($self, $n = undef) {
Subroutine assert_n_paragraphs: called CC=4 SCAR=13.9
Pod documented
236 2 ▶ Carp::confess("no paragraph count given") unless defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 2 2
237  
238 2 my @para_indices = @{ $self->_para_indices };
239 2 ▶ unless ($n == @para_indices) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($n == @para_indices) 1 1 2
240 1 $self->abort("expected $n paragraphs but got " . @para_indices)
241 }
242  
243 1 return $self->paragraphs;
244 }
245  
246 #pod =method paragraph_by_client_id
247 #pod
248 #pod my $para = $response->paragraph_by_client_id($cid);
249 #pod
250 #pod This returns the paragraph for the given client id. If there is no paragraph
251 #pod for that client id, an empty list is returned.
252 #pod
253 #pod =cut
254  
255 3 ▶sub paragraph_by_client_id ($self, $cid = undef) {
Subroutine paragraph_by_client_id: called CC=4 SCAR=14.2
Pod documented
256 3 ▶ Carp::confess("no client id given") unless defined $cid;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cid 0 3 3
257  
258 $self->abort("there is no paragraph for client_id $cid")
259 3 ▶ unless my $indices = $self->_cid_indices->{$cid};
Branch: 1 branch
BranchTrueFalseTotal
unless my $indices = $self->_cid_indices->{$cid} 0 3 3
260  
261 3 my @items = $self->items;
262 3 my @selected = @items[ @$indices ];
263  
264 3 return $self->paragraph_for_items(\@selected);
265 }
266  
267 #pod =method as_triples
268 #pod
269 #pod =method as_stripped_triples
270 #pod
271 #pod This method returns an arrayref of arrayrefs, holding the data returned by the
272 #pod JMAP server. With C<as_triples>, some of the JSON data may be in objects
273 #pod provided by L<JSON::Typist>. If you'd prefer raw data, use the
274 #pod C<as_stripped_triples> form.
275 #pod
276 #pod =cut
277  
278 2 ▶sub as_triples ($self) {
Subroutine as_triples: called CC=1 SCAR=0.0
Pod documented
279 return [
280 2 map {; $self->sentence_for_item($_)->as_triple }
281 $self->items
282 ];
283 }
284  
285 1 ▶sub as_stripped_triples ($self) {
Subroutine as_stripped_triples: called CC=1 SCAR=0.0
Pod documented
286 1 return $self->strip_json_types($self->as_triples);
287 }
288  
289 #pod =method as_pairs
290 #pod
291 #pod =method as_stripped_pairs
292 #pod
293 #pod These methods do the same thing as C<as_triples> and <as_stripped_triples>,
294 #pod but omit client ids.
295 #pod
296 #pod =cut
297  
298 6 ▶sub as_pairs ($self) {
Subroutine as_pairs: called CC=1 SCAR=0.0
Pod documented
299 return [
300 6 map {; $self->sentence_for_item($_)->as_pair }
301 $self->items
302 ];
303 }
304  
305 1 ▶sub as_stripped_pairs ($self) {
Subroutine as_stripped_pairs: called CC=1 SCAR=0.0
Pod documented
306 1 return $self->strip_json_types($self->as_pairs);
307 }
308  
309 2 1;
310  
311 __END__
« Tester.pm ↑ Summary Tester/Role/Result.pm »