File Coverage

blib/lib/JMAP/Tester/Role/Result.pm
Criterion Covered Total %
statement 60 64 93.7
branch 11 14 78.5
condition 4 6 66.6
subroutine 12 13 92.3
pod 3 6 50.0
total 90 103 87.3


line stmt bran cond sub pod time code
1 2     2   910 use v5.20.0;
  2         6  
2 2     2   8 use warnings;
  2         3  
  2         90  
3             package JMAP::Tester::Role::Result 0.110;
4             # ABSTRACT: the kind of thing that you get back for a request
5              
6 2     2   7 use Moo::Role;
  2         3  
  2         7  
7              
8 2     2   650 use experimental 'signatures';
  2         2  
  2         8  
9              
10 2     2   219 use Data::OptList ();
  2         2  
  2         25  
11 2     2   7 use JMAP::Tester::Abort ();
  2         7  
  2         23  
12              
13 2     2   4 use namespace::clean;
  2         3  
  2         9  
14              
15             #pod =head1 OVERVIEW
16             #pod
17             #pod This is the role consumed by the class of any object returned by JMAP::Tester's
18             #pod C method. Its only guarantee, for now, is an C method,
19             #pod and a C method.
20             #pod
21             #pod =cut
22              
23             requires 'is_success';
24             requires 'response_payload';
25              
26             #pod =method assert_successful
27             #pod
28             #pod This method returns the result if it's a success and otherwise aborts.
29             #pod
30             #pod =cut
31              
32 6     6 1 4551 sub assert_successful ($self) {
  6         11  
  6         10  
33 6 100       21 return $self if $self->is_success;
34              
35 2 100 66     27 my $str = $self->can('has_ident') && $self->has_ident
36             ? $self->ident
37             : "JMAP failure";
38              
39 2         84 die JMAP::Tester::Abort->new($str);
40             }
41              
42             #pod =method assert_successful_set
43             #pod
44             #pod $result->assert_successful_set($name);
45             #pod
46             #pod This method is equivalent to:
47             #pod
48             #pod $result->assert_successful->sentence_named($name)->as_set->assert_no_errors;
49             #pod
50             #pod C<$name> must be provided.
51             #pod
52             #pod =cut
53              
54 1     1 1 2 sub assert_successful_set ($self, $name) {
  1         3  
  1         3  
  1         2  
55 1         5 $self->assert_successful->sentence_named($name)->as_set->assert_no_errors;
56             }
57              
58             #pod =method assert_single_successful_set
59             #pod
60             #pod $result->assert_single_successful_set($name);
61             #pod
62             #pod This method is equivalent to:
63             #pod
64             #pod $result->assert_successful->single_sentence($name)->as_set->assert_no_errors;
65             #pod
66             #pod C<$name> may be omitted, in which case the sentence name is not checked.
67             #pod
68             #pod =cut
69              
70 2     2 1 873 sub assert_single_successful_set ($self, $name = undef) {
  2         7  
  2         6  
  2         3  
71 2         8 $self->assert_successful->single_sentence($name)->as_set->assert_no_errors;
72             }
73              
74             has diagnostic_dumper => (
75             is => 'ro',
76             required => 1,
77             );
78              
79 30     30 0 29 sub dump_diagnostic ($self, $value) {
  30         33  
  30         30  
  30         26  
80 30         80 $self->diagnostic_dumper->($value);
81             }
82              
83 0     0 0 0 sub default_diagnostics ($self) {
  0         0  
  0         0  
84 0         0 return;
85             }
86              
87 32     32 0 365 sub abort ($self, $string, $diag_spec = undef) {
  32         40  
  32         33  
  32         49  
  32         33  
88 32   66     106 $diag_spec //= $self->default_diagnostics;
89              
90 32         45 my @diagnostics;
91              
92 32 50       97 if ($diag_spec) {
93 32         104 my $todo = Data::OptList::mkopt($diag_spec);
94              
95 32         912 PAIR: for my $pair (@$todo) {
96 34         55 my ($label, $value) = @$pair;
97              
98 34 100       54 if (not defined $value) {
99 4         9 push @diagnostics, "$label\n";
100 4         9 next PAIR;
101             }
102              
103 30 50       54 if (ref $value) {
104 30         57 $value = $self->dump_diagnostic($value);
105             }
106              
107 30         131 push @diagnostics, "$label: $value";
108 30 50       121 $diagnostics[-1] .= "\n" unless $value =~ /\n\z/;
109             }
110             }
111              
112 32 100       655 die JMAP::Tester::Abort->new({
113             message => $string,
114             (@diagnostics ? (diagnostics => \@diagnostics) : ()),
115             });
116             }
117              
118             1;
119              
120             __END__