4 use Test::More tests=>16;
9 package Test::MooseX::Meta::TypeConstraint::Structured::Coerce;
12 use MooseX::Types::Structured qw(Dict Tuple);
13 use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef);
14 use MooseX::Types -declare => [qw(
15 myDict myTuple Fullname
20 as Dict[name=>Str, age=>Int];
23 as Dict[first=>Str, last=>Str];
27 via { +{first=>$_->[0], last=>$_->[1]} };
32 ## Create some coercions. Note the dob_epoch could be a more useful convert
33 ## from a dob datetime object, I'm just lazy.
37 via { +{name=>'JohnDoe', age=>$_} },
38 from Dict[aname=>HashRef, dob_in_years=>Int],
40 name=> $_->{aname}->{first} .' '. $_->{aname}->{last},
41 age=>$_->{dob_in_years},
44 from Dict[bname=>HashRef, dob_in_years=>Int],
46 name=> $_->{bname}->{first} .' '. $_->{bname}->{last},
47 age=>$_->{dob_in_years},
50 from Dict[fullname=>Fullname, dob_epoch=>Int],
52 name=> $_->{fullname}->{first} .' '. $_->{fullname}->{last},
56 via { +{name=>$_->[0], age=>$_->[1]} };
58 has 'stuff' => (is=>'rw', isa=>myDict, coerce=>1);
61 ## Create an object to test
63 ok my $person = Test::MooseX::Meta::TypeConstraint::Structured::Coerce->new();
64 isa_ok $person, 'Test::MooseX::Meta::TypeConstraint::Structured::Coerce';## Try out the coercions
66 ok $person->stuff({name=>"John",age=>25}), 'Set Stuff {name=>"John",age=>25}';
67 is_deeply $person->stuff, {name=>"John",age=>25}, 'Correct set';
69 ok $person->stuff(30), 'Set Stuff 30';
70 is_deeply $person->stuff, {name=>"JohnDoe",age=>30}, 'Correct set';
72 ok $person->stuff({aname=>{first=>"frank", last=>"herbert"},dob_in_years=>80}),
73 '{{first=>"frank", last=>"herbert"},80}';
75 is_deeply $person->stuff, {name=>"frank herbert",age=>80}, 'Correct set';
77 ok $person->stuff({bname=>{first=>"frankbbb", last=>"herbert"},dob_in_years=>84}),
78 '{{first=>"frankbbb", last=>"herbert"},84}';
80 is_deeply $person->stuff, {name=>"frankbbb herbert",age=>84}, 'Correct set';
82 ok $person->stuff(["mary",40]), 'Set Stuff ["mary",40]';
83 is_deeply $person->stuff, {name=>"mary",age=>40}, 'Correct set';
85 ok $person->stuff({fullname=>{first=>"frank", last=>"herbert1"},dob_epoch=>85}),
86 '{{first=>"frank", last=>"herbert1"},85}';
88 is_deeply $person->stuff, {name=>"frank herbert1",age=>85}, 'Correct set';
91 skip 'deep coercions not yet supported', 2, 1;
93 ok $person->stuff({fullname=>["frank", "herbert2"],dob_epoch=>86}),
94 '{fullname=>["frank", "herbert2"],dob_epoch=>86}';
96 is_deeply $person->stuff, {name=>"frank herbert2",age=>86}, 'Correct set';