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) = @_; |
842180f7 |
87 | $c->res->body('tuple'); |
88 | } |
89 | |
6f0b85d2 |
90 | sub match :Local Args(StrMatch[qr{\d\d-\d\d-\d\d}]) { |
91 | my ($self, $c, $int) = @_; |
92 | $c->res->body('match'); |
93 | } |
e5604544 |
94 | sub any_priority :Path('priority_test') Args(1) { $_[1]->res->body('any_priority') } |
842180f7 |
95 | |
b7791bd7 |
96 | sub int_priority :Path('priority_test') Args(Int) { $_[1]->res->body('int_priority') } |
e5604544 |
97 | |
6d62355b |
98 | sub default :Default { |
99 | my ($self, $c, $int) = @_; |
100 | $c->res->body('default'); |
d91504e3 |
101 | } |
102 | |
103 | MyApp::Controller::Root->config(namespace=>''); |
104 | |
105 | package MyApp; |
106 | use Catalyst; |
107 | |
108 | MyApp->setup; |
109 | } |
110 | |
111 | use Catalyst::Test 'MyApp'; |
112 | |
113 | { |
6d62355b |
114 | my $res = request '/an_int/1'; |
115 | is $res->content, 'an_int'; |
116 | } |
117 | |
118 | { |
337a627a |
119 | my $res = request '/an_int/aa'; |
120 | is $res->content, 'default'; |
121 | } |
122 | |
123 | { |
4a0218ca |
124 | my $res = request '/many_ints/1'; |
125 | is $res->content, 'many_ints'; |
126 | } |
127 | |
128 | { |
129 | my $res = request '/many_ints/1/2'; |
130 | is $res->content, 'many_ints'; |
131 | } |
132 | |
133 | { |
134 | my $res = request '/many_ints/1/2/3'; |
135 | is $res->content, 'many_ints'; |
136 | } |
137 | |
138 | { |
139 | my $res = request '/many_ints/1/2/a'; |
140 | is $res->content, 'default'; |
141 | } |
142 | |
e5604544 |
143 | { |
144 | my $res = request '/priority_test/1'; |
145 | is $res->content, 'int_priority'; |
146 | } |
842180f7 |
147 | |
e5604544 |
148 | { |
149 | my $res = request '/priority_test/a'; |
150 | is $res->content, 'any_priority'; |
151 | } |
152 | |
842180f7 |
153 | { |
154 | my $res = request '/tuple/aaa/111'; |
155 | is $res->content, 'tuple'; |
156 | } |
157 | |
158 | { |
159 | my $res = request '/tuple/aaa/aaa'; |
160 | is $res->content, 'default'; |
161 | } |
162 | |
6f0b85d2 |
163 | { |
164 | my $res = request '/match/11-22-33'; |
165 | is $res->content, 'match'; |
166 | } |
81436df9 |
167 | |
6f0b85d2 |
168 | { |
169 | my $res = request '/match/aaa'; |
170 | is $res->content, 'default'; |
171 | } |
172 | |
173 | { |
174 | my $res = request '/user/2'; |
175 | is $res->content, 'name: mary, age: 36'; |
176 | } |
177 | |
178 | { |
179 | my $res = request '/user/20'; |
180 | is $res->content, 'default'; |
181 | } |
182 | |
183 | { |
184 | my $res = request '/user_object/20'; |
185 | is $res->content, 'default'; |
186 | } |
187 | |
188 | { |
189 | my $res = request '/user_object/2'; |
190 | is $res->content, 'name: mary, age: 36'; |
191 | } |
192 | |
193 | done_testing; |