6 eval "use MooseX::Types::Structured qw(Tuple Dict slurpy)"; if($@) {
7 plan skip_all => "MooseX::Types:Structured Required for advanced Tests";
9 eval "use Set::Scalar"; if($@) {
10 plan skip_all => "Set::Scalar Required for advanced Tests";
17 use MooseX::Types::Parameterizable qw(Parameterizable);
18 use MooseX::Types::Moose qw(Int Str);
19 use Moose::Util::TypeConstraints;
21 use MooseX::Types -declare=>[qw(
22 Set UniqueInt UniqueInSet Range RangedInt PositiveRangedInt1
23 PositiveRangedInt2 PositiveInt PositiveRange NameAge NameBetween18and35Age
26 ok class_type("Set::Scalar"), 'Created Set::Scalar class_type';
27 ok subtype( Set, as "Set::Scalar"), 'Created Set subtype';
29 ok subtype( UniqueInt,
30 as Parameterizable[Int, Set],
33 return !$set->has($int);
34 }), 'Created UniqueInt Parameterizable Type';
36 ok( (my $set_obj = Set::Scalar->new(1,2,3,4,5)), 'Create Set Object');
38 ok !UniqueInt([$set_obj])->check(1), "Not OK, since one isn't unique in $set_obj";
39 ok !UniqueInt([$set_obj])->check('AAA'), "Not OK, since AAA is not an Int";
40 ok UniqueInt([$set_obj])->check(100), "OK, since 100 isn't in the set";
42 ok( (my $unique = UniqueInt[$set_obj]), 'Created Anonymous typeconstraint');
43 ok $unique->check(10), "OK, 10 is unique";
44 ok !$unique->check(2), "Not OK, '2' is already in the set";
46 ok( subtype(UniqueInSet, as UniqueInt[$set_obj]), 'Created Subtype');
47 ok UniqueInSet->check(99), '99 is unique';
48 ok !UniqueInSet->check(3), 'Not OK, 3 is already in the set';
51 eval { UniqueInt->check(1000) };
53 qr/Validation failed for 'main::Set' with value undef/,
56 eval { UniqueInt->validate(1000) };
58 qr/Validation failed for 'main::Set' with value undef/,
63 as Dict[max=>Int, min=>Int],
66 return $range->{max} > $range->{min};
70 as Parameterizable[Int, Range],
72 my ($value, $range) = @_;
73 return ($value >= $range->{min} &&
74 $value <= $range->{max});
77 ok RangedInt([{min=>10,max=>100}])->check(50), '50 in the range';
78 ok !RangedInt([{min=>50, max=>75}])->check(99),'99 exceeds max';
79 ok !RangedInt([{min=>50, max=>75}])->check('aa'), '"aa" not even an Int';
83 RangedInt([{min=>99, max=>10}])->check(10); ## Not OK, not a valid Range!
87 qr/Validation failed for 'main::Range'/,
91 ok RangedInt([min=>10,max=>100])->check(50), '50 in the range';
92 ok !RangedInt([min=>50, max=>75])->check(99),'99 exceeds max';
93 ok !RangedInt([min=>50, max=>75])->check('aa'), '"aa" not even an Int';
97 RangedInt([min=>99, max=>10])->check(10); ## Not OK, not a valid Range!
101 qr/Validation failed for 'main::Range'/,
102 'Got Expected Error';
105 subtype PositiveRangedInt1,
111 ok PositiveRangedInt1([min=>10,max=>100])->check(50), '50 in the range';
112 ok !PositiveRangedInt1([min=>50, max=>75])->check(99),'99 exceeds max';
113 ok !PositiveRangedInt1([min=>50, max=>75])->check('aa'), '"aa" not even an Int';
117 PositiveRangedInt1([min=>99, max=>10])->check(10); ## Not OK, not a valid Range!
121 qr/Validation failed for 'main::Range'/,
122 'Got Expected Error';
125 ok !PositiveRangedInt1([min=>-100,max=>100])->check(-10), '-10 is not positive';
130 my ($value, $range) = @_;
134 ## subtype Range to re-parameterize Range with subtypes
135 subtype PositiveRange,
136 as Range[max=>PositiveInt, min=>PositiveInt];
138 ## create subtype via reparameterizing
139 subtype PositiveRangedInt2,
140 as RangedInt[PositiveRange];
142 ok PositiveRangedInt2([min=>10,max=>100])->check(50), '50 in the range';
143 ok !PositiveRangedInt2([min=>50, max=>75])->check(99),'99 exceeds max';
144 ok !PositiveRangedInt2([min=>50, max=>75])->check('aa'), '"aa" not even an Int';
148 PositiveRangedInt2([min=>-100,max=>100])->check(-10); ## Not OK, not a valid Range!
152 qr/Validation failed for 'main::PositiveRange'/,
153 'Got Expected Error';
159 ok NameAge->check(['John',28]), 'Good NameAge';
160 ok !NameAge->check(['John','Napiorkowski']), 'Bad NameAge';
162 subtype NameBetween18and35Age,
165 PositiveRangedInt2[min=>18,max=>35],
168 ok NameBetween18and35Age->check(['John',28]), 'Good NameBetween18and35Age';
169 ok !NameBetween18and35Age->check(['John','Napiorkowski']), 'Bad NameBetween18and35Age';
170 ok !NameBetween18and35Age->check(['John',99]), 'Bad NameBetween18and35Age';