5 use Test::More tests => 47;
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]]] );
33 ## Make sure we have a 'create object sanity check'
35 ok my $type = Test::MooseX::TypeLibrary::TypeDecorator->new(),
36 => 'Created some sort of object';
38 isa_ok $type, 'Test::MooseX::TypeLibrary::TypeDecorator'
39 => "Yes, it's the correct kind of object";
41 ## test arrayrefbase normal and coercion
43 ok $type->arrayrefbase([qw(a b c d e)])
44 => 'Assigned arrayrefbase qw(a b c d e)';
46 is_deeply $type->arrayrefbase, [qw(a b c d e)],
47 => 'Assignment is correct';
49 ok $type->arrayrefbase('d,e,f')
50 => 'Assignment arrayrefbase d,e,f to test coercion';
52 is_deeply $type->arrayrefbase, [qw(d e f)],
53 => 'Assignment and coercion is correct';
55 ## test arrayrefint01 normal and coercion
57 ok $type->arrayrefint01([qw(1 2 3)])
58 => 'Assignment arrayrefint01 qw(1 2 3)';
60 is_deeply $type->arrayrefint01, [qw(1 2 3)],
61 => 'Assignment is correct';
63 ok $type->arrayrefint01('4.5.6')
64 => 'Assigned arrayrefint01 4.5.6 to test coercion from Str';
66 is_deeply $type->arrayrefint01, [qw(4 5 6)],
67 => 'Assignment and coercion is correct';
69 ok $type->arrayrefint01({a=>7,b=>8})
70 => 'Assigned arrayrefint01 {a=>7,b=>8} to test coercion from HashRef';
72 is_deeply $type->arrayrefint01, [qw(7 8)],
73 => 'Assignment and coercion is correct';
76 $type->arrayrefint01([qw(a b c)])
77 }, qr/Attribute \(arrayrefint01\) does not pass the type constraint/ => 'Dies when values are strings';
79 ## test arrayrefint02 normal and coercion
81 ok $type->arrayrefint02([qw(1 2 3)])
82 => 'Assigned arrayrefint02 qw(1 2 3)';
84 is_deeply $type->arrayrefint02, [qw(1 2 3)],
85 => 'Assignment is correct';
87 ok $type->arrayrefint02('4:5:6')
88 => 'Assigned arrayrefint02 4:5:6 to test coercion from Str';
90 is_deeply $type->arrayrefint02, [qw(4 5 6)],
91 => 'Assignment and coercion is correct';
93 ok $type->arrayrefint02({a=>7,b=>8})
94 => 'Assigned arrayrefint02 {a=>7,b=>8} to test coercion from HashRef';
96 is_deeply $type->arrayrefint02, [qw(7 8)],
97 => 'Assignment and coercion is correct';
99 ok $type->arrayrefint02({a=>'AA',b=>'BBB', c=>'CCCCCCC'})
100 => "Assigned arrayrefint02 {a=>'AA',b=>'BBB', c=>'CCCCCCC'} to test coercion from HashRef";
102 is_deeply $type->arrayrefint02, [qw(2 3 7)],
103 => 'Assignment and coercion is correct';
105 ok $type->arrayrefint02({a=>[1,2],b=>[3,4]})
106 => "Assigned arrayrefint02 {a=>[1,2],b=>[3,4]} to test coercion from HashRef";
108 is_deeply $type->arrayrefint02, [qw(1 2 3 4)],
109 => 'Assignment and coercion is correct';
113 ok $type->arrayrefint03([qw(11 12 13)])
114 => 'Assigned arrayrefint01 qw(11 12 13)';
116 is_deeply $type->arrayrefint03, [qw(11 12 13)],
117 => 'Assignment is correct';
120 $type->arrayrefint03([qw(a b c)])
121 }, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings';
125 ok $type->StrOrArrayRef('string')
126 => 'String part of union is good';
128 ok $type->StrOrArrayRef([1,2,3])
129 => 'arrayref part of union is good';
132 $type->StrOrArrayRef({a=>111});
133 }, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref';
137 ok $type->AtLeastOneInt([1,2]),
138 => 'Good assignment';
140 is_deeply $type->AtLeastOneInt, [1,2]
141 => "Got expected values.";
144 $type->AtLeastOneInt([]);
145 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails';
148 $type->AtLeastOneInt(['a','b']);
149 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails arrayref of strings';
151 ## Test pipeoverloading
153 ok $type->pipeoverloading(1)
154 => 'Integer for union test accepted';
156 ok $type->pipeoverloading('a')
157 => 'String for union test accepted';
160 $type->pipeoverloading({a=>1,b=>2});
161 }, qr/Validation failed for 'Int|Str'/ => 'Union test corrected fails a HashRef';
163 ## test deep (ArrayRef[ArrayRef[HashRef[Int]]])
165 ok $type->deep([[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]])
166 => 'Assigned deep to [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]]';
168 is_deeply $type->deep, [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]],
169 => 'Assignment is correct';
172 $type->deep({a=>1,b=>2});
173 }, qr/Attribute \(deep\) does not pass the type constraint/ => 'Deep Constraints properly fail';
175 # test deep2 (ArrayRef[Int|ArrayRef[HashRef[Int|Object]]])
177 ok $type->deep2([[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]])
178 => 'Assigned deep2 to [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]]';
180 is_deeply $type->deep2, [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]],
181 => 'Assignment is correct';
184 $type->deep2({a=>1,b=>2});
185 }, qr/Attribute \(deep2\) does not pass the type constraint/ => 'Deep Constraints properly fail';
188 $type->deep2([[{a=>1,b=>2},{c=>3,d=>'noway'}],[{e=>5}]]);
189 }, qr/Attribute \(deep2\) does not pass the type constraint/ => 'Deep Constraints properly fail';
192 ok $type->deep2([[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]])
193 => 'Assigned deep2 to [[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]]';
196 is_deeply $type->deep2, [[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]],
197 => 'Assignment is correct';
199 ok $type->deep2([1,2,3])
200 => 'Assigned deep2 to [1,2,3]';
203 is_deeply $type->deep2, [1,2,3],
204 => 'Assignment is correct';