rollback some stuff to reset my brain a bit
[gitmo/MooseX-Types-Structured.git] / t / optional.t
CommitLineData
78f55946 1BEGIN {
2 use strict;
3 use warnings;
4 use Test::More tests=>16;
5 use Test::Exception;
6 use Data::Dump qw/dump/;
7
8 use_ok 'Moose::Util::TypeConstraints';
9}
10
11Moose::Util::TypeConstraints::register_type_constraint(
12 Moose::Meta::TypeConstraint::Parameterizable->new(
13 name => 'Optional',
14 package_defined_in => __PACKAGE__,
15 parent => find_type_constraint('Item'),
16 constraint => sub { 1 },
17 constraint_generator => sub {
18 my $type_parameter = shift;
19 my $check = $type_parameter->_compiled_type_constraint;
20 return sub {
21 use Data::Dump qw/dump/;
22 warn dump @_;
23 return 1 if not(defined($_)) || $check->($_);
24 return;
25 }
26 }
27 )
28);
29
30ok Moose::Util::TypeConstraints::find_type_constraint('Optional')
31 => 'Found the Optional Type';
32
33{
34 package Test::MooseX::Types::Optional;
35 use Moose;
36
37 has 'Maybe_Int' => (is=>'rw', isa=>'Maybe[Int]');
38 has 'Maybe_ArrayRef' => (is=>'rw', isa=>'Maybe[ArrayRef]');
39 has 'Maybe_HashRef' => (is=>'rw', isa=>'Maybe[HashRef]');
40 has 'Maybe_ArrayRefInt' => (is=>'rw', isa=>'Maybe[ArrayRef[Int]]');
41 has 'Maybe_HashRefInt' => (is=>'rw', isa=>'Maybe[HashRef[Int]]');
42}
43
44ok my $obj = Test::MooseX::Types::Optional->new
45 => 'Create good test object';
46
47## Maybe[Int]
48
49ok my $Maybe_Int = Moose::Util::TypeConstraints::find_or_parse_type_constraint('Maybe[Int]')
50 => 'made TC Maybe[Int]';
51
52ok $Maybe_Int->check(1)
53 => 'passed (1)';
54
55 ok $obj->Maybe_Int(1)
56 => 'assigned (1)';
57
58ok $Maybe_Int->check()
59 => 'passed ()';
60
61 ok $obj->Maybe_Int()
62 => 'assigned ()';
63
64ok $Maybe_Int->check(0)
65 => 'passed (0)';
66
67 ok defined $obj->Maybe_Int(0)
68 => 'assigned (0)';
69
70ok $Maybe_Int->check(undef)
71 => 'passed (undef)';
72
73 ok sub {$obj->Maybe_Int(undef); 1}->()
74 => 'assigned (undef)';
75
76ok !$Maybe_Int->check("")
77 => 'failed ("")';
78
79 throws_ok sub { $obj->Maybe_Int("") },
80 qr/Attribute \(Maybe_Int\) does not pass the type constraint/
81 => 'failed assigned ("")';
82
83ok !$Maybe_Int->check("a")
84 => 'failed ("a")';
85
86 throws_ok sub { $obj->Maybe_Int("a") },
87 qr/Attribute \(Maybe_Int\) does not pass the type constraint/
88 => 'failed assigned ("a")';
89
90__END__
91
92
93ok $obj->Maybe_Int(undef)
94 => 'passed 1';
95
96ok $obj->Maybe_Int();
97
98ok $obj->Maybe_Int('')
99 => 'passed 1';
100
101ok $obj->Maybe_Int('a')
102 => 'passed 1';
103
104
105
106
107ok $obj->tuple([1,'hello',3])
108 => "[1,'hello',3] properly suceeds";
109
110throws_ok sub {
111 $obj->tuple([1,2,'world']);
112}, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
113
114throws_ok sub {
115 $obj->tuple(['hello1',2,3]);
116}, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
117
118throws_ok sub {
119 $obj->tuple(['hello2',2,'world']);
120}, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
121
122
123## Test tuple_with_parameterized (Tuple[Int,Str,Int,ArrayRef[Int]])
124
125ok $obj->tuple_with_parameterized([1,'hello',3,[1,2,3]])
126 => "[1,'hello',3,[1,2,3]] properly suceeds";
127
128throws_ok sub {
129 $obj->tuple_with_parameterized([1,2,'world']);
130}, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
131
132throws_ok sub {
133 $obj->tuple_with_parameterized(['hello1',2,3]);
134}, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
135
136throws_ok sub {
137 $obj->tuple_with_parameterized(['hello2',2,'world']);
138}, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
139
140throws_ok sub {
141 $obj->tuple_with_parameterized([1,'hello',3,[1,2,'world']]);
142}, qr/Validation failed for 'ArrayRef\[Int\]'/ => "[1,'hello',3,[1,2,'world']] properly fails";
143
144
145## Test tuple_with_optional (Tuple[Int,Str,Int,Optional[Int,Int]])
146
147ok $obj->tuple_with_optional([1,'hello',3])
148 => "[1,'hello',3] properly suceeds";
149
150ok $obj->tuple_with_optional([1,'hello',3,1])
151 => "[1,'hello',3,1] properly suceeds";
152
153ok $obj->tuple_with_optional([1,'hello',3,4])
154 => "[1,'hello',3,4] properly suceeds";
155
156ok $obj->tuple_with_optional([1,'hello',3,4,5])
157 => "[1,'hello',3,4,5] properly suceeds";
158
159throws_ok sub {
160 $obj->tuple_with_optional([1,'hello',3,4,5,6]);
161}, qr/Too Many arguments for the available type constraints/ => "[1,'hello',3,4,5,6] properly fails";
162
163throws_ok sub {
164 $obj->tuple_with_optional([1,2,'world']);
165}, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
166
167throws_ok sub {
168 $obj->tuple_with_optional(['hello1',2,3]);
169}, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
170
171throws_ok sub {
172 $obj->tuple_with_optional(['hello2',2,'world']);
173}, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
174
175## tuple_with_union Tuple[Int,Str,Int|Object,Optional[Int|Object,Int]]
176
177SKIP: {
178
179 skip "Unions not supported for string parsed type constraints" => 8;
180
181 ok $obj->tuple_with_union([1,'hello',3])
182 => "[1,'hello',3] properly suceeds";
183
184 ok $obj->tuple_with_union([1,'hello',3,1])
185 => "[1,'hello',3,1] properly suceeds";
186
187 ok $obj->tuple_with_union([1,'hello',3,4])
188 => "[1,'hello',3,4] properly suceeds";
189
190 ok $obj->tuple_with_union([1,'hello',3,4,5])
191 => "[1,'hello',3,4,5] properly suceeds";
192
193 throws_ok sub {
194 $obj->tuple_with_union([1,'hello',3,4,5,6]);
195 }, qr/Too Many arguments for the available type constraints/ => "[1,'hello',3,4,5,6] properly fails";
196
197 throws_ok sub {
198 $obj->tuple_with_union([1,2,'world']);
199 }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
200
201 throws_ok sub {
202 $obj->tuple_with_union(['hello1',2,3]);
203 }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
204
205 throws_ok sub {
206 $obj->tuple_with_union(['hello2',2,'world']);
207 }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
208}
209