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