merged
[gitmo/MooseX-Types-Structured.git] / t / 07-coerce.t
CommitLineData
c81443cb 1BEGIN {
8dbdca20 2 use strict;
3 use warnings;
4 use Test::More tests=>16;
c81443cb 5}
6
7{
8 package Test::MooseX::Meta::TypeConstraint::Structured::Coerce;
9
10 use Moose;
11 use MooseX::Types::Structured qw(Dict Tuple);
8dbdca20 12 use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef);
13 use MooseX::Types -declare => [qw(
16aea7bf 14 myDict myTuple Fullname
c81443cb 15
16 )];
8dbdca20 17
16aea7bf 18 subtype myDict,
19 as Dict[name=>Str, age=>Int];
8dbdca20 20
16aea7bf 21 subtype Fullname,
22 as Dict[first=>Str, last=>Str];
8dbdca20 23
16aea7bf 24 coerce Fullname,
25 from ArrayRef,
26 via { +{first=>$_->[0], last=>$_->[1]} };
8dbdca20 27
16aea7bf 28 subtype myTuple,
29 as Tuple[Str, Int];
c81443cb 30
16aea7bf 31 ## Create some coercions. Note the dob_epoch could be a more useful convert
32 ## from a dob datetime object, I'm just lazy.
8dbdca20 33
16aea7bf 34 coerce myDict,
35 from Int,
36 via { +{name=>'JohnDoe', age=>$_} },
37 from Dict[aname=>HashRef, dob_in_years=>Int],
38 via { +{
39 name=> $_->{aname}->{first} .' '. $_->{aname}->{last},
40 age=>$_->{dob_in_years},
41 }
42 },
43 from Dict[bname=>HashRef, dob_in_years=>Int],
44 via { +{
45 name=> $_->{bname}->{first} .' '. $_->{bname}->{last},
46 age=>$_->{dob_in_years},
47 }
48 },
49 from Dict[fullname=>Fullname, dob_epoch=>Int],
50 via { +{
51 name=> $_->{fullname}->{first} .' '. $_->{fullname}->{last},
52 age=>$_->{dob_epoch}}
53 },
54 from myTuple,
55 via { +{name=>$_->[0], age=>$_->[1]} };
56
57 has 'stuff' => (is=>'rw', isa=>myDict, coerce=>1);
c81443cb 58}
59
16aea7bf 60## Create an object to test
61
62ok my $person = Test::MooseX::Meta::TypeConstraint::Structured::Coerce->new();
63isa_ok $person, 'Test::MooseX::Meta::TypeConstraint::Structured::Coerce';## Try out the coercions
64
65ok $person->stuff({name=>"John",age=>25}), 'Set Stuff {name=>"John",age=>25}';
66is_deeply $person->stuff, {name=>"John",age=>25}, 'Correct set';
67
68ok $person->stuff(30), 'Set Stuff 30';
69is_deeply $person->stuff, {name=>"JohnDoe",age=>30}, 'Correct set';
70
71ok $person->stuff({aname=>{first=>"frank", last=>"herbert"},dob_in_years=>80}),
72 '{{first=>"frank", last=>"herbert"},80}';
8dbdca20 73
16aea7bf 74is_deeply $person->stuff, {name=>"frank herbert",age=>80}, 'Correct set';
c81443cb 75
16aea7bf 76ok $person->stuff({bname=>{first=>"frankbbb", last=>"herbert"},dob_in_years=>84}),
77 '{{first=>"frankbbb", last=>"herbert"},84}';
8dbdca20 78
16aea7bf 79is_deeply $person->stuff, {name=>"frankbbb herbert",age=>84}, 'Correct set';
80
81ok $person->stuff(["mary",40]), 'Set Stuff ["mary",40]';
82is_deeply $person->stuff, {name=>"mary",age=>40}, 'Correct set';
83
84ok $person->stuff({fullname=>{first=>"frank", last=>"herbert1"},dob_epoch=>85}),
85 '{{first=>"frank", last=>"herbert1"},85}';
8dbdca20 86
16aea7bf 87is_deeply $person->stuff, {name=>"frank herbert1",age=>85}, 'Correct set';
88
89SKIP: {
90 skip 'deep coercions not yet supported', 2, 1;
8dbdca20 91
16aea7bf 92 ok $person->stuff({fullname=>["frank", "herbert2"],dob_epoch=>86}),
93 '{fullname=>["frank", "herbert2"],dob_epoch=>86}';
8dbdca20 94
95 is_deeply $person->stuff, {name=>"frank herbert2",age=>86}, 'Correct set';
16aea7bf 96}
c81443cb 97
98