4 use Test::More tests=>34;
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 Moose::Util::TypeConstraints::register_type_constraint($optional);
29 Moose::Util::TypeConstraints::register_type_constraint($tuple);
31 ## Make sure the new type constraints have been registered
33 ok Moose::Util::TypeConstraints::find_type_constraint('Tuple')
34 => 'Found the Tuple Type';
37 package Test::MooseX::Types::Structured::BasicAttributes;
40 use Moose::Util::TypeConstraints;
42 has 'tuple' => (is=>'rw', isa=>'Tuple[Int,Str,Int]');
43 has 'tuple_with_parameterized' => (is=>'rw', isa=>'Tuple[Int,Str,Int,ArrayRef[Int]]');
44 has 'tuple_with_optional' => (is=>'rw', isa=>'Tuple[Int,Str,Int,Optional[Int,Int]]');
45 has 'tuple_with_union' => (is=>'rw', isa=>'Tuple[Int,Str,Int|Object,Optional[Int|Object,Int]]');
48 #use Data::Dump qw/dump/;
49 #warn dump Moose::Util::TypeConstraints::list_all_type_constraints;
51 ok my $positioned_obj = Test::MooseX::Types::Structured::BasicAttributes->new,
52 => 'Got a good object';
54 ok Moose::Util::TypeConstraints::find_type_constraint('Tuple[Int,Str,Int]')
55 => 'Found expected type constraint';
57 ok Moose::Util::TypeConstraints::find_type_constraint('Tuple[Int,Str,Int,Optional[Int,Int]]')
58 => 'Found expected type constraint';
60 ## Test tuple (Tuple[Int,Str,Int])
62 ok $positioned_obj->tuple([1,'hello',3])
63 => "[1,'hello',3] properly suceeds";
66 $positioned_obj->tuple([1,2,'world']);
67 }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
70 $positioned_obj->tuple(['hello1',2,3]);
71 }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
74 $positioned_obj->tuple(['hello2',2,'world']);
75 }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
78 ## Test tuple_with_parameterized (Tuple[Int,Str,Int,ArrayRef[Int]])
80 ok $positioned_obj->tuple_with_parameterized([1,'hello',3,[1,2,3]])
81 => "[1,'hello',3,[1,2,3]] properly suceeds";
84 $positioned_obj->tuple_with_parameterized([1,2,'world']);
85 }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
88 $positioned_obj->tuple_with_parameterized(['hello1',2,3]);
89 }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
92 $positioned_obj->tuple_with_parameterized(['hello2',2,'world']);
93 }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
96 $positioned_obj->tuple_with_parameterized([1,'hello',3,[1,2,'world']]);
97 }, qr/Validation failed for 'ArrayRef\[Int\]'/ => "[1,'hello',3,[1,2,'world']] properly fails";
100 ## Test tuple_with_optional (Tuple[Int,Str,Int,Optional[Int,Int]])
102 ok $positioned_obj->tuple_with_optional([1,'hello',3])
103 => "[1,'hello',3] properly suceeds";
105 ok $positioned_obj->tuple_with_optional([1,'hello',3,1])
106 => "[1,'hello',3,1] properly suceeds";
108 ok $positioned_obj->tuple_with_optional([1,'hello',3,4])
109 => "[1,'hello',3,4] properly suceeds";
111 ok $positioned_obj->tuple_with_optional([1,'hello',3,4,5])
112 => "[1,'hello',3,4,5] properly suceeds";
115 $positioned_obj->tuple_with_optional([1,'hello',3,4,5,6]);
116 }, qr/Too Many arguments for the available type constraints/ => "[1,'hello',3,4,5,6] properly fails";
119 $positioned_obj->tuple_with_optional([1,2,'world']);
120 }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
123 $positioned_obj->tuple_with_optional(['hello1',2,3]);
124 }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
127 $positioned_obj->tuple_with_optional(['hello2',2,'world']);
128 }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
130 ## tuple_with_union Tuple[Int,Str,Int|Object,Optional[Int|Object,Int]]
132 ok $positioned_obj->tuple_with_union([1,'hello',3])
133 => "[1,'hello',3] properly suceeds";
135 ok $positioned_obj->tuple_with_union([1,'hello',3,1])
136 => "[1,'hello',3,1] properly suceeds";
138 ok $positioned_obj->tuple_with_union([1,'hello',3,4])
139 => "[1,'hello',3,4] properly suceeds";
141 ok $positioned_obj->tuple_with_union([1,'hello',3,4,5])
142 => "[1,'hello',3,4,5] properly suceeds";
145 $positioned_obj->tuple_with_union([1,'hello',3,4,5,6]);
146 }, qr/Too Many arguments for the available type constraints/ => "[1,'hello',3,4,5,6] properly fails";
149 $positioned_obj->tuple_with_union([1,2,'world']);
150 }, qr/Validation failed for 'Int' failed with value world/ => "[1,2,'world'] properly fails";
153 $positioned_obj->tuple_with_union(['hello1',2,3]);
154 }, qr/Validation failed for 'Int' failed with value hello1/ => "['hello',2,3] properly fails";
157 $positioned_obj->tuple_with_union(['hello2',2,'world']);
158 }, qr/Validation failed for 'Int' failed with value hello2/ => "['hello',2,'world'] properly fails";
161 #ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint('HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]')
162 # => 'detected correctly';
165 # [Moose::Util::TypeConstraints::_parse_parameterized_type_constraint('HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]')],
166 # ["HashRef", "key1", "Int", "key2", "Int", "key3", "ArrayRef[Int]"]
167 # => 'Correctly parsed HashRef[key1 => Int, key2=>Int, key3=>ArrayRef[Int]]';