basic chaining in place
[catagits/Catalyst-Runtime.git] / t / arg_constraints.t
CommitLineData
d91504e3 1use warnings;
2use strict;
842180f7 3
4BEGIN {
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
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
6d62355b 114 sub default :Default {
115 my ($self, $c, $int) = @_;
116 $c->res->body('default');
d91504e3 117 }
118
119 MyApp::Controller::Root->config(namespace=>'');
120
121 package MyApp;
122 use Catalyst;
123
124 MyApp->setup;
125}
126
127use Catalyst::Test 'MyApp';
128
129{
6d62355b 130 my $res = request '/an_int/1';
131 is $res->content, 'an_int';
132}
133
134{
337a627a 135 my $res = request '/an_int/aa';
136 is $res->content, 'default';
137}
138
139{
4a0218ca 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
e5604544 159{
160 my $res = request '/priority_test/1';
161 is $res->content, 'int_priority';
162}
842180f7 163
e5604544 164{
165 my $res = request '/priority_test/a';
166 is $res->content, 'any_priority';
167}
168
842180f7 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
6f0b85d2 179{
180 my $res = request '/match/11-22-33';
181 is $res->content, 'match';
182}
81436df9 183
6f0b85d2 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
a7ab9aa9 199
200SKIP: {
201 skip "coercion support needs more thought", 1;
6f0b85d2 202 my $res = request '/user_object/20';
203 is $res->content, 'default';
204}
205
a7ab9aa9 206SKIP: {
207 skip "coercion support needs more thought", 1;
6f0b85d2 208 my $res = request '/user_object/2';
209 is $res->content, 'name: mary, age: 36';
210}
211
a82c96cf 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
6f0b85d2 232done_testing;