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