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