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