Commit | Line | Data |
bfef1b30 |
1 | BEGIN { |
2 | use strict; |
3 | use warnings; |
67be6b65 |
4 | use Test::More tests=>36; |
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 | |
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]]'); |
64 | |
bfef1b30 |
65 | } |
66 | |
99b27cbd |
67 | #use Data::Dump qw/dump/; |
68 | #warn dump Moose::Util::TypeConstraints::list_all_type_constraints; |
011bacc6 |
69 | |
dbd75632 |
70 | ok my $positioned_obj = Test::MooseX::Types::Structured::BasicAttributes->new, |
bfef1b30 |
71 | => 'Got a good object'; |
72 | |
99b27cbd |
73 | ok Moose::Util::TypeConstraints::find_type_constraint('Tuple[Int,Str,Int]') |
74 | => 'Found expected type constraint'; |
75 | |
76 | ok Moose::Util::TypeConstraints::find_type_constraint('Tuple[Int,Str,Int,Optional[Int,Int]]') |
77 | => 'Found expected type constraint'; |
78 | |
79 | ## Test tuple (Tuple[Int,Str,Int]) |
80 | |
011bacc6 |
81 | ok $positioned_obj->tuple([1,'hello',3]) |
82 | => "[1,'hello',3] properly suceeds"; |
83 | |
84 | throws_ok sub { |
85 | $positioned_obj->tuple([1,2,'world']); |
86 | }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails"; |
bfef1b30 |
87 | |
011bacc6 |
88 | throws_ok sub { |
89 | $positioned_obj->tuple(['hello1',2,3]); |
90 | }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails"; |
91 | |
92 | throws_ok sub { |
93 | $positioned_obj->tuple(['hello2',2,'world']); |
94 | }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails"; |
95 | |
96 | |
99b27cbd |
97 | ## Test tuple_with_parameterized (Tuple[Int,Str,Int,ArrayRef[Int]]) |
98 | |
99 | ok $positioned_obj->tuple_with_parameterized([1,'hello',3,[1,2,3]]) |
100 | => "[1,'hello',3,[1,2,3]] properly suceeds"; |
101 | |
102 | throws_ok sub { |
103 | $positioned_obj->tuple_with_parameterized([1,2,'world']); |
104 | }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails"; |
105 | |
106 | throws_ok sub { |
107 | $positioned_obj->tuple_with_parameterized(['hello1',2,3]); |
108 | }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails"; |
109 | |
110 | throws_ok sub { |
111 | $positioned_obj->tuple_with_parameterized(['hello2',2,'world']); |
112 | }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails"; |
113 | |
114 | throws_ok sub { |
115 | $positioned_obj->tuple_with_parameterized([1,'hello',3,[1,2,'world']]); |
116 | }, qr/Validation failed for 'ArrayRef\[Int\]'/ => "[1,'hello',3,[1,2,'world']] properly fails"; |
117 | |
118 | |
119 | ## Test tuple_with_optional (Tuple[Int,Str,Int,Optional[Int,Int]]) |
120 | |
121 | ok $positioned_obj->tuple_with_optional([1,'hello',3]) |
122 | => "[1,'hello',3] properly suceeds"; |
123 | |
124 | ok $positioned_obj->tuple_with_optional([1,'hello',3,1]) |
125 | => "[1,'hello',3,1] properly suceeds"; |
126 | |
127 | ok $positioned_obj->tuple_with_optional([1,'hello',3,4]) |
128 | => "[1,'hello',3,4] properly suceeds"; |
129 | |
130 | ok $positioned_obj->tuple_with_optional([1,'hello',3,4,5]) |
131 | => "[1,'hello',3,4,5] properly suceeds"; |
132 | |
133 | throws_ok sub { |
134 | $positioned_obj->tuple_with_optional([1,'hello',3,4,5,6]); |
135 | }, qr/Too Many arguments for the available type constraints/ => "[1,'hello',3,4,5,6] properly fails"; |
136 | |
137 | throws_ok sub { |
138 | $positioned_obj->tuple_with_optional([1,2,'world']); |
139 | }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails"; |
140 | |
141 | throws_ok sub { |
142 | $positioned_obj->tuple_with_optional(['hello1',2,3]); |
143 | }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails"; |
144 | |
145 | throws_ok sub { |
146 | $positioned_obj->tuple_with_optional(['hello2',2,'world']); |
147 | }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails"; |
148 | |
149 | ## tuple_with_union Tuple[Int,Str,Int|Object,Optional[Int|Object,Int]] |
150 | |
67be6b65 |
151 | SKIP: { |
99b27cbd |
152 | |
67be6b65 |
153 | skip "Unions not supported for string parsed type constraints" => 8; |
99b27cbd |
154 | |
67be6b65 |
155 | ok $positioned_obj->tuple_with_union([1,'hello',3]) |
156 | => "[1,'hello',3] properly suceeds"; |
99b27cbd |
157 | |
67be6b65 |
158 | ok $positioned_obj->tuple_with_union([1,'hello',3,1]) |
159 | => "[1,'hello',3,1] properly suceeds"; |
99b27cbd |
160 | |
67be6b65 |
161 | ok $positioned_obj->tuple_with_union([1,'hello',3,4]) |
162 | => "[1,'hello',3,4] properly suceeds"; |
99b27cbd |
163 | |
67be6b65 |
164 | ok $positioned_obj->tuple_with_union([1,'hello',3,4,5]) |
165 | => "[1,'hello',3,4,5] properly suceeds"; |
99b27cbd |
166 | |
67be6b65 |
167 | throws_ok sub { |
168 | $positioned_obj->tuple_with_union([1,'hello',3,4,5,6]); |
169 | }, qr/Too Many arguments for the available type constraints/ => "[1,'hello',3,4,5,6] properly fails"; |
99b27cbd |
170 | |
67be6b65 |
171 | throws_ok sub { |
172 | $positioned_obj->tuple_with_union([1,2,'world']); |
173 | }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails"; |
011bacc6 |
174 | |
67be6b65 |
175 | throws_ok sub { |
176 | $positioned_obj->tuple_with_union(['hello1',2,3]); |
177 | }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails"; |
178 | |
179 | throws_ok sub { |
180 | $positioned_obj->tuple_with_union(['hello2',2,'world']); |
181 | }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails"; |
182 | } |
011bacc6 |
183 | |