Commit | Line | Data |
bfef1b30 |
1 | BEGIN { |
2 | use strict; |
3 | use warnings; |
78f55946 |
4 | use Test::More tests=>37; |
bfef1b30 |
5 | use Test::Exception; |
6 | |
7 | use_ok 'Moose::Util::TypeConstraints'; |
dbd75632 |
8 | use_ok 'MooseX::Meta::TypeConstraint::Structured::Generator'; |
7e2f0558 |
9 | use_ok 'MooseX::Meta::TypeConstraint::Structured::Positional'; |
99b27cbd |
10 | use_ok 'MooseX::Meta::TypeConstraint::Structured::Optional'; |
7e2f0558 |
11 | use_ok 'MooseX::Meta::TypeConstraint::Structured::Named'; |
bfef1b30 |
12 | } |
13 | |
99b27cbd |
14 | my $optional = MooseX::Meta::TypeConstraint::Structured::Generator->new( |
15 | name => 'Optional', |
16 | structured_type => 'MooseX::Meta::TypeConstraint::Structured::Optional', |
17 | package_defined_in => __PACKAGE__, |
18 | parent => find_type_constraint('ArrayRef'), |
19 | ); |
20 | |
dbd75632 |
21 | my $tuple = MooseX::Meta::TypeConstraint::Structured::Generator->new( |
bfef1b30 |
22 | name => 'Tuple', |
7e2f0558 |
23 | structured_type => 'MooseX::Meta::TypeConstraint::Structured::Positional', |
bfef1b30 |
24 | package_defined_in => __PACKAGE__, |
7e2f0558 |
25 | parent => find_type_constraint('ArrayRef'), |
bfef1b30 |
26 | ); |
67be6b65 |
27 | |
28 | my $dict = MooseX::Meta::TypeConstraint::Structured::Generator->new( |
29 | name => 'Dict', |
30 | structured_type => 'MooseX::Meta::TypeConstraint::Structured::Named', |
31 | package_defined_in => __PACKAGE__, |
32 | parent => find_type_constraint('HashRef'), |
33 | ); |
bfef1b30 |
34 | |
99b27cbd |
35 | Moose::Util::TypeConstraints::register_type_constraint($optional); |
011bacc6 |
36 | Moose::Util::TypeConstraints::register_type_constraint($tuple); |
67be6b65 |
37 | Moose::Util::TypeConstraints::register_type_constraint($dict); |
bfef1b30 |
38 | |
011bacc6 |
39 | ## Make sure the new type constraints have been registered |
bfef1b30 |
40 | |
011bacc6 |
41 | ok Moose::Util::TypeConstraints::find_type_constraint('Tuple') |
42 | => 'Found the Tuple Type'; |
67be6b65 |
43 | |
44 | ok Moose::Util::TypeConstraints::find_type_constraint('Dict') |
45 | => 'Found the Tuple Type'; |
46 | |
47 | ok Moose::Util::TypeConstraints::find_type_constraint('Optional') |
48 | => 'Found the Tuple Type'; |
bfef1b30 |
49 | |
bfef1b30 |
50 | { |
dbd75632 |
51 | package Test::MooseX::Types::Structured::BasicAttributes; |
bfef1b30 |
52 | |
011bacc6 |
53 | use Moose; |
54 | use Moose::Util::TypeConstraints; |
bfef1b30 |
55 | |
011bacc6 |
56 | has 'tuple' => (is=>'rw', isa=>'Tuple[Int,Str,Int]'); |
99b27cbd |
57 | has 'tuple_with_parameterized' => (is=>'rw', isa=>'Tuple[Int,Str,Int,ArrayRef[Int]]'); |
58 | has 'tuple_with_optional' => (is=>'rw', isa=>'Tuple[Int,Str,Int,Optional[Int,Int]]'); |
59 | has 'tuple_with_union' => (is=>'rw', isa=>'Tuple[Int,Str,Int|Object,Optional[Int|Object,Int]]'); |
67be6b65 |
60 | |
78f55946 |
61 | has 'dict' => (is=>'rw', isa=>'Dict[name=>Str,age=>Int]'); |
62 | has 'dict_with_parameterized' => (is=>'rw', isa=>'Dict[name=>Str, age=>Int, telephone=>ArrayRef[Int]]'); |
63 | has 'dict_with_optional' => (is=>'rw', isa=>'Dict[name=>Str, age=>Int, Optional[opt1=>Str,opt2=>Object]]'); |
67be6b65 |
64 | |
bfef1b30 |
65 | } |
66 | |
011bacc6 |
67 | |
78f55946 |
68 | ok my $obj = Test::MooseX::Types::Structured::BasicAttributes->new, |
bfef1b30 |
69 | => 'Got a good object'; |
70 | |
99b27cbd |
71 | ok Moose::Util::TypeConstraints::find_type_constraint('Tuple[Int,Str,Int]') |
72 | => 'Found expected type constraint'; |
73 | |
74 | ok Moose::Util::TypeConstraints::find_type_constraint('Tuple[Int,Str,Int,Optional[Int,Int]]') |
75 | => 'Found expected type constraint'; |
76 | |
78f55946 |
77 | ## dict Dict[name=>Str, Age=>Int] |
78 | |
79 | ok $obj->dict({name=>'John', age=>39}) |
80 | => 'Dict[name=>Str, Age=>Int] properly succeeds'; |
81 | |
82 | |
83 | |
84 | |
85 | |
86 | |
99b27cbd |
87 | ## Test tuple (Tuple[Int,Str,Int]) |
88 | |
78f55946 |
89 | ok $obj->tuple([1,'hello',3]) |
011bacc6 |
90 | => "[1,'hello',3] properly suceeds"; |
91 | |
92 | throws_ok sub { |
78f55946 |
93 | $obj->tuple([1,2,'world']); |
011bacc6 |
94 | }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails"; |
bfef1b30 |
95 | |
011bacc6 |
96 | throws_ok sub { |
78f55946 |
97 | $obj->tuple(['hello1',2,3]); |
011bacc6 |
98 | }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails"; |
99 | |
100 | throws_ok sub { |
78f55946 |
101 | $obj->tuple(['hello2',2,'world']); |
011bacc6 |
102 | }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails"; |
103 | |
104 | |
99b27cbd |
105 | ## Test tuple_with_parameterized (Tuple[Int,Str,Int,ArrayRef[Int]]) |
106 | |
78f55946 |
107 | ok $obj->tuple_with_parameterized([1,'hello',3,[1,2,3]]) |
99b27cbd |
108 | => "[1,'hello',3,[1,2,3]] properly suceeds"; |
109 | |
110 | throws_ok sub { |
78f55946 |
111 | $obj->tuple_with_parameterized([1,2,'world']); |
99b27cbd |
112 | }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails"; |
113 | |
114 | throws_ok sub { |
78f55946 |
115 | $obj->tuple_with_parameterized(['hello1',2,3]); |
99b27cbd |
116 | }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails"; |
117 | |
118 | throws_ok sub { |
78f55946 |
119 | $obj->tuple_with_parameterized(['hello2',2,'world']); |
99b27cbd |
120 | }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails"; |
121 | |
122 | throws_ok sub { |
78f55946 |
123 | $obj->tuple_with_parameterized([1,'hello',3,[1,2,'world']]); |
99b27cbd |
124 | }, qr/Validation failed for 'ArrayRef\[Int\]'/ => "[1,'hello',3,[1,2,'world']] properly fails"; |
125 | |
126 | |
127 | ## Test tuple_with_optional (Tuple[Int,Str,Int,Optional[Int,Int]]) |
128 | |
78f55946 |
129 | ok $obj->tuple_with_optional([1,'hello',3]) |
99b27cbd |
130 | => "[1,'hello',3] properly suceeds"; |
131 | |
78f55946 |
132 | ok $obj->tuple_with_optional([1,'hello',3,1]) |
99b27cbd |
133 | => "[1,'hello',3,1] properly suceeds"; |
134 | |
78f55946 |
135 | ok $obj->tuple_with_optional([1,'hello',3,4]) |
99b27cbd |
136 | => "[1,'hello',3,4] properly suceeds"; |
137 | |
78f55946 |
138 | ok $obj->tuple_with_optional([1,'hello',3,4,5]) |
99b27cbd |
139 | => "[1,'hello',3,4,5] properly suceeds"; |
140 | |
141 | throws_ok sub { |
78f55946 |
142 | $obj->tuple_with_optional([1,'hello',3,4,5,6]); |
99b27cbd |
143 | }, qr/Too Many arguments for the available type constraints/ => "[1,'hello',3,4,5,6] properly fails"; |
144 | |
145 | throws_ok sub { |
78f55946 |
146 | $obj->tuple_with_optional([1,2,'world']); |
99b27cbd |
147 | }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails"; |
148 | |
149 | throws_ok sub { |
78f55946 |
150 | $obj->tuple_with_optional(['hello1',2,3]); |
99b27cbd |
151 | }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails"; |
152 | |
153 | throws_ok sub { |
78f55946 |
154 | $obj->tuple_with_optional(['hello2',2,'world']); |
99b27cbd |
155 | }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails"; |
156 | |
157 | ## tuple_with_union Tuple[Int,Str,Int|Object,Optional[Int|Object,Int]] |
158 | |
67be6b65 |
159 | SKIP: { |
99b27cbd |
160 | |
67be6b65 |
161 | skip "Unions not supported for string parsed type constraints" => 8; |
99b27cbd |
162 | |
78f55946 |
163 | ok $obj->tuple_with_union([1,'hello',3]) |
67be6b65 |
164 | => "[1,'hello',3] properly suceeds"; |
99b27cbd |
165 | |
78f55946 |
166 | ok $obj->tuple_with_union([1,'hello',3,1]) |
67be6b65 |
167 | => "[1,'hello',3,1] properly suceeds"; |
99b27cbd |
168 | |
78f55946 |
169 | ok $obj->tuple_with_union([1,'hello',3,4]) |
67be6b65 |
170 | => "[1,'hello',3,4] properly suceeds"; |
99b27cbd |
171 | |
78f55946 |
172 | ok $obj->tuple_with_union([1,'hello',3,4,5]) |
67be6b65 |
173 | => "[1,'hello',3,4,5] properly suceeds"; |
99b27cbd |
174 | |
67be6b65 |
175 | throws_ok sub { |
78f55946 |
176 | $obj->tuple_with_union([1,'hello',3,4,5,6]); |
67be6b65 |
177 | }, qr/Too Many arguments for the available type constraints/ => "[1,'hello',3,4,5,6] properly fails"; |
99b27cbd |
178 | |
67be6b65 |
179 | throws_ok sub { |
78f55946 |
180 | $obj->tuple_with_union([1,2,'world']); |
67be6b65 |
181 | }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails"; |
011bacc6 |
182 | |
67be6b65 |
183 | throws_ok sub { |
78f55946 |
184 | $obj->tuple_with_union(['hello1',2,3]); |
67be6b65 |
185 | }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails"; |
186 | |
187 | throws_ok sub { |
78f55946 |
188 | $obj->tuple_with_union(['hello2',2,'world']); |
67be6b65 |
189 | }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails"; |
190 | } |
011bacc6 |
191 | |