File: 08-examples.t

package info (click to toggle)
libmoosex-types-structured-perl 0.36-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 492 kB
  • sloc: perl: 757; makefile: 11
file content (74 lines) | stat: -rw-r--r-- 2,104 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
use strict;
use warnings;
use Test::More;
use Test::Needs 'MooseX::Types::DateTime';
plan tests => 10;

{
    ## Normalize a HashRef
    package Test::MooseX::Meta::TypeConstraint::Structured::Examples::Normalize;

    use Moose;
    use DateTime;
    use MooseX::Types::Structured qw(Dict Tuple);
    use MooseX::Types::DateTime qw(DateTime);
    use MooseX::Types::Moose qw(Int Str Object ArrayRef HashRef);
    use MooseX::Types -declare => [qw(
        Name Age Person FullName

    )];

    ## So that our test works, we'll set Now to 2008.
    sub Now {
        return 'DateTime'->new(year=>2008);
    }

    subtype FullName,
     as Dict[last=>Str, first=>Str];

    subtype Person,
     as Dict[name=>Str, age=>Int];

    coerce Person,
     from Dict[first=>Str, last=>Str, years=>Int],
     via { +{
        name => "$_->{first} $_->{last}",
        age=>$_->{years},
     }},
     from Dict[fullname=>FullName, dob=>DateTime],
     via { +{
        name => "$_->{fullname}{first} $_->{fullname}{last}",
        age => ($_->{dob} - Now)->years,
     }};

    has person => (is=>'rw', isa=>Person, coerce=>1);
}

NORMALIZE: {
    ok my $normalize = Test::MooseX::Meta::TypeConstraint::Structured::Examples::Normalize->new();
    isa_ok $normalize, 'Test::MooseX::Meta::TypeConstraint::Structured::Examples::Normalize';

    ok $normalize->person({name=>'John', age=>25})
     => 'Set value';

    is_deeply $normalize->person, {name=>'John', age=>25}
     => 'Value is correct';

    ok $normalize->person({first=>'John', last=>'Napiorkowski', years=>35})
     => 'Set value';

    is_deeply $normalize->person, {name=>'John Napiorkowski', age=>35}
     => 'Value is correct';

    ok $normalize->person({years=>36, last=>'Napiorkowski', first=>'John'})
     => 'Set value';

    is_deeply $normalize->person, {name=>'John Napiorkowski', age=>36}
     => 'Value is correct';

    ok $normalize->person({fullname=>{first=>'Vanessa', last=>'Li'}, dob=>DateTime->new(year=>1974)})
     => 'Set value';

    is_deeply $normalize->person, {name=>'Vanessa Li', age=>34}
     => 'Value is correct';
}