7 eval "use MooseX::Types::Structured"; if($@) {
8 plan skip_all => "MooseX::Types:Structured Required for advanced Tests";
10 eval "use Set::Scalar"; if($@) {
11 plan skip_all => "Set::Scalar Required for advanced Tests";
17 use MooseX::Types::Structured qw(Tuple Dict slurpy);
18 use MooseX::Parameterizable::Types qw(Parameterizable);
19 use MooseX::Types::Moose qw(Int Str);
20 use Moose::Util::TypeConstraints;
22 use MooseX::Types -declare=>[qw(
23 Set UniqueInt UniqueInSet Range RangedInt PositiveRangedInt1
24 PositiveRangedInt2 PositiveInt PositiveRange NameAge NameBetween18and35Age
27 ok class_type("Set::Scalar"), 'Created Set::Scalar class_type';
28 ok subtype( Set, as "Set::Scalar"), 'Created Set subtype';
30 ok subtype( UniqueInt,
31 as Parameterizable[Int, Set],
34 return !$set->has($int);
35 }), 'Created UniqueInt Parameterizable Type';
37 ok( (my $set_obj = Set::Scalar->new(1,2,3,4,5)), 'Create Set Object');
39 ok !UniqueInt([$set_obj])->check(1), "Not OK, since one isn't unique in $set_obj";
40 ok !UniqueInt([$set_obj])->check('AAA'), "Not OK, since AAA is not an Int";
41 ok UniqueInt([$set_obj])->check(100), "OK, since 100 isn't in the set";
43 ok( (my $unique = UniqueInt[$set_obj]), 'Created Anonymous typeconstraint');
44 ok $unique->check(10), "OK, 10 is unique";
45 ok !$unique->check(2), "Not OK, '2' is already in the set";
47 ok( subtype(UniqueInSet, as UniqueInt[$set_obj]), 'Created Subtype');
48 ok UniqueInSet->check(99), '99 is unique';
49 ok !UniqueInSet->check(3), 'Not OK, 3 is already in the set';
52 eval { UniqueInt->check(1000) };
54 qr/Validation failed for 'main::Set' failed with value undef/,
57 eval { UniqueInt->validate(1000) };
59 qr/Validation failed for 'main::Set' failed with value undef/,
64 as Dict[max=>Int, min=>Int],
67 return $range->{max} > $range->{min};
71 as Parameterizable[Int, Range],
73 my ($value, $range) = @_;
74 return ($value >= $range->{min} &&
75 $value <= $range->{max});
78 ok RangedInt([{min=>10,max=>100}])->check(50), '50 in the range';
79 ok !RangedInt([{min=>50, max=>75}])->check(99),'99 exceeds max';
80 ok !RangedInt([{min=>50, max=>75}])->check('aa'), '"aa" not even an Int';
84 RangedInt([{min=>99, max=>10}])->check(10); ## Not OK, not a valid Range!
88 qr/Validation failed for 'main::Range'/,
92 ok RangedInt([min=>10,max=>100])->check(50), '50 in the range';
93 ok !RangedInt([min=>50, max=>75])->check(99),'99 exceeds max';
94 ok !RangedInt([min=>50, max=>75])->check('aa'), '"aa" not even an Int';
98 RangedInt([min=>99, max=>10])->check(10); ## Not OK, not a valid Range!
102 qr/Validation failed for 'main::Range'/,
103 'Got Expected Error';
106 subtype PositiveRangedInt1,
112 ok PositiveRangedInt1([min=>10,max=>100])->check(50), '50 in the range';
113 ok !PositiveRangedInt1([min=>50, max=>75])->check(99),'99 exceeds max';
114 ok !PositiveRangedInt1([min=>50, max=>75])->check('aa'), '"aa" not even an Int';
118 PositiveRangedInt1([min=>99, max=>10])->check(10); ## Not OK, not a valid Range!
122 qr/Validation failed for 'main::Range'/,
123 'Got Expected Error';
126 ok !PositiveRangedInt1([min=>-100,max=>100])->check(-10), '-10 is not positive';
131 my ($value, $range) = @_;
135 ## subtype Range to re-parameterize Range with subtypes
136 subtype PositiveRange,
137 as Range[max=>PositiveInt, min=>PositiveInt];
139 ## create subtype via reparameterizing
140 subtype PositiveRangedInt2,
141 as RangedInt[PositiveRange];
143 ok PositiveRangedInt2([min=>10,max=>100])->check(50), '50 in the range';
144 ok !PositiveRangedInt2([min=>50, max=>75])->check(99),'99 exceeds max';
145 ok !PositiveRangedInt2([min=>50, max=>75])->check('aa'), '"aa" not even an Int';
149 PositiveRangedInt2([min=>-100,max=>100])->check(-10); ## Not OK, not a valid Range!
153 qr/Validation failed for 'main::PositiveRange'/,
154 'Got Expected Error';
160 ok NameAge->check(['John',28]), 'Good NameAge';
161 ok !NameAge->check(['John','Napiorkowski']), 'Bad NameAge';
163 subtype NameBetween18and35Age,
166 PositiveRangedInt2[min=>18,max=>35],
169 ok NameBetween18and35Age->check(['John',28]), 'Good NameBetween18and35Age';
170 ok !NameBetween18and35Age->check(['John','Napiorkowski']), 'Bad NameBetween18and35Age';
171 ok !NameBetween18and35Age->check(['John',99]), 'Bad NameBetween18and35Age';