a418d1b22a2ef0d2cbb2c0e92140513266923040
[gitmo/MooseX-Types.git] / t / 13_typedecorator.t
1 #!/usr/bin/env perl
2 use warnings;
3 use strict;
4
5 use Test::More;
6 use Test::Fatal;
7 use FindBin;
8 use lib "$FindBin::Bin/lib";
9
10 {
11     package Test::MooseX::TypeLibrary::TypeDecorator;
12     
13     use Moose;
14     use MooseX::Types::Moose qw(
15         Int Str ArrayRef HashRef Object
16     );
17     use DecoratorLibrary qw(
18         MyArrayRefBase MyArrayRefInt01 MyArrayRefInt02 StrOrArrayRef
19         AtLeastOneInt Jobs SubOfMyArrayRefInt01 WierdIntegersArrayRef1
20         WierdIntegersArrayRef2
21     );
22     
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);   
36 }
37
38 ## Make sure we have a 'create object sanity check'
39
40 ok my $type = Test::MooseX::TypeLibrary::TypeDecorator->new(),
41  => 'Created some sort of object';
42  
43 isa_ok $type, 'Test::MooseX::TypeLibrary::TypeDecorator'
44  => "Yes, it's the correct kind of object";
45
46 ## test arrayrefbase normal and coercion
47
48 ok $type->arrayrefbase([qw(a b c d e)])
49  => 'Assigned arrayrefbase qw(a b c d e)';
50  
51 is_deeply $type->arrayrefbase, [qw(a b c d e)],
52  => 'Assignment is correct';
53
54 ok $type->arrayrefbase('d,e,f')
55  => 'Assignment arrayrefbase d,e,f to test coercion';
56  
57 is_deeply $type->arrayrefbase, [qw(d e f)],
58  => 'Assignment and coercion is correct';
59
60 ## test arrayrefint01 normal and coercion
61
62 ok $type->arrayrefint01([qw(1 2 3)])
63  => 'Assignment arrayrefint01 qw(1 2 3)';
64  
65 is_deeply $type->arrayrefint01, [qw(1 2 3)],
66  => 'Assignment is correct';
67
68 ok $type->arrayrefint01('4.5.6')
69  => 'Assigned arrayrefint01 4.5.6 to test coercion from Str';
70  
71 is_deeply $type->arrayrefint01, [qw(4 5 6)],
72  => 'Assignment and coercion is correct';
73
74 ok $type->arrayrefint01({a=>7,b=>8})
75  => 'Assigned arrayrefint01 {a=>7,b=>8} to test coercion from HashRef';
76  
77 is_deeply $type->arrayrefint01, [qw(7 8)],
78  => 'Assignment and coercion is correct';
79  
80 like exception {
81     $type->arrayrefint01([qw(a b c)])
82 }, qr/Attribute \(arrayrefint01\) does not pass the type constraint/ => 'Dies when values are strings';
83
84 ## test arrayrefint02 normal and coercion
85
86 ok $type->arrayrefint02([qw(1 2 3)])
87  => 'Assigned arrayrefint02 qw(1 2 3)';
88  
89 is_deeply $type->arrayrefint02, [qw(1 2 3)],
90  => 'Assignment is correct';
91
92 ok $type->arrayrefint02('4:5:6')
93  => 'Assigned arrayrefint02 4:5:6 to test coercion from Str';
94  
95 is_deeply $type->arrayrefint02, [qw(4 5 6)],
96  => 'Assignment and coercion is correct';
97
98 ok $type->arrayrefint02({a=>7,b=>8})
99  => 'Assigned arrayrefint02 {a=>7,b=>8} to test coercion from HashRef';
100  
101 is_deeply $type->arrayrefint02, [qw(7 8)],
102  => 'Assignment and coercion is correct';
103  
104 ok $type->arrayrefint02({a=>'AA',b=>'BBB', c=>'CCCCCCC'})
105  => "Assigned arrayrefint02 {a=>'AA',b=>'BBB', c=>'CCCCCCC'} to test coercion from HashRef";
106  
107 is_deeply $type->arrayrefint02, [qw(2 3 7)],
108  => 'Assignment and coercion is correct';
109
110 ok $type->arrayrefint02({a=>[1,2],b=>[3,4]})
111  => "Assigned arrayrefint02 {a=>[1,2],b=>[3,4]} to test coercion from HashRef";
112  
113 is_deeply $type->arrayrefint02, [qw(1 2 3 4)],
114  => 'Assignment and coercion is correct';
115  
116 # test arrayrefint03 
117
118 ok $type->arrayrefint03([qw(11 12 13)])
119  => 'Assigned arrayrefint01 qw(11 12 13)';
120  
121 is_deeply $type->arrayrefint03, [qw(11 12 13)],
122  => 'Assignment is correct';
123  
124 like exception {
125     $type->arrayrefint03([qw(a b c)])
126 }, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings';
127
128 # TEST StrOrArrayRef
129
130 ok $type->StrOrArrayRef_attr('string')
131  => 'String part of union is good';
132
133 ok $type->StrOrArrayRef_attr([1,2,3])
134  => 'arrayref part of union is good';
135  
136 like exception {
137     $type->StrOrArrayRef_attr({a=>111});
138 }, qr/Attribute \(StrOrArrayRef_attr\) does not pass the type constraint/ => 'Correctly failed to use a hashref';
139
140 # Test AtLeastOneInt
141
142 ok $type->AtLeastOneInt_attr([1,2]),
143  => 'Good assignment';
144
145 is_deeply $type->AtLeastOneInt_attr, [1,2]
146  => "Got expected values.";
147  
148 like exception {
149     $type->AtLeastOneInt_attr([]);
150 }, qr/Attribute \(AtLeastOneInt_attr\) does not pass the type constraint/ => 'properly fails to assign as []';
151
152 like exception {
153     $type->AtLeastOneInt_attr(['a','b']);
154 }, qr/Attribute \(AtLeastOneInt_attr\) does not pass the type constraint/ => 'properly fails arrayref of strings';
155
156 ## Test pipeoverloading
157
158 ok $type->pipeoverloading(1)
159  => 'Integer for union test accepted';
160  
161 ok $type->pipeoverloading('a')
162  => 'String for union test accepted';
163
164 like exception {
165     $type->pipeoverloading({a=>1,b=>2});
166 }, qr/Validation failed for 'Int|Str'/ => 'Union test corrected fails a HashRef';
167
168 ## test deep (ArrayRef[ArrayRef[HashRef[Int]]])
169
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}]]';
172
173 is_deeply $type->deep, [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]],
174  => 'Assignment is correct';
175  
176 like exception {
177     $type->deep({a=>1,b=>2});
178 }, qr/Attribute \(deep\) does not pass the type constraint/ => 'Deep Constraints properly fail';
179
180 # test deep2 (ArrayRef[Int|ArrayRef[HashRef[Int|Object]]])
181
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}]]';
184
185 is_deeply $type->deep2, [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]],
186  => 'Assignment is correct';
187  
188 like exception {
189     $type->deep2({a=>1,b=>2});
190 }, qr/Attribute \(deep2\) does not pass the type constraint/ => 'Deep Constraints properly fail';
191
192 like exception {
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';
195
196
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}]]';
199
200
201 is_deeply $type->deep2, [[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]],
202  => 'Assignment is correct';
203  
204 ok $type->deep2([1,2,3])
205  => 'Assigned deep2 to [1,2,3]';
206
207
208 is_deeply $type->deep2, [1,2,3],
209  => 'Assignment is correct';
210  
211 ## Test jobs
212
213 ok $type->enum('Programming')
214  => 'Good Assignment of Programming to Enum';
215
216
217 like exception {
218     $type->enum('ddddd');
219 }, qr/Attribute \(enum\) does not pass the type constraint/ => 'Enum properly fails';
220
221 ## Test SubOfMyArrayRefInt01_attr
222
223 ok $type->SubOfMyArrayRefInt01_attr([15,20,25])
224  => 'Assigned SubOfMyArrayRefInt01_attr to [15,20,25]';
225
226 is_deeply $type->SubOfMyArrayRefInt01_attr, [15,20,25],
227  => 'Assignment is correct';
228  
229 like exception {
230     $type->SubOfMyArrayRefInt01_attr([15,5,20]);
231 }, qr/Attribute \(SubOfMyArrayRefInt01_attr\) does not pass the type constraint/
232  => 'SubOfMyArrayRefInt01 Constraints properly fail';
233
234 ## test WierdIntegersArrayRef1 
235
236 ok $type->WierdIntegersArrayRef1_attr([5,10,1000])
237  => 'Assigned deep2 to [5,10,1000]';
238
239 is_deeply $type->WierdIntegersArrayRef1_attr, [5,10,1000],
240  => 'Assignment is correct';
241  
242 like exception {
243     $type->WierdIntegersArrayRef1_attr({a=>1,b=>2});
244 }, qr/Attribute \(WierdIntegersArrayRef1_attr\) does not pass the type constraint/
245  => 'Constraints properly fail';
246
247 like exception {
248     $type->WierdIntegersArrayRef1_attr([5,10,1]);
249 }, qr/Attribute \(WierdIntegersArrayRef1_attr\) does not pass the type constraint/
250  => 'Constraints properly fail';
251
252 like exception {
253     $type->WierdIntegersArrayRef1_attr([1]);
254 }, qr/Attribute \(WierdIntegersArrayRef1_attr\) does not pass the type constraint/
255  => 'Constraints properly fail';
256
257 ## test WierdIntegersArrayRef2 
258
259 ok $type->WierdIntegersArrayRef2_attr([5,10,$type])
260  => 'Assigned deep2 to [5,10,$type]';
261
262 is_deeply $type->WierdIntegersArrayRef2_attr, [5,10,$type],
263  => 'Assignment is correct';
264  
265 like exception {
266     $type->WierdIntegersArrayRef2_attr({a=>1,b=>2});
267 }, qr/Attribute \(WierdIntegersArrayRef2_attr\) does not pass the type constraint/
268  => 'Constraints properly fail';
269
270 like exception {
271     $type->WierdIntegersArrayRef2_attr([5,10,1]);
272 }, qr/Attribute \(WierdIntegersArrayRef2_attr\) does not pass the type constraint/
273  => 'Constraints properly fail';
274
275 like exception {
276     $type->WierdIntegersArrayRef2_attr([1]);
277 }, qr/Attribute \(WierdIntegersArrayRef2_attr\) does not pass the type constraint/
278  => 'Constraints properly fail';
279
280 done_testing();