4 use Test::More tests=>32;
9 package Test::MooseX::Meta::TypeConstraint::Structured::Tuple;
12 use MooseX::Types::Structured qw(Tuple);
13 use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe);
14 use MooseX::Types -declare => [qw(MyString MoreThanFive FiveByFive MyArrayRefMoreThanTwoInt)];
24 subtype MyArrayRefMoreThanTwoInt,
25 as ArrayRef[MoreThanFive],
26 where { scalar @$_ > 2 };
29 as Tuple[MoreThanFive, MyArrayRefMoreThanTwoInt];
31 #use Data::Dump qw/dump/; warn dump Tuple;
33 has 'tuple' => (is=>'rw', isa=>Tuple[Int, Str, MyString]);
34 has 'tuple_with_param' => (is=>'rw', isa=>Tuple[Int, Str, ArrayRef[Int]]);
35 has 'tuple_with_maybe' => (is=>'rw', isa=>Tuple[Int, Str, Maybe[Int], Object]);
36 has 'tuple_with_maybe2' => (is=>'rw', isa=>Tuple[Int, Str, Maybe[Int]]);
37 has 'tuple_with_union' => (is=>'rw', isa=>Tuple[Int,Str,Int|Object,Int]);
38 has 'tuple2' => (is=>'rw', isa=>Tuple[Int,Str,Int]);
39 has 'tuple_with_parameterized' => (is=>'rw', isa=>Tuple[Int,Str,Int,ArrayRef[Int]]);
40 has 'FiveByFiveAttr' => (is=>'rw', isa=>FiveByFive);
43 ## Instantiate a new test object
45 ok my $record = Test::MooseX::Meta::TypeConstraint::Structured::Tuple->new
46 => 'Instantiated new Record test class.';
48 isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured::Tuple'
49 => 'Created correct object type.';
51 ## Test Tuple type constraint
54 $record->tuple([1,'hello', 'test.abc.test']);
55 } => 'Set tuple attribute without error';
57 is $record->tuple->[0], 1
58 => 'correct set the tuple attribute index 0';
60 is $record->tuple->[1], 'hello'
61 => 'correct set the tuple attribute index 1';
63 is $record->tuple->[2], 'test.abc.test'
64 => 'correct set the tuple attribute index 2';
67 $record->tuple([1,'hello', 'test.xxx.test']);
68 }, qr/Attribute \(tuple\) does not pass the type constraint/
69 => 'Properly failed for bad value in custom type constraint';
72 $record->tuple(['asdasd',2, 'test.abc.test']);
73 }, qr/Attribute \(tuple\) does not pass the type constraint/
74 => 'Got Expected Error for violating constraints';
76 ## Test tuple_with_maybe
79 $record->tuple_with_maybe([1,'hello', 1, $record]);
80 } => 'Set tuple attribute without error';
83 $record->tuple_with_maybe([1,'hello', 'a', $record]);
84 }, qr/Attribute \(tuple_with_maybe\) does not pass the type constraint/
85 => 'Properly failed for bad value parameterized constraint';
88 $record->tuple_with_maybe([1,'hello',undef, $record]);
89 } => 'Set tuple attribute without error skipping optional parameter';
91 ## Test tuple_with_maybe2
94 $record->tuple_with_maybe2([1,'hello', 1]);
95 } => 'Set tuple attribute without error';
98 $record->tuple_with_maybe2([1,'hello', 'a']);
99 }, qr/Attribute \(tuple_with_maybe2\) does not pass the type constraint/
100 => 'Properly failed for bad value parameterized constraint';
103 $record->tuple_with_maybe2([1,'hello',undef]);
104 } => 'Set tuple attribute without error skipping optional parameter';
107 $record->tuple_with_maybe2([1,'hello']);
108 }, qr/Attribute \(tuple_with_maybe2\) does not pass the type constraint/
109 => 'Properly fails for missing maybe (needs to be at least undef)';
111 ## Test Tuple with parameterized type
114 $record->tuple_with_param([1,'hello', [1,2,3]]);
115 } => 'Set tuple attribute without error';
118 $record->tuple_with_param([1,'hello', [qw/a b c/]]);
119 }, qr/Attribute \(tuple_with_param\) does not pass the type constraint/
120 => 'Properly failed for bad value parameterized constraint';
122 ## Test tuple2 (Tuple[Int,Str,Int])
124 ok $record->tuple2([1,'hello',3])
125 => "[1,'hello',3] properly suceeds";
128 $record->tuple2([1,2,'world']);
129 }, qr/Attribute \(tuple2\) does not pass the type constraint/ => "[1,2,'world'] properly fails";
132 $record->tuple2(['hello1',2,3]);
133 }, qr/Attribute \(tuple2\) does not pass the type constraint/ => "['hello',2,3] properly fails";
136 $record->tuple2(['hello2',2,'world']);
137 }, qr/Attribute \(tuple2\) does not pass the type constraint/ => "['hello',2,'world'] properly fails";
140 ## Test tuple_with_parameterized (Tuple[Int,Str,Int,ArrayRef[Int]])
142 ok $record->tuple_with_parameterized([1,'hello',3,[1,2,3]])
143 => "[1,'hello',3,[1,2,3]] properly suceeds";
146 $record->tuple_with_parameterized([1,2,'world']);
147 }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
148 => "[1,2,'world'] properly fails";
151 $record->tuple_with_parameterized(['hello1',2,3]);
152 }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
153 => "['hello',2,3] properly fails";
156 $record->tuple_with_parameterized(['hello2',2,'world']);
157 }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
158 => "['hello',2,'world'] properly fails";
161 $record->tuple_with_parameterized([1,'hello',3,[1,2,'world']]);
162 }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
163 => "[1,'hello',3,[1,2,'world']] properly fails";
165 ## Test FiveByFiveAttr
168 $record->FiveByFiveAttr([6,[7,8,9]]);
169 } => 'Set FiveByFiveAttr correctly';
172 $record->FiveByFiveAttr([1,'hello', 'test']);
173 }, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/
174 => q{Properly failed for bad value in FiveByFiveAttr [1,'hello', 'test']};
177 $record->FiveByFiveAttr([1,[8,9,10]]);
178 }, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/
179 => q{Properly failed for bad value in FiveByFiveAttr [1,[8,9,10]]};
182 $record->FiveByFiveAttr([10,[11,12,0]]);
183 }, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/
184 => q{Properly failed for bad value in FiveByFiveAttr [10,[11,12,0]]};
187 $record->FiveByFiveAttr([1,[1,1,0]]);
188 }, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/
189 => q{Properly failed for bad value in FiveByFiveAttr [1,[1,1,0]]};
192 $record->FiveByFiveAttr([10,[11,12]]);
193 }, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/
194 => q{Properly failed for bad value in FiveByFiveAttr [10,[11,12]};