5 use Test::More tests => 49;
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
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);
34 ## Make sure we have a 'create object sanity check'
36 ok my $type = Test::MooseX::TypeLibrary::TypeDecorator->new(),
37 => 'Created some sort of object';
39 isa_ok $type, 'Test::MooseX::TypeLibrary::TypeDecorator'
40 => "Yes, it's the correct kind of object";
42 ## test arrayrefbase normal and coercion
44 ok $type->arrayrefbase([qw(a b c d e)])
45 => 'Assigned arrayrefbase qw(a b c d e)';
47 is_deeply $type->arrayrefbase, [qw(a b c d e)],
48 => 'Assignment is correct';
50 ok $type->arrayrefbase('d,e,f')
51 => 'Assignment arrayrefbase d,e,f to test coercion';
53 is_deeply $type->arrayrefbase, [qw(d e f)],
54 => 'Assignment and coercion is correct';
56 ## test arrayrefint01 normal and coercion
58 ok $type->arrayrefint01([qw(1 2 3)])
59 => 'Assignment arrayrefint01 qw(1 2 3)';
61 is_deeply $type->arrayrefint01, [qw(1 2 3)],
62 => 'Assignment is correct';
64 ok $type->arrayrefint01('4.5.6')
65 => 'Assigned arrayrefint01 4.5.6 to test coercion from Str';
67 is_deeply $type->arrayrefint01, [qw(4 5 6)],
68 => 'Assignment and coercion is correct';
70 ok $type->arrayrefint01({a=>7,b=>8})
71 => 'Assigned arrayrefint01 {a=>7,b=>8} to test coercion from HashRef';
73 is_deeply $type->arrayrefint01, [qw(7 8)],
74 => 'Assignment and coercion is correct';
77 $type->arrayrefint01([qw(a b c)])
78 }, qr/Attribute \(arrayrefint01\) does not pass the type constraint/ => 'Dies when values are strings';
80 ## test arrayrefint02 normal and coercion
82 ok $type->arrayrefint02([qw(1 2 3)])
83 => 'Assigned arrayrefint02 qw(1 2 3)';
85 is_deeply $type->arrayrefint02, [qw(1 2 3)],
86 => 'Assignment is correct';
88 ok $type->arrayrefint02('4:5:6')
89 => 'Assigned arrayrefint02 4:5:6 to test coercion from Str';
91 is_deeply $type->arrayrefint02, [qw(4 5 6)],
92 => 'Assignment and coercion is correct';
94 ok $type->arrayrefint02({a=>7,b=>8})
95 => 'Assigned arrayrefint02 {a=>7,b=>8} to test coercion from HashRef';
97 is_deeply $type->arrayrefint02, [qw(7 8)],
98 => 'Assignment and coercion is correct';
100 ok $type->arrayrefint02({a=>'AA',b=>'BBB', c=>'CCCCCCC'})
101 => "Assigned arrayrefint02 {a=>'AA',b=>'BBB', c=>'CCCCCCC'} to test coercion from HashRef";
103 is_deeply $type->arrayrefint02, [qw(2 3 7)],
104 => 'Assignment and coercion is correct';
106 ok $type->arrayrefint02({a=>[1,2],b=>[3,4]})
107 => "Assigned arrayrefint02 {a=>[1,2],b=>[3,4]} to test coercion from HashRef";
109 is_deeply $type->arrayrefint02, [qw(1 2 3 4)],
110 => 'Assignment and coercion is correct';
114 ok $type->arrayrefint03([qw(11 12 13)])
115 => 'Assigned arrayrefint01 qw(11 12 13)';
117 is_deeply $type->arrayrefint03, [qw(11 12 13)],
118 => 'Assignment is correct';
121 $type->arrayrefint03([qw(a b c)])
122 }, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings';
126 ok $type->StrOrArrayRef('string')
127 => 'String part of union is good';
129 ok $type->StrOrArrayRef([1,2,3])
130 => 'arrayref part of union is good';
133 $type->StrOrArrayRef({a=>111});
134 }, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref';
138 ok $type->AtLeastOneInt([1,2]),
139 => 'Good assignment';
141 is_deeply $type->AtLeastOneInt, [1,2]
142 => "Got expected values.";
145 $type->AtLeastOneInt([]);
146 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails to assign as []';
149 $type->AtLeastOneInt(['a','b']);
150 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails arrayref of strings';
152 ## Test pipeoverloading
154 ok $type->pipeoverloading(1)
155 => 'Integer for union test accepted';
157 ok $type->pipeoverloading('a')
158 => 'String for union test accepted';
161 $type->pipeoverloading({a=>1,b=>2});
162 }, qr/Validation failed for 'Int|Str'/ => 'Union test corrected fails a HashRef';
164 ## test deep (ArrayRef[ArrayRef[HashRef[Int]]])
166 ok $type->deep([[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]])
167 => 'Assigned deep to [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]]';
169 is_deeply $type->deep, [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]],
170 => 'Assignment is correct';
173 $type->deep({a=>1,b=>2});
174 }, qr/Attribute \(deep\) does not pass the type constraint/ => 'Deep Constraints properly fail';
176 # test deep2 (ArrayRef[Int|ArrayRef[HashRef[Int|Object]]])
178 ok $type->deep2([[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]])
179 => 'Assigned deep2 to [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]]';
181 is_deeply $type->deep2, [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]],
182 => 'Assignment is correct';
185 $type->deep2({a=>1,b=>2});
186 }, qr/Attribute \(deep2\) does not pass the type constraint/ => 'Deep Constraints properly fail';
189 $type->deep2([[{a=>1,b=>2},{c=>3,d=>'noway'}],[{e=>5}]]);
190 }, qr/Attribute \(deep2\) does not pass the type constraint/ => 'Deep Constraints properly fail';
193 ok $type->deep2([[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]])
194 => 'Assigned deep2 to [[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]]';
197 is_deeply $type->deep2, [[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]],
198 => 'Assignment is correct';
200 ok $type->deep2([1,2,3])
201 => 'Assigned deep2 to [1,2,3]';
204 is_deeply $type->deep2, [1,2,3],
205 => 'Assignment is correct';
209 ok $type->enum('Programming')
210 => 'Good Assignment of Programming to Enum';
214 $type->enum('ddddd');
215 }, qr/Attribute \(enum\) does not pass the type constraint/ => 'Enum properly fails';