just synchronizing the examples and more tests with where clauses
[gitmo/MooseX-Dependent.git] / t / 02-depending.t
CommitLineData
d70071de 1use Test::More tests=>24; {
9b6d2e22 2
3 use strict;
4 use warnings;
5
6 use Test::Exception;
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(
10 IntGreaterThanInt
8b51c998 11 UniqueInt
c8951a99 12 UniqueInt2
9b6d2e22 13 )];
c8951a99 14
15 ## sugar for alternative syntax: depending {} TC,TC
1622ae1b 16 sub depending(&@) {
17 my ($coderef, $dependent_tc, $constraining_tc, @args) = @_;
18 if(@args) {
19 return (Depending[$dependent_tc,$coderef,$constraining_tc],@args);
20 } else {
21 return Depending[$dependent_tc,$coderef,$constraining_tc];
22 }
c8951a99 23 }
9b6d2e22 24
8b51c998 25 ## The dependent value must exceed the constraining value
9b6d2e22 26 subtype IntGreaterThanInt,
27 as Depending[
28 Int,
29 sub {
01a12424 30 my ($dependent_val, $constraining_val) = @_;
31 return ($dependent_val > $constraining_val) ? 1:undef;
9b6d2e22 32 },
33 Int,
34 ];
8b51c998 35
9b6d2e22 36 isa_ok IntGreaterThanInt, 'MooseX::Meta::TypeConstraint::Dependent';
9b6d2e22 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.";
8b51c998 44
45 ## The dependent value cannot exist in the constraining arrayref
46 subtype UniqueInt,
47 as Depending[
48 Int,
49 sub {
50 my ($dependent_int, $constraining_arrayref) = @_;
58e765da 51 (grep { $_ == $dependent_int} @$constraining_arrayref) ? undef:1
8b51c998 52 },
53 ArrayRef[Int],
d70071de 54 ],
55 where {
56 my ($dependent_val, $constraining_value) = @$_;
57 return $dependent_val > 2 ? 1:undef;
58 };
8b51c998 59
60 isa_ok UniqueInt, 'MooseX::Meta::TypeConstraint::Dependent';
61 ok !UniqueInt->check(['a',[1,2,3]]), '"a" not an Int';
62 ok !UniqueInt->check([1,['b','c']]), '"b","c" not an arrayref';
63 ok !UniqueInt->check([1,[1,2,3]]), 'not unique in set';
64 ok !UniqueInt->check([10,[1,10,15]]), 'not unique in set';
d70071de 65 ok !UniqueInt->check([2,[3..6]]), 'FAIL dependent is too small';
c8951a99 66 ok UniqueInt->check([3,[100..110]]), 'PASS unique in set';
d70071de 67 ok UniqueInt->check([4,[100..110]]), 'PASS unique in set';
c8951a99 68
d70071de 69 ## Basically as above, with sugar
c8951a99 70 subtype UniqueInt2,
71 as depending {
72 my ($dependent_int, $constraining_arrayref) = @_;
58e765da 73 (grep { $_ == $dependent_int} @$constraining_arrayref) ? undef:1
74 } Int, ArrayRef[Int],
75 where {
1622ae1b 76 my ($dependent_val, $constraining_value) = @$_;
77 return $dependent_val > 2 ? 1:undef;
78 };
c8951a99 79
80 isa_ok UniqueInt2, 'MooseX::Meta::TypeConstraint::Dependent';
81 ok !UniqueInt2->check(['a',[1,2,3]]), '"a" not an Int';
82 ok !UniqueInt2->check([1,['b','c']]), '"b","c" not an arrayref';
83 ok !UniqueInt2->check([1,[1,2,3]]), 'not unique in set';
84 ok !UniqueInt2->check([10,[1,10,15]]), 'not unique in set';
1622ae1b 85 ok !UniqueInt2->check([2,[3..6]]), 'FAIL dependent is too small';
86 ok UniqueInt2->check([3,[100..110]]), 'PASS unique in set';
87 ok UniqueInt2->check([4,[100..110]]), 'PASS unique in set';
9b6d2e22 88}