Commit | Line | Data |
c81443cb |
1 | BEGIN { |
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 | |
62 | ok my $person = Test::MooseX::Meta::TypeConstraint::Structured::Coerce->new(); |
63 | isa_ok $person, 'Test::MooseX::Meta::TypeConstraint::Structured::Coerce';## Try out the coercions |
64 | |
65 | ok $person->stuff({name=>"John",age=>25}), 'Set Stuff {name=>"John",age=>25}'; |
66 | is_deeply $person->stuff, {name=>"John",age=>25}, 'Correct set'; |
67 | |
68 | ok $person->stuff(30), 'Set Stuff 30'; |
69 | is_deeply $person->stuff, {name=>"JohnDoe",age=>30}, 'Correct set'; |
70 | |
71 | ok $person->stuff({aname=>{first=>"frank", last=>"herbert"},dob_in_years=>80}), |
72 | '{{first=>"frank", last=>"herbert"},80}'; |
8dbdca20 |
73 | |
16aea7bf |
74 | is_deeply $person->stuff, {name=>"frank herbert",age=>80}, 'Correct set'; |
c81443cb |
75 | |
16aea7bf |
76 | ok $person->stuff({bname=>{first=>"frankbbb", last=>"herbert"},dob_in_years=>84}), |
77 | '{{first=>"frankbbb", last=>"herbert"},84}'; |
8dbdca20 |
78 | |
16aea7bf |
79 | is_deeply $person->stuff, {name=>"frankbbb herbert",age=>84}, 'Correct set'; |
80 | |
81 | ok $person->stuff(["mary",40]), 'Set Stuff ["mary",40]'; |
82 | is_deeply $person->stuff, {name=>"mary",age=>40}, 'Correct set'; |
83 | |
84 | ok $person->stuff({fullname=>{first=>"frank", last=>"herbert1"},dob_epoch=>85}), |
85 | '{{first=>"frank", last=>"herbert1"},85}'; |
8dbdca20 |
86 | |
16aea7bf |
87 | is_deeply $person->stuff, {name=>"frank herbert1",age=>85}, 'Correct set'; |
88 | |
89 | SKIP: { |
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 | |