Commit | Line | Data |
8b51c998 |
1 | use Test::More tests=>15; { |
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 |
9b6d2e22 |
12 | )]; |
13 | |
8b51c998 |
14 | ## The dependent value must exceed the constraining value |
9b6d2e22 |
15 | subtype IntGreaterThanInt, |
16 | as Depending[ |
17 | Int, |
18 | sub { |
19 | my ($dependent_val, $constraining_val) = @_; |
20 | return ($dependent_val > $constraining_val) ? 1:undef; |
21 | }, |
22 | Int, |
23 | ]; |
8b51c998 |
24 | |
9b6d2e22 |
25 | isa_ok IntGreaterThanInt, 'MooseX::Meta::TypeConstraint::Dependent'; |
9b6d2e22 |
26 | ok !IntGreaterThanInt->check(['a',10]), "Fails, 'a' is not an Int."; |
27 | ok !IntGreaterThanInt->check([5,'b']), "Fails, 'b' is not an Int either."; |
28 | ok !IntGreaterThanInt->check({4,1}), "Fails, since this isn't an arrayref"; |
29 | ok !IntGreaterThanInt->check([5,10]), "Fails, 5 is less than 10"; |
30 | ok IntGreaterThanInt->check([11,6]), "Success, 11 is greater than 6."; |
31 | ok IntGreaterThanInt->check([12,1]), "Success, 12 is greater than1."; |
32 | ok IntGreaterThanInt->check([0,-10]), "Success, 0 is greater than -10."; |
8b51c998 |
33 | |
34 | ## The dependent value cannot exist in the constraining arrayref |
35 | subtype UniqueInt, |
36 | as Depending[ |
37 | Int, |
38 | sub { |
39 | my ($dependent_int, $constraining_arrayref) = @_; |
f510c7d2 |
40 | ## Yes, this is braindead way to check for uniques in an array |
41 | ## but this doesn't require additional dependencies. |
8b51c998 |
42 | (grep { $_ == $dependent_int} @$constraining_arrayref) ? 0:1 |
43 | }, |
44 | ArrayRef[Int], |
45 | ]; |
46 | |
47 | isa_ok UniqueInt, 'MooseX::Meta::TypeConstraint::Dependent'; |
48 | ok !UniqueInt->check(['a',[1,2,3]]), '"a" not an Int'; |
49 | ok !UniqueInt->check([1,['b','c']]), '"b","c" not an arrayref'; |
50 | ok !UniqueInt->check([1,[1,2,3]]), 'not unique in set'; |
51 | ok !UniqueInt->check([10,[1,10,15]]), 'not unique in set'; |
52 | ok UniqueInt->check([2,[3..6]]), 'PASS unique in set'; |
53 | ok UniqueInt->check([3,[100..110]]), 'PASS unique in set'; |
9b6d2e22 |
54 | } |