removed useless comments (just distract you from the code)
[gitmo/MooseX-Dependent.git] / t / 02-depending.t
CommitLineData
c8951a99 1use 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) = @_;
58e765da 47 (grep { $_ == $dependent_int} @$constraining_arrayref) ? undef:1
8b51c998 48 },
49 ArrayRef[Int],
50 ];
51
52 isa_ok UniqueInt, 'MooseX::Meta::TypeConstraint::Dependent';
53 ok !UniqueInt->check(['a',[1,2,3]]), '"a" not an Int';
54 ok !UniqueInt->check([1,['b','c']]), '"b","c" not an arrayref';
55 ok !UniqueInt->check([1,[1,2,3]]), 'not unique in set';
56 ok !UniqueInt->check([10,[1,10,15]]), 'not unique in set';
57 ok UniqueInt->check([2,[3..6]]), 'PASS unique in set';
c8951a99 58 ok UniqueInt->check([3,[100..110]]), 'PASS unique in set';
59
60 ## Same as above, with suger
61 subtype UniqueInt2,
62 as depending {
63 my ($dependent_int, $constraining_arrayref) = @_;
58e765da 64 (grep { $_ == $dependent_int} @$constraining_arrayref) ? undef:1
65 } Int, ArrayRef[Int],
66 where {
67 use Data::Dump qw/dump/;
68 warn dump @_;
69 }
c8951a99 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}