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 | |
31 | coerce User, |
32 | from ContextLike, |
33 | via { $_->model('User')->find( $_->req->args->[0] ) }; |
842180f7 |
34 | } |
d91504e3 |
35 | |
36 | { |
6f0b85d2 |
37 | package MyApp::Model::User; |
38 | $INC{'MyApp/Model/User.pm'} = __FILE__; |
39 | |
40 | use base 'Catalyst::Model'; |
41 | |
42 | our %users = ( |
43 | 1 => { name => 'john', age => 46 }, |
44 | 2 => { name => 'mary', age => 36 }, |
45 | 3 => { name => 'ian', age => 25 }, |
46 | 4 => { name => 'visha', age => 18 }, |
47 | ); |
48 | |
49 | sub find { |
50 | my ($self, $id) = @_; |
51 | my $user = $users{$id} || return; |
52 | return bless $user, "MyApp::Model::User::user"; |
53 | } |
54 | |
d91504e3 |
55 | package MyApp::Controller::Root; |
56 | $INC{'MyApp/Controller/Root.pm'} = __FILE__; |
57 | |
58 | use Moose; |
59 | use MooseX::MethodAttributes; |
6f0b85d2 |
60 | use MyApp::Types qw/Tuple Int Str StrMatch UserId User/; |
d91504e3 |
61 | |
62 | extends 'Catalyst::Controller'; |
63 | |
6f0b85d2 |
64 | sub user :Local Args(UserId) { |
65 | my ($self, $c, $int) = @_; |
66 | my $user = $c->model("User")->find($int); |
67 | $c->res->body("name: $user->{name}, age: $user->{age}"); |
68 | } |
69 | |
70 | sub user_object :Local Args(User) Coerce(1) { |
71 | my ($self, $c, $user) = @_; |
72 | $c->res->body("name: $user->{name}, age: $user->{age}"); |
73 | } |
74 | |
6d62355b |
75 | sub an_int :Local Args(Int) { |
76 | my ($self, $c, $int) = @_; |
6d62355b |
77 | $c->res->body('an_int'); |
78 | } |
79 | |
4a0218ca |
80 | sub many_ints :Local Args(ArrayRef[Int]) { |
81 | my ($self, $c, $int) = @_; |
82 | $c->res->body('many_ints'); |
83 | } |
84 | |
842180f7 |
85 | sub tuple :Local Args(Tuple[Str,Int]) { |
6f0b85d2 |
86 | my ($self, $c, $str, $int) = @_; |
87 | warn "$str $int"; |
842180f7 |
88 | $c->res->body('tuple'); |
89 | } |
90 | |
6f0b85d2 |
91 | sub match :Local Args(StrMatch[qr{\d\d-\d\d-\d\d}]) { |
92 | my ($self, $c, $int) = @_; |
93 | $c->res->body('match'); |
94 | } |
e5604544 |
95 | sub any_priority :Path('priority_test') Args(1) { $_[1]->res->body('any_priority') } |
842180f7 |
96 | |
b7791bd7 |
97 | sub int_priority :Path('priority_test') Args(Int) { $_[1]->res->body('int_priority') } |
e5604544 |
98 | |
6d62355b |
99 | sub default :Default { |
100 | my ($self, $c, $int) = @_; |
101 | $c->res->body('default'); |
d91504e3 |
102 | } |
103 | |
104 | MyApp::Controller::Root->config(namespace=>''); |
105 | |
106 | package MyApp; |
107 | use Catalyst; |
108 | |
109 | MyApp->setup; |
110 | } |
111 | |
112 | use Catalyst::Test 'MyApp'; |
113 | |
114 | { |
6d62355b |
115 | my $res = request '/an_int/1'; |
116 | is $res->content, 'an_int'; |
117 | } |
118 | |
119 | { |
337a627a |
120 | my $res = request '/an_int/aa'; |
121 | is $res->content, 'default'; |
122 | } |
123 | |
124 | { |
4a0218ca |
125 | my $res = request '/many_ints/1'; |
126 | is $res->content, 'many_ints'; |
127 | } |
128 | |
129 | { |
130 | my $res = request '/many_ints/1/2'; |
131 | is $res->content, 'many_ints'; |
132 | } |
133 | |
134 | { |
135 | my $res = request '/many_ints/1/2/3'; |
136 | is $res->content, 'many_ints'; |
137 | } |
138 | |
139 | { |
140 | my $res = request '/many_ints/1/2/a'; |
141 | is $res->content, 'default'; |
142 | } |
143 | |
e5604544 |
144 | { |
145 | my $res = request '/priority_test/1'; |
146 | is $res->content, 'int_priority'; |
147 | } |
842180f7 |
148 | |
e5604544 |
149 | { |
150 | my $res = request '/priority_test/a'; |
151 | is $res->content, 'any_priority'; |
152 | } |
153 | |
842180f7 |
154 | { |
155 | my $res = request '/tuple/aaa/111'; |
156 | is $res->content, 'tuple'; |
157 | } |
158 | |
159 | { |
160 | my $res = request '/tuple/aaa/aaa'; |
161 | is $res->content, 'default'; |
162 | } |
163 | |
6f0b85d2 |
164 | { |
165 | my $res = request '/match/11-22-33'; |
166 | is $res->content, 'match'; |
167 | } |
81436df9 |
168 | |
6f0b85d2 |
169 | { |
170 | my $res = request '/match/aaa'; |
171 | is $res->content, 'default'; |
172 | } |
173 | |
174 | { |
175 | my $res = request '/user/2'; |
176 | is $res->content, 'name: mary, age: 36'; |
177 | } |
178 | |
179 | { |
180 | my $res = request '/user/20'; |
181 | is $res->content, 'default'; |
182 | } |
183 | |
184 | { |
185 | my $res = request '/user_object/20'; |
186 | is $res->content, 'default'; |
187 | } |
188 | |
189 | { |
190 | my $res = request '/user_object/2'; |
191 | is $res->content, 'name: mary, age: 36'; |
192 | } |
193 | |
194 | done_testing; |