2 use Test::More tests=>53; {
7 use MooseX::Dependent::Types qw(Dependent);
8 use MooseX::Types::Moose qw(Int Any);
9 use MooseX::Types -declare=>[qw(SubDependent IntLessThan EvenInt)];
10 use Moose::Util::TypeConstraints;
12 ok Dependent->check(1),
13 'Dependent is basically an "Any"';
15 ok !Dependent->validate(1),
18 is Dependent->parent, 'Any',
19 'Dependent is an Any';
21 is Dependent->name, 'MooseX::Dependent::Types::Dependent',
22 'Dependent has expected name';
24 is Dependent->get_message,
25 "Validation failed for 'MooseX::Dependent::Types::Dependent' failed with value undef",
26 'Got Expected Message';
28 ok Dependent->equals(Dependent),
29 'Dependent equal Dependent';
31 ok Dependent->is_a_type_of(Dependent),
32 'Dependent is_a_type_of Dependent';
34 ok Dependent->is_a_type_of('Any'),
35 'Dependent is_a_type_of Any';
37 ok Dependent->is_subtype_of('Any'),
38 'Dependent is_subtype_of Dependent';
40 is Dependent->parent_type_constraint, 'Any',
41 'Correct parent type';
43 is subtype( SubDependent, as Dependent ),
45 'Create a useless subtype';
47 ok SubDependent->check(1),
48 'SubDependent is basically an "Any"';
50 ok !SubDependent->validate(1),
51 'validate returned no error message';
53 is SubDependent->parent, 'MooseX::Dependent::Types::Dependent',
54 'SubDependent is a Dependent';
56 is SubDependent->name, 'main::SubDependent',
57 'Dependent has expected name';
59 is SubDependent->get_message,
60 "Validation failed for 'main::SubDependent' failed with value undef",
61 'Got Expected Message';
63 ok SubDependent->equals(SubDependent),
64 'SubDependent equal SubDependent';
66 ok !SubDependent->equals(Dependent),
67 'SubDependent does not equal Dependent';
69 ok SubDependent->is_a_type_of(Dependent),
70 'SubDependent is_a_type_of Dependent';
72 ok SubDependent->is_a_type_of(Any),
73 'SubDependent is_a_type_of Any';
75 ok SubDependent->is_subtype_of('Any'),
76 'SubDependent is_subtype_of Dependent';
78 ok !SubDependent->is_subtype_of(SubDependent),
79 'SubDependent is not is_subtype_of SubDependent';
85 return $val % 2 ? 0:1;
87 'Created a subtype of Int';
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!';
93 ok subtype( IntLessThan,
94 as Dependent[EvenInt, Int],
97 my $constraining = shift @_ || 200;
98 return ($value < $constraining && $value > 5);
100 'Created IntLessThan subtype';
102 ok !IntLessThan->check('aaa'),
103 '"aaa" is not an integer';
105 is IntLessThan->validate('aaa'),
106 "Validation failed for 'main::EvenInt' failed with value aaa",
107 'Got expected error messge for "aaa"';
109 ok !IntLessThan->check(1),
112 ok !IntLessThan->check(2),
115 ok !IntLessThan->check(15),
116 '15 greater than 5 (but odd)';
118 ok !IntLessThan->check(301),
121 ok !IntLessThan->check(400),
124 ok IntLessThan->check(10),
125 '10 greater than 5 (and even)';
127 is IntLessThan->validate(1),
128 "Validation failed for 'main::EvenInt' failed with value 1",
129 'error message is correct';
131 is IntLessThan->name, 'main::IntLessThan',
132 'Got correct name for IntLessThan';
134 is IntLessThan->parent, 'MooseX::Dependent::Types::Dependent[main::EvenInt, Int]',
135 'IntLessThan is a Dependent';
137 is IntLessThan->parent_type_constraint, EvenInt,
140 is IntLessThan->constraining_value_type_constraint, Int,
141 'constraining is an Int';
143 ok IntLessThan->equals(IntLessThan),
144 'IntLessThan equals IntLessThan';
146 ok IntLessThan->is_subtype_of(Dependent),
147 'IntLessThan is_subtype_of Dependent';
149 ok IntLessThan->is_subtype_of(Int),
150 'IntLessThan is_subtype_of Int';
152 ok IntLessThan->is_a_type_of(Dependent),
153 'IntLessThan is_a_type_of Dependent';
155 ok IntLessThan->is_a_type_of(Int),
156 'IntLessThan is_a_type_of Int';
158 ok IntLessThan->is_a_type_of(IntLessThan),
159 'IntLessThan is_a_type_of IntLessThan';
161 ok( (my $lessThan100GreatThen5andEvenInt = IntLessThan[100]),
164 ok !$lessThan100GreatThen5andEvenInt->check(150),
165 '150 Not less than 100';
167 ok !$lessThan100GreatThen5andEvenInt->check(151),
168 '151 Not less than 100';
170 ok !$lessThan100GreatThen5andEvenInt->check(2),
171 'Not greater than 5';
173 ok !$lessThan100GreatThen5andEvenInt->check(51),
176 ok !$lessThan100GreatThen5andEvenInt->check('aaa'),
179 ok $lessThan100GreatThen5andEvenInt->check(42),
180 'is Int, is even, greater than 5, less than 100';
182 #die IntLessThan->validate(100);
183 #use Data::Dump qw/dump/;
184 #warn dump IntLessThan;