Commit | Line | Data |
d91504e3 |
1 | use warnings; |
2 | use strict; |
842180f7 |
3 | |
4 | BEGIN { |
5 | use Test::More; |
6 | eval "use Types::Standard; 1;" || do { |
7 | plan skip_all => "Trouble loading Types::Standard => $@"; |
8 | }; |
6f0b85d2 |
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 | |
a7ab9aa9 |
31 | # Tests using this are skipped pending deeper thought |
6f0b85d2 |
32 | coerce User, |
33 | from ContextLike, |
34 | via { $_->model('User')->find( $_->req->args->[0] ) }; |
842180f7 |
35 | } |
d91504e3 |
36 | |
37 | { |
6f0b85d2 |
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 | |
d91504e3 |
56 | package MyApp::Controller::Root; |
57 | $INC{'MyApp/Controller/Root.pm'} = __FILE__; |
58 | |
59 | use Moose; |
60 | use MooseX::MethodAttributes; |
6f0b85d2 |
61 | use MyApp::Types qw/Tuple Int Str StrMatch UserId User/; |
d91504e3 |
62 | |
63 | extends 'Catalyst::Controller'; |
64 | |
6f0b85d2 |
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 | |
a7ab9aa9 |
71 | # Tests using this are current skipped pending coercion rethink |
6f0b85d2 |
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 | |
6d62355b |
77 | sub an_int :Local Args(Int) { |
78 | my ($self, $c, $int) = @_; |
6d62355b |
79 | $c->res->body('an_int'); |
80 | } |
81 | |
4a0218ca |
82 | sub many_ints :Local Args(ArrayRef[Int]) { |
83 | my ($self, $c, $int) = @_; |
84 | $c->res->body('many_ints'); |
85 | } |
86 | |
842180f7 |
87 | sub tuple :Local Args(Tuple[Str,Int]) { |
6f0b85d2 |
88 | my ($self, $c, $str, $int) = @_; |
842180f7 |
89 | $c->res->body('tuple'); |
90 | } |
91 | |
6f0b85d2 |
92 | sub match :Local Args(StrMatch[qr{\d\d-\d\d-\d\d}]) { |
93 | my ($self, $c, $int) = @_; |
94 | $c->res->body('match'); |
95 | } |
a82c96cf |
96 | |
e5604544 |
97 | sub any_priority :Path('priority_test') Args(1) { $_[1]->res->body('any_priority') } |
842180f7 |
98 | |
b7791bd7 |
99 | sub int_priority :Path('priority_test') Args(Int) { $_[1]->res->body('int_priority') } |
e5604544 |
100 | |
a82c96cf |
101 | sub chain_base :Chained(/) CaptureArgs(1) { } |
102 | |
103 | sub any_priority_chain :Chained(chain_base) PathPart('') Args(1) { $_[1]->res->body('any_priority_chain') } |
104 | |
105 | sub int_priority_chain :Chained(chain_base) PathPart('') Args(Int) { $_[1]->res->body('int_priority_chain') } |
106 | |
480d94b5 |
107 | sub link_any :Chained(chain_base) PathPart('') CaptureArgs(1) { } |
108 | |
109 | sub any_priority_link_any :Chained(link_any) PathPart('') Args(1) { $_[1]->res->body('any_priority_link_any') } |
110 | |
111 | sub int_priority_link_any :Chained(link_any) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link_any') } |
112 | |
a82c96cf |
113 | sub link_int :Chained(chain_base) PathPart('') CaptureArgs(Int) { } |
114 | |
115 | sub any_priority_link :Chained(link_int) PathPart('') Args(1) { $_[1]->res->body('any_priority_link') } |
116 | |
117 | sub int_priority_link :Chained(link_int) PathPart('') Args(Int) { $_[1]->res->body('int_priority_link') } |
118 | |
119 | |
6d62355b |
120 | sub default :Default { |
121 | my ($self, $c, $int) = @_; |
122 | $c->res->body('default'); |
d91504e3 |
123 | } |
124 | |
125 | MyApp::Controller::Root->config(namespace=>''); |
126 | |
127 | package MyApp; |
128 | use Catalyst; |
129 | |
130 | MyApp->setup; |
131 | } |
132 | |
133 | use Catalyst::Test 'MyApp'; |
134 | |
135 | { |
6d62355b |
136 | my $res = request '/an_int/1'; |
137 | is $res->content, 'an_int'; |
138 | } |
139 | |
140 | { |
337a627a |
141 | my $res = request '/an_int/aa'; |
142 | is $res->content, 'default'; |
143 | } |
144 | |
145 | { |
4a0218ca |
146 | my $res = request '/many_ints/1'; |
147 | is $res->content, 'many_ints'; |
148 | } |
149 | |
150 | { |
151 | my $res = request '/many_ints/1/2'; |
152 | is $res->content, 'many_ints'; |
153 | } |
154 | |
155 | { |
156 | my $res = request '/many_ints/1/2/3'; |
157 | is $res->content, 'many_ints'; |
158 | } |
159 | |
160 | { |
161 | my $res = request '/many_ints/1/2/a'; |
162 | is $res->content, 'default'; |
163 | } |
164 | |
e5604544 |
165 | { |
166 | my $res = request '/priority_test/1'; |
167 | is $res->content, 'int_priority'; |
168 | } |
842180f7 |
169 | |
e5604544 |
170 | { |
171 | my $res = request '/priority_test/a'; |
172 | is $res->content, 'any_priority'; |
173 | } |
174 | |
842180f7 |
175 | { |
176 | my $res = request '/tuple/aaa/111'; |
177 | is $res->content, 'tuple'; |
178 | } |
179 | |
180 | { |
181 | my $res = request '/tuple/aaa/aaa'; |
182 | is $res->content, 'default'; |
183 | } |
184 | |
6f0b85d2 |
185 | { |
186 | my $res = request '/match/11-22-33'; |
187 | is $res->content, 'match'; |
188 | } |
81436df9 |
189 | |
6f0b85d2 |
190 | { |
191 | my $res = request '/match/aaa'; |
192 | is $res->content, 'default'; |
193 | } |
194 | |
195 | { |
196 | my $res = request '/user/2'; |
197 | is $res->content, 'name: mary, age: 36'; |
198 | } |
199 | |
200 | { |
201 | my $res = request '/user/20'; |
202 | is $res->content, 'default'; |
203 | } |
204 | |
a7ab9aa9 |
205 | |
206 | SKIP: { |
207 | skip "coercion support needs more thought", 1; |
6f0b85d2 |
208 | my $res = request '/user_object/20'; |
209 | is $res->content, 'default'; |
210 | } |
211 | |
a7ab9aa9 |
212 | SKIP: { |
213 | skip "coercion support needs more thought", 1; |
6f0b85d2 |
214 | my $res = request '/user_object/2'; |
215 | is $res->content, 'name: mary, age: 36'; |
216 | } |
217 | |
a82c96cf |
218 | { |
219 | my $res = request '/chain_base/capture/arg'; |
220 | is $res->content, 'any_priority_chain'; |
221 | } |
222 | |
223 | { |
224 | my $res = request '/chain_base/cap1/100/arg'; |
225 | is $res->content, 'any_priority_link'; |
226 | } |
227 | |
228 | { |
229 | my $res = request '/chain_base/cap1/101/102'; |
230 | is $res->content, 'int_priority_link'; |
231 | } |
232 | |
233 | { |
234 | my $res = request '/chain_base/capture/100'; |
235 | is $res->content, 'int_priority_chain', 'got expected'; |
236 | } |
237 | |
480d94b5 |
238 | { |
239 | my $res = request '/chain_base/cap1/a/arg'; |
240 | is $res->content, 'any_priority_link_any'; |
241 | } |
242 | |
243 | { |
244 | my $res = request '/chain_base/cap1/a/102'; |
245 | is $res->content, 'int_priority_link_any'; |
246 | } |
247 | |
6f0b85d2 |
248 | done_testing; |