cfd4f0983be48644aa789dd1c1587ea97eb0f83a
[catagits/Catalyst-Runtime.git] / t / arg_constraints.t
1 use warnings;
2 use strict;
3
4 BEGIN {
5   use Test::More;
6   eval "use Types::Standard; 1;" || do {
7     plan skip_all => "Trouble loading Types::Standard => $@";
8   };
9
10   package MyApp::Types;
11   $INC{'MyApp/Types.pm'} = __FILE__;
12
13   use strict;
14   use warnings;
15  
16   use Type::Utils -all;
17   use Types::Standard -types;
18   use Type::Library
19    -base,
20    -declare => qw( UserId User ContextLike );
21
22   extends "Types::Standard"; 
23
24   class_type User, { class => "MyApp::Model::User::user" };
25   duck_type ContextLike, [qw/model/];
26
27   declare UserId,
28    as Int,
29    where { $_ < 5 };
30
31   # Tests using this are skipped pending deeper thought
32   coerce User,
33    from ContextLike,
34      via { $_->model('User')->find( $_->req->args->[0] ) };
35 }
36
37 {
38   package MyApp::Model::User;
39   $INC{'MyApp/Model/User.pm'} = __FILE__;
40
41   use base 'Catalyst::Model';
42
43   our %users = (
44     1 => { name => 'john', age => 46 },
45     2 => { name => 'mary', age => 36 },
46     3 => { name => 'ian', age => 25 },
47     4 => { name => 'visha', age => 18 },
48   );
49
50   sub find {
51     my ($self, $id) = @_;
52     my $user = $users{$id} || return;
53     return bless $user, "MyApp::Model::User::user";
54   }
55
56   package MyApp::Controller::Root;
57   $INC{'MyApp/Controller/Root.pm'} = __FILE__;
58
59   use Moose;
60   use MooseX::MethodAttributes;
61   use MyApp::Types qw/Tuple Int Str StrMatch ArrayRef UserId User/;
62
63   extends 'Catalyst::Controller';
64
65   sub user :Local Args(UserId) {
66     my ($self, $c, $int) = @_;
67     my $user = $c->model("User")->find($int);
68     $c->res->body("name: $user->{name}, age: $user->{age}");
69   }
70
71   # Tests using this are current skipped pending coercion rethink
72   sub user_object :Local Args(User) Coerce(1) {
73     my ($self, $c, $user) = @_;
74     $c->res->body("name: $user->{name}, age: $user->{age}");
75   }
76
77   sub an_int :Local Args(Int) {
78     my ($self, $c, $int) = @_;
79     $c->res->body('an_int');
80   }
81
82   sub two_ints :Local Args(Int,Int) {
83     my ($self, $c, $int) = @_;
84     $c->res->body('two_ints');
85   }
86
87   sub many_ints :Local Args(ArrayRef[Int]) {
88     my ($self, $c, $int) = @_;
89     $c->res->body('many_ints');
90   }
91
92   sub tuple :Local Args(Tuple[Str,Int]) {
93     my ($self, $c, $str, $int) = @_;
94     $c->res->body('tuple');
95   }
96
97   sub match :Local Args(StrMatch[qr{\d\d-\d\d-\d\d}]) {
98     my ($self, $c, $int) = @_;
99     $c->res->body('match');
100   }
101
102   sub any_priority :Path('priority_test') Args(1) { $_[1]->res->body('any_priority') }
103
104   sub int_priority :Path('priority_test') Args(Int) { $_[1]->res->body('int_priority') }
105
106   sub chain_base :Chained(/) CaptureArgs(1) { }
107
108     sub any_priority_chain :Chained(chain_base) PathPart('') Args(1) { $_[1]->res->body('any_priority_chain') }
109
110     sub int_priority_chain :Chained(chain_base) PathPart('') Args(Int) { $_[1]->res->body('int_priority_chain') }
111
112     sub link_any :Chained(chain_base) PathPart('') CaptureArgs(1) { }
113
114       sub any_priority_link_any :Chained(link_any) PathPart('') Args(1) { $_[1]->res->body('any_priority_link_any') }
115
116       sub int_priority_link_any :Chained(link_any) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link_any') }
117     
118     sub link_int :Chained(chain_base) PathPart('') CaptureArgs(Int) { }
119
120       sub any_priority_link :Chained(link_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link') }
121
122       sub int_priority_link :Chained(link_int) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link') }
123
124     sub link_int_int :Chained(chain_base) PathPart('') CaptureArgs(Int,Int) { }
125
126       sub any_priority_link2 :Chained(link_int_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link2') }
127
128       sub int_priority_link2 :Chained(link_int_int) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link2') }
129
130     sub link_tuple :Chained(chain_base) PathPart('') CaptureArgs(Tuple[Int,Int,Int]) { }
131
132       sub any_priority_link3 :Chained(link_tuple) PathPart('') Args(1) { $_[1]->res->body('any_priority_link3') }
133
134       sub int_priority_link3 :Chained(link_tuple) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link3') }
135
136       sub link2_int :Chained(link_tuple) PathPart('') CaptureArgs(UserId) { }
137
138         sub finally :Chained(link2_int) PathPart('') Args(Int) { $_[1]->res->body('finally') }
139
140   sub default :Default {
141     my ($self, $c, $int) = @_;
142     $c->res->body('default');
143   }
144
145   MyApp::Controller::Root->config(namespace=>'');
146
147   package MyApp;
148   use Catalyst;
149
150   MyApp->setup;
151 }
152
153 use Catalyst::Test 'MyApp';
154
155 {
156   my $res = request '/an_int/1';
157   is $res->content, 'an_int';
158 }
159
160 {
161   my $res = request '/an_int/aa';
162   is $res->content, 'default';
163 }
164
165 {
166   my $res = request '/many_ints/1';
167   is $res->content, 'many_ints';
168 }
169
170 {
171   my $res = request '/many_ints/1/2';
172   is $res->content, 'many_ints';
173 }
174
175 {
176   my $res = request '/many_ints/1/2/3';
177   is $res->content, 'many_ints';
178 }
179
180 {
181   my $res = request '/priority_test/1';
182   is $res->content, 'int_priority';
183 }
184
185 {
186   my $res = request '/priority_test/a';
187   is $res->content, 'any_priority';
188 }
189
190 {
191   my $res = request '/match/11-22-33';
192   is $res->content, 'match';
193 }
194
195 {
196   my $res = request '/match/aaa';
197   is $res->content, 'default';
198 }
199
200 {
201   my $res = request '/user/2';
202   is $res->content, 'name: mary, age: 36';
203 }
204
205 {
206   my $res = request '/user/20';
207   is $res->content, 'default';
208 }
209
210
211 SKIP: {
212   skip "coercion support needs more thought", 1;
213   my $res = request '/user_object/20';
214   is $res->content, 'default';
215 }
216
217 SKIP: {
218   skip "coercion support needs more thought", 1;
219   my $res = request '/user_object/2';
220   is $res->content, 'name: mary, age: 36';
221 }
222
223 {
224   my $res = request '/chain_base/capture/arg';
225   is $res->content, 'any_priority_chain';
226 }
227
228 {
229   my $res = request '/chain_base/cap1/100/arg';
230   is $res->content, 'any_priority_link';
231 }
232
233 {
234   my $res = request '/chain_base/cap1/101/102';
235   is $res->content, 'int_priority_link';
236 }
237
238 {
239   my $res = request '/chain_base/capture/100';
240   is $res->content, 'int_priority_chain', 'got expected';
241 }
242
243 {
244   my $res = request '/chain_base/cap1/a/arg';
245   is $res->content, 'any_priority_link_any';
246 }
247
248 {
249   my $res = request '/chain_base/cap1/a/102';
250   is $res->content, 'int_priority_link_any';
251 }
252
253 {
254   my $res = request '/two_ints/1/2';
255   is $res->content, 'two_ints';
256 }
257
258 {
259   my $res = request '/two_ints/aa/111';
260   is $res->content, 'default';
261 }
262
263 {
264   my $res = request '/tuple/aaa/aaa';
265   is $res->content, 'default';
266 }
267
268 {
269   my $res = request '/tuple/aaa/111';
270   is $res->content, 'tuple';
271 }
272
273 {
274   my $res = request '/many_ints/1/2/a';
275   is $res->content, 'default';
276 }
277
278 {
279   my $res = request '/chain_base/100/100/100/100';
280   is $res->content, 'int_priority_link2';
281 }
282
283 {
284   my $res = request '/chain_base/100/ss/100/100';
285   is $res->content, 'default';
286 }
287
288 {
289   my $res = request '/chain_base/100/100/100/100/100';
290   is $res->content, 'int_priority_link3';
291 }
292
293 {
294   my $res = request '/chain_base/100/ss/100/100/100';
295   is $res->content, 'default';
296 }
297
298 =over
299
300 | /chain_base/*/*/*/*/*/*         | /chain_base (1)                    |
301 |                                 | -> /link_tuple (3)                 |
302 |                                 | -> /link2_int (1)                  |
303 |                                 | => /finally (missing...)           |
304
305 =cut
306
307 {
308   # URI testing
309   my ($res, $c) = ctx_request '/';
310   ok my $url1 = $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,5],6);
311   warn $url1;
312
313   ok my $url2 = $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,5,6]);
314   warn $url2;
315 }
316
317 done_testing;