3f250e66feaa682acdbfae0b0e7d5f7aae324c95
[gitmo/MooseX-Types.git] / t / 13_typedecorator.t
1 #!/usr/bin/env perl
2 use warnings;
3 use strict;
4
5 use Test::More tests => 49;
6 use Test::Exception;
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
20     );
21     
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 }
33
34 ## Make sure we have a 'create object sanity check'
35
36 ok my $type = Test::MooseX::TypeLibrary::TypeDecorator->new(),
37  => 'Created some sort of object';
38  
39 isa_ok $type, 'Test::MooseX::TypeLibrary::TypeDecorator'
40  => "Yes, it's the correct kind of object";
41
42 ## test arrayrefbase normal and coercion
43
44 ok $type->arrayrefbase([qw(a b c d e)])
45  => 'Assigned arrayrefbase qw(a b c d e)';
46  
47 is_deeply $type->arrayrefbase, [qw(a b c d e)],
48  => 'Assignment is correct';
49
50 ok $type->arrayrefbase('d,e,f')
51  => 'Assignment arrayrefbase d,e,f to test coercion';
52  
53 is_deeply $type->arrayrefbase, [qw(d e f)],
54  => 'Assignment and coercion is correct';
55
56 ## test arrayrefint01 normal and coercion
57
58 ok $type->arrayrefint01([qw(1 2 3)])
59  => 'Assignment arrayrefint01 qw(1 2 3)';
60  
61 is_deeply $type->arrayrefint01, [qw(1 2 3)],
62  => 'Assignment is correct';
63
64 ok $type->arrayrefint01('4.5.6')
65  => 'Assigned arrayrefint01 4.5.6 to test coercion from Str';
66  
67 is_deeply $type->arrayrefint01, [qw(4 5 6)],
68  => 'Assignment and coercion is correct';
69
70 ok $type->arrayrefint01({a=>7,b=>8})
71  => 'Assigned arrayrefint01 {a=>7,b=>8} to test coercion from HashRef';
72  
73 is_deeply $type->arrayrefint01, [qw(7 8)],
74  => 'Assignment and coercion is correct';
75  
76 throws_ok sub {
77     $type->arrayrefint01([qw(a b c)])
78 }, qr/Attribute \(arrayrefint01\) does not pass the type constraint/ => 'Dies when values are strings';
79
80 ## test arrayrefint02 normal and coercion
81
82 ok $type->arrayrefint02([qw(1 2 3)])
83  => 'Assigned arrayrefint02 qw(1 2 3)';
84  
85 is_deeply $type->arrayrefint02, [qw(1 2 3)],
86  => 'Assignment is correct';
87
88 ok $type->arrayrefint02('4:5:6')
89  => 'Assigned arrayrefint02 4:5:6 to test coercion from Str';
90  
91 is_deeply $type->arrayrefint02, [qw(4 5 6)],
92  => 'Assignment and coercion is correct';
93
94 ok $type->arrayrefint02({a=>7,b=>8})
95  => 'Assigned arrayrefint02 {a=>7,b=>8} to test coercion from HashRef';
96  
97 is_deeply $type->arrayrefint02, [qw(7 8)],
98  => 'Assignment and coercion is correct';
99  
100 ok $type->arrayrefint02({a=>'AA',b=>'BBB', c=>'CCCCCCC'})
101  => "Assigned arrayrefint02 {a=>'AA',b=>'BBB', c=>'CCCCCCC'} to test coercion from HashRef";
102  
103 is_deeply $type->arrayrefint02, [qw(2 3 7)],
104  => 'Assignment and coercion is correct';
105
106 ok $type->arrayrefint02({a=>[1,2],b=>[3,4]})
107  => "Assigned arrayrefint02 {a=>[1,2],b=>[3,4]} to test coercion from HashRef";
108  
109 is_deeply $type->arrayrefint02, [qw(1 2 3 4)],
110  => 'Assignment and coercion is correct';
111  
112 # test arrayrefint03 
113
114 ok $type->arrayrefint03([qw(11 12 13)])
115  => 'Assigned arrayrefint01 qw(11 12 13)';
116  
117 is_deeply $type->arrayrefint03, [qw(11 12 13)],
118  => 'Assignment is correct';
119  
120 throws_ok sub {
121     $type->arrayrefint03([qw(a b c)])
122 }, qr/Attribute \(arrayrefint03\) does not pass the type constraint/ => 'Dies when values are strings';
123
124 # TEST StrOrArrayRef
125
126 ok $type->StrOrArrayRef('string')
127  => 'String part of union is good';
128
129 ok $type->StrOrArrayRef([1,2,3])
130  => 'arrayref part of union is good';
131  
132 throws_ok sub {
133     $type->StrOrArrayRef({a=>111});
134 }, qr/Attribute \(StrOrArrayRef\) does not pass the type constraint/ => 'Correctly failed to use a hashref';
135
136 # Test AtLeastOneInt
137
138 ok $type->AtLeastOneInt([1,2]),
139  => 'Good assignment';
140
141 is_deeply $type->AtLeastOneInt, [1,2]
142  => "Got expected values.";
143  
144 throws_ok sub {
145     $type->AtLeastOneInt([]);
146 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails to assign as []';
147
148 throws_ok sub {
149     $type->AtLeastOneInt(['a','b']);
150 }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails arrayref of strings';
151
152 ## Test pipeoverloading
153
154 ok $type->pipeoverloading(1)
155  => 'Integer for union test accepted';
156  
157 ok $type->pipeoverloading('a')
158  => 'String for union test accepted';
159
160 throws_ok sub {
161     $type->pipeoverloading({a=>1,b=>2});
162 }, qr/Validation failed for 'Int|Str'/ => 'Union test corrected fails a HashRef';
163
164 ## test deep (ArrayRef[ArrayRef[HashRef[Int]]])
165
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}]]';
168
169 is_deeply $type->deep, [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]],
170  => 'Assignment is correct';
171  
172 throws_ok sub {
173     $type->deep({a=>1,b=>2});
174 }, qr/Attribute \(deep\) does not pass the type constraint/ => 'Deep Constraints properly fail';
175
176 # test deep2 (ArrayRef[Int|ArrayRef[HashRef[Int|Object]]])
177
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}]]';
180
181 is_deeply $type->deep2, [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]],
182  => 'Assignment is correct';
183  
184 throws_ok sub {
185     $type->deep2({a=>1,b=>2});
186 }, qr/Attribute \(deep2\) does not pass the type constraint/ => 'Deep Constraints properly fail';
187
188 throws_ok sub {
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';
191
192
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}]]';
195
196
197 is_deeply $type->deep2, [[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]],
198  => 'Assignment is correct';
199  
200 ok $type->deep2([1,2,3])
201  => 'Assigned deep2 to [1,2,3]';
202
203
204 is_deeply $type->deep2, [1,2,3],
205  => 'Assignment is correct';
206  
207 ## Test jobs
208
209 ok $type->enum('Programming')
210  => 'Good Assignment of Programming to Enum';
211
212
213 throws_ok sub {
214     $type->enum('ddddd');
215 }, qr/Attribute \(enum\) does not pass the type constraint/ => 'Enum properly fails';