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