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