test fixes and start at test for anonymous types
[gitmo/MooseX-Dependent.git] / t / 03-coercions.t
CommitLineData
54f0d8d6 1
ffd6c41d 2use strict;
3use warnings;
54f0d8d6 4
ffd6c41d 5use Test::More;
6use MooseX::Types::Parameterizable qw(Parameterizable);
7use MooseX::Types::Moose qw(Int Str HashRef ArrayRef);
8use MooseX::Types -declare=>[qw(
9 InfoHash OlderThanAge DefinedOlderThanAge
10)];
11
12ok subtype( InfoHash,
13 as HashRef[Int],
14 where {
15 defined $_->{older_than};
16 }), 'Created InfoHash Set (reduce need to depend on Dict type';
54f0d8d6 17
ffd6c41d 18ok InfoHash->check({older_than=>25}), 'Good InfoHash';
19ok !InfoHash->check({older_than=>'aaa'}), 'Bad InfoHash';
20ok !InfoHash->check({at_least=>25}), 'Bad InfoHash';
21
22ok subtype( OlderThanAge,
23 as Parameterizable[Int, InfoHash],
24 where {
25 my ($value, $dict) = @_;
26 return $value > $dict->{older_than} ? 1:0;
27 }), 'Created the OlderThanAge subtype';
28
29ok OlderThanAge([{older_than=>25}])->check(39), '39 is older than 25';
30ok OlderThanAge([older_than=>1])->check(9), '9 is older than 1';
31ok !OlderThanAge([older_than=>1])->check('aaa'), '"aaa" not an int';
32ok !OlderThanAge([older_than=>10])->check(9), '9 is not older than 10';
d1cfb043 33
ffd6c41d 34coerce OlderThanAge,
35 from HashRef,
36 via {
37 my ($hashref, $constraining_value) = @_;
38 return scalar(keys(%$hashref));
39 },
40 from ArrayRef,
41 via {
42 my ($arrayref, $constraining_value) = @_;
43 my $age;
44 $age += $_ for @$arrayref;
45 return $age;
46 };
47
48is OlderThanAge->name, 'main::OlderThanAge',
49 'Got corect name for OlderThanAge';
50is OlderThanAge([older_than=>5])->coerce([1..10]), 55,
51 'Coerce works';
52is OlderThanAge([older_than=>5])->coerce({a=>1,b=>2,c=>3,d=>4}), 4,
53 'Coerce works';
54like OlderThanAge([older_than=>2])->name, qr/main::OlderThanAge\[/,
55 'Got correct name for OlderThanAge([older_than=>2])';
56is OlderThanAge([older_than=>2])->coerce({a=>5,b=>6,c=>7,d=>8}), 4,
57 'Coerce works';
58
59SKIP: {
60 skip 'Type Coercions on defined types not supported yet', 1;
61
62 subtype DefinedOlderThanAge, as OlderThanAge([older_than=>1]);
d1cfb043 63
ffd6c41d 64 coerce DefinedOlderThanAge,
d1cfb043 65 from ArrayRef,
ffd6c41d 66 via {
d1cfb043 67 my ($arrayref, $constraining_value) = @_;
d1cfb043 68 my $age;
69 $age += $_ for @$arrayref;
70 return $age;
71 };
ffd6c41d 72
73 is DefinedOlderThanAge->coerce([1,2,3]), 6, 'Got expected Value';
88f7dcd2 74}
ffd6c41d 75
76done_testing;