4 use Test::More tests=>26;
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)];
20 #use Data::Dump qw/dump/; warn dump Tuple;
22 has 'tuple' => (is=>'rw', isa=>Tuple[Int, Str, MyString]);
23 has 'tuple_with_param' => (is=>'rw', isa=>Tuple[Int, Str, ArrayRef[Int]]);
24 has 'tuple_with_maybe' => (is=>'rw', isa=>Tuple[Int, Str, Maybe[Int], Object]);
25 has 'tuple_with_maybe2' => (is=>'rw', isa=>Tuple[Int, Str, Maybe[Int]]);
26 has 'tuple_with_union' => (is=>'rw', isa=>Tuple[Int,Str,Int|Object,Int]);
27 has 'tuple2' => (is=>'rw', isa=>Tuple[Int,Str,Int]);
28 has 'tuple_with_parameterized' => (is=>'rw', isa=>Tuple[Int,Str,Int,ArrayRef[Int]]);
31 ## Instantiate a new test object
33 ok my $record = Test::MooseX::Meta::TypeConstraint::Structured::Tuple->new
34 => 'Instantiated new Record test class.';
36 isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured::Tuple'
37 => 'Created correct object type.';
39 ## Test Tuple type constraint
42 $record->tuple([1,'hello', 'test.abc.test']);
43 } => 'Set tuple attribute without error';
45 is $record->tuple->[0], 1
46 => 'correct set the tuple attribute index 0';
48 is $record->tuple->[1], 'hello'
49 => 'correct set the tuple attribute index 1';
51 is $record->tuple->[2], 'test.abc.test'
52 => 'correct set the tuple attribute index 2';
55 $record->tuple([1,'hello', 'test.xxx.test']);
56 }, qr/Attribute \(tuple\) does not pass the type constraint/
57 => 'Properly failed for bad value in custom type constraint';
60 $record->tuple(['asdasd',2, 'test.abc.test']);
61 }, qr/Attribute \(tuple\) does not pass the type constraint/
62 => 'Got Expected Error for violating constraints';
64 ## Test tuple_with_maybe
67 $record->tuple_with_maybe([1,'hello', 1, $record]);
68 } => 'Set tuple attribute without error';
71 $record->tuple_with_maybe([1,'hello', 'a', $record]);
72 }, qr/Attribute \(tuple_with_maybe\) does not pass the type constraint/
73 => 'Properly failed for bad value parameterized constraint';
76 $record->tuple_with_maybe([1,'hello',undef, $record]);
77 } => 'Set tuple attribute without error skipping optional parameter';
79 ## Test tuple_with_maybe2
82 $record->tuple_with_maybe2([1,'hello', 1]);
83 } => 'Set tuple attribute without error';
86 $record->tuple_with_maybe2([1,'hello', 'a']);
87 }, qr/Attribute \(tuple_with_maybe2\) does not pass the type constraint/
88 => 'Properly failed for bad value parameterized constraint';
91 $record->tuple_with_maybe2([1,'hello',undef]);
92 } => 'Set tuple attribute without error skipping optional parameter';
95 $record->tuple_with_maybe2([1,'hello']);
96 }, qr/Attribute \(tuple_with_maybe2\) does not pass the type constraint/
97 => 'Properly fails for missing maybe (needs to be at least undef)';
99 ## Test Tuple with parameterized type
102 $record->tuple_with_param([1,'hello', [1,2,3]]);
103 } => 'Set tuple attribute without error';
106 $record->tuple_with_param([1,'hello', [qw/a b c/]]);
107 }, qr/Attribute \(tuple_with_param\) does not pass the type constraint/
108 => 'Properly failed for bad value parameterized constraint';
110 ## Test tuple2 (Tuple[Int,Str,Int])
112 ok $record->tuple2([1,'hello',3])
113 => "[1,'hello',3] properly suceeds";
116 $record->tuple2([1,2,'world']);
117 }, qr/Attribute \(tuple2\) does not pass the type constraint/ => "[1,2,'world'] properly fails";
120 $record->tuple2(['hello1',2,3]);
121 }, qr/Attribute \(tuple2\) does not pass the type constraint/ => "['hello',2,3] properly fails";
124 $record->tuple2(['hello2',2,'world']);
125 }, qr/Attribute \(tuple2\) does not pass the type constraint/ => "['hello',2,'world'] properly fails";
128 ## Test tuple_with_parameterized (Tuple[Int,Str,Int,ArrayRef[Int]])
130 ok $record->tuple_with_parameterized([1,'hello',3,[1,2,3]])
131 => "[1,'hello',3,[1,2,3]] properly suceeds";
134 $record->tuple_with_parameterized([1,2,'world']);
135 }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
136 => "[1,2,'world'] properly fails";
139 $record->tuple_with_parameterized(['hello1',2,3]);
140 }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
141 => "['hello',2,3] properly fails";
144 $record->tuple_with_parameterized(['hello2',2,'world']);
145 }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
146 => "['hello',2,'world'] properly fails";
149 $record->tuple_with_parameterized([1,'hello',3,[1,2,'world']]);
150 }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
151 => "[1,'hello',3,[1,2,'world']] properly fails";