File Coverage

blib/lib/Test/Path/Router.pm
Criterion Covered Total %
statement 72 75 96.0
branch 19 20 95.0
condition 13 21 61.9
subroutine 15 15 100.0
pod 7 7 100.0
total 126 138 91.3


line stmt bran cond sub pod time code
1             package Test::Path::Router;
2             BEGIN {
3 11     11   177560 $Test::Path::Router::AUTHORITY = 'cpan:STEVAN';
4             }
5             # ABSTRACT: A testing module for testing routes
6             $Test::Path::Router::VERSION = '0.14_01';
7 11     11   72 use strict;
  11         12  
  11         288  
8 11     11   38 use warnings;
  11         15  
  11         275  
9              
10 11     11   42 use Test::Builder 1.001013 ();
  11         163  
  11         246  
11 11     11   6279 use Test::Deep 0.113 ();
  11         95435  
  11         373  
12 11     11   6617 use Data::Dumper 2.154 ();
  11         61246  
  11         318  
13 11     11   5740 use Sub::Exporter 0.981;
  11         89060  
  11         82  
14              
15             my @exports = qw/
16             routes_ok
17             path_ok
18             path_not_ok
19             path_is
20             mapping_ok
21             mapping_not_ok
22             mapping_is
23             /;
24              
25             Sub::Exporter::setup_exporter({
26             exports => \@exports,
27             groups => { default => \@exports }
28             });
29              
30             our $Test = Test::Builder->new;
31              
32             sub routes_ok {
33 8     8 1 3083 my ($router, $routes, $message) = @_;
34 8         15 my ($passed, $reason);
35 8         28 foreach my $path (keys %$routes) {
36 53         68177 my $mapping = $routes->{$path};
37              
38 53         66 my $generated_path = $router->uri_for(%{$mapping});
  53         231  
39              
40 53 100       116 $generated_path = '' unless defined $generated_path;
41              
42             # the path generated from the hash
43             # is the same as the path supplied
44 53 100       108 if ($path ne $generated_path) {
45 1         5 $Test->ok(0, $message);
46 1         371 $Test->diag("... paths do not match\n" .
47             " got: '" . $generated_path . "'\n" .
48             " expected: '" . $path . "'");
49 1         45 return;
50             }
51              
52 52         135 my $match = $router->match($path);
53 52   33     308 my $generated_mapping = $match && $match->mapping;
54              
55 52         319 $Test->ok( $match->path eq $path, "matched path (" . $match->path . ") and requested paths ($path) match" );
56              
57             # the path supplied produces the
58             # same match as the hash supplied
59              
60 52 50       17330 unless (Test::Deep::eq_deeply($generated_mapping, $mapping)) {
61 0         0 $Test->ok(0, $message);
62 0         0 $Test->diag("... mappings do not match for '$path'\n" .
63             " got: " . _dump_mapping_info($generated_mapping) . "\n" .
64             " expected: " . _dump_mapping_info($mapping));
65 0         0 return;
66             }
67             }
68 7         4759 $Test->ok(1, $message);
69             }
70              
71             sub path_ok {
72 35     35 1 9752 my ($router, $path, $message) = @_;
73 35 100       94 if ($router->match($path)) {
74 34         117 $Test->ok(1, $message);
75             }
76             else {
77 1         3 $Test->ok(0, $message);
78             }
79             }
80              
81             sub path_not_ok {
82 25     25 1 6936 my ($router, $path, $message) = @_;
83 25 100       67 unless ($router->match($path)) {
84 24         75 $Test->ok(1, $message);
85             }
86             else {
87 1         4 $Test->ok(0, $message);
88             }
89             }
90              
91             sub path_is {
92 6     6 1 2019 my ($router, $path, $expected, $message) = @_;
93              
94 6         23 my $generated_mapping = $router->match($path)->mapping;
95              
96             # the path supplied produces the
97             # same match as the hash supplied
98              
99 6 100       30 unless (Test::Deep::eq_deeply($generated_mapping, $expected)) {
100 1         843 $Test->ok(0, $message);
101 1         378 $Test->diag("... mappings do not match for '$path'\n" .
102             " got: '" . _dump_mapping_info($generated_mapping) . "'\n" .
103             " expected: '" . _dump_mapping_info($expected) . "'");
104             }
105             else {
106 5         20287 $Test->ok(1, $message);
107             }
108             }
109              
110             sub mapping_ok {
111 2     2 1 977 my ($router, $mapping, $message) = @_;
112 2 100       2 if (defined $router->uri_for(%{$mapping})) {
  2         9  
113 1         3 $Test->ok(1, $message);
114             }
115             else {
116 1         4 $Test->ok(0, $message);
117             }
118             }
119              
120             sub mapping_not_ok {
121 2     2 1 616 my ($router, $mapping, $message) = @_;
122 2 100       2 unless (defined $router->uri_for(%{$mapping})) {
  2         12  
123 1         3 $Test->ok(1, $message);
124             }
125             else {
126 1         4 $Test->ok(0, $message);
127             }
128             }
129              
130             sub mapping_is {
131 9     9 1 2044 my ($router, $mapping, $expected, $message) = @_;
132              
133 9         11 my $generated_path = $router->uri_for(%{$mapping});
  9         39  
134              
135             # the path generated from the hash
136             # is the same as the path supplied
137 9 100 66     98 if (
      100        
      33        
      66        
      66        
      66        
138             (defined $generated_path and not defined $expected) or
139             (defined $expected and not defined $generated_path) or
140             (defined $generated_path and defined $expected
141             and $generated_path ne $expected)
142             ) {
143             $_ = defined $_ ? qq{'$_'} : qq{undef}
144 1 100       6 for $generated_path, $expected;
145 1         3 $Test->ok(0, $message);
146 1         414 $Test->diag("... paths do not match\n" .
147             " got: $generated_path\n" .
148             " expected: $expected");
149             }
150             else {
151 8         28 $Test->ok(1, $message);
152             }
153             }
154              
155             ## helper function
156              
157             sub _dump_mapping_info {
158 2     2   3 my ($mapping) = @_;
159 2         2 local $Data::Dumper::Indent = 0;
160 2         11 my $out = Data::Dumper::Dumper($mapping);
161 2         119 $out =~ s/\$VAR\d\s*=\s*//;
162 2         10 return $out;
163             }
164              
165             1;
166              
167             __END__
168              
169             =pod
170              
171             =encoding UTF-8
172              
173             =head1 NAME
174              
175             Test::Path::Router - A testing module for testing routes
176              
177             =head1 VERSION
178              
179             version 0.14_01
180              
181             =head1 SYNOPSIS
182              
183             use Test::More plan => 1;
184             use Test::Path::Router;
185              
186             my $router = Path::Router->new;
187              
188             # ... define some routes
189              
190             path_ok($router, 'admin/remove_user/56', '... this is a valid path');
191              
192             path_is($router,
193             'admin/edit_user/5',
194             {
195             controller => 'admin',
196             action => 'edit_user',
197             id => 5,
198             },
199             '... the path and mapping match');
200              
201             mapping_ok($router, {
202             controller => 'admin',
203             action => 'edit_user',
204             id => 5,
205             }, '... this maps to a valid path');
206              
207             mapping_is($router,
208             {
209             controller => 'admin',
210             action => 'edit_user',
211             id => 5,
212             },
213             'admin/edit_user/5',
214             '... the mapping and path match');
215              
216             routes_ok($router, {
217             'admin' => {
218             controller => 'admin',
219             action => 'index',
220             },
221             'admin/add_user' => {
222             controller => 'admin',
223             action => 'add_user',
224             },
225             'admin/edit_user/5' => {
226             controller => 'admin',
227             action => 'edit_user',
228             id => 5,
229             }
230             },
231             "... our routes are valid");
232              
233             =head1 DESCRIPTION
234              
235             This module helps in testing out your path routes, to make sure
236             they are valid.
237              
238             =head1 EXPORTED FUNCTIONS
239              
240             =over 4
241              
242             =item B<path_ok ($router, $path, ?$message)>
243              
244             =item B<path_not_ok ($router, $path, ?$message)>
245              
246             =item B<path_is ($router, $path, $mapping, ?$message)>
247              
248             =item B<mapping_ok ($router, $mapping, ?$message)>
249              
250             =item B<mapping_not_ok ($router, $mapping, ?$message)>
251              
252             =item B<mapping_is ($router, $mapping, $path, ?$message)>
253              
254             =item B<routes_ok ($router, \%test_routes, ?$message)>
255              
256             This test function will accept a set of C<%test_routes> which
257             will get checked against your C<$router> instance. This will
258             check to be sure that all paths in C<%test_routes> procude
259             the expected mappings, and that all mappings also produce the
260             expected paths. It basically assures you that your paths
261             are roundtrippable, so that you can be confident in them.
262              
263             =back
264              
265             =head1 BUGS
266              
267             All complex software has bugs lurking in it, and this module is no
268             exception. If you find a bug please either email me, or add the bug
269             to cpan-RT.
270              
271             =head1 AUTHOR
272              
273             Stevan Little E<lt>stevan@cpan.orgE<gt>
274              
275             =head1 COPYRIGHT AND LICENSE
276              
277             Copyright 2008-2011 Infinity Interactive, Inc.
278              
279             L<http://www.iinteractive.com>
280              
281             This library is free software; you can redistribute it and/or modify
282             it under the same terms as Perl itself.
283              
284             =head1 AUTHOR
285              
286             Stevan Little <stevan@cpan.org>
287              
288             =head1 COPYRIGHT AND LICENSE
289              
290             This software is copyright (c) 2015 by Infinity Interactive.
291              
292             This is free software; you can redistribute it and/or modify it under
293             the same terms as the Perl 5 programming language system itself.
294              
295             =cut