File Coverage

blib/lib/JMAP/Tester/Response/Paragraph.pm
Criterion Covered Total %
statement 83 83 100.0
branch 20 20 100.0
condition 3 3 100.0
subroutine 16 16 100.0
pod 8 11 72.7
total 130 133 97.7


line stmt bran cond sub pod time code
1 2     2   19 use v5.20.0;
  2         6  
2             package JMAP::Tester::Response::Paragraph 0.110;
3             # ABSTRACT: a group of sentences in a JMAP response
4              
5 2     2   8 use Moo;
  2         2  
  2         21  
6 2     2   582 use experimental 'signatures';
  2         3  
  2         9  
7              
8 2     2   215 use JMAP::Tester::Abort 'abort';
  2         4  
  2         22  
9              
10 2     2   231 use namespace::clean;
  2         3  
  2         9  
11              
12             #pod =head1 OVERVIEW
13             #pod
14             #pod These objects represent paragraphs in the JMAP response. That is, if your
15             #pod response is:
16             #pod
17             #pod [
18             #pod [ "messages", { ... }, "a" ], # 1
19             #pod [ "smellUpdates", { ... }, "b" ], # 2
20             #pod [ "smells", { ... }, "b" ], # 3
21             #pod ]
22             #pod
23             #pod ...then #1 forms one paragraph and #2 and #3 together form another. It goes by
24             #pod matching client ids.
25             #pod
26             #pod =cut
27              
28 1     1 0 14 sub client_id ($self) {
  1         2  
  1         1  
29 1         7 $self->_sentences->[0]->client_id;
30             }
31              
32             sub BUILD {
33             abort("tried to build 0-sentence paragraph")
34 23 100   23 0 5967 unless @{ $_[0]->_sentences };
  23         69  
35              
36 22         50 my $client_id = $_[0]->_sentences->[0]->client_id;
37             abort("tried to build paragraph with non-uniform client_ids")
38 22 100       23 if grep {; $_->client_id ne $client_id } @{ $_[0]->_sentences };
  37         256  
  22         38  
39             }
40              
41             has sentences => (is => 'bare', reader => '_sentences', required => 1);
42              
43             #pod =method sentences
44             #pod
45             #pod The C method returns a list of
46             #pod L objects, one for each sentence
47             #pod in the paragraph.
48             #pod
49             #pod =cut
50              
51 16     16 1 2082 sub sentences ($self) { @{ $self->_sentences } }
  16         18  
  16         17  
  16         16  
  16         71  
52              
53             #pod =method sentence
54             #pod
55             #pod my $sentence = $para->sentence($n);
56             #pod
57             #pod This method returns the Ith sentence of the paragraph.
58             #pod
59             #pod =cut
60              
61 6     6 1 80 sub sentence ($self, $n) {
  6         9  
  6         7  
  6         6  
62 6 100       43 abort("there is no sentence for index $n")
63             unless $self->_sentences->[$n];
64             }
65              
66             #pod =method single
67             #pod
68             #pod my $sentence = $para->single;
69             #pod my $sentence = $para->single($name);
70             #pod
71             #pod This method throws an exception if there is more than one sentence in the
72             #pod paragraph. If a C<$name> argument is given and the paragraph's single
73             #pod sentence doesn't have that name, an exception is raised.
74             #pod
75             #pod Otherwise, this method returns the sentence.
76             #pod
77             #pod =cut
78              
79 6     6 1 76 sub single ($self, $name = undef) {
  6         8  
  6         7  
  6         7  
80 6         9 my @sentences = $self->sentences;
81              
82 6 100       13 abort("more than one sentence in paragraph, but ->single called")
83             if @sentences > 1;
84              
85 5 100 100     42 abort("single sentence not of expected name <$name>")
86             if defined $name && $name ne $sentences[0]->name;
87              
88 4         13 return $sentences[0];
89             }
90              
91             #pod =method assert_n_sentences
92             #pod
93             #pod my ($s1, $s2, ...) = $paragraph->assert_n_sentences($n);
94             #pod
95             #pod This method returns all the sentences in the paragarph, as long as there are
96             #pod exactly C<$n>. Otherwise, it aborts.
97             #pod
98             #pod =cut
99              
100 3     3 1 63 sub assert_n_sentences ($self, $n) {
  3         4  
  3         4  
  3         2  
101 3 100       291 Carp::confess("no sentence count given") unless defined $n;
102              
103 2         5 my @sentences = $self->sentences;
104              
105 2 100       5 unless (@sentences == $n) {
106 1         5 abort("expected $n sentences but got " . @sentences)
107             }
108              
109 1         3 return @sentences;
110             }
111              
112             #pod =method sentence_named
113             #pod
114             #pod my $sentence = $paragraph->sentence_named($name);
115             #pod
116             #pod This method returns the sentence with the given name. If no such sentence
117             #pod exists, or if two sentences with the name exist, the tester will abort.
118             #pod
119             #pod =cut
120              
121 4     4 1 2165 sub sentence_named ($self, $name) {
  4         6  
  4         6  
  4         5  
122 4 100       343 Carp::confess("no sentence name given") unless defined $name;
123              
124 3         5 my @sentences = grep {; $_->name eq $name } $self->sentences;
  5         23  
125              
126 3 100       17 unless (@sentences) {
127 1         4 abort(qq{no sentence found with name "$name"});
128             }
129              
130 2 100       7 if (@sentences > 1) {
131 1         4 abort(qq{found more than one sentence with name "$name"});
132             }
133              
134 1         4 return $sentences[0];
135             }
136              
137             #pod =method as_triples
138             #pod
139             #pod =method as_stripped_triples
140             #pod
141             #pod C returns an arrayref containing the result of calling
142             #pod C on each sentence in the paragraph. C removes
143             #pod JSON types.
144             #pod
145             #pod =cut
146              
147 1     1 1 29 sub as_triples ($self) {
  1         2  
  1         2  
148 1         5 [ map {; $_->as_triple } $self->sentences ]
  2         6  
149             }
150              
151 1     1 1 14 sub as_stripped_triples ($self) {
  1         2  
  1         2  
152 1         3 [ map {; $_->as_stripped_triple } $self->sentences ]
  2         44  
153             }
154              
155             #pod =method as_pairs
156             #pod
157             #pod C returns an arrayref containing the result of calling C
158             #pod on each sentence in the paragraph. C removes JSON types.
159             #pod
160             #pod =cut
161              
162 1     1 1 14 sub as_pairs ($self) {
  1         1  
  1         2  
163 1         3 [ map {; $_->as_pair } $self->sentences ]
  2         4  
164             }
165              
166 1     1 0 14 sub as_stripped_pairs ($self) {
  1         1  
  1         2  
167 1         3 [ map {; $_->as_stripped_pair } $self->sentences ]
  2         38  
168             }
169              
170             1;
171              
172             __END__