1 use Test::More tests=>23; {
7 use MooseX::Types::Dependent qw(Depending);
8 use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef Maybe);
9 use MooseX::Types -declare => [qw(
15 ## sugar for alternative syntax: depending {} TC,TC
17 my ($coderef, $dependent_tc, $constraining_tc, @args) = @_;
19 return (Depending[$dependent_tc,$coderef,$constraining_tc],@args);
21 return Depending[$dependent_tc,$coderef,$constraining_tc];
25 ## The dependent value must exceed the constraining value
26 subtype IntGreaterThanInt,
30 my ($dependent_val, $constraining_val) = @_;
31 return ($dependent_val > $constraining_val) ? 1:undef;
36 isa_ok IntGreaterThanInt, 'MooseX::Meta::TypeConstraint::Dependent';
37 ok !IntGreaterThanInt->check(['a',10]), "Fails, 'a' is not an Int.";
38 ok !IntGreaterThanInt->check([5,'b']), "Fails, 'b' is not an Int either.";
39 ok !IntGreaterThanInt->check({4,1}), "Fails, since this isn't an arrayref";
40 ok !IntGreaterThanInt->check([5,10]), "Fails, 5 is less than 10";
41 ok IntGreaterThanInt->check([11,6]), "Success, 11 is greater than 6.";
42 ok IntGreaterThanInt->check([12,1]), "Success, 12 is greater than1.";
43 ok IntGreaterThanInt->check([0,-10]), "Success, 0 is greater than -10.";
45 ## The dependent value cannot exist in the constraining arrayref
50 my ($dependent_int, $constraining_arrayref) = @_;
51 (grep { $_ == $dependent_int} @$constraining_arrayref) ? undef:1
56 isa_ok UniqueInt, 'MooseX::Meta::TypeConstraint::Dependent';
57 ok !UniqueInt->check(['a',[1,2,3]]), '"a" not an Int';
58 ok !UniqueInt->check([1,['b','c']]), '"b","c" not an arrayref';
59 ok !UniqueInt->check([1,[1,2,3]]), 'not unique in set';
60 ok !UniqueInt->check([10,[1,10,15]]), 'not unique in set';
61 ok UniqueInt->check([2,[3..6]]), 'PASS unique in set';
62 ok UniqueInt->check([3,[100..110]]), 'PASS unique in set';
64 ## Same as above, with sugar
67 my ($dependent_int, $constraining_arrayref) = @_;
68 (grep { $_ == $dependent_int} @$constraining_arrayref) ? undef:1
71 my ($dependent_val, $constraining_value) = @$_;
72 return $dependent_val > 2 ? 1:undef;
75 isa_ok UniqueInt2, 'MooseX::Meta::TypeConstraint::Dependent';
76 ok !UniqueInt2->check(['a',[1,2,3]]), '"a" not an Int';
77 ok !UniqueInt2->check([1,['b','c']]), '"b","c" not an arrayref';
78 ok !UniqueInt2->check([1,[1,2,3]]), 'not unique in set';
79 ok !UniqueInt2->check([10,[1,10,15]]), 'not unique in set';
80 ok !UniqueInt2->check([2,[3..6]]), 'FAIL dependent is too small';
81 ok UniqueInt2->check([3,[100..110]]), 'PASS unique in set';
82 ok UniqueInt2->check([4,[100..110]]), 'PASS unique in set';