6 eval "use Types::Standard; 1;" || do {
7 plan skip_all => "Trouble loading Types::Standard => $@";
11 $INC{'MyApp/Types.pm'} = __FILE__;
17 use Types::Standard -types;
20 -declare => qw( UserId User ContextLike );
22 extends "Types::Standard";
24 class_type User, { class => "MyApp::Model::User::user" };
25 duck_type ContextLike, [qw/model/];
31 # Tests using this are skipped pending deeper thought
34 via { $_->model('User')->find( $_->req->args->[0] ) };
38 package MyApp::Model::User;
39 $INC{'MyApp/Model/User.pm'} = __FILE__;
41 use base 'Catalyst::Model';
44 1 => { name => 'john', age => 46 },
45 2 => { name => 'mary', age => 36 },
46 3 => { name => 'ian', age => 25 },
47 4 => { name => 'visha', age => 18 },
52 my $user = $users{$id} || return;
53 return bless $user, "MyApp::Model::User::user";
56 package MyApp::Controller::Root;
57 $INC{'MyApp/Controller/Root.pm'} = __FILE__;
60 use MooseX::MethodAttributes;
61 use MyApp::Types qw/Tuple Int Str StrMatch ArrayRef UserId User/;
63 extends 'Catalyst::Controller';
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}");
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}");
77 sub an_int :Local Args(Int) {
78 my ($self, $c, $int) = @_;
79 $c->res->body('an_int');
82 sub two_ints :Local Args(Int,Int) {
83 my ($self, $c, $int) = @_;
84 $c->res->body('two_ints');
87 sub many_ints :Local Args(ArrayRef[Int]) {
88 my ($self, $c, $int) = @_;
89 $c->res->body('many_ints');
92 sub tuple :Local Args(Tuple[Str,Int]) {
93 my ($self, $c, $str, $int) = @_;
94 $c->res->body('tuple');
97 sub match :Local Args(StrMatch[qr{\d\d-\d\d-\d\d}]) {
98 my ($self, $c, $int) = @_;
99 $c->res->body('match');
102 sub any_priority :Path('priority_test') Args(1) { $_[1]->res->body('any_priority') }
104 sub int_priority :Path('priority_test') Args(Int) { $_[1]->res->body('int_priority') }
106 sub chain_base :Chained(/) CaptureArgs(1) { }
108 sub any_priority_chain :GET Chained(chain_base) PathPart('') Args(1) { $_[1]->res->body('any_priority_chain') }
110 sub int_priority_chain :Chained(chain_base) PathPart('') Args(Int) { $_[1]->res->body('int_priority_chain') }
112 sub link_any :Chained(chain_base) PathPart('') CaptureArgs(1) { }
114 sub any_priority_link_any :Chained(link_any) PathPart('') Args(1) { $_[1]->res->body('any_priority_link_any') }
116 sub int_priority_link_any :Chained(link_any) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link_any') }
118 sub link_int :Chained(chain_base) PathPart('') CaptureArgs(Int) { }
120 sub any_priority_link :Chained(link_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link') }
122 sub int_priority_link :Chained(link_int) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link') }
124 sub link_int_int :Chained(chain_base) PathPart('') CaptureArgs(Int,Int) { }
126 sub any_priority_link2 :Chained(link_int_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link2') }
128 sub int_priority_link2 :Chained(link_int_int) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link2') }
130 sub link_tuple :Chained(chain_base) PathPart('') CaptureArgs(Tuple[Int,Int,Int]) { }
132 sub any_priority_link3 :Chained(link_tuple) PathPart('') Args(1) { $_[1]->res->body('any_priority_link3') }
134 sub int_priority_link3 :Chained(link_tuple) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link3') }
136 sub link2_int :Chained(link_tuple) PathPart('') CaptureArgs(UserId) { }
138 sub finally2 :GET Chained(link2_int) PathPart('') Args { $_[1]->res->body('finally2') }
139 sub finally :GET Chained(link2_int) PathPart('') Args(Int) { $_[1]->res->body('finally') }
141 sub default :Default {
142 my ($self, $c, $int) = @_;
143 $c->res->body('default');
146 MyApp::Controller::Root->config(namespace=>'');
154 use Catalyst::Test 'MyApp';
157 my $res = request '/an_int/1';
158 is $res->content, 'an_int';
162 my $res = request '/an_int/aa';
163 is $res->content, 'default';
167 my $res = request '/many_ints/1';
168 is $res->content, 'many_ints';
172 my $res = request '/many_ints/1/2';
173 is $res->content, 'many_ints';
177 my $res = request '/many_ints/1/2/3';
178 is $res->content, 'many_ints';
182 my $res = request '/priority_test/1';
183 is $res->content, 'int_priority';
187 my $res = request '/priority_test/a';
188 is $res->content, 'any_priority';
192 my $res = request '/match/11-22-33';
193 is $res->content, 'match';
197 my $res = request '/match/aaa';
198 is $res->content, 'default';
202 my $res = request '/user/2';
203 is $res->content, 'name: mary, age: 36';
207 my $res = request '/user/20';
208 is $res->content, 'default';
213 skip "coercion support needs more thought", 1;
214 my $res = request '/user_object/20';
215 is $res->content, 'default';
219 skip "coercion support needs more thought", 1;
220 my $res = request '/user_object/2';
221 is $res->content, 'name: mary, age: 36';
225 my $res = request '/chain_base/capture/arg';
226 is $res->content, 'any_priority_chain';
230 my $res = request '/chain_base/cap1/100/arg';
231 is $res->content, 'any_priority_link';
235 my $res = request '/chain_base/cap1/101/102';
236 is $res->content, 'int_priority_link';
240 my $res = request '/chain_base/capture/100';
241 is $res->content, 'int_priority_chain', 'got expected';
245 my $res = request '/chain_base/cap1/a/arg';
246 is $res->content, 'any_priority_link_any';
250 my $res = request '/chain_base/cap1/a/102';
251 is $res->content, 'int_priority_link_any';
255 my $res = request '/two_ints/1/2';
256 is $res->content, 'two_ints';
260 my $res = request '/two_ints/aa/111';
261 is $res->content, 'default';
265 my $res = request '/tuple/aaa/aaa';
266 is $res->content, 'default';
270 my $res = request '/tuple/aaa/111';
271 is $res->content, 'tuple';
275 my $res = request '/many_ints/1/2/a';
276 is $res->content, 'default';
280 my $res = request '/chain_base/100/100/100/100';
281 is $res->content, 'int_priority_link2';
285 my $res = request '/chain_base/100/ss/100/100';
286 is $res->content, 'default';
290 my $res = request '/chain_base/100/100/100/100/100';
291 is $res->content, 'int_priority_link3';
295 my $res = request '/chain_base/100/ss/100/100/100';
296 is $res->content, 'default';
300 my $res = request '/chain_base/1/2/3/3/3/6';
301 is $res->content, 'finally';
305 my $res = request '/chain_base/1/2/3/3/3/a';
306 is $res->content, 'finally2';
310 my $res = request '/chain_base/1/2/3/3/3/6/7/8/9';
311 is $res->content, 'finally2';
316 | /chain_base/*/*/*/*/*/* | /chain_base (1) |
317 | | -> /link_tuple (Tuple[Int,Int,Int]) |
318 | | -> /link2_int (UserId) |
319 | | => GET /finally (Int)
324 my ($res, $c) = ctx_request '/';
325 ok my $url1 = $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,5],6);
328 ok my $url2 = $c->uri_for($c->controller('Root')->action_for('finally'), [1,2,3,4,5,6]);