3743b80eb22fd1a3f4298d4831a03b04dc06b879
[gitmo/MooseX-Dependent.git] / t / 03-coercions.t
1
2 use Test::More tests=>9; {
3         
4         use strict;
5         use warnings;
6
7         use MooseX::Dependent::Types qw(Dependent);
8         use MooseX::Types::Moose qw(Int Str HashRef ArrayRef);
9         
10         use MooseX::Types -declare=>[qw(
11                 InfoHash OlderThanAge
12         )];
13         
14         ok subtype( InfoHash,
15                 as HashRef[Int],
16                 where {
17                         defined $_->{older_than};
18                 }), 'Created InfoHash Set (reduce need to depend on Dict type';
19
20         ok InfoHash->check({older_than=>25}), 'Good InfoHash';
21         ok !InfoHash->check({older_than=>'aaa'}), 'Bad InfoHash';
22         ok !InfoHash->check({at_least=>25}), 'Bad InfoHash';
23         
24         ok subtype( OlderThanAge,
25                 as Dependent[Int, InfoHash],
26                 where {
27                         my ($value, $dict) = @_;
28                         return $value > $dict->{older_than} ? 1:0;
29                 }), 'Created the OlderThanAge subtype';
30         
31         ok OlderThanAge([{older_than=>25}])->check(39), '39 is older than 25';
32         ok OlderThanAge([older_than=>1])->check(9), '9 is older than 1';
33         ok !OlderThanAge([older_than=>1])->check('aaa'), '"aaa" not an int';
34         ok !OlderThanAge([older_than=>10])->check(9), '9 is not older than 10';
35         
36         coerce OlderThanAge,
37                 from ArrayRef,
38                 via {
39                         my ($arrayref, $constraining_value) = @_;
40                         my $age;
41                         $age += $_ for @$arrayref;
42                         return $age;
43                 };
44                 
45         #warn OlderThanAge([older_than=>1])->coerce([1,2,3,4]);
46 }