4 use Test::More tests=>47;
9 package Test::MooseX::Meta::TypeConstraint::Structured;
12 use MooseX::Types::Structured qw(Tuple Dict Optional);
13 use Moose::Util::TypeConstraints;
19 has 'tuple' => (is=>'rw', isa=>Tuple['Int', 'Str', 'MyString']);
20 has 'dict' => (is=>'rw', isa=>Dict[name=>'Str', age=>'Int']);
21 has 'dict_with_maybe' => (is=>'rw', isa=>Dict[name=>'Str', age=>'Maybe[Int]']);
22 has 'tuple_with_param' => (is=>'rw', isa=>Tuple['Int', 'Str', 'ArrayRef[Int]']);
23 has 'tuple_with_maybe' => (is=>'rw', isa=>Tuple['Int', 'Str', 'Maybe[Int]']);
24 has 'dict_with_tuple' => (is=>'rw', isa=>Dict[key1=>'Str', key2=>Tuple['Int','Str']]);
25 has 'optional_tuple' => (is=>'rw', isa=>Tuple['Int', 'Int', Optional['Int']] );
26 has 'optional_dict' => (is=>'rw', isa=>Dict[key1=>'Int', Optional[key2=>'Int']] );
27 has 'dict_with_tuple_with_union' => (is=>'rw', isa=>Dict[key1=>'Str|Object', key2=>Tuple['Int','Str|Object']] );
32 ## First ArrayRef Arg is the required type constraints for the top
37 ## The third required element is a Dict type constraint, which
38 ## itself has two required keys and a third optional key.
39 Dict[name=>'Str',age=>'Int', Optional[visits=>'Int']],
42 ## This Tuple has one required type constraint and two optional.
43 Tuple['Int', Optional['Int','HashRef']],
49 ## Instantiate a new test object
51 ok my $record = Test::MooseX::Meta::TypeConstraint::Structured->new
52 => 'Instantiated new Record test class.';
54 isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured'
55 => 'Created correct object type.';
60 $record->crazy([1,'hello.abc.world', {name=>'John', age=>39}]);
61 } => 'Set crazy attribute with no optionals used';
63 is_deeply $record->crazy, [1, 'hello.abc.world', {name=>'John', age=>39}]
64 => 'correct values for crazy attributes no optionals';
67 $record->crazy([1,'hello.abc.world', {name=>'John', age=>39, visits=>10},10, [1,2,{key=>'value'}]]);
68 } => 'Set crazy attribute with all optionals used';
70 is_deeply $record->crazy, [1,'hello.abc.world', {name=>'John', age=>39, visits=>10},10, [1,2,{key=>'value'}]]
71 => 'correct values for crazy attributes all optionals';
74 $record->crazy([1,'hello.abc.world', {name=>'John', age=>39},10, [1,2]]);
75 } => 'Set crazy attribute with some optionals used';
78 $record->crazy([1,'hello', 'test.xxx.test']);
79 }, qr/Validation failed for 'MyString'/
80 => 'Properly failed for bad value in crazy attribute 01';
83 $record->crazy([1,'hello.abc.world', {notname=>'John', notage=>39}]);
84 }, qr/Validation failed for 'Str'/
85 => 'Properly failed for bad value in crazy attribute 02';
87 ## Test Tuple type constraint
90 $record->tuple([1,'hello', 'test.abc.test']);
91 } => 'Set tuple attribute without error';
93 is $record->tuple->[0], 1
94 => 'correct set the tuple attribute index 0';
96 is $record->tuple->[1], 'hello'
97 => 'correct set the tuple attribute index 1';
99 is $record->tuple->[2], 'test.abc.test'
100 => 'correct set the tuple attribute index 2';
103 $record->tuple([1,'hello', 'test.xxx.test']);
104 }, qr/Validation failed for 'MyString'/
105 => 'Properly failed for bad value in custom type constraint';
108 $record->tuple(['asdasd',2, 'test.abc.test']);
109 }, qr/Validation failed for 'Int'/
110 => 'Got Expected Error for violating constraints';
112 ## Test the Dictionary type constraint
115 $record->dict({name=>'frith', age=>23});
116 } => 'Set dict attribute without error';
118 is $record->dict->{name}, 'frith'
119 => 'correct set the dict attribute name';
121 is $record->dict->{age}, 23
122 => 'correct set the dict attribute age';
125 $record->dict({name=>[1,2,3], age=>'sdfsdfsd'});
126 }, qr/Validation failed for 'Str'/
127 => 'Got Expected Error for bad value in dict';
129 ## Test tuple_with_maybe
132 $record->tuple_with_maybe([1,'hello', 1]);
133 } => 'Set tuple attribute without error';
136 $record->tuple_with_maybe([1,'hello', 'a']);
137 }, qr/Validation failed for 'Maybe\[Int\]'/
138 => 'Properly failed for bad value parameterized constraint';
141 $record->tuple_with_maybe([1,'hello']);
142 } => 'Set tuple attribute without error skipping optional parameter';
144 ## Test Tuple with parameterized type
147 $record->tuple_with_param([1,'hello', [1,2,3]]);
148 } => 'Set tuple attribute without error';
151 $record->tuple_with_param([1,'hello', [qw/a b c/]]);
152 }, qr/Validation failed for 'ArrayRef\[Int\]'/
153 => 'Properly failed for bad value parameterized constraint';
155 ## Test dict_with_maybe
158 $record->dict_with_maybe({name=>'frith', age=>23});
159 } => 'Set dict attribute without error';
161 is $record->dict_with_maybe->{name}, 'frith'
162 => 'correct set the dict attribute name';
164 is $record->dict_with_maybe->{age}, 23
165 => 'correct set the dict attribute age';
168 $record->dict_with_maybe({name=>[1,2,3], age=>'sdfsdfsd'});
169 }, qr/Validation failed for 'Str'/
170 => 'Got Expected Error for bad value in dict';
173 $record->dict_with_maybe({age=>30});
174 }, qr/Validation failed for 'Str'/
175 => 'Got Expected Error for missing named parameter';
178 $record->dict_with_maybe({name=>'usal'});
179 } => 'Set dict attribute without error, skipping optional';
181 ## Test dict_with_tuple
184 $record->dict_with_tuple({key1=>'Hello', key2=>[1,'World']});
185 } => 'Set tuple attribute without error';
188 $record->dict_with_tuple({key1=>'Hello', key2=>['World',2]});
189 }, qr/Validation failed for 'Int'/
190 => 'Threw error on bad constraint';
192 ## Test optional_tuple
195 $record->optional_tuple([1,2,3]);
196 } => 'Set tuple attribute with optional bits';
198 is_deeply $record->optional_tuple, [1,2,3]
199 => 'correct values set';
202 $record->optional_tuple([4,5]);
203 } => 'Set tuple attribute withOUT optional bits';
205 is_deeply $record->optional_tuple, [4,5]
206 => 'correct values set again';
209 $record->optional_tuple([1,2,'bad']);
210 }, qr/Validation failed for 'Int'/
211 => 'Properly failed for bad value in optional bit';
216 $record->optional_dict({key1=>1,key2=>2});
217 } => 'Set tuple attribute with optional bits';
219 is_deeply $record->optional_dict, {key1=>1,key2=>2}
220 => 'correct values set';
223 $record->optional_dict({key1=>3});
224 } => 'Set tuple attribute withOUT optional bits';
226 is_deeply $record->optional_dict, {key1=>3}
227 => 'correct values set again';
230 $record->optional_dict({key1=>1,key2=>'bad'});
231 }, qr/Validation failed for 'Int'/
232 => 'Properly failed for bad value in optional bit';
235 ## Test dict_with_tuple_with_union: Dict[key1=>'Str|Object', key2=>Tuple['Int','Str|Object']]
238 $record->dict_with_tuple_with_union({key1=>'Hello', key2=>[1,'World']});
239 } => 'Set tuple attribute without error';
242 $record->dict_with_tuple_with_union({key1=>'Hello', key2=>['World',2]});
243 }, qr/Validation failed for 'Int'/
244 => 'Threw error on bad constraint';
247 $record->dict_with_tuple_with_union({key1=>$record, key2=>[1,'World']});
248 } => 'Set tuple attribute without error';
251 $record->dict_with_tuple_with_union({key1=>'Hello', key2=>[1,$record]});
252 } => 'Set tuple attribute without error';
255 $record->dict_with_tuple_with_union({key1=>1, key2=>['World',2]});
256 }, qr/Validation failed for 'Int'/
257 => 'Threw error on bad constraint';