136928b4086dad375a19c4b0bd4dcf2c2a610c38
[gitmo/MooseX-Dependent.git] / t / 01-basic.t
1
2 use Test::More tests=>8; {
3         
4         use strict;
5         use warnings;
6         
7         use_ok 'MooseX::Meta::TypeConstraint::Dependent';
8         use_ok 'Moose::Util::TypeConstraints';
9
10         ## A sample dependent type constraint the requires two ints and see which
11         ## is the greater.
12         
13         ok my $int = find_type_constraint('Int') => 'Got Int';
14         
15         my $dep_tc = MooseX::Meta::TypeConstraint::Dependent->new(
16                 name => "MooseX::Types::Dependent::Depending" ,
17                 parent => find_type_constraint('ArrayRef'),
18                 dependent_type_constraint=>$int,
19                 comparision_callback=>sub {
20                         my ($constraining_value, $check_value) = @_;
21                         return $constraining_value > $check_value ? 0:1;
22                 },
23                 constraint_generator =>$int,
24                 constraint_generator=> sub { 
25                         my ($callback, $constraining_value, $check_value) = @_;
26                         return $callback->($constraining_value, $check_value) ? 1:0;
27                 },
28         );
29         
30         ## Does this work at all?
31
32         isa_ok $dep_tc, 'MooseX::Meta::TypeConstraint::Dependent';
33
34         ok !$dep_tc->check([5,10]), "Fails, 5 is less than 10";
35         ok !$dep_tc->check(['a',10]), "Fails, 'a' is not an Int.";
36         ok !$dep_tc->check([5,'b']), "Fails, 'b' is not an Int either.";
37         ok $dep_tc->check([10,5]), "Success, 10 is greater than 5.";
38 }