4 use Test::More tests=>42;
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']] );
31 ## First ArrayRef Arg is the required type constraints for the top
36 ## The third required element is a Dict type constraint, which
37 ## itself has two required keys and a third optional key.
38 Dict[name=>'Str',age=>'Int', Optional[visits=>'Int']],
41 ## This Tuple has one required type constraint and two optional.
42 Tuple['Int', Optional['Int','HashRef']],
48 ## Instantiate a new test object
50 ok my $record = Test::MooseX::Meta::TypeConstraint::Structured->new
51 => 'Instantiated new Record test class.';
53 isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured'
54 => 'Created correct object type.';
59 $record->crazy([1,'hello.abc.world', {name=>'John', age=>39}]);
60 } => 'Set crazy attribute with no optionals used';
62 is_deeply $record->crazy, [1, 'hello.abc.world', {name=>'John', age=>39}]
63 => 'correct values for crazy attributes no optionals';
66 $record->crazy([1,'hello.abc.world', {name=>'John', age=>39, visits=>10},10, [1,2,{key=>'value'}]]);
67 } => 'Set crazy attribute with all optionals used';
69 is_deeply $record->crazy, [1,'hello.abc.world', {name=>'John', age=>39, visits=>10},10, [1,2,{key=>'value'}]]
70 => 'correct values for crazy attributes all optionals';
73 $record->crazy([1,'hello.abc.world', {name=>'John', age=>39},10, [1,2]]);
74 } => 'Set crazy attribute with some optionals used';
77 $record->crazy([1,'hello', 'test.xxx.test']);
78 }, qr/Validation failed for 'MyString'/
79 => 'Properly failed for bad value in crazy attribute 01';
82 $record->crazy([1,'hello.abc.world', {notname=>'John', notage=>39}]);
83 }, qr/Validation failed for 'Str'/
84 => 'Properly failed for bad value in crazy attribute 02';
86 ## Test Tuple type constraint
89 $record->tuple([1,'hello', 'test.abc.test']);
90 } => 'Set tuple attribute without error';
92 is $record->tuple->[0], 1
93 => 'correct set the tuple attribute index 0';
95 is $record->tuple->[1], 'hello'
96 => 'correct set the tuple attribute index 1';
98 is $record->tuple->[2], 'test.abc.test'
99 => 'correct set the tuple attribute index 2';
102 $record->tuple([1,'hello', 'test.xxx.test']);
103 }, qr/Validation failed for 'MyString'/
104 => 'Properly failed for bad value in custom type constraint';
107 $record->tuple(['asdasd',2, 'test.abc.test']);
108 }, qr/Validation failed for 'Int'/
109 => 'Got Expected Error for violating constraints';
111 ## Test the Dictionary type constraint
114 $record->dict({name=>'frith', age=>23});
115 } => 'Set dict attribute without error';
117 is $record->dict->{name}, 'frith'
118 => 'correct set the dict attribute name';
120 is $record->dict->{age}, 23
121 => 'correct set the dict attribute age';
124 $record->dict({name=>[1,2,3], age=>'sdfsdfsd'});
125 }, qr/Validation failed for 'Str'/
126 => 'Got Expected Error for bad value in dict';
128 ## Test tuple_with_maybe
131 $record->tuple_with_maybe([1,'hello', 1]);
132 } => 'Set tuple attribute without error';
135 $record->tuple_with_maybe([1,'hello', 'a']);
136 }, qr/Validation failed for 'Maybe\[Int\]'/
137 => 'Properly failed for bad value parameterized constraint';
140 $record->tuple_with_maybe([1,'hello']);
141 } => 'Set tuple attribute without error skipping optional parameter';
143 ## Test Tuple with parameterized type
146 $record->tuple_with_param([1,'hello', [1,2,3]]);
147 } => 'Set tuple attribute without error';
150 $record->tuple_with_param([1,'hello', [qw/a b c/]]);
151 }, qr/Validation failed for 'ArrayRef\[Int\]'/
152 => 'Properly failed for bad value parameterized constraint';
154 ## Test dict_with_maybe
157 $record->dict_with_maybe({name=>'frith', age=>23});
158 } => 'Set dict attribute without error';
160 is $record->dict_with_maybe->{name}, 'frith'
161 => 'correct set the dict attribute name';
163 is $record->dict_with_maybe->{age}, 23
164 => 'correct set the dict attribute age';
167 $record->dict_with_maybe({name=>[1,2,3], age=>'sdfsdfsd'});
168 }, qr/Validation failed for 'Str'/
169 => 'Got Expected Error for bad value in dict';
172 $record->dict_with_maybe({age=>30});
173 }, qr/Validation failed for 'Str'/
174 => 'Got Expected Error for missing named parameter';
177 $record->dict_with_maybe({name=>'usal'});
178 } => 'Set dict attribute without error, skipping optional';
180 ## Test dict_with_tuple
183 $record->dict_with_tuple({key1=>'Hello', key2=>[1,'World']});
184 } => 'Set tuple attribute without error';
187 $record->dict_with_tuple({key1=>'Hello', key2=>['World',2]});
188 }, qr/Validation failed for 'Int'/
189 => 'Threw error on bad constraint';
191 ## Test optional_tuple
194 $record->optional_tuple([1,2,3]);
195 } => 'Set tuple attribute with optional bits';
197 is_deeply $record->optional_tuple, [1,2,3]
198 => 'correct values set';
201 $record->optional_tuple([4,5]);
202 } => 'Set tuple attribute withOUT optional bits';
204 is_deeply $record->optional_tuple, [4,5]
205 => 'correct values set again';
208 $record->optional_tuple([1,2,'bad']);
209 }, qr/Validation failed for 'Int'/
210 => 'Properly failed for bad value in optional bit';
215 $record->optional_dict({key1=>1,key2=>2});
216 } => 'Set tuple attribute with optional bits';
218 is_deeply $record->optional_dict, {key1=>1,key2=>2}
219 => 'correct values set';
222 $record->optional_dict({key1=>3});
223 } => 'Set tuple attribute withOUT optional bits';
225 is_deeply $record->optional_dict, {key1=>3}
226 => 'correct values set again';
229 $record->optional_dict({key1=>1,key2=>'bad'});
230 }, qr/Validation failed for 'Int'/
231 => 'Properly failed for bad value in optional bit';