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