5 use Test::More tests => 52;
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
22 has 'arrayrefbase' => (is=>'rw', isa=>MyArrayRefBase, coerce=>1);
23 has 'arrayrefint01' => (is=>'rw', isa=>MyArrayRefInt01, coerce=>1);
24 has 'arrayrefint02' => (is=>'rw', isa=>MyArrayRefInt02, coerce=>1);
25 has 'arrayrefint03' => (is=>'rw', isa=>MyArrayRefBase[Int]);
26 has 'StrOrArrayRef' => (is=>'rw', isa=>StrOrArrayRef);
27 has 'AtLeastOneInt' => (is=>'rw', isa=>AtLeastOneInt);
28 has 'pipeoverloading' => (is=>'rw', isa=>Int|Str);
29 has 'deep' => (is=>'rw', isa=>ArrayRef[ArrayRef[HashRef[Int]]] );
30 has 'deep2' => (is=>'rw', isa=>ArrayRef[Int|ArrayRef[HashRef[Int|Object]]] );
31 has 'enum' => (is=>'rw', isa=>Jobs);
32 has 'SubOfMyArrayRefInt01_attr' => (is=>'rw', isa=>SubOfMyArrayRefInt01);
35 ## Make sure we have a 'create object sanity check'
37 ok my $type = Test::MooseX::TypeLibrary::TypeDecorator->new(),
38 => 'Created some sort of object';
40 isa_ok $type, 'Test::MooseX::TypeLibrary::TypeDecorator'
41 => "Yes, it's the correct kind of object";
43 ## test arrayrefbase normal and coercion
45 ok $type->arrayrefbase([qw(a b c d e)])
46 => 'Assigned arrayrefbase qw(a b c d e)';
48 is_deeply $type->arrayrefbase, [qw(a b c d e)],
49 => 'Assignment is correct';
51 ok $type->arrayrefbase('d,e,f')
52 => 'Assignment arrayrefbase d,e,f to test coercion';
54 is_deeply $type->arrayrefbase, [qw(d e f)],
55 => 'Assignment and coercion is correct';
57 ## test arrayrefint01 normal and coercion
59 ok $type->arrayrefint01([qw(1 2 3)])
60 => 'Assignment arrayrefint01 qw(1 2 3)';
62 is_deeply $type->arrayrefint01, [qw(1 2 3)],
63 => 'Assignment is correct';
65 ok $type->arrayrefint01('4.5.6')
66 => 'Assigned arrayrefint01 4.5.6 to test coercion from Str';
68 is_deeply $type->arrayrefint01, [qw(4 5 6)],
69 => 'Assignment and coercion is correct';
71 ok $type->arrayrefint01({a=>7,b=>8})
72 => 'Assigned arrayrefint01 {a=>7,b=>8} to test coercion from HashRef';
74 is_deeply $type->arrayrefint01, [qw(7 8)],
75 => 'Assignment and coercion is correct';
78 $type->arrayrefint01([qw(a b c)])
79 }, qr/Attribute \(arrayrefint01\) does not pass the type constraint/ => 'Dies when values are strings';
81 ## test arrayrefint02 normal and coercion
83 ok $type->arrayrefint02([qw(1 2 3)])
84 => 'Assigned arrayrefint02 qw(1 2 3)';
86 is_deeply $type->arrayrefint02, [qw(1 2 3)],
87 => 'Assignment is correct';
89 ok $type->arrayrefint02('4:5:6')
90 => 'Assigned arrayrefint02 4:5:6 to test coercion from Str';
92 is_deeply $type->arrayrefint02, [qw(4 5 6)],
93 => 'Assignment and coercion is correct';
95 ok $type->arrayrefint02({a=>7,b=>8})
96 => 'Assigned arrayrefint02 {a=>7,b=>8} to test coercion from HashRef';
98 is_deeply $type->arrayrefint02, [qw(7 8)],
99 => 'Assignment and coercion is correct';
101 ok $type->arrayrefint02({a=>'AA',b=>'BBB', c=>'CCCCCCC'})
102 => "Assigned arrayrefint02 {a=>'AA',b=>'BBB', c=>'CCCCCCC'} to test coercion from HashRef";
104 is_deeply $type->arrayrefint02, [qw(2 3 7)],
105 => 'Assignment and coercion is correct';
107 ok $type->arrayrefint02({a=>[1,2],b=>[3,4]})
108 => "Assigned arrayrefint02 {a=>[1,2],b=>[3,4]} to test coercion from HashRef";
110 is_deeply $type->arrayrefint02, [qw(1 2 3 4)],
111 => 'Assignment and coercion is correct';
115 ok $type->arrayrefint03([qw(11 12 13)])
116 => 'Assigned arrayrefint01 qw(11 12 13)';
118 is_deeply $type->arrayrefint03, [qw(11 12 13)],
119 => 'Assignment is correct';
122 $type->arrayrefint03([qw(a b c)])
123 }, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings';
127 ok $type->StrOrArrayRef('string')
128 => 'String part of union is good';
130 ok $type->StrOrArrayRef([1,2,3])
131 => 'arrayref part of union is good';
134 $type->StrOrArrayRef({a=>111});
135 }, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref';
139 ok $type->AtLeastOneInt([1,2]),
140 => 'Good assignment';
142 is_deeply $type->AtLeastOneInt, [1,2]
143 => "Got expected values.";
146 $type->AtLeastOneInt([]);
147 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails to assign as []';
150 $type->AtLeastOneInt(['a','b']);
151 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails arrayref of strings';
153 ## Test pipeoverloading
155 ok $type->pipeoverloading(1)
156 => 'Integer for union test accepted';
158 ok $type->pipeoverloading('a')
159 => 'String for union test accepted';
162 $type->pipeoverloading({a=>1,b=>2});
163 }, qr/Validation failed for 'Int|Str'/ => 'Union test corrected fails a HashRef';
165 ## test deep (ArrayRef[ArrayRef[HashRef[Int]]])
167 ok $type->deep([[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]])
168 => 'Assigned deep to [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]]';
170 is_deeply $type->deep, [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]],
171 => 'Assignment is correct';
174 $type->deep({a=>1,b=>2});
175 }, qr/Attribute \(deep\) does not pass the type constraint/ => 'Deep Constraints properly fail';
177 # test deep2 (ArrayRef[Int|ArrayRef[HashRef[Int|Object]]])
179 ok $type->deep2([[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]])
180 => 'Assigned deep2 to [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]]';
182 is_deeply $type->deep2, [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]],
183 => 'Assignment is correct';
186 $type->deep2({a=>1,b=>2});
187 }, qr/Attribute \(deep2\) does not pass the type constraint/ => 'Deep Constraints properly fail';
190 $type->deep2([[{a=>1,b=>2},{c=>3,d=>'noway'}],[{e=>5}]]);
191 }, qr/Attribute \(deep2\) does not pass the type constraint/ => 'Deep Constraints properly fail';
194 ok $type->deep2([[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]])
195 => 'Assigned deep2 to [[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]]';
198 is_deeply $type->deep2, [[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]],
199 => 'Assignment is correct';
201 ok $type->deep2([1,2,3])
202 => 'Assigned deep2 to [1,2,3]';
205 is_deeply $type->deep2, [1,2,3],
206 => 'Assignment is correct';
210 ok $type->enum('Programming')
211 => 'Good Assignment of Programming to Enum';
215 $type->enum('ddddd');
216 }, qr/Attribute \(enum\) does not pass the type constraint/ => 'Enum properly fails';
218 ## Test SubOfMyArrayRefInt01_attr
220 ok $type->SubOfMyArrayRefInt01_attr([15,20,25])
221 => 'Assigned SubOfMyArrayRefInt01_attr to [15,20,25]';
223 is_deeply $type->SubOfMyArrayRefInt01_attr, [15,20,25],
224 => 'Assignment is correct';
227 $type->SubOfMyArrayRefInt01_attr([15,5,20]);
228 }, qr/Attribute \(SubOfMyArrayRefInt01_attr\) does not pass the type constraint/ => 'SubOfMyArrayRefInt01 Constraints properly fail';