Got the basic requirement in place!
[gitmo/MooseX-Dependent.git] / t / 01-basic.t
CommitLineData
a018b5bb 1
3a5dab74 2use Test::More tests=>9; {
3cfd35fd 3
4 use strict;
5 use warnings;
6
7 use_ok 'MooseX::Meta::TypeConstraint::Dependent';
8 use_ok 'Moose::Util::TypeConstraints';
9
3a5dab74 10 ## A sample dependent type constraint the requires two ints and sees if
11 ## the dependent value (the first) is greater than the constraining value
12 ## (the second).
3cfd35fd 13
14 ok my $int = find_type_constraint('Int') => 'Got Int';
15
16 my $dep_tc = MooseX::Meta::TypeConstraint::Dependent->new(
17 name => "MooseX::Types::Dependent::Depending" ,
3a5dab74 18 parent => find_type_constraint('Int'),
3cfd35fd 19 dependent_type_constraint=>$int,
3a5dab74 20 comparison_callback=>sub {
3cfd35fd 21 my ($constraining_value, $check_value) = @_;
3a5dab74 22 return $check_value > $constraining_value ? 0:1;
3cfd35fd 23 },
3a5dab74 24 constraining_type_constraint =>$int,
25 constraint_generator=> sub {
26 ## Because "shift->(shift,shift)" is not very clear, is it :)
3cfd35fd 27 my ($callback, $constraining_value, $check_value) = @_;
3a5dab74 28 return $callback->($constraining_value, $check_value);
3cfd35fd 29 },
30 );
3cfd35fd 31
32 isa_ok $dep_tc, 'MooseX::Meta::TypeConstraint::Dependent';
3a5dab74 33
34 ok !$dep_tc->check('a',10), "Fails, 'a' is not an Int.";
35 ok !$dep_tc->check(5,'b'), "Fails, 'b' is not an Int either.";
36 ok !$dep_tc->check([4,1]), "Fails, since this isn't an arrayref";
37 ok !$dep_tc->check(5,10), "Fails, 5 is less than 10";
38 ok $dep_tc->check(11,6), "Success, 11 is greater than 6.";
a018b5bb 39}