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