8 use lib "$FindBin::Bin/lib";
11 package Test::MooseX::TypeLibrary::TypeDecorator;
14 use MooseX::Types::Moose qw(
15 Int Str ArrayRef HashRef Object
17 use DecoratorLibrary qw(
18 MyArrayRefBase MyArrayRefInt01 MyArrayRefInt02 StrOrArrayRef
19 AtLeastOneInt Jobs SubOfMyArrayRefInt01 WierdIntegersArrayRef1
20 WierdIntegersArrayRef2
23 has 'arrayrefbase' => (is=>'rw', isa=>MyArrayRefBase, coerce=>1);
24 has 'arrayrefint01' => (is=>'rw', isa=>MyArrayRefInt01, coerce=>1);
25 has 'arrayrefint02' => (is=>'rw', isa=>MyArrayRefInt02, coerce=>1);
26 has 'arrayrefint03' => (is=>'rw', isa=>MyArrayRefBase[Int]);
27 has 'StrOrArrayRef_attr' => (is=>'rw', isa=>StrOrArrayRef);
28 has 'AtLeastOneInt_attr' => (is=>'rw', isa=>AtLeastOneInt);
29 has 'pipeoverloading' => (is=>'rw', isa=>Int|Str);
30 has 'deep' => (is=>'rw', isa=>ArrayRef[ArrayRef[HashRef[Int]]] );
31 has 'deep2' => (is=>'rw', isa=>ArrayRef[Int|ArrayRef[HashRef[Int|Object]]] );
32 has 'enum' => (is=>'rw', isa=>Jobs);
33 has 'SubOfMyArrayRefInt01_attr' => (is=>'rw', isa=>SubOfMyArrayRefInt01);
34 has 'WierdIntegersArrayRef1_attr' => (is=>'rw', isa=>WierdIntegersArrayRef1);
35 has 'WierdIntegersArrayRef2_attr' => (is=>'rw', isa=>WierdIntegersArrayRef2);
38 ## Make sure we have a 'create object sanity check'
40 ok my $type = Test::MooseX::TypeLibrary::TypeDecorator->new(),
41 => 'Created some sort of object';
43 isa_ok $type, 'Test::MooseX::TypeLibrary::TypeDecorator'
44 => "Yes, it's the correct kind of object";
46 ## test arrayrefbase normal and coercion
48 ok $type->arrayrefbase([qw(a b c d e)])
49 => 'Assigned arrayrefbase qw(a b c d e)';
51 is_deeply $type->arrayrefbase, [qw(a b c d e)],
52 => 'Assignment is correct';
54 ok $type->arrayrefbase('d,e,f')
55 => 'Assignment arrayrefbase d,e,f to test coercion';
57 is_deeply $type->arrayrefbase, [qw(d e f)],
58 => 'Assignment and coercion is correct';
60 ## test arrayrefint01 normal and coercion
62 ok $type->arrayrefint01([qw(1 2 3)])
63 => 'Assignment arrayrefint01 qw(1 2 3)';
65 is_deeply $type->arrayrefint01, [qw(1 2 3)],
66 => 'Assignment is correct';
68 ok $type->arrayrefint01('4.5.6')
69 => 'Assigned arrayrefint01 4.5.6 to test coercion from Str';
71 is_deeply $type->arrayrefint01, [qw(4 5 6)],
72 => 'Assignment and coercion is correct';
74 ok $type->arrayrefint01({a=>7,b=>8})
75 => 'Assigned arrayrefint01 {a=>7,b=>8} to test coercion from HashRef';
77 is_deeply $type->arrayrefint01, [qw(7 8)],
78 => 'Assignment and coercion is correct';
81 $type->arrayrefint01([qw(a b c)])
82 }, qr/Attribute \(arrayrefint01\) does not pass the type constraint/ => 'Dies when values are strings';
84 ## test arrayrefint02 normal and coercion
86 ok $type->arrayrefint02([qw(1 2 3)])
87 => 'Assigned arrayrefint02 qw(1 2 3)';
89 is_deeply $type->arrayrefint02, [qw(1 2 3)],
90 => 'Assignment is correct';
92 ok $type->arrayrefint02('4:5:6')
93 => 'Assigned arrayrefint02 4:5:6 to test coercion from Str';
95 is_deeply $type->arrayrefint02, [qw(4 5 6)],
96 => 'Assignment and coercion is correct';
98 ok $type->arrayrefint02({a=>7,b=>8})
99 => 'Assigned arrayrefint02 {a=>7,b=>8} to test coercion from HashRef';
101 is_deeply $type->arrayrefint02, [qw(7 8)],
102 => 'Assignment and coercion is correct';
104 ok $type->arrayrefint02({a=>'AA',b=>'BBB', c=>'CCCCCCC'})
105 => "Assigned arrayrefint02 {a=>'AA',b=>'BBB', c=>'CCCCCCC'} to test coercion from HashRef";
107 is_deeply $type->arrayrefint02, [qw(2 3 7)],
108 => 'Assignment and coercion is correct';
110 ok $type->arrayrefint02({a=>[1,2],b=>[3,4]})
111 => "Assigned arrayrefint02 {a=>[1,2],b=>[3,4]} to test coercion from HashRef";
113 is_deeply $type->arrayrefint02, [qw(1 2 3 4)],
114 => 'Assignment and coercion is correct';
118 ok $type->arrayrefint03([qw(11 12 13)])
119 => 'Assigned arrayrefint01 qw(11 12 13)';
121 is_deeply $type->arrayrefint03, [qw(11 12 13)],
122 => 'Assignment is correct';
125 $type->arrayrefint03([qw(a b c)])
126 }, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings';
130 ok $type->StrOrArrayRef_attr('string')
131 => 'String part of union is good';
133 ok $type->StrOrArrayRef_attr([1,2,3])
134 => 'arrayref part of union is good';
137 $type->StrOrArrayRef_attr({a=>111});
138 }, qr/Attribute \(StrOrArrayRef_attr\) does not pass the type constraint/ => 'Correctly failed to use a hashref';
142 ok $type->AtLeastOneInt_attr([1,2]),
143 => 'Good assignment';
145 is_deeply $type->AtLeastOneInt_attr, [1,2]
146 => "Got expected values.";
149 $type->AtLeastOneInt_attr([]);
150 }, qr/Attribute \(AtLeastOneInt_attr\) does not pass the type constraint/ => 'properly fails to assign as []';
153 $type->AtLeastOneInt_attr(['a','b']);
154 }, qr/Attribute \(AtLeastOneInt_attr\) does not pass the type constraint/ => 'properly fails arrayref of strings';
156 ## Test pipeoverloading
158 ok $type->pipeoverloading(1)
159 => 'Integer for union test accepted';
161 ok $type->pipeoverloading('a')
162 => 'String for union test accepted';
165 $type->pipeoverloading({a=>1,b=>2});
166 }, qr/Validation failed for 'Int|Str'/ => 'Union test corrected fails a HashRef';
168 ## test deep (ArrayRef[ArrayRef[HashRef[Int]]])
170 ok $type->deep([[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]])
171 => 'Assigned deep to [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]]';
173 is_deeply $type->deep, [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]],
174 => 'Assignment is correct';
177 $type->deep({a=>1,b=>2});
178 }, qr/Attribute \(deep\) does not pass the type constraint/ => 'Deep Constraints properly fail';
180 # test deep2 (ArrayRef[Int|ArrayRef[HashRef[Int|Object]]])
182 ok $type->deep2([[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]])
183 => 'Assigned deep2 to [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]]';
185 is_deeply $type->deep2, [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]],
186 => 'Assignment is correct';
189 $type->deep2({a=>1,b=>2});
190 }, qr/Attribute \(deep2\) does not pass the type constraint/ => 'Deep Constraints properly fail';
193 $type->deep2([[{a=>1,b=>2},{c=>3,d=>'noway'}],[{e=>5}]]);
194 }, qr/Attribute \(deep2\) does not pass the type constraint/ => 'Deep Constraints properly fail';
197 ok $type->deep2([[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]])
198 => 'Assigned deep2 to [[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]]';
201 is_deeply $type->deep2, [[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]],
202 => 'Assignment is correct';
204 ok $type->deep2([1,2,3])
205 => 'Assigned deep2 to [1,2,3]';
208 is_deeply $type->deep2, [1,2,3],
209 => 'Assignment is correct';
213 ok $type->enum('Programming')
214 => 'Good Assignment of Programming to Enum';
218 $type->enum('ddddd');
219 }, qr/Attribute \(enum\) does not pass the type constraint/ => 'Enum properly fails';
221 ## Test SubOfMyArrayRefInt01_attr
223 ok $type->SubOfMyArrayRefInt01_attr([15,20,25])
224 => 'Assigned SubOfMyArrayRefInt01_attr to [15,20,25]';
226 is_deeply $type->SubOfMyArrayRefInt01_attr, [15,20,25],
227 => 'Assignment is correct';
230 $type->SubOfMyArrayRefInt01_attr([15,5,20]);
231 }, qr/Attribute \(SubOfMyArrayRefInt01_attr\) does not pass the type constraint/
232 => 'SubOfMyArrayRefInt01 Constraints properly fail';
234 ## test WierdIntegersArrayRef1
236 ok $type->WierdIntegersArrayRef1_attr([5,10,1000])
237 => 'Assigned deep2 to [5,10,1000]';
239 is_deeply $type->WierdIntegersArrayRef1_attr, [5,10,1000],
240 => 'Assignment is correct';
243 $type->WierdIntegersArrayRef1_attr({a=>1,b=>2});
244 }, qr/Attribute \(WierdIntegersArrayRef1_attr\) does not pass the type constraint/
245 => 'Constraints properly fail';
248 $type->WierdIntegersArrayRef1_attr([5,10,1]);
249 }, qr/Attribute \(WierdIntegersArrayRef1_attr\) does not pass the type constraint/
250 => 'Constraints properly fail';
253 $type->WierdIntegersArrayRef1_attr([1]);
254 }, qr/Attribute \(WierdIntegersArrayRef1_attr\) does not pass the type constraint/
255 => 'Constraints properly fail';
257 ## test WierdIntegersArrayRef2
259 ok $type->WierdIntegersArrayRef2_attr([5,10,$type])
260 => 'Assigned deep2 to [5,10,$type]';
262 is_deeply $type->WierdIntegersArrayRef2_attr, [5,10,$type],
263 => 'Assignment is correct';
266 $type->WierdIntegersArrayRef2_attr({a=>1,b=>2});
267 }, qr/Attribute \(WierdIntegersArrayRef2_attr\) does not pass the type constraint/
268 => 'Constraints properly fail';
271 $type->WierdIntegersArrayRef2_attr([5,10,1]);
272 }, qr/Attribute \(WierdIntegersArrayRef2_attr\) does not pass the type constraint/
273 => 'Constraints properly fail';
276 $type->WierdIntegersArrayRef2_attr([1]);
277 }, qr/Attribute \(WierdIntegersArrayRef2_attr\) does not pass the type constraint/
278 => 'Constraints properly fail';