File Coverage

lib/Test/HTTP/Router.pm
Criterion Covered Total %
statement 42 52 80.7
branch 14 34 41.1
condition 10 30 33.3
subroutine 11 13 84.6
pod 6 7 85.7
total 83 136 61.0


line stmt bran cond sub pod time code
1             package Test::HTTP::Router;
2              
3 13     13   7713 use strict;
  13         22  
  13         453  
4 13     13   62 use warnings;
  13         21  
  13         372  
5 13     13   61 use Exporter 'import';
  13         22  
  13         424  
6 13     13   63 use Test::Builder;
  13         37  
  13         296  
7 13     13   7126 use Test::Deep;
  13         42  
  13         3011  
8 13     13   8218 use Test::MockObject;
  13         119  
  13         153  
9              
10             our @EXPORT = qw(
11             path_ok path_not_ok
12             match_ok match_not_ok
13             params_ok params_not_ok
14             );
15              
16             our $Test = Test::Builder->new;
17              
18             sub to_request {
19 85 50   85 0 575 my $args = ref $_[0] ? shift : { @_ };
20 85         625 my $req = Test::MockObject->new;
21 85         1856 $req->set_always($_ => $args->{$_}) for keys %$args;
22 85         258 $req;
23             }
24              
25             sub path_ok {
26 17     17 1 207 my ($router, $path, $message) = @_;
27 17         54 my $req = to_request(path => $path);
28 17 50 33     270 $Test->ok(my $match = $router->match($req) ? 1 : 0, $message || "matched $path");
29             }
30              
31             sub path_not_ok {
32 0     0 1 0 my ($router, $path, $message) = @_;
33 0         0 my $req = to_request(path => $path);
34 0 0 0     0 $Test->ok(my $match = $router->match($req) ? 0 : 1, $message || "not matched $path");
35             }
36              
37             sub match_ok {
38 4     4 1 9 my $router = shift;
39 4 50 33     47 my $message = (@_ == 3 || @_ == 2 and not ref $_[-1]) ? pop : undef;
40 4 50       14 my $req = ref $_[0] ? $_[0] : to_request(%{ $_[1] || {} }, path => $_[0]);
  4 50       27  
41 4 50 33     21 $Test->ok(my $match = $router->match($req) ? 1 : 0, $message || "matched @{[$req->path]} with conditions");
42             }
43              
44             sub match_not_ok {
45 6     6 1 193 my $router = shift;
46 6 100 66     250 my $message = (@_ == 3 || @_ == 2 and not ref $_[-1]) ? pop : undef;
47 6 50       28 my $req = ref $_[0] ? $_[0] : to_request(%{ $_[1] || {} }, path => $_[0]);
  6 50       54  
48 6 50 66     217 $Test->ok(my $match = $router->match($req) ? 0 : 1, $message || "not matched @{[$req->path]} with conditions");
49             }
50              
51             sub params_ok {
52 58     58 1 118 my $router = shift;
53 58 50 66     580 my $message = (@_ == 4 || @_ == 3 and not ref $_[-1]) ? pop : undef;
54 58         101 my $params = $_[-1];
55 58 50       183 my $req = ref $_[0] ? $_[0] : to_request(%{ $_[1] || {} }, path => $_[0]);
  58 50       384  
56              
57 58         322 my $match = $router->match($req);
58 58   33     866 $Test->ok($match and eq_deeply($match->params, $params) ? 1 : 0, $message || "valid params at @{[$req->path]}");
59             }
60              
61             sub params_not_ok {
62 0     0 1   my $router = shift;
63 0 0 0       my $message = (@_ == 4 || @_ == 3 and not ref $_[-1]) ? pop : undef;
64 0           my $params = $_[-1];
65 0 0         my $req = ref $_[0] ? $_[0] : to_request(%{ $_[1] || {} }, path => $_[0]);
  0 0          
66              
67 0           my $match = $router->match($req);
68 0   0       $Test->ok($match and eq_deeply($match->params, $params) ? 0 : 1, $message || "invalid params at @{[$req->path]}");
69             }
70              
71             1;
72              
73             =head1 NAME
74              
75             Test::HTTP::Router - Route Testing
76              
77             =head1 SYNOPSIS
78              
79             use Test::More;
80             use Test::HTTP::Router;
81             use HTTP::Router;
82              
83             my $router = HTTP::Router->new;
84             $router->add_route('/' => (
85             conditions => { method => 'GET' },
86             params => { controller => 'Root', action => 'index' },
87             ));
88              
89             match_ok $router, '/', { method => 'GET' };
90             params_ok $router, '/', { method => 'GET' }, { controller => 'Root', action => 'index' };
91              
92             =head1 METHODS
93              
94             =head2 path_ok($router, $path, $message?)
95              
96             =head2 path_not_ok($router, $path, $message?)
97              
98             =head2 match_ok($router, $path, $conditions, $message?)
99              
100             =head2 match_not_ok($router, $path, $conditions, $message?)
101              
102             =head2 params_ok($router, $path, $conditions, $params, $message?)
103              
104             =head2 params_not_ok($router, $path, $conditions, $params, $message?)
105              
106             =cut