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