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