blib/lib/Dancer2/Test.pm

filter: Statementstmt 76.1% 223 / 293 Branchbran 55.0% 66 / 120 Conditioncond 16.6% 5 / 30 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 79.0% 34 / 43 Podpod 100% 15 / 15 totaltotal 66.0% 343 / 519 CC 86 SCAR 23.0
CC
86
Cov
66%
response_headers_include
20.0
_check_header
20.0
route_pod_coverage
18.8
CRAP 9.9
« lib/Dancer2/Handler/AutoPage.pm ↑ Summary lib/Dancer2/ConfigReader.pm »
1 package Dancer2::Test;
2 # ABSTRACT: Useful routines for testing Dancer2 apps
3 3 $Dancer2::Test::VERSION = '2.3.0';
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use Carp qw<carp croak>;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Test::More;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Test::Builder;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use URI::Escape;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 ▶use File::Temp;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 3 ▶use Ref::Util qw<is_arrayref>;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 3 ▶use parent 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
16 3 our @EXPORT = qw(
17 dancer_response
18  
19 response_content_is
20 response_content_isnt
21 response_content_is_deeply
22 response_content_like
23 response_content_unlike
24  
25 response_status_is
26 response_status_isnt
27  
28 response_headers_include
29 response_headers_are_deeply
30  
31 response_is_file
32  
33 route_exists
34 route_doesnt_exist
35  
36 is_pod_covered
37 route_pod_coverage
38  
39 );
40  
41 #dancer1 also has read_logs, response_redirect_location_is
42 #cf. https://github.com/PerlDancer2/Dancer22/issues/25
43  
44 3 ▶use Dancer2::Core::MIME;
Subroutine BEGIN: called CC=1 SCAR=0.0
45 3 ▶use Dancer2::Core::Dispatcher;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 3 ▶use Dancer2::Core::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
47  
48 # singleton to store all the apps
49 3 my $_dispatcher = Dancer2::Core::Dispatcher->new;
50  
51 # prevent deprecation warnings
52 3 our $NO_WARN = 0;
53  
54 # can be called with the ($method, $path, $option) triplet,
55 # or can be fed a request object directly, or can be fed
56 # a single string, assumed to be [ GET => $string ]
57 # or can be fed a response (which is passed through without
58 # any modification)
59 34 ▶sub dancer_response {
Subroutine dancer_response: called CC=4 SCAR=13.9
Pod documented
60 34 ▶ croak 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead'
Branch: 1 branch
BranchTrueFalseTotal
unless $Dancer2::Test::NO_WARN 0 34 34
61 unless $NO_WARN;
62  
63 34 _find_dancer_apps_for_dispatcher();
64  
65 # useful for the high-level tests
66 34 ▶ return $_[0] if ref $_[0] eq 'Dancer2::Core::Response';
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq "Dancer2::Core::Response" 7 27 34
67  
68 27 ▶ my ( $request, $env ) =
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'Dancer2::Core::Request' ? : 7 20 27
69 ref $_[0] eq 'Dancer2::Core::Request'
70 ? _build_env_from_request(@_)
71 : _build_request_from_env(@_);
72  
73 # override the set_request so it actually sets our request instead
74 {
75 ## no critic qw(TestingAndDebugging::ProhibitNoWarnings)
76 3 ▶ no warnings qw<redefine once>;
Subroutine BEGIN: called CC=1 SCAR=0.0
77 27 ▶ *Dancer2::Core::App::set_request = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
78 27 my $self = shift;
79 27 $self->_set_request( $request );
80 27 $_->set_request( $request ) for @{ $self->defined_engines };
81 27 };
82 }
83  
84 # since the response is a PSGI response
85 # we create a Response object which was originally expected
86 27 my $psgi_response = $_dispatcher->dispatch($env);
87 27 return Dancer2::Core::Response->new(
88 status => $psgi_response->[0],
89 headers => $psgi_response->[1],
90 content => $psgi_response->[2][0],
91 mime_type => Dancer2::Core::MIME->new(),
92 );
93 }
94  
95  
96  
97 25 ▶sub _build_request_from_env {
Subroutine _build_request_from_env: called CC=16 SCAR=27.8
98  
99 # arguments can be passed as the triplet
100 # or as a arrayref, or as a simple string
101 my ( $method, $path, $options ) =
102 @_ > 1 ? @_
103 25 ▶ : is_arrayref($_[0]) ? @{ $_[0] }
Branch: 2 branches
BranchTrueFalseTotal
@_ > 1 ? : 6 19 25
is_arrayref($_[0]) ? : 9 10 19
104 : ( GET => $_[0], {} );
105  
106 25 my $env = {
107 %ENV,
108 REQUEST_METHOD => uc($method),
109 PATH_INFO => $path,
110 QUERY_STRING => '',
111 'psgi.url_scheme' => 'http',
112 SERVER_PROTOCOL => 'HTTP/1.0',
113 SERVER_NAME => 'localhost',
114 SERVER_PORT => 3000,
115 HTTP_HOST => 'localhost',
116 HTTP_USER_AGENT => "Dancer2::Test simulator v " . Dancer2->VERSION,
117 };
118  
119 25 ▶ if ( defined $options->{params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $options->{"params"}) 3 22 25
120 3 my @params;
121 3 ▶ while ( my ( $p, $value ) = each %{ $options->{params} } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$options{"params"};}) 3 3 6
122 3 ▶ if ( is_arrayref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_arrayref($value)) { } 2 1 3
123 2 for my $v (@$value) {
124 4 push @params,
125 uri_escape_utf8($p) . '=' . uri_escape_utf8($v);
126 }
127 }
128 else {
129 1 push @params,
130 uri_escape_utf8($p) . '=' . uri_escape_utf8($value);
131 }
132 }
133 3 $env->{QUERY_STRING} = join( '&', @params );
134 }
135  
136 25 my $request = Dancer2::Core::Request->new( env => $env );
137  
138 # body
139 25 ▶ $request->body( $options->{body} ) if exists $options->{body};
Branch: 1 branch
BranchTrueFalseTotal
if exists $options->{"body"} 0 25 25
140  
141 # headers
142 25 ▶ if ( $options->{headers} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"headers"}) 1 24 25
143 1 for my $header ( @{ $options->{headers} } ) {
144 2 my ( $name, $value ) = @{$header};
145 2 $request->header( $name => $value );
146 2 ▶ if ( $name =~ /^cookie$/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^cookie$/i) 1 1 2
147 1 $env->{HTTP_COOKIE} = $value;
148 }
149 }
150 }
151  
152 # files
153 25 ▶ if ( $options->{files} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"files"}) 2 23 25
154 2 for my $file ( @{ $options->{files} } ) {
155 2 my $headers = $file->{headers};
156 2 ▶ $headers->{'Content-Type'} ||= 'text/plain';
Condition50%1/2
$headers->{"Content-Type"} ||= "text/plain"
l!l
02
MC/DC0%0/1
$headers->{"Content-Type"} ||= "text/plain"
$headers->{"Content-Type"}
Truth table50%1/2
A
A= $headers->{"Content-Type"}
Aresult
11
00
157  
158 2 my $temp = File::Temp->new();
159 2 ▶ if ( $file->{data} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file->{'data'}) { } 1 1 2
160 1 print $temp $file->{data};
161 1 close($temp);
162 }
163 else {
164 1 require File::Copy;
165 1 File::Copy::copy( $file->{filename}, $temp );
166 }
167  
168 my $upload = Dancer2::Core::Request::Upload->new(
169 filename => $file->{filename},
170 2 size => -s $temp->filename,
171 tempname => $temp->filename,
172 headers => $headers,
173 );
174  
175 ## keep temp_fh in scope so it doesn't get deleted too early
176 ## But will get deleted by the time the test is finished.
177 2 $upload->{temp_fh} = $temp;
178  
179 2 $request->uploads->{ $file->{name} } = $upload;
180 }
181 }
182  
183 # content-type
184 25 ▶ if ( $options->{content_type} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"content_type"}) 0 25 25
185 0 $request->content_type( $options->{content_type} );
186 }
187  
188 25 return ( $request, $env );
189 }
190  
191 9 ▶sub _build_env_from_request {
Subroutine _build_env_from_request: called CC=5 SCAR=26.7
192 9 my ($request) = @_;
193  
194 9 my $env = {
195 REQUEST_METHOD => $request->method,
196 PATH_INFO => $request->path,
197 QUERY_STRING => '',
198 'psgi.url_scheme' => 'http',
199 SERVER_PROTOCOL => 'HTTP/1.0',
200 SERVER_NAME => 'localhost',
201 SERVER_PORT => 3000,
202 HTTP_HOST => 'localhost',
203 HTTP_USER_AGENT => "Dancer2::Test simulator v" . Dancer2->VERSION,
204 };
205  
206 # TODO
207 9 ▶ if ( my $params = $request->{_query_params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $params = $request->{"_query_params"}) 0 9 9
208 0 my @params;
209 0 ▶ while ( my ( $p, $value ) = each %{$params} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$params;}) 0 0 0
210 0 ▶ if ( is_arrayref($value) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_arrayref($value)) { } 0 0 0
211 0 for my $v (@$value) {
212 0 push @params,
213 uri_escape_utf8($p) . '=' . uri_escape_utf8($v);
214 }
215 }
216 else {
217 0 push @params,
218 uri_escape_utf8($p) . '=' . uri_escape_utf8($value);
219 }
220 }
221 0 $env->{QUERY_STRING} = join( '&', @params );
222 }
223  
224 # TODO files
225  
226 9 return ( $request, $env );
227 }
228  
229 4 ▶sub response_status_is {
Subroutine response_status_is: called CC=3 SCAR=11.4
Pod documented
230 4 my ( $req, $status, $test_name ) = @_;
231 4 ▶ carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead'
Branch: 1 branch
BranchTrueFalseTotal
unless $Dancer2::Test::NO_WARN 0 4 4
232 unless $NO_WARN;
233  
234 4 ▶ $test_name ||= "response status is $status for " . _req_label($req);
Condition33%1/3
$test_name ||= "response status is $status for " . _req_label($req)
l!l&&r!l&&!r
040
MC/DC0%0/2
$test_name ||= "response status is $status for " . _req_label($req)
$test_name "response status is $status for " . _req_label($req)
Truth table33%1/3
A ||= B
A= $test_nameB= "response status is $status for " . _req_label($req)
ABresult
1X1
011
000
235  
236 4 my $response = dancer_response($req);
237  
238 4 my $tb = Test::Builder->new;
239 4 local $Test::Builder::Level = $Test::Builder::Level + 1;
240 4 $tb->is_eq( $response->[0], $status, $test_name );
241 }
242  
243 7 ▶sub _find_route_match {
Subroutine _find_route_match: called CC=5 SCAR=16.1
244 7 ▶ my ( $request, $env ) =
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'Dancer2::Core::Request' ? : 2 5 7
245 ref $_[0] eq 'Dancer2::Core::Request'
246 ? _build_env_from_request(@_)
247 : _build_request_from_env(@_);
248  
249 7 for my $app (@{$_dispatcher->apps}) {
250 7 for my $route (@{$app->routes->{lc($request->method)}}) {
251 3 ▶ if ( $route->match($request) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($route->match($request)) 3 0 3
252 3 return 1;
253 }
254 }
255 }
256 4 return 0;
257 }
258  
259 3 ▶sub route_exists {
Subroutine route_exists: called CC=2 SCAR=7.0
Pod documented
260 3 ▶ carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead'
Branch: 1 branch
BranchTrueFalseTotal
unless $Dancer2::Test::NO_WARN 0 3 3
261 unless $NO_WARN;
262  
263 3 my $tb = Test::Builder->new;
264 3 local $Test::Builder::Level = $Test::Builder::Level + 1;
265 3 $tb->ok( _find_route_match($_[0]), $_[1]);
266 }
267  
268 4 ▶sub route_doesnt_exist {
Subroutine route_doesnt_exist: called CC=2 SCAR=7.0
Pod documented
269 4 ▶ carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead'
Branch: 1 branch
BranchTrueFalseTotal
unless $Dancer2::Test::NO_WARN 0 4 4
270 unless $NO_WARN;
271  
272 4 my $tb = Test::Builder->new;
273 4 local $Test::Builder::Level = $Test::Builder::Level + 1;
274 4 $tb->ok( !_find_route_match($_[0]), $_[1]);
275 }
276  
277 4 ▶sub response_status_isnt {
Subroutine response_status_isnt: called CC=3 SCAR=11.4
Pod documented
278 4 my ( $req, $status, $test_name ) = @_;
279  
280 4 ▶ carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead'
Branch: 1 branch
BranchTrueFalseTotal
unless $Dancer2::Test::NO_WARN 0 4 4
281 unless $NO_WARN;
282  
283 4 ▶ $test_name ||= "response status is not $status for " . _req_label($req);
Condition33%1/3
$test_name ||= "response status is not $status for " . _req_label($req)
l!l&&r!l&&!r
040
MC/DC0%0/2
$test_name ||= "response status is not $status for " . _req_label($req)
$test_name "response status is not $status for " . _req_label($req)
Truth table33%1/3
A ||= B
A= $test_nameB= "response status is not $status for " . _req_label($req)
ABresult
1X1
011
000
284  
285 4 my $response = dancer_response($req);
286  
287 4 my $tb = Test::Builder->new;
288 4 local $Test::Builder::Level = $Test::Builder::Level + 1;
289 4 $tb->isnt_eq( $response->[0], $status, $test_name );
290 }
291  
292 {
293 # Map comparison operator names to human-friendly ones
294 3 my %cmp_name = (
295 is_eq => "is",
296 isnt_eq => "is not",
297 like => "matches",
298 unlike => "doesn't match",
299 );
300  
301 16 ▶ sub _cmp_response_content {
Subroutine _cmp_response_content: called CC=2 SCAR=6.9
302 16 my ( $req, $want, $test_name, $cmp ) = @_;
303  
304 16 ▶ if ( @_ == 3 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 3) 8 8 16
305 8 $cmp = $test_name;
306 8 $test_name = $cmp_name{$cmp};
307 8 $test_name =
308 "response content $test_name $want for " . _req_label($req);
309 }
310  
311 16 my $response = dancer_response($req);
312  
313 16 my $tb = Test::Builder->new;
314 16 local $Test::Builder::Level = $Test::Builder::Level + 1;
315 16 $tb->$cmp( $response->[2][0], $want, $test_name );
316 }
317 }
318  
319 3 ▶sub response_content_is {
Subroutine response_content_is: called CC=2 SCAR=7.0
Pod documented
320 4 ▶ carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead'
Branch: 1 branch
BranchTrueFalseTotal
unless $Dancer2::Test::NO_WARN 0 4 4
321 unless $NO_WARN;
322 4 local $Test::Builder::Level = $Test::Builder::Level + 1;
323 4 _cmp_response_content( @_, 'is_eq' );
324 }
325  
326 4 ▶sub response_content_isnt {
Subroutine response_content_isnt: called CC=2 SCAR=7.1
Pod documented
327 4 ▶ carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead'
Branch: 1 branch
BranchTrueFalseTotal
unless $Dancer2::Test::NO_WARN 0 4 4
328 unless $NO_WARN;
329 4 local $Test::Builder::Level = $Test::Builder::Level + 1;
330 4 _cmp_response_content( @_, 'isnt_eq' );
331 }
332  
333 4 ▶sub response_content_like {
Subroutine response_content_like: called CC=2 SCAR=7.1
Pod documented
334 4 ▶ carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead'
Branch: 1 branch
BranchTrueFalseTotal
unless $Dancer2::Test::NO_WARN 0 4 4
335 unless $NO_WARN;
336 4 local $Test::Builder::Level = $Test::Builder::Level + 1;
337 4 _cmp_response_content( @_, 'like' );
338 }
339  
340 4 ▶sub response_content_unlike {
Subroutine response_content_unlike: called CC=2 SCAR=7.1
Pod documented
341 4 ▶ carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead'
Branch: 1 branch
BranchTrueFalseTotal
unless $Dancer2::Test::NO_WARN 0 4 4
342 unless $NO_WARN;
343 4 local $Test::Builder::Level = $Test::Builder::Level + 1;
344 4 _cmp_response_content( @_, 'unlike' );
345 }
346  
347 0 ▶sub response_content_is_deeply {
Subroutine response_content_is_deeply: not called CC=3 SCAR=24.8
Pod documented
348 0 my ( $req, $matcher, $test_name ) = @_;
349 0 ▶ carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead'
Branch: 1 branch
BranchTrueFalseTotal
unless $Dancer2::Test::NO_WARN 0 0 0
350 unless $NO_WARN;
351 0 ▶ $test_name ||= "response content looks good for " . _req_label($req);
Condition0%0/2
$test_name ||= 'response content looks good for ' . _req_label($req)
l!l
00
MC/DC0%0/1
$test_name ||= 'response content looks good for ' . _req_label($req)
$test_name
Truth table0%0/2
A
A= $test_name
Aresult
11
00
352  
353 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
354 0 my $response = _req_to_response($req);
355 0 is_deeply $response->[2][0], $matcher, $test_name;
356 }
357  
358 0 ▶sub response_is_file {
Subroutine response_is_file: not called CC=3 SCAR=24.8
Pod documented
359 0 my ( $req, $test_name ) = @_;
360 0 ▶ carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead'
Branch: 1 branch
BranchTrueFalseTotal
unless $Dancer2::Test::NO_WARN 0 0 0
361 unless $NO_WARN;
362 0 ▶ $test_name ||= "a file is returned for " . _req_label($req);
Condition0%0/2
$test_name ||= 'a file is returned for ' . _req_label($req)
l!l
00
MC/DC0%0/1
$test_name ||= 'a file is returned for ' . _req_label($req)
$test_name
Truth table0%0/2
A
A= $test_name
Aresult
11
00
363  
364 0 my $response = _get_file_response($req);
365 0 my $tb = Test::Builder->new;
366 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
367 0 return $tb->ok( defined($response), $test_name );
368 }
369  
370 0 ▶sub response_headers_are_deeply {
Subroutine response_headers_are_deeply: not called CC=3 SCAR=24.8
Pod documented
371 0 my ( $req, $expected, $test_name ) = @_;
372 0 ▶ carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead'
Branch: 1 branch
BranchTrueFalseTotal
unless $Dancer2::Test::NO_WARN 0 0 0
373 unless $NO_WARN;
374 0 ▶ $test_name ||= "headers are as expected for " . _req_label($req);
Condition0%0/2
$test_name ||= 'headers are as expected for ' . _req_label($req)
l!l
00
MC/DC0%0/1
$test_name ||= 'headers are as expected for ' . _req_label($req)
$test_name
Truth table0%0/2
A
A= $test_name
Aresult
11
00
375  
376 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
377 0 my $response = dancer_response( _expand_req($req) );
378  
379 0 is_deeply(
380 _sort_headers( $response->[1] ),
381 _sort_headers($expected), $test_name
382 );
383 }
384  
385 0 ▶sub response_headers_include {
Subroutine response_headers_include: not called CC=4 SCAR=30.0
Pod documented
386 0 my ( $req, $expected, $test_name ) = @_;
387 0 ▶ carp 'DEPRECATED: Dancer2::Test. Please use Plack::Test instead'
Branch: 1 branch
BranchTrueFalseTotal
unless $Dancer2::Test::NO_WARN 0 0 0
388 unless $NO_WARN;
389 0 ▶ $test_name ||= "headers include expected data for " . _req_label($req);
Condition0%0/2
$test_name ||= 'headers include expected data for ' . _req_label($req)
l!l
00
MC/DC0%0/1
$test_name ||= 'headers include expected data for ' . _req_label($req)
$test_name
Truth table0%0/2
A
A= $test_name
Aresult
11
00
390 0 my $tb = Test::Builder->new;
391  
392 0 my $response = dancer_response($req);
393 0 local $Test::Builder::Level = $Test::Builder::Level + 1;
394  
395 0 ▶ print STDERR "Headers are: "
Branch: 1 branch
BranchTrueFalseTotal
if not $tb->ok(_include_in_headers($response->[1], $expected), $test_name) 0 0 0
396 . Dumper( $response->[1] )
397 . "\n Expected to find header: "
398 . Dumper($expected)
399 if !$tb->ok(
400 _include_in_headers( $response->[1], $expected ),
401 $test_name
402 );
403 }
404  
405 2 ▶sub route_pod_coverage {
Subroutine route_pod_coverage: called CC=17 SCAR=29.3
Pod documented
406  
407 2 require Pod::Simple::Search;
408 2 require Pod::Simple::SimpleTree;
409  
410 2 my $all_routes = {};
411  
412 2 foreach my $app ( @{ $_dispatcher->apps } ) {
413 2 my $routes = $app->routes;
414 2 my $available_routes = [];
415 2 foreach my $method ( sort { $b cmp $a } keys %$routes ) {
416 12 foreach my $r ( @{ $routes->{$method} } ) {
417  
418 # we don't need pod coverage for head
419 16 ▶ next if $method eq 'head';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "head" 6 10 16
420 10 push @$available_routes, $method . ' ' . $r->spec_route;
421 }
422 }
423 ## copy dereferenced array
424 2 ▶ $all_routes->{ $app->name }{routes} = [@$available_routes]
Branch: 1 branch
BranchTrueFalseTotal
if @$available_routes 2 0 2
425 if @$available_routes;
426  
427 # Pod::Simple v3.30 excluded the current directory even when in @INC.
428 # include the current directory as a search path; its backwards compatible
429 # with previous version.
430 2 my $undocumented_routes = [];
431 2 my $file = Pod::Simple::Search->new->find( $app->name, '.' );
432 2 ▶ if ($file) {
Branch: 1 branch
BranchTrueFalseTotal
if ($file) { } 2 0 2
433 2 $all_routes->{ $app->name }{has_pod} = 1;
434 2 my $parser = Pod::Simple::SimpleTree->new->parse_file($file);
435 2 my $pod_dataref = $parser->root;
436 2 my $found_routes = {};
437 2 ▶ for ( my $i = 0; $i < @$available_routes; $i++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$available_routes) 10 2 12
438  
439 10 my $r = $available_routes->[$i];
440 10 my $app_string = lc $r;
441 10 $app_string =~ s/\*/_REPLACED_STAR_/g;
442  
443 10 ▶ for ( my $idx = 0; $idx < @$pod_dataref; $idx++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($idx < @$pod_dataref) 380 10 390
444 380 my $pod_part = $pod_dataref->[$idx];
445  
446 380 ▶ next if !is_arrayref($pod_part);
Branch: 1 branch
BranchTrueFalseTotal
unless is_arrayref($pod_part) 20 360 380
447 360 foreach my $ref_part (@$pod_part) {
448 1140 ▶ is_arrayref($ref_part)
Branch: 1 branch
BranchTrueFalseTotal
is_arrayref($ref_part) and push @$pod_dataref, $ref_part 90 1050 1140
449 and push @$pod_dataref, $ref_part;
450 }
451  
452 360 my $pod_string = lc $pod_part->[2];
453 360 $pod_string =~ s/['|"|\s]+/ /g;
454 360 $pod_string =~ s/\s$//g;
455 360 $pod_string =~ s/\*/_REPLACED_STAR_/g;
456 360 ▶ if ( $pod_string =~ m/^$app_string$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pod_string =~ /^$app_string$/) 34 326 360
457 34 $found_routes->{$app_string} = 1;
458 34 next;
459 }
460 }
461 10 ▶ if ( !$found_routes->{$app_string} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($found_routes->{$app_string}) 0 10 10
462 0 push @$undocumented_routes, $r;
463 }
464 }
465 }
466 else { ### no POD found
467 0 $all_routes->{ $app->name }{has_pod} = 0;
468 }
469 2 ▶ if (@$undocumented_routes) {
Branch: 2 branches
BranchTrueFalseTotal
if (@$undocumented_routes) { } 0 2 2
elsif (not $$all_routes{$app->name}{'has_pod'} and @{$$all_routes{$app->name}{'routes'};}) { } 0 4 4
470 $all_routes->{ $app->name }{undocumented_routes} =
471 0 $undocumented_routes;
472 }
473 ▶ elsif ( !$all_routes->{ $app->name }{has_pod}
Condition33%1/3
not $$all_routes{$app->name}{"has_pod"} and @{$$all_routes{$app->name}{"routes"};}
!ll&&!rl&&r
200
MC/DC0%0/2
not $$all_routes{$app->name}{"has_pod"} and @{$$all_routes{$app->name}{"routes"};}
not $$all_routes{$app->name}{"has_pod"} @{$$all_routes{$app->name}{"routes"};}
Truth table33%1/3
not(A) and B
A= not $$all_routes{$app->name}{"has_pod"}B= @{$$all_routes{$app->name}{"routes"};}
ABresult
0X0
100
111
474 0 && @{ $all_routes->{ $app->name }{routes} } )
475 {
476 ## copy dereferenced array
477 $all_routes->{ $app->name }{undocumented_routes} =
478 0 [ @{ $all_routes->{ $app->name }{routes} } ];
479 }
480 }
481  
482 2 return $all_routes;
483 }
484  
485 1 ▶sub is_pod_covered {
Subroutine is_pod_covered: called CC=3 SCAR=11.0
Pod documented
486 1 my ($test_name) = @_;
487  
488 1 ▶ $test_name ||= "is pod covered";
Condition50%1/2
$test_name ||= "is pod covered"
l!l
10
MC/DC0%0/1
$test_name ||= "is pod covered"
$test_name
Truth table50%1/2
A
A= $test_name
Aresult
11
00
489 1 my $route_pod_coverage = route_pod_coverage();
490  
491 1 my $tb = Test::Builder->new;
492 1 local $Test::Builder::Level = $Test::Builder::Level + 1;
493  
494 1 foreach my $app ( @{ $_dispatcher->apps } ) {
495 my %undocumented_route =
496 0 ( map { $_ => 1 }
497 1 @{ $route_pod_coverage->{ $app->name }{undocumented_routes} } );
498 $tb->subtest(
499 $app->name . $test_name,
500 1 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
501 1 foreach my $route (
502 1 @{ $route_pod_coverage->{ $app->name }{routes} } )
503 {
504 5 ok( !$undocumented_route{$route}, "$route is documented" );
505 }
506 }
507 1 );
508 }
509 }
510  
511 2 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
512 2 my ( $class, %options ) = @_;
513  
514 2 my @applications;
515 2 ▶ if ( ref $options{apps} eq ref( [] ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $options{'apps'} eq ref []) { } 1 1 2
516 1 @applications = @{ $options{apps} };
517 }
518 else {
519 1 my ( $caller, $script ) = caller;
520  
521 # if no app is passed, assume the caller is one.
522 1 ▶ @applications = ($caller) if $caller->can('dancer_app');
Branch: 1 branch
BranchTrueFalseTotal
if $caller->can("dancer_app") 1 0 1
523 }
524  
525 # register the apps to the test dispatcher
526 $_dispatcher->apps( [ map {
527 2 $_->dancer_app->finish();
528 2 $_->dancer_app;
529 } @applications ] );
530  
531 2 $class->export_to_level( 1, $class, @EXPORT );
532 }
533  
534 # private
535  
536 16 ▶sub _req_label {
Subroutine _req_label: called CC=4 SCAR=13.9
537 16 my $req = shift;
538  
539 return
540 ref $req eq 'Dancer2::Core::Response' ? 'response object'
541 : ref $req eq 'Dancer2::Core::Request'
542 16 ▶ ? join( ' ', map { $req->$_ } qw/ method path / )
Branch: 3 branches
BranchTrueFalseTotal
ref $req eq 'Dancer2::Core::Response' ? : 4 12 16
ref $req eq 'Dancer2::Core::Request' ? : 4 8 12
is_arrayref($req) ? : 4 4 8
543 : is_arrayref($req) ? join( ' ', @$req )
544 : "GET $req";
545 }
546  
547 0 ▶sub _expand_req {
Subroutine _expand_req: not called CC=2 SCAR=17.9
548 0 my $req = shift;
549 0 ▶ return is_arrayref($req) ? @$req : ( 'GET', $req );
Branch: 1 branch
BranchTrueFalseTotal
is_arrayref($req) ? : 0 0 0
550 }
551  
552 # Sort arrayref of headers (turn it into a list of arrayrefs, sort by the header
553 # & value, then turn it back into an arrayref)
554 0 ▶sub _sort_headers {
Subroutine _sort_headers: not called CC=3 SCAR=24.8
555 0 my @originalheaders = @{ shift() }; # take a copy we can modify
556 0 my @headerpairs;
557 0 ▶ while ( my ( $header, $value ) = splice @originalheaders, 0, 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@originalheaders, 0, 2)) 0 0 0
558 0 push @headerpairs, [ $header, $value ];
559 }
560  
561 # We have an array of arrayrefs holding header => value pairs; sort them by
562 # header then value, and return them flattened back into an arrayref
563 return [
564 0 map {@$_}
565 0 ▶ sort { $a->[0] cmp $b->[0] || $a->[1] cmp $b->[1] } @headerpairs
Condition0%0/3
$Dancer2::Test::a->[0] cmp $Dancer2::Test::b->[0] || $Dancer2::Test::a->[1] cmp $Dancer2::Test::b->[1]
l!l&&r!l&&!r
000
MC/DC0%0/2
$Dancer2::Test::a->[0] cmp $Dancer2::Test::b->[0] || $Dancer2::Test::a->[1] cmp $Dancer2::Test::b->[1]
$Dancer2::Test::a->[0] cmp $Dancer2::Test::b->[0] $Dancer2::Test::a->[1] cmp $Dancer2::Test::b->[1]
Truth table0%0/3
A || B
A= $Dancer2::Test::a->[0] cmp $Dancer2::Test::b->[0]B= $Dancer2::Test::a->[1] cmp $Dancer2::Test::b->[1]
ABresult
1X1
011
000
566 ];
567 }
568  
569 # make sure the given header sublist is included in the full headers array
570 0 ▶sub _include_in_headers {
Subroutine _include_in_headers: not called CC=3 SCAR=24.8
571 0 my ( $full_headers, $expected_subset ) = @_;
572  
573 # walk through all the expected header pairs, make sure
574 # they exist with the same value in the full_headers list
575 # return false as soon as one is not.
576 0 ▶ for ( my $i = 0; $i < scalar(@$expected_subset); $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$expected_subset) 0 0 0
577 0 my ( $name, $value ) =
578 ( $expected_subset->[$i], $expected_subset->[ $i + 1 ] );
579 0 ▶ return 0
Branch: 1 branch
BranchTrueFalseTotal
unless _check_header($full_headers, $name, $value) 0 0 0
580 unless _check_header( $full_headers, $name, $value );
581 }
582  
583 # we've found all the expected pairs in the $full_headers list
584 0 return 1;
585 }
586  
587 0 ▶sub _check_header {
Subroutine _check_header: not called CC=4 SCAR=30.0
588 0 my ( $headers, $key, $value ) = @_;
589 0 ▶ for ( my $i = 0; $i < scalar(@$headers); $i += 2 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$headers) 0 0 0
590 0 my ( $name, $val ) = ( $headers->[$i], $headers->[ $i + 1 ] );
591 0 ▶ return 1 if $name eq $key && $value eq $val;
Branch: 1 branch
BranchTrueFalseTotal
if $name eq $key and $value eq $val 0 0 0
Condition0%0/3
$name eq $key and $value eq $val
!ll&&!rl&&r
000
MC/DC0%0/2
$name eq $key and $value eq $val
$name eq $key $value eq $val
Truth table0%0/3
A and B
A= $name eq $keyB= $value eq $val
ABresult
0X0
100
111
592 }
593 0 return 0;
594 }
595  
596 0 ▶sub _req_to_response {
Subroutine _req_to_response: not called CC=3 SCAR=24.8
597 0 my $req = shift;
598  
599 # already a response object
600 0 ▶ return $req if ref $req eq 'Dancer2::Core::Response';
Branch: 1 branch
BranchTrueFalseTotal
if ref $req eq "Dancer2::Core::Response" 0 0 0
601  
602 0 ▶ return dancer_response( is_arrayref($req) ? @$req : ( 'GET', $req ) );
Branch: 1 branch
BranchTrueFalseTotal
is_arrayref($req) ? : 0 0 0
603 }
604  
605 # make sure we have at least one app in the dispatcher, and if not,
606 # we must have at this point an app within the caller
607 34 ▶sub _find_dancer_apps_for_dispatcher {
Subroutine _find_dancer_apps_for_dispatcher: called CC=5 SCAR=29.1
608 34 ▶ return if scalar( @{ $_dispatcher->apps } );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$_dispatcher->apps;} 34 0 34
609  
610 0 ▶ for ( my $deep = 0; $deep < 5; $deep++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($deep < 5) 0 0 0
611 0 my $caller = caller($deep);
612 0 ▶ next if !$caller || !$caller->can('dancer_app');
Branch: 1 branch
BranchTrueFalseTotal
unless $caller and $caller->can("dancer_app") 0 0 0
Condition0%0/3
$caller and $caller->can("dancer_app")
!ll&&!rl&&r
000
MC/DC0%0/2
$caller and $caller->can("dancer_app")
$caller $caller->can("dancer_app")
Truth table0%0/3
A and B
A= $callerB= $caller->can("dancer_app")
ABresult
0X0
100
111
613  
614 0 return $_dispatcher->apps( [ $caller->dancer_app ] );
615 }
616  
617 0 croak "Unable to find a Dancer2 app, did you use Dancer2 in your test?";
618 }
619  
620 3 1;
621  
622 __END__
« lib/Dancer2/Handler/AutoPage.pm ↑ Summary lib/Dancer2/ConfigReader.pm »