Commit | Line | Data |
c2463b82 |
1 | #!/usr/bin/env perl |
2 | use warnings; |
3 | use strict; |
4 | |
686e5888 |
5 | use Test::More tests => 49; |
a706b0f2 |
6 | use Test::Exception; |
c2463b82 |
7 | use FindBin; |
8 | use lib "$FindBin::Bin/lib"; |
c2463b82 |
9 | |
20b6a7d1 |
10 | { |
11 | package Test::MooseX::TypeLibrary::TypeDecorator; |
12 | |
13 | use Moose; |
a706b0f2 |
14 | use MooseX::Types::Moose qw( |
575e6fc1 |
15 | Int Str ArrayRef HashRef Object |
a706b0f2 |
16 | ); |
20b6a7d1 |
17 | use DecoratorLibrary qw( |
cf1a8bfa |
18 | MyArrayRefBase MyArrayRefInt01 MyArrayRefInt02 StrOrArrayRef |
686e5888 |
19 | AtLeastOneInt Jobs |
20b6a7d1 |
20 | ); |
21 | |
22 | has 'arrayrefbase' => (is=>'rw', isa=>MyArrayRefBase, coerce=>1); |
23 | has 'arrayrefint01' => (is=>'rw', isa=>MyArrayRefInt01, coerce=>1); |
a706b0f2 |
24 | has 'arrayrefint02' => (is=>'rw', isa=>MyArrayRefInt02, coerce=>1); |
25 | has 'arrayrefint03' => (is=>'rw', isa=>MyArrayRefBase[Int]); |
cf1a8bfa |
26 | has 'StrOrArrayRef' => (is=>'rw', isa=>StrOrArrayRef); |
e088dd03 |
27 | has 'AtLeastOneInt' => (is=>'rw', isa=>AtLeastOneInt); |
575e6fc1 |
28 | has 'pipeoverloading' => (is=>'rw', isa=>Int|Str); |
475bbd1d |
29 | has 'deep' => (is=>'rw', isa=>ArrayRef[ArrayRef[HashRef[Int]]] ); |
575e6fc1 |
30 | has 'deep2' => (is=>'rw', isa=>ArrayRef[Int|ArrayRef[HashRef[Int|Object]]] ); |
686e5888 |
31 | has 'enum' => (is=>'rw', isa=>Jobs); |
20b6a7d1 |
32 | } |
c2463b82 |
33 | |
20b6a7d1 |
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 | |
bb5b7b28 |
44 | ok $type->arrayrefbase([qw(a b c d e)]) |
45 | => 'Assigned arrayrefbase qw(a b c d e)'; |
20b6a7d1 |
46 | |
bb5b7b28 |
47 | is_deeply $type->arrayrefbase, [qw(a b c d e)], |
a706b0f2 |
48 | => 'Assignment is correct'; |
20b6a7d1 |
49 | |
50 | ok $type->arrayrefbase('d,e,f') |
a706b0f2 |
51 | => 'Assignment arrayrefbase d,e,f to test coercion'; |
20b6a7d1 |
52 | |
53 | is_deeply $type->arrayrefbase, [qw(d e f)], |
a706b0f2 |
54 | => 'Assignment and coercion is correct'; |
20b6a7d1 |
55 | |
56 | ## test arrayrefint01 normal and coercion |
57 | |
54f5d4e6 |
58 | ok $type->arrayrefint01([qw(1 2 3)]) |
a706b0f2 |
59 | => 'Assignment arrayrefint01 qw(1 2 3)'; |
20b6a7d1 |
60 | |
54f5d4e6 |
61 | is_deeply $type->arrayrefint01, [qw(1 2 3)], |
a706b0f2 |
62 | => 'Assignment is correct'; |
20b6a7d1 |
63 | |
54f5d4e6 |
64 | ok $type->arrayrefint01('4.5.6') |
a706b0f2 |
65 | => 'Assigned arrayrefint01 4.5.6 to test coercion from Str'; |
20b6a7d1 |
66 | |
54f5d4e6 |
67 | is_deeply $type->arrayrefint01, [qw(4 5 6)], |
a706b0f2 |
68 | => 'Assignment and coercion is correct'; |
20b6a7d1 |
69 | |
54f5d4e6 |
70 | ok $type->arrayrefint01({a=>7,b=>8}) |
a706b0f2 |
71 | => 'Assigned arrayrefint01 {a=>7,b=>8} to test coercion from HashRef'; |
54f5d4e6 |
72 | |
73 | is_deeply $type->arrayrefint01, [qw(7 8)], |
a706b0f2 |
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'; |
54f5d4e6 |
119 | |
a706b0f2 |
120 | throws_ok sub { |
121 | $type->arrayrefint03([qw(a b c)]) |
cf1a8bfa |
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}); |
e088dd03 |
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([]); |
d9002a85 |
146 | }, qr/Attribute \(AtLeastOneInt\) does not pass the type constraint/ => 'properly fails to assign as []'; |
e088dd03 |
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 | |
377378b8 |
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}); |
575e6fc1 |
162 | }, qr/Validation failed for 'Int|Str'/ => 'Union test corrected fails a HashRef'; |
377378b8 |
163 | |
575e6fc1 |
164 | ## test deep (ArrayRef[ArrayRef[HashRef[Int]]]) |
377378b8 |
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}); |
575e6fc1 |
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}]]) |
764e65c8 |
179 | => 'Assigned deep2 to [[{a=>1,b=>2},{c=>3,d=>4}],[{e=>5}]]'; |
575e6fc1 |
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}]]) |
764e65c8 |
194 | => 'Assigned deep2 to [[{a=>1,b=>2},{c=>3,d=>$type}],[{e=>5}]]'; |
575e6fc1 |
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]) |
764e65c8 |
201 | => 'Assigned deep2 to [1,2,3]'; |
575e6fc1 |
202 | |
203 | |
204 | is_deeply $type->deep2, [1,2,3], |
205 | => 'Assignment is correct'; |
686e5888 |
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'; |