got all the tests working again, and basic type tests in place
[gitmo/MooseX-Dependent.git] / t / 01-dependent.t
CommitLineData
a588ee00 1
66efbe23 2use Test::More tests=>62; {
a588ee00 3
4 use strict;
5 use warnings;
6
7 use MooseX::Dependent::Types qw(Dependent);
0a9f5b94 8 use MooseX::Types::Moose qw(Int Any);
66efbe23 9 use MooseX::Types -declare=>[qw(SubDependent IntLessThan EvenInt
10 LessThan100GreatThen5andEvenIntNot44)];
a588ee00 11 use Moose::Util::TypeConstraints;
0a9f5b94 12
1e87d1a7 13 ok Dependent->check(1),
0a9f5b94 14 'Dependent is basically an "Any"';
15
66efbe23 16 is Dependent->validate(1), undef,
0a9f5b94 17 'No Error Message';
18
1e87d1a7 19 is Dependent->parent, 'Any',
20 'Dependent is an Any';
0a9f5b94 21
22 is Dependent->name, 'MooseX::Dependent::Types::Dependent',
23 'Dependent has expected name';
24
25 is Dependent->get_message,
26 "Validation failed for 'MooseX::Dependent::Types::Dependent' failed with value undef",
27 'Got Expected Message';
28
29 ok Dependent->equals(Dependent),
30 'Dependent equal Dependent';
31
32 ok Dependent->is_a_type_of(Dependent),
33 'Dependent is_a_type_of Dependent';
34
35 ok Dependent->is_a_type_of('Any'),
36 'Dependent is_a_type_of Any';
37
38 ok Dependent->is_subtype_of('Any'),
39 'Dependent is_subtype_of Dependent';
40
41 is Dependent->parent_type_constraint, 'Any',
42 'Correct parent type';
43
44 is subtype( SubDependent, as Dependent ),
45 'main::SubDependent',
46 'Create a useless subtype';
47
48 ok SubDependent->check(1),
49 'SubDependent is basically an "Any"';
50
66efbe23 51 is SubDependent->validate(1), undef,
0a9f5b94 52 'validate returned no error message';
53
1e87d1a7 54 is SubDependent->parent, 'MooseX::Dependent::Types::Dependent',
55 'SubDependent is a Dependent';
0a9f5b94 56
57 is SubDependent->name, 'main::SubDependent',
58 'Dependent has expected name';
59
60 is SubDependent->get_message,
61 "Validation failed for 'main::SubDependent' failed with value undef",
62 'Got Expected Message';
63
64 ok SubDependent->equals(SubDependent),
65 'SubDependent equal SubDependent';
66
67 ok !SubDependent->equals(Dependent),
68 'SubDependent does not equal Dependent';
69
70 ok SubDependent->is_a_type_of(Dependent),
71 'SubDependent is_a_type_of Dependent';
72
73 ok SubDependent->is_a_type_of(Any),
74 'SubDependent is_a_type_of Any';
75
76 ok SubDependent->is_subtype_of('Any'),
77 'SubDependent is_subtype_of Dependent';
78
79 ok !SubDependent->is_subtype_of(SubDependent),
80 'SubDependent is not is_subtype_of SubDependent';
81
82 ok subtype( EvenInt,
83 as Int,
84 where {
85 my $val = shift @_;
86 return $val % 2 ? 0:1;
87 }),
88 'Created a subtype of Int';
1e87d1a7 89
0a9f5b94 90 ok !EvenInt->check('aaa'), '"aaa" not an Int';
91 ok !EvenInt->check(1), '1 is not even';
92 ok EvenInt->check(2), 'but 2 is!';
93
94 ok subtype( IntLessThan,
95 as Dependent[EvenInt, Int],
96 where {
97 my $value = shift @_;
66efbe23 98 my $constraining = shift @_ || 200; #warn "..... $constraining ......";
0a9f5b94 99 return ($value < $constraining && $value > 5);
100 }),
101 'Created IntLessThan subtype';
102
103 ok !IntLessThan->check('aaa'),
104 '"aaa" is not an integer';
105
106 is IntLessThan->validate('aaa'),
107 "Validation failed for 'main::EvenInt' failed with value aaa",
108 'Got expected error messge for "aaa"';
109
110 ok !IntLessThan->check(1),
111 '1 smaller than 5';
1e87d1a7 112
0a9f5b94 113 ok !IntLessThan->check(2),
114 '2 smaller than 5';
115
116 ok !IntLessThan->check(15),
117 '15 greater than 5 (but odd)';
118
119 ok !IntLessThan->check(301),
120 '301 is too big';
121
122 ok !IntLessThan->check(400),
123 '400 is too big';
124
125 ok IntLessThan->check(10),
126 '10 greater than 5 (and even)';
127
128 is IntLessThan->validate(1),
129 "Validation failed for 'main::EvenInt' failed with value 1",
130 'error message is correct';
131
132 is IntLessThan->name, 'main::IntLessThan',
133 'Got correct name for IntLessThan';
134
135 is IntLessThan->parent, 'MooseX::Dependent::Types::Dependent[main::EvenInt, Int]',
136 'IntLessThan is a Dependent';
137
138 is IntLessThan->parent_type_constraint, EvenInt,
139 'Parent is an Int';
140
141 is IntLessThan->constraining_value_type_constraint, Int,
142 'constraining is an Int';
143
144 ok IntLessThan->equals(IntLessThan),
145 'IntLessThan equals IntLessThan';
146
147 ok IntLessThan->is_subtype_of(Dependent),
148 'IntLessThan is_subtype_of Dependent';
149
150 ok IntLessThan->is_subtype_of(Int),
151 'IntLessThan is_subtype_of Int';
152
153 ok IntLessThan->is_a_type_of(Dependent),
154 'IntLessThan is_a_type_of Dependent';
155
156 ok IntLessThan->is_a_type_of(Int),
157 'IntLessThan is_a_type_of Int';
158
159 ok IntLessThan->is_a_type_of(IntLessThan),
160 'IntLessThan is_a_type_of IntLessThan';
161
162 ok( (my $lessThan100GreatThen5andEvenInt = IntLessThan[100]),
163 'Parameterized!');
164
165 ok !$lessThan100GreatThen5andEvenInt->check(150),
166 '150 Not less than 100';
167
168 ok !$lessThan100GreatThen5andEvenInt->check(151),
169 '151 Not less than 100';
170
171 ok !$lessThan100GreatThen5andEvenInt->check(2),
172 'Not greater than 5';
173
174 ok !$lessThan100GreatThen5andEvenInt->check(51),
175 'Not even';
176
177 ok !$lessThan100GreatThen5andEvenInt->check('aaa'),
178 'Not Int';
179
180 ok $lessThan100GreatThen5andEvenInt->check(42),
181 'is Int, is even, greater than 5, less than 100';
66efbe23 182
183 ok subtype( LessThan100GreatThen5andEvenIntNot44,
184 as IntLessThan[100],
185 where {
186 my $value = shift @_;
187 return $value == 44 ? 0:1;
188 }),
189 'Created LessThan100GreatThen5andEvenIntNot44 subtype';
190
191 ok !LessThan100GreatThen5andEvenIntNot44->check(150),
192 '150 Not less than 100';
193
194 ok !LessThan100GreatThen5andEvenIntNot44->check(300),
195 '300 Not less than 100 (check to make sure we are not defaulting 200)';
196
197 ok !LessThan100GreatThen5andEvenIntNot44->check(151),
198 '151 Not less than 100';
199
200 ok !LessThan100GreatThen5andEvenIntNot44->check(2),
201 'Not greater than 5';
202
203 ok !LessThan100GreatThen5andEvenIntNot44->check(51),
204 'Not even';
205
206 ok !LessThan100GreatThen5andEvenIntNot44->check('aaa'),
207 'Not Int';
208
209 ok LessThan100GreatThen5andEvenIntNot44->check(42),
210 'is Int, is even, greater than 5, less than 100';
211
212 ok !LessThan100GreatThen5andEvenIntNot44->check(44),
213 'is Int, is even, greater than 5, less than 100 BUT 44!';
0a9f5b94 214
215 #die IntLessThan->validate(100);
216 #use Data::Dump qw/dump/;
217 #warn dump IntLessThan;
218}