Commit | Line | Data |
59deb858 |
1 | BEGIN { |
8dbdca20 |
2 | use strict; |
3 | use warnings; |
4 | use Test::More; |
5 | |
59deb858 |
6 | eval "use MooseX::Types::DateTime"; |
7 | plan $@ |
8 | ? ( skip_all => "Tests require MooseX::Types::DateTime" ) |
9 | : ( tests => 10 ); |
10 | } |
11 | |
12 | { |
8dbdca20 |
13 | ## Normalize a HashRef |
59deb858 |
14 | package Test::MooseX::Meta::TypeConstraint::Structured::Examples::Normalize; |
15 | |
16 | use Moose; |
8dbdca20 |
17 | use DateTime; |
59deb858 |
18 | use MooseX::Types::Structured qw(Dict Tuple); |
8dbdca20 |
19 | use MooseX::Types::DateTime qw(DateTime); |
20 | use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef); |
21 | use MooseX::Types -declare => [qw( |
59deb858 |
22 | Name Age Person FullName |
23 | |
24 | )]; |
8dbdca20 |
25 | |
26 | ## So that our test works, we'll set Now to 2008. |
27 | sub Now { |
28 | return 'DateTime'->new(year=>2008); |
29 | } |
30 | |
31 | subtype FullName, |
32 | as Dict[last=>Str, first=>Str]; |
33 | |
59deb858 |
34 | subtype Person, |
8dbdca20 |
35 | as Dict[name=>Str, age=>Int]; |
36 | |
37 | coerce Person, |
38 | from Dict[first=>Str, last=>Str, years=>Int], |
39 | via { +{ |
40 | name => "$_->{first} $_->{last}", |
41 | age=>$_->{years}, |
42 | }}, |
43 | from Dict[fullname=>FullName, dob=>DateTime], |
44 | via { +{ |
45 | name => "$_->{fullname}{first} $_->{fullname}{last}", |
46 | age => ($_->{dob} - Now)->years, |
47 | }}; |
48 | |
49 | has person => (is=>'rw', isa=>Person, coerce=>1); |
59deb858 |
50 | } |
51 | |
52 | NORMALIZE: { |
8dbdca20 |
53 | ok my $normalize = Test::MooseX::Meta::TypeConstraint::Structured::Examples::Normalize->new(); |
54 | isa_ok $normalize, 'Test::MooseX::Meta::TypeConstraint::Structured::Examples::Normalize'; |
55 | |
56 | ok $normalize->person({name=>'John', age=>25}) |
57 | => 'Set value'; |
58 | |
59 | is_deeply $normalize->person, {name=>'John', age=>25} |
60 | => 'Value is correct'; |
61 | |
62 | ok $normalize->person({first=>'John', last=>'Napiorkowski', years=>35}) |
63 | => 'Set value'; |
64 | |
65 | is_deeply $normalize->person, {name=>'John Napiorkowski', age=>35} |
66 | => 'Value is correct'; |
67 | |
68 | ok $normalize->person({years=>36, last=>'Napiorkowski', first=>'John'}) |
69 | => 'Set value'; |
70 | |
71 | is_deeply $normalize->person, {name=>'John Napiorkowski', age=>36} |
72 | => 'Value is correct'; |
73 | |
74 | ok $normalize->person({fullname=>{first=>'Vanessa', last=>'Li'}, dob=>DateTime->new(year=>1974)}) |
75 | => 'Set value'; |
76 | |
77 | is_deeply $normalize->person, {name=>'Vanessa Li', age=>34} |
78 | => 'Value is correct'; |
59deb858 |
79 | } |