4 use Test::More tests=>25;
9 package Test::MooseX::Meta::TypeConstraint::Structured;
12 use Moose::Util::TypeConstraints;
13 use MooseX::Meta::TypeConstraint::Structured::Named;
14 use MooseX::Meta::TypeConstraint::Structured::Positional;
21 my ($args, $optional) = @_;
23 my @optional = ref $optional eq 'ARRAY' ? @$optional : ();
25 return MooseX::Meta::TypeConstraint::Structured::Positional->new(
27 parent => find_type_constraint('ArrayRef'),
28 package_defined_in => __PACKAGE__,
30 _normalize_type_constraint($_);
36 my ($args, $optional) = @_;
38 my %optional = ref $optional eq 'HASH' ? @$optional : ();
40 return MooseX::Meta::TypeConstraint::Structured::Named->new(
42 parent => find_type_constraint('HashRef'),
43 package_defined_in => __PACKAGE__,
45 $_ => _normalize_type_constraint($args{$_});
50 sub _normalize_type_constraint {
52 if($tc && blessed $tc && $tc->isa('Moose::Meta::TypeConstraint')) {
55 return Moose::Util::TypeConstraints::find_or_parse_type_constraint($tc);
59 has 'tuple' => (is=>'rw', isa=>Tuple['Int', 'Str', 'MyString']);
60 has 'dict' => (is=>'rw', isa=>Dict[name=>'Str', age=>'Int']);
61 has 'dict_with_maybe' => (is=>'rw', isa=>Dict[name=>'Str', age=>'Maybe[Int]']);
62 has 'tuple_with_param' => (is=>'rw', isa=>Tuple['Int', 'Str', 'ArrayRef[Int]']);
63 has 'tuple_with_maybe' => (is=>'rw', isa=>Tuple['Int', 'Str', 'Maybe[Int]']);
64 has 'dict_with_tuple' => (is=>'rw', isa=>Dict[key1=>'Str', key2=>Tuple['Int','Str']]);
67 ## Instantiate a new test object
69 ok my $record = Test::MooseX::Meta::TypeConstraint::Structured->new
70 => 'Instantiated new Record test class.';
72 isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured'
73 => 'Created correct object type.';
75 ## Test Tuple type constraint
78 $record->tuple([1,'hello', 'test.abc.test']);
79 } => 'Set tuple attribute without error';
81 is $record->tuple->[0], 1
82 => 'correct set the tuple attribute index 0';
84 is $record->tuple->[1], 'hello'
85 => 'correct set the tuple attribute index 1';
87 is $record->tuple->[2], 'test.abc.test'
88 => 'correct set the tuple attribute index 2';
91 $record->tuple([1,'hello', 'test.xxx.test']);
92 }, qr/Validation failed for 'MyString'/
93 => 'Properly failed for bad value in custom type constraint';
96 $record->tuple(['asdasd',2, 'test.abc.test']);
97 }, qr/Validation failed for 'Int'/
98 => 'Got Expected Error for violating constraints';
100 ## Test the Dictionary type constraint
103 $record->dict({name=>'frith', age=>23});
104 } => 'Set dict attribute without error';
106 is $record->dict->{name}, 'frith'
107 => 'correct set the dict attribute name';
109 is $record->dict->{age}, 23
110 => 'correct set the dict attribute age';
113 $record->dict({name=>[1,2,3], age=>'sdfsdfsd'});
114 }, qr/Validation failed for 'Str'/
115 => 'Got Expected Error for bad value in dict';
117 ## Test tuple_with_maybe
120 $record->tuple_with_maybe([1,'hello', 1]);
121 } => 'Set tuple attribute without error';
124 $record->tuple_with_maybe([1,'hello', 'a']);
125 }, qr/Validation failed for 'Maybe\[Int\]'/
126 => 'Properly failed for bad value parameterized constraint';
129 $record->tuple_with_maybe([1,'hello']);
130 } => 'Set tuple attribute without error skipping optional parameter';
132 ## Test Tuple with parameterized type
135 $record->tuple_with_param([1,'hello', [1,2,3]]);
136 } => 'Set tuple attribute without error';
139 $record->tuple_with_param([1,'hello', [qw/a b c/]]);
140 }, qr/Validation failed for 'ArrayRef\[Int\]'/
141 => 'Properly failed for bad value parameterized constraint';
143 ## Test dict_with_maybe
146 $record->dict_with_maybe({name=>'frith', age=>23});
147 } => 'Set dict attribute without error';
149 is $record->dict_with_maybe->{name}, 'frith'
150 => 'correct set the dict attribute name';
152 is $record->dict_with_maybe->{age}, 23
153 => 'correct set the dict attribute age';
156 $record->dict_with_maybe({name=>[1,2,3], age=>'sdfsdfsd'});
157 }, qr/Validation failed for 'Str'/
158 => 'Got Expected Error for bad value in dict';
161 $record->dict_with_maybe({age=>30});
162 }, qr/Validation failed for 'Str'/
163 => 'Got Expected Error for missing named parameter';
166 $record->dict_with_maybe({name=>'usal'});
167 } => 'Set dict attribute without error, skipping optional';
169 ## Test dict_with_tuple
172 $record->dict_with_tuple({key1=>'Hello', key2=>[1,'World']});
173 } => 'Set tuple attribute without error';
176 $record->dict_with_tuple({key1=>'Hello', key2=>['World',2]});
177 }, qr/Validation failed for 'Int'/
178 => 'Threw error on bad constraint';