create a basic type, clarified and regularized some of the naming conventions for...
[gitmo/MooseX-Dependent.git] / t / 01-basic.t
1
2 use Test::More tests=>9; {
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 sees if
11         ## the dependent value (the first) is greater than the constraining value
12         ## (the second).
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" ,
18                 parent => find_type_constraint('ArrayRef'),
19                 dependent_type_constraint=>$int,
20                 comparison_callback=>sub {
21                         my ($dependent_val, $constraining_val) = @_;
22                         return ($dependent_val > $constraining_val) ? 1:undef;
23                 },
24                 constraining_type_constraint =>$int,
25                 constraint_generator=> sub {
26                         my ($dependent_val, $callback, $constraining_val) = @_;
27                         return $callback->($dependent_val, $constraining_val);
28                 },
29         );
30
31         isa_ok $dep_tc, 'MooseX::Meta::TypeConstraint::Dependent';
32         
33         ok !$dep_tc->check(['a',10]), "Fails, 'a' is not an Int.";
34         ok !$dep_tc->check([5,'b']), "Fails, 'b' is not an Int either.";
35         ok !$dep_tc->check({4,1}), "Fails, since this isn't an arrayref";
36         ok !$dep_tc->check([5,10]), "Fails, 5 is less than 10";
37         ok $dep_tc->check([11,6]), "Success, 11 is greater than 6.";
38 }