4 use Test::More tests=>36;
7 use_ok 'Moose::Util::TypeConstraints';
8 use_ok 'MooseX::Meta::TypeConstraint::Structured::Generator';
9 use_ok 'MooseX::Meta::TypeConstraint::Structured::Positional';
10 use_ok 'MooseX::Meta::TypeConstraint::Structured::Optional';
11 use_ok 'MooseX::Meta::TypeConstraint::Structured::Named';
14 my $optional = MooseX::Meta::TypeConstraint::Structured::Generator->new(
16 structured_type => 'MooseX::Meta::TypeConstraint::Structured::Optional',
17 package_defined_in => __PACKAGE__,
18 parent => find_type_constraint('ArrayRef'),
21 my $tuple = MooseX::Meta::TypeConstraint::Structured::Generator->new(
23 structured_type => 'MooseX::Meta::TypeConstraint::Structured::Positional',
24 package_defined_in => __PACKAGE__,
25 parent => find_type_constraint('ArrayRef'),
28 my $dict = MooseX::Meta::TypeConstraint::Structured::Generator->new(
30 structured_type => 'MooseX::Meta::TypeConstraint::Structured::Named',
31 package_defined_in => __PACKAGE__,
32 parent => find_type_constraint('HashRef'),
35 Moose::Util::TypeConstraints::register_type_constraint($optional);
36 Moose::Util::TypeConstraints::register_type_constraint($tuple);
37 Moose::Util::TypeConstraints::register_type_constraint($dict);
39 ## Make sure the new type constraints have been registered
41 ok Moose::Util::TypeConstraints::find_type_constraint('Tuple')
42 => 'Found the Tuple Type';
44 ok Moose::Util::TypeConstraints::find_type_constraint('Dict')
45 => 'Found the Tuple Type';
47 ok Moose::Util::TypeConstraints::find_type_constraint('Optional')
48 => 'Found the Tuple Type';
51 package Test::MooseX::Types::Structured::BasicAttributes;
54 use Moose::Util::TypeConstraints;
56 has 'tuple' => (is=>'rw', isa=>'Tuple[Int,Str,Int]');
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]]');
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]]');
67 #use Data::Dump qw/dump/;
68 #warn dump Moose::Util::TypeConstraints::list_all_type_constraints;
70 ok my $positioned_obj = Test::MooseX::Types::Structured::BasicAttributes->new,
71 => 'Got a good object';
73 ok Moose::Util::TypeConstraints::find_type_constraint('Tuple[Int,Str,Int]')
74 => 'Found expected type constraint';
76 ok Moose::Util::TypeConstraints::find_type_constraint('Tuple[Int,Str,Int,Optional[Int,Int]]')
77 => 'Found expected type constraint';
79 ## Test tuple (Tuple[Int,Str,Int])
81 ok $positioned_obj->tuple([1,'hello',3])
82 => "[1,'hello',3] properly suceeds";
85 $positioned_obj->tuple([1,2,'world']);
86 }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
89 $positioned_obj->tuple(['hello1',2,3]);
90 }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
93 $positioned_obj->tuple(['hello2',2,'world']);
94 }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
97 ## Test tuple_with_parameterized (Tuple[Int,Str,Int,ArrayRef[Int]])
99 ok $positioned_obj->tuple_with_parameterized([1,'hello',3,[1,2,3]])
100 => "[1,'hello',3,[1,2,3]] properly suceeds";
103 $positioned_obj->tuple_with_parameterized([1,2,'world']);
104 }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
107 $positioned_obj->tuple_with_parameterized(['hello1',2,3]);
108 }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
111 $positioned_obj->tuple_with_parameterized(['hello2',2,'world']);
112 }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
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";
119 ## Test tuple_with_optional (Tuple[Int,Str,Int,Optional[Int,Int]])
121 ok $positioned_obj->tuple_with_optional([1,'hello',3])
122 => "[1,'hello',3] properly suceeds";
124 ok $positioned_obj->tuple_with_optional([1,'hello',3,1])
125 => "[1,'hello',3,1] properly suceeds";
127 ok $positioned_obj->tuple_with_optional([1,'hello',3,4])
128 => "[1,'hello',3,4] properly suceeds";
130 ok $positioned_obj->tuple_with_optional([1,'hello',3,4,5])
131 => "[1,'hello',3,4,5] properly suceeds";
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";
138 $positioned_obj->tuple_with_optional([1,2,'world']);
139 }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
142 $positioned_obj->tuple_with_optional(['hello1',2,3]);
143 }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
146 $positioned_obj->tuple_with_optional(['hello2',2,'world']);
147 }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
149 ## tuple_with_union Tuple[Int,Str,Int|Object,Optional[Int|Object,Int]]
153 skip "Unions not supported for string parsed type constraints" => 8;
155 ok $positioned_obj->tuple_with_union([1,'hello',3])
156 => "[1,'hello',3] properly suceeds";
158 ok $positioned_obj->tuple_with_union([1,'hello',3,1])
159 => "[1,'hello',3,1] properly suceeds";
161 ok $positioned_obj->tuple_with_union([1,'hello',3,4])
162 => "[1,'hello',3,4] properly suceeds";
164 ok $positioned_obj->tuple_with_union([1,'hello',3,4,5])
165 => "[1,'hello',3,4,5] properly suceeds";
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";
172 $positioned_obj->tuple_with_union([1,2,'world']);
173 }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
176 $positioned_obj->tuple_with_union(['hello1',2,3]);
177 }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
180 $positioned_obj->tuple_with_union(['hello2',2,'world']);
181 }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";