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