68718aa038c3d8ad9b73ab1ec92ba69250e027bc
[gitmo/MooseX-Types-Structured.git] / t / 02-tuple.t
1 BEGIN {
2     use strict;
3     use warnings;
4     use Test::More tests=>32;
5     use Test::Exception;
6 }
7
8 {
9     package Test::MooseX::Meta::TypeConstraint::Structured::Tuple;
10
11     use Moose;
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)];
15
16     subtype MyString,
17      as Str,
18      where { $_=~m/abc/};
19
20     subtype MoreThanFive,
21      as Int,
22      where { $_ > 5};
23
24     subtype MyArrayRefMoreThanTwoInt,
25      as ArrayRef[MoreThanFive],
26      where { scalar @$_ > 2 };
27
28     subtype FiveByFive,
29      as Tuple[MoreThanFive, MyArrayRefMoreThanTwoInt];
30
31     #use Data::Dump qw/dump/; warn dump Tuple;
32
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);
41 }
42
43 ## Instantiate a new test object
44
45 ok my $record = Test::MooseX::Meta::TypeConstraint::Structured::Tuple->new
46  => 'Instantiated new Record test class.';
47
48 isa_ok $record => 'Test::MooseX::Meta::TypeConstraint::Structured::Tuple'
49  => 'Created correct object type.';
50
51 ## Test Tuple type constraint
52
53 lives_ok sub {
54     $record->tuple([1,'hello', 'test.abc.test']);
55 } => 'Set tuple attribute without error';
56
57 is $record->tuple->[0], 1
58  => 'correct set the tuple attribute index 0';
59
60 is $record->tuple->[1], 'hello'
61  => 'correct set the tuple attribute index 1';
62
63 is $record->tuple->[2], 'test.abc.test'
64  => 'correct set the tuple attribute index 2';
65
66 throws_ok sub {
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';
70
71 throws_ok sub {
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';
75
76 ## Test tuple_with_maybe
77
78 lives_ok sub {
79     $record->tuple_with_maybe([1,'hello', 1, $record]);
80 } => 'Set tuple attribute without error';
81
82 throws_ok sub {
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';
86
87 lives_ok sub {
88     $record->tuple_with_maybe([1,'hello',undef, $record]);
89 } => 'Set tuple attribute without error skipping optional parameter';
90
91 ## Test tuple_with_maybe2
92
93 lives_ok sub {
94     $record->tuple_with_maybe2([1,'hello', 1]);
95 } => 'Set tuple attribute without error';
96
97 throws_ok sub {
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';
101
102 lives_ok sub {
103     $record->tuple_with_maybe2([1,'hello',undef]);
104 } => 'Set tuple attribute without error skipping optional parameter';
105
106 SKIP: {
107     skip 'Core Maybe incorrectly allows null.', 1, 1;
108     throws_ok sub {
109         $record->tuple_with_maybe2([1,'hello']);
110     }, qr/Attribute \(tuple_with_maybe2\) does not pass the type constraint/
111      => 'Properly fails for missing maybe (needs to be at least undef)';
112 }
113
114 ## Test Tuple with parameterized type
115
116 lives_ok sub {
117     $record->tuple_with_param([1,'hello', [1,2,3]]);
118 } => 'Set tuple attribute without error';
119
120 throws_ok sub {
121     $record->tuple_with_param([1,'hello', [qw/a b c/]]);
122 }, qr/Attribute \(tuple_with_param\) does not pass the type constraint/
123  => 'Properly failed for bad value parameterized constraint';
124
125 ## Test tuple2 (Tuple[Int,Str,Int])
126
127 ok $record->tuple2([1,'hello',3])
128  => "[1,'hello',3] properly suceeds";
129
130 throws_ok sub {
131     $record->tuple2([1,2,'world']);
132 }, qr/Attribute \(tuple2\) does not pass the type constraint/ => "[1,2,'world'] properly fails";
133
134 throws_ok sub {
135     $record->tuple2(['hello1',2,3]);
136 }, qr/Attribute \(tuple2\) does not pass the type constraint/ => "['hello',2,3] properly fails";
137
138 throws_ok sub {
139     $record->tuple2(['hello2',2,'world']);
140 }, qr/Attribute \(tuple2\) does not pass the type constraint/ => "['hello',2,'world'] properly fails";
141
142
143 ## Test tuple_with_parameterized (Tuple[Int,Str,Int,ArrayRef[Int]])
144
145 ok $record->tuple_with_parameterized([1,'hello',3,[1,2,3]])
146  => "[1,'hello',3,[1,2,3]] properly suceeds";
147
148 throws_ok sub {
149     $record->tuple_with_parameterized([1,2,'world']);
150 }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
151  => "[1,2,'world'] properly fails";
152
153 throws_ok sub {
154     $record->tuple_with_parameterized(['hello1',2,3]);
155 }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
156  => "['hello',2,3] properly fails";
157
158 throws_ok sub {
159     $record->tuple_with_parameterized(['hello2',2,'world']);
160 }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
161  => "['hello',2,'world'] properly fails";
162
163 throws_ok sub {
164     $record->tuple_with_parameterized([1,'hello',3,[1,2,'world']]);
165 }, qr/Attribute \(tuple_with_parameterized\) does not pass the type constraint/
166  => "[1,'hello',3,[1,2,'world']] properly fails";
167
168 ## Test FiveByFiveAttr
169
170 lives_ok sub {
171     $record->FiveByFiveAttr([6,[7,8,9]]);
172 } => 'Set FiveByFiveAttr correctly';
173
174 throws_ok sub {
175     $record->FiveByFiveAttr([1,'hello', 'test']);
176 }, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/
177  => q{Properly failed for bad value in FiveByFiveAttr [1,'hello', 'test']};
178
179 throws_ok sub {
180     $record->FiveByFiveAttr([1,[8,9,10]]);
181 }, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/
182  => q{Properly failed for bad value in FiveByFiveAttr [1,[8,9,10]]};
183
184 throws_ok sub {
185     $record->FiveByFiveAttr([10,[11,12,0]]);
186 }, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/
187  => q{Properly failed for bad value in FiveByFiveAttr [10,[11,12,0]]};
188
189 throws_ok sub {
190     $record->FiveByFiveAttr([1,[1,1,0]]);
191 }, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/
192  => q{Properly failed for bad value in FiveByFiveAttr [1,[1,1,0]]};
193
194 throws_ok sub {
195     $record->FiveByFiveAttr([10,[11,12]]);
196 }, qr/Attribute \(FiveByFiveAttr\) does not pass the type constraint/
197  => q{Properly failed for bad value in FiveByFiveAttr [10,[11,12]};
198