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