dbffb72b00d930b787ba9c828d46cebab7574318
[gitmo/MooseX-Dependent.git] / t / 02-depending.t
1 use Test::More tests=>24; {
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
11         UniqueInt
12                 UniqueInt2
13     )];
14         
15         ## sugar for alternative syntax: depending {} TC,TC
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                 }
23         }
24     
25     ## The dependent value must exceed the constraining value
26     subtype IntGreaterThanInt,
27       as Depending[
28         Int,
29         sub {
30             my ($dependent_val, $constraining_val) = @_;
31             return ($dependent_val > $constraining_val) ? 1:undef;
32         },
33         Int,
34       ];
35
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.";
44     
45     ## The dependent value cannot exist in the constraining arrayref.  Also, it
46         ## (the dependent type) must exceed 2.
47     subtype UniqueInt,
48       as Depending[
49         Int,
50         sub {
51             my ($dependent_int, $constraining_arrayref) = @_;
52             (grep { $_ == $dependent_int} @$constraining_arrayref) ? undef:1
53         },
54         ArrayRef[Int],
55       ],
56           where {
57                 my ($dependent_val, $constraining_value) = @$_;
58                 return $dependent_val > 2 ? 1:undef;
59           };
60       
61     isa_ok UniqueInt, 'MooseX::Meta::TypeConstraint::Dependent';
62     ok !UniqueInt->check(['a',[1,2,3]]), '"a" not an Int';
63     ok !UniqueInt->check([1,['b','c']]), '"b","c" not an arrayref';    
64     ok !UniqueInt->check([1,[1,2,3]]), 'not unique in set';
65     ok !UniqueInt->check([10,[1,10,15]]), 'not unique in set';
66     ok !UniqueInt->check([2,[3..6]]), 'FAIL dependent is too small';
67     ok UniqueInt->check([3,[100..110]]), 'PASS unique in set';
68     ok UniqueInt->check([4,[100..110]]), 'PASS unique in set';  
69         
70         ## Basically as above, with sugar.
71     subtype UniqueInt2,
72           as depending {
73             my ($dependent_int, $constraining_arrayref) = @_;
74             (grep { $_ == $dependent_int} @$constraining_arrayref) ? undef:1            
75           } Int, ArrayRef[Int],
76           where {
77                 my ($dependent_val, $constraining_value) = @$_;
78                 return $dependent_val > 2 ? 1:undef;
79           };
80
81     isa_ok UniqueInt2, 'MooseX::Meta::TypeConstraint::Dependent';
82     ok !UniqueInt2->check(['a',[1,2,3]]), '"a" not an Int';
83     ok !UniqueInt2->check([1,['b','c']]), '"b","c" not an arrayref';    
84     ok !UniqueInt2->check([1,[1,2,3]]), 'not unique in set';
85     ok !UniqueInt2->check([10,[1,10,15]]), 'not unique in set';
86     ok !UniqueInt2->check([2,[3..6]]), 'FAIL dependent is too small';
87     ok UniqueInt2->check([3,[100..110]]), 'PASS unique in set';
88     ok UniqueInt2->check([4,[100..110]]), 'PASS unique in set';
89
90         ## Basic error messages.  TODO should be it's own test
91         
92         warn UniqueInt2->validate(['a',[1,2,3]]);
93 }