File Coverage

blib/lib/JMAP/Tester/Response/Paragraph.pm
Criterion Covered Total %
statement 45 51 88.2
branch 11 20 55.0
condition 1 3 33.3
subroutine 14 15 93.3
pod 8 11 72.7
total 79 100 79.0


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