blib/lib/Kelp/Test.pm

filter: Statementstmt 88.2% 142 / 161 Branchbran 62.5% 10 / 16 Conditioncond 49.0% 25 / 51 MC/DCmcdc 13.7% 4 / 29 Subroutinesub 85.0% 34 / 40 Podpod 100% 21 / 21 totaltotal 74.2% 236 / 318 CC 31 SCAR 10.5
CC
31
Cov
78%
header_unlike
12.0
content_type_isnt
6.0
code_is
4.4
CRAP 2.9
« blib/lib/Kelp/Base.pm ↑ Summary blib/lib/Kelp/Template.pm »
1 package Kelp::Test;
2  
3 31 ▶use Kelp::Base;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 31 ▶use Plack::Test;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 31 ▶use Plack::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 31 ▶use Test::More import => ['!note'];
Subroutine BEGIN: called CC=1 SCAR=0.0
7 31 ▶use Test::Deep;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 31 ▶use Kelp::Test::CookieJar;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 31 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 31 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 31 ▶use Kelp::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 31 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
14 31 $ENV{KELP_TESTING} = 1; # Set the ENV for testing
15 }
16  
17 sub import
18 31 ▶{
Subroutine import: called CC=3 SCAR=11.0
19 31 my ($me, @args) = @_;
20  
21 31 ▶ if ($args[0] && $args[0] eq -utf8) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args[0] and $args[0] eq "-utf8") 6 25 31
Condition66%2/3
$args[0] and $args[0] eq "-utf8"
!ll&&!rl&&r
2506
MC/DC50%1/2
$args[0] and $args[0] eq "-utf8"
$args[0] $args[0] eq "-utf8"
Truth table66%2/3
A and B
A= $args[0]B= $args[0] eq "-utf8"
ABresult
0X0
100
111
22 6 my $builder = Test::More->builder;
23 6 ▶ binmode $builder->output, ":encoding(utf8)";
Subroutine BEGIN: called CC=1 SCAR=0.0
24 6 binmode $builder->failure_output, ":encoding(utf8)";
25 6 binmode $builder->todo_output, ":encoding(utf8)";
26 }
27 }
28  
29 31 attr -psgi => undef;
30  
31 1 ▶attr -app => sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
32 1 my $self = shift;
33 1 ▶ return defined $self->psgi
Branch: 1 branch
BranchTrueFalseTotal
defined $self->psgi ? : 1 0 1
34 ? Plack::Util::load_psgi($self->psgi)
35 : die "'app' or 'psgi' parameter is required";
36 31 };
37 31 attr charset => undef;
38  
39 0 ▶attr res => sub { die "res is not initialized" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
40  
41 32 ▶attr cookies => sub { Kelp::Test::CookieJar->new };
Subroutine __ANON__: called CC=1 SCAR=0.0
42  
43 sub _decode
44 222 ▶{
Subroutine _decode: called CC=2 SCAR=7.1
45 222 my ($self, $string) = @_;
46 222 ▶ return Kelp::Util::charset_decode($self->charset // $self->app->charset, $string);
Condition66%2/3
$self->charset // $self->app->charset
l!l&&r!l&&!r
82140
MC/DC0%0/2
$self->charset // $self->app->charset
$self->charset $self->app->charset
Truth table66%2/3
A // B
A= $self->charsetB= $self->app->charset
ABresult
1X1
011
000
47 }
48  
49 sub request
50 255 ▶{
Subroutine request: called CC=4 SCAR=13.9
Pod documented
51 255 my ($self, $req) = @_;
52 255 ▶ croak "HTTP::Request object needed" unless ref($req) eq 'HTTP::Request';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $req eq "HTTP::Request" 0 255 255
53 255 $self->note($req->method . ' ' . $req->uri);
54  
55 # Most likely the request was not initialized with a URI that had a scheme,
56 # so we add a default http to prevent unitialized regex matches further
57 # down the chain
58 255 ▶ $req->uri->scheme('http') unless $req->uri->scheme;
Branch: 1 branch
BranchTrueFalseTotal
unless $req->uri->scheme 255 0 255
59  
60 # If no host was given to the request's uri (most likely), then add
61 # localhost. This is needed by the cookies header, which will not be
62 # applied unless the request uri has a proper domain.
63 255 ▶ if ($req->uri->opaque =~ qr|^/{1}|) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req->uri->opaque =~ qr"^/{1}") 254 1 255
64 254 $req->uri->opaque('//localhost' . $req->uri->opaque);
65 }
66  
67 # Add the current cookie to the request headers
68 255 $self->cookies->add_cookie_header($req);
69  
70 255 ▶ my $res = test_psgi($self->app->run, sub { shift->($req) });
Subroutine __ANON__: called CC=1 SCAR=0.0
71  
72 # Extract the cookies from the response and add them to the cookie jar
73 255 $self->cookies->extract_cookies($res);
74  
75 255 $self->res($res);
76 255 return $self;
77 }
78  
79 sub request_ok
80 23 ▶{
Subroutine request_ok: called CC=1 SCAR=0.0
Pod documented
81 23 my ($self, $req, $test_name) = @_;
82 23 local $Test::Builder::Level = $Test::Builder::Level + 1;
83  
84 23 $self->request($req)->code_is(200, $test_name);
85 }
86  
87 sub code_is
88 180 ▶{
Subroutine code_is: called CC=4 SCAR=14.8
Pod documented
89 180 my ($self, $code, $test_name) = @_;
90 180 local $Test::Builder::Level = $Test::Builder::Level + 1;
91  
92 180 ▶ $test_name ||= "Response code is $code";
Condition50%1/2
$test_name ||= "Response code is $code"
l!l
0180
MC/DC0%0/1
$test_name ||= "Response code is $code"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
93 180 is $self->res->code, $code, $test_name;
94  
95 # If we got 500 back and shouldn't have, we show the content
96 180 ▶ if ($code != 500 && $self->res->code == 500) {
Branch: 1 branch
BranchTrueFalseTotal
if ($code != 500 and $self->res->code == 500) 0 180 180
Condition66%2/3
$code != 500 and $self->res->code == 500
!ll&&!rl&&r
401400
MC/DC0%0/2
$code != 500 and $self->res->code == 500
$code != 500 $self->res->code == 500
Truth table66%2/3
A and B
A= $code != 500B= $self->res->code == 500
ABresult
0X0
100
111
97 0 fail $self->res->content;
98 }
99  
100 180 return $self;
101 }
102  
103 sub code_isnt
104 2 ▶{
Subroutine code_isnt: called CC=2 SCAR=7.0
Pod documented
105 2 my ($self, $code, $test_name) = @_;
106 2 local $Test::Builder::Level = $Test::Builder::Level + 1;
107  
108 2 ▶ $test_name ||= "Response code is not $code";
Condition50%1/2
$test_name ||= "Response code is not $code"
l!l
02
MC/DC0%0/1
$test_name ||= "Response code is not $code"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
109 2 isnt $self->res->code, $code, $test_name;
110 2 return $self;
111 }
112  
113 sub content_is
114 117 ▶{
Subroutine content_is: called CC=2 SCAR=7.0
Pod documented
115 117 my ($self, $value, $test_name) = @_;
116 117 local $Test::Builder::Level = $Test::Builder::Level + 1;
117  
118 117 ▶ $test_name ||= "Content is '$value'";
Condition50%1/2
$test_name ||= "Content is '${value}'"
l!l
0117
MC/DC0%0/1
$test_name ||= "Content is '${value}'"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
119 117 is $self->_decode($self->res->content), $value,
120 $test_name;
121 117 return $self;
122 }
123  
124 sub content_bytes_are
125 11 ▶{
Subroutine content_bytes_are: called CC=2 SCAR=7.0
Pod documented
126 11 my ($self, $value, $test_name) = @_;
127 11 local $Test::Builder::Level = $Test::Builder::Level + 1;
128  
129 11 ▶ $test_name ||= "Content is '$value'";
Condition50%1/2
$test_name ||= "Content is '${value}'"
l!l
110
MC/DC0%0/1
$test_name ||= "Content is '${value}'"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
130 11 my $got = unpack 'H*', $self->res->content;
131 11 my $expected = unpack 'H*', $value;
132 11 is $got, $expected, $test_name;
133 11 return $self;
134 }
135  
136 sub content_isnt
137 1 ▶{
Subroutine content_isnt: called CC=2 SCAR=7.0
Pod documented
138 1 my ($self, $value, $test_name) = @_;
139 1 local $Test::Builder::Level = $Test::Builder::Level + 1;
140  
141 1 ▶ $test_name ||= "Content is not '$value'";
Condition50%1/2
$test_name ||= "Content is not '${value}'"
l!l
01
MC/DC0%0/1
$test_name ||= "Content is not '${value}'"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
142 1 isnt $self->_decode($self->res->content), $value,
143 $test_name;
144 1 return $self;
145 }
146  
147 sub content_like
148 62 ▶{
Subroutine content_like: called CC=2 SCAR=6.9
Pod documented
149 62 my ($self, $regexp, $test_name) = @_;
150 62 local $Test::Builder::Level = $Test::Builder::Level + 1;
151  
152 62 ▶ $test_name ||= "Content matches $regexp";
Condition100%2/2
$test_name ||= "Content matches $regexp"
l!l
755
MC/DC100%1/1
$test_name ||= "Content matches $regexp"
$test_name
Truth table100%2/2
A
A= $test_name
Aresult
11
00
153 62 like $self->_decode($self->res->content), $regexp,
154 $test_name;
155 62 return $self;
156 }
157  
158 sub content_unlike
159 23 ▶{
Subroutine content_unlike: called CC=2 SCAR=6.9
Pod documented
160 23 my ($self, $regexp, $test_name) = @_;
161 23 local $Test::Builder::Level = $Test::Builder::Level + 1;
162  
163 23 ▶ $test_name ||= "Content does not match $regexp";
Condition100%2/2
$test_name ||= "Content does not match $regexp"
l!l
221
MC/DC100%1/1
$test_name ||= "Content does not match $regexp"
$test_name
Truth table100%2/2
A
A= $test_name
Aresult
11
00
164 23 unlike $self->_decode($self->res->content), $regexp,
165 $test_name;
166 23 return $self;
167 }
168  
169 sub content_type_is
170 40 ▶{
Subroutine content_type_is: called CC=2 SCAR=7.0
Pod documented
171 40 my ($self, $value, $test_name) = @_;
172 40 local $Test::Builder::Level = $Test::Builder::Level + 1;
173  
174 40 ▶ $test_name ||= "Content-Type is '$value'";
Condition50%1/2
$test_name ||= "Content-Type is '${value}'"
l!l
040
MC/DC0%0/1
$test_name ||= "Content-Type is '${value}'"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
175 40 is $self->res->content_type, $value, $test_name;
176 40 return $self;
177 }
178  
179 sub full_content_type_is
180 6 ▶{
Subroutine full_content_type_is: called CC=2 SCAR=7.0
Pod documented
181 6 my ($self, $value, $test_name) = @_;
182 6 local $Test::Builder::Level = $Test::Builder::Level + 1;
183  
184 6 ▶ $test_name ||= "Full Content-Type is '$value'";
Condition50%1/2
$test_name ||= "Full Content-Type is '${value}'"
l!l
06
MC/DC0%0/1
$test_name ||= "Full Content-Type is '${value}'"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
185 6 is join('; ', $self->res->content_type), $value, $test_name;
186 6 return $self;
187 }
188  
189 sub content_type_isnt
190 0 ▶{
Subroutine content_type_isnt: not called CC=2 SCAR=17.9
Pod documented
191 0 my ($self, $value, $test_name) = @_;
192 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
193  
194 0 ▶ $test_name ||= "Content-Type is not '$value'";
Condition0%0/2
$test_name ||= "Content-Type is not '${value}'"
l!l
00
MC/DC0%0/1
$test_name ||= "Content-Type is not '${value}'"
$test_name
Truth table0%0/2
A
A= $test_name
Aresult
11
00
195 0 isnt $self->res->content_type, $value, $test_name;
196 0 return $self;
197 }
198  
199 sub header_is
200 24 ▶{
Subroutine header_is: called CC=3 SCAR=11.8
Pod documented
201 24 my ($self, $header, $value, $test_name) = @_;
202 24 local $Test::Builder::Level = $Test::Builder::Level + 1;
203  
204 24 ▶ $test_name ||= "Header '$header' => '$value'";
Condition50%1/2
$test_name ||= "Header '${header}' => '${value}'"
l!l
024
MC/DC0%0/1
$test_name ||= "Header '${header}' => '${value}'"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
205 24 ▶ is $self->res->header($header), $value, $test_name
Condition33%1/3
$test_name || $self->diag_headers
l!l&&r!l&&!r
2400
MC/DC0%0/2
$test_name || $self->diag_headers
$test_name $self->diag_headers
Truth table33%1/3
A || B
A= $test_nameB= $self->diag_headers
ABresult
1X1
011
000
206 || $self->diag_headers();
207 24 return $self;
208 }
209  
210 sub header_isnt
211 3 ▶{
Subroutine header_isnt: called CC=3 SCAR=11.8
Pod documented
212 3 my ($self, $header, $value, $test_name) = @_;
213 3 local $Test::Builder::Level = $Test::Builder::Level + 1;
214  
215 3 ▶ $test_name ||= "Header '$header' is not '$value'";
Condition50%1/2
$test_name ||= "Header '${header}' is not '${value}'"
l!l
03
MC/DC0%0/1
$test_name ||= "Header '${header}' is not '${value}'"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
216 3 ▶ isnt $self->res->header($header), $value, $test_name
Condition33%1/3
$test_name || $self->diag_headers
l!l&&r!l&&!r
300
MC/DC0%0/2
$test_name || $self->diag_headers
$test_name $self->diag_headers
Truth table33%1/3
A || B
A= $test_nameB= $self->diag_headers
ABresult
1X1
011
000
217 || $self->diag_headers();
218 3 return $self;
219 }
220  
221 sub header_like
222 1 ▶{
Subroutine header_like: called CC=3 SCAR=11.8
Pod documented
223 1 my ($self, $header, $regexp, $test_name) = @_;
224 1 local $Test::Builder::Level = $Test::Builder::Level + 1;
225  
226 1 ▶ $test_name ||= "Header '$header' =~ $regexp";
Condition50%1/2
$test_name ||= "Header '${header}' =~ $regexp"
l!l
01
MC/DC0%0/1
$test_name ||= "Header '${header}' =~ $regexp"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
227 1 ▶ like $self->res->header($header), $regexp, $test_name
Condition33%1/3
$test_name || $self->diag_headers
l!l&&r!l&&!r
100
MC/DC0%0/2
$test_name || $self->diag_headers
$test_name $self->diag_headers
Truth table33%1/3
A || B
A= $test_nameB= $self->diag_headers
ABresult
1X1
011
000
228 || $self->diag_headers();
229 1 return $self;
230 }
231  
232 sub header_unlike
233 0 ▶{
Subroutine header_unlike: not called CC=3 SCAR=24.8
Pod documented
234 0 my ($self, $header, $regexp, $test_name) = @_;
235 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
236  
237 0 ▶ $test_name ||= "Header '$header' !~ $regexp";
Condition0%0/2
$test_name ||= "Header '${header}' !~ $regexp"
l!l
00
MC/DC0%0/1
$test_name ||= "Header '${header}' !~ $regexp"
$test_name
Truth table0%0/2
A
A= $test_name
Aresult
11
00
238 0 ▶ unlike $self->res->header($header), $regexp, $test_name
Condition0%0/3
$test_name || $self->diag_headers
l!l&&r!l&&!r
000
MC/DC0%0/2
$test_name || $self->diag_headers
$test_name $self->diag_headers
Truth table0%0/3
A || B
A= $test_nameB= $self->diag_headers
ABresult
1X1
011
000
239 || $self->diag_headers();
240 0 return $self;
241 }
242  
243 sub json_content
244 19 ▶{
Subroutine json_content: called CC=1 SCAR=0.0
Pod documented
245 19 my $self = shift;
246 19 my $result;
247 19 my $decoder = $self->app->get_encoder(json => 'internal');
248 19 ▶ try {
Subroutine __ANON__: called CC=1 SCAR=0.0
249 19 $result = $decoder->decode(
250 $self->_decode($self->res->content)
251 );
252 }
253 0 ▶ catch {
Subroutine __ANON__: not called CC=1 SCAR=6.9
254 0 fail("Poorly formatted JSON");
255 19 };
256 19 return $result;
257 }
258  
259 sub json_cmp
260 18 ▶{
Subroutine json_cmp: called CC=4 SCAR=14.0
Pod documented
261 18 my ($self, $expected, $test_name) = @_;
262 18 local $Test::Builder::Level = $Test::Builder::Level + 1;
263  
264 18 ▶ $test_name ||= "JSON structure matches";
Condition100%2/2
$test_name ||= "JSON structure matches"
l!l
117
MC/DC100%1/1
$test_name ||= "JSON structure matches"
$test_name
Truth table100%2/2
A
A= $test_name
Aresult
11
00
265 18 ▶ like $self->res->header('content-type'), qr/json/, 'Content-Type is JSON'
Branch: 1 branch
BranchTrueFalseTotal
&like(scalar $self->res->header("content-type"), qr/json/, "Content-Type is JSON") or return $self 18 0 18
266 or return $self;
267 18 my $json = $self->json_content;
268 18 ▶ cmp_deeply($json, $expected, $test_name) or diag explain $json;
Branch: 1 branch
BranchTrueFalseTotal
cmp_deeply($json, $expected, $test_name) or diag(explain($json)) 18 0 18
269 18 return $self;
270 }
271  
272 sub note
273 255 ▶{
Subroutine note: called CC=1 SCAR=0.0
Pod documented
274 255 my $self = shift;
275 255 Test::More::note @_;
276 255 return $self;
277 }
278  
279 sub diag_headers
280 0 ▶{
Subroutine diag_headers: not called CC=1 SCAR=6.9
Pod documented
281 0 my $self = shift;
282 0 diag $self->res->headers->as_string;
283 0 return $self;
284 }
285  
286 sub diag_content
287 0 ▶{
Subroutine diag_content: not called CC=1 SCAR=6.9
Pod documented
288 0 my $self = shift;
289 0 diag $self->res->content;
290 0 return $self;
291 }
292  
293 31 1;
294  
295 __END__
« blib/lib/Kelp/Base.pm ↑ Summary blib/lib/Kelp/Template.pm »