4 use Test::More tests=>25;
9 package Test::MooseX::Meta::TypeConstraint::Structured;
12 use Moose::Util::TypeConstraints;
13 use MooseX::Meta::TypeConstraint::Structured;
20 my @args = @{shift @_};
21 return MooseX::Meta::TypeConstraint::Structured->new(
23 parent => find_type_constraint('ArrayRef'),
24 package_defined_in => __PACKAGE__,
26 _normalize_type_constraint($_);
32 my %args = @{shift @_};
33 return MooseX::Meta::TypeConstraint::Structured->new(
35 parent => find_type_constraint('HashRef'),
36 package_defined_in => __PACKAGE__,
38 $_ => _normalize_type_constraint($args{$_});
43 sub _normalize_type_constraint {
45 if($tc && blessed $tc && $tc->isa('Moose::Meta::TypeConstraint')) {
48 return Moose::Util::TypeConstraints::find_or_parse_type_constraint($tc);
52 has 'tuple' => (is=>'rw', isa=>Tuple['Int', 'Str', 'MyString']);
53 has 'dict' => (is=>'rw', isa=>Dict[name=>'Str', age=>'Int']);
54 has 'dict_with_maybe' => (is=>'rw', isa=>Dict[name=>'Str', age=>'Maybe[Int]']);
55 has 'tuple_with_param' => (is=>'rw', isa=>Tuple['Int', 'Str', 'ArrayRef[Int]']);
56 has 'tuple_with_maybe' => (is=>'rw', isa=>Tuple['Int', 'Str', 'Maybe[Int]']);
57 has 'dict_with_tuple' => (is=>'rw', isa=>Dict[key1=>'Str', key2=>Tuple['Int','Str']]);
60 ## Instantiate a new test object
62 ok my $record = Test::MooseX::Meta::TypeConstraint::Structured->new
63 => 'Instantiated new Record test class.';
65 isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured'
66 => 'Created correct object type.';
68 ## Test Tuple type constraint
71 $record->tuple([1,'hello', 'test.abc.test']);
72 } => 'Set tuple attribute without error';
74 is $record->tuple->[0], 1
75 => 'correct set the tuple attribute index 0';
77 is $record->tuple->[1], 'hello'
78 => 'correct set the tuple attribute index 1';
80 is $record->tuple->[2], 'test.abc.test'
81 => 'correct set the tuple attribute index 2';
84 $record->tuple([1,'hello', 'test.xxx.test']);
85 }, qr/Validation failed for 'MyString'/
86 => 'Properly failed for bad value in custom type constraint';
89 $record->tuple(['asdasd',2, 'test.abc.test']);
90 }, qr/Validation failed for 'Int'/
91 => 'Got Expected Error for violating constraints';
93 ## Test the Dictionary type constraint
96 $record->dict({name=>'frith', age=>23});
97 } => 'Set dict attribute without error';
99 is $record->dict->{name}, 'frith'
100 => 'correct set the dict attribute name';
102 is $record->dict->{age}, 23
103 => 'correct set the dict attribute age';
106 $record->dict({name=>[1,2,3], age=>'sdfsdfsd'});
107 }, qr/Validation failed for 'Str'/
108 => 'Got Expected Error for bad value in dict';
110 ## Test tuple_with_maybe
113 $record->tuple_with_maybe([1,'hello', 1]);
114 } => 'Set tuple attribute without error';
117 $record->tuple_with_maybe([1,'hello', 'a']);
118 }, qr/Validation failed for 'Maybe\[Int\]'/
119 => 'Properly failed for bad value parameterized constraint';
122 $record->tuple_with_maybe([1,'hello']);
123 } => 'Set tuple attribute without error skipping optional parameter';
125 ## Test Tuple with parameterized type
128 $record->tuple_with_param([1,'hello', [1,2,3]]);
129 } => 'Set tuple attribute without error';
132 $record->tuple_with_param([1,'hello', [qw/a b c/]]);
133 }, qr/Validation failed for 'ArrayRef\[Int\]'/
134 => 'Properly failed for bad value parameterized constraint';
136 ## Test dict_with_maybe
139 $record->dict_with_maybe({name=>'frith', age=>23});
140 } => 'Set dict attribute without error';
142 is $record->dict_with_maybe->{name}, 'frith'
143 => 'correct set the dict attribute name';
145 is $record->dict_with_maybe->{age}, 23
146 => 'correct set the dict attribute age';
149 $record->dict_with_maybe({name=>[1,2,3], age=>'sdfsdfsd'});
150 }, qr/Validation failed for 'Str'/
151 => 'Got Expected Error for bad value in dict';
154 $record->dict_with_maybe({age=>30});
155 }, qr/Validation failed for 'Str'/
156 => 'Got Expected Error for missing named parameter';
159 $record->dict_with_maybe({name=>'usal'});
160 } => 'Set dict attribute without error, skipping optional';
162 ## Test dict_with_tuple
165 $record->dict_with_tuple({key1=>'Hello', key2=>[1,'World']});
166 } => 'Set tuple attribute without error';
169 $record->dict_with_tuple({key1=>'Hello', key2=>['World',2]});
170 }, qr/Validation failed for 'Int'/
171 => 'Threw error on bad constraint';