Actually do something useful with the engine_traits parameter, by appling a role...
[gitmo/MooseX-Storage.git] / t / 070_basic_maybe.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 22;
7
8 BEGIN {
9     use_ok('MooseX::Storage');
10 }
11
12 {
13
14     package Foo;
15     use Moose;
16     use MooseX::Storage;
17
18     with Storage;
19
20     has 'number'  => ( is => 'ro', isa => 'Maybe[Int]' );
21     has 'string'  => ( is => 'ro', isa => 'Maybe[Str]' );
22     has 'boolean' => ( is => 'ro', isa => 'Maybe[Bool]' );
23     has 'float'   => ( is => 'ro', isa => 'Maybe[Num]' );
24     has 'array'   => ( is => 'ro', isa => 'Maybe[ArrayRef]' );
25     has 'hash'    => ( is => 'ro', isa => 'Maybe[HashRef]' );
26     has 'object'  => ( is => 'ro', isa => 'Maybe[Foo]' );
27 }
28
29 {
30     my $foo = Foo->new(
31         number  => 10,
32         string  => 'foo',
33         boolean => 1,
34         float   => 10.5,
35         array   => [ 1 .. 10 ],
36         hash    => { map { $_ => undef } ( 1 .. 10 ) },
37         object  => Foo->new( number => 2 ),
38     );
39     isa_ok( $foo, 'Foo' );
40     
41     is_deeply(
42         $foo->pack,
43         {
44             __CLASS__ => 'Foo',
45             number    => 10,
46             string    => 'foo',
47             boolean   => 1,
48             float     => 10.5,
49             array     => [ 1 .. 10 ],
50             hash      => { map { $_ => undef } ( 1 .. 10 ) },
51             object    => { 
52                             __CLASS__ => 'Foo',                
53                             number    => 2 
54                          },            
55         },
56         '... got the right frozen class'
57     );
58 }
59
60 {
61     my $foo = Foo->unpack(
62         {
63             __CLASS__ => 'Foo',
64             number    => 10,
65             string    => 'foo',
66             boolean   => 1,
67             float     => 10.5,
68             array     => [ 1 .. 10 ],
69             hash      => { map { $_ => undef } ( 1 .. 10 ) },
70             object    => { 
71                             __CLASS__ => 'Foo',                
72                             number    => 2 
73                          },            
74         }        
75     );
76     isa_ok( $foo, 'Foo' );
77
78     is( $foo->number, 10,    '... got the right number' );
79     is( $foo->string, 'foo', '... got the right string' );
80     ok( $foo->boolean,       '... got the right boolean' );
81     is( $foo->float,  10.5,  '... got the right float' );
82     is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
83     is_deeply(
84         $foo->hash,
85         { map { $_ => undef } ( 1 .. 10 ) },
86         '... got the right hash'
87     );
88
89     isa_ok( $foo->object, 'Foo' );
90     is( $foo->object->number, 2,
91         '... got the right number (in the embedded object)' );
92 }
93
94
95 {
96
97     package Foo;
98     use Moose;
99     use Moose::Util::TypeConstraints;
100     use MooseX::Storage;
101
102     use Scalar::Util 'looks_like_number';
103
104     with Storage;    
105     
106     subtype 'Natural' 
107         => as 'Int'
108         => where { $_ > 0 };
109         
110     subtype 'HalfNum' 
111         => as 'Num'
112         => where { "$_" =~ /\.5$/ };    
113     
114     subtype 'FooString'
115         => as 'Str'
116         => where { lc($_) eq 'foo' };
117         
118     subtype 'IntArray' 
119         => as 'ArrayRef'
120         => where { scalar grep { looks_like_number($_) } @{$_} };
121
122     subtype 'UndefHash' 
123         => as 'HashRef'
124         => where { scalar grep { !defined($_) } values %{$_} };
125
126     has 'number' => ( is => 'ro', isa => 'Maybe[Natural]' );
127     has 'string' => ( is => 'ro', isa => 'Maybe[FooString]' );
128     has 'float'  => ( is => 'ro', isa => 'Maybe[HalfNum]' );
129     has 'array'  => ( is => 'ro', isa => 'Maybe[IntArray]' );
130     has 'hash'   => ( is => 'ro', isa => 'Maybe[UndefHash]' );
131     has 'object' => ( is => 'ro', isa => 'Maybe[Foo]' );
132 }
133
134 {
135     my $foo = Foo->new(
136         number => 10,
137         string => 'foo',
138         float  => 10.5,
139         array  => [ 1 .. 10 ],
140         hash   => { map { $_ => undef } ( 1 .. 10 ) },
141         object => Foo->new( number => 2 ),
142     );
143     isa_ok( $foo, 'Foo' );
144     
145     is_deeply(
146         $foo->pack,
147         {
148             __CLASS__ => 'Foo',
149             number    => 10,
150             string    => 'foo',
151             float     => 10.5,
152             array     => [ 1 .. 10 ],
153             hash      => { map { $_ => undef } ( 1 .. 10 ) },
154             object    => { 
155                             __CLASS__ => 'Foo',                
156                             number    => 2 
157                          },            
158         },
159         '... got the right frozen class'
160     );
161 }
162
163 {
164     my $foo = Foo->unpack(
165         {
166             __CLASS__ => 'Foo',
167             number    => 10,
168             string    => 'foo',
169             float     => 10.5,
170             array     => [ 1 .. 10 ],
171             hash      => { map { $_ => undef } ( 1 .. 10 ) },
172             object    => { 
173                             __CLASS__ => 'Foo',                
174                             number    => 2 
175                          },            
176         }        
177     );
178     isa_ok( $foo, 'Foo' );
179
180     is( $foo->number, 10,    '... got the right number' );
181     is( $foo->string, 'foo', '... got the right string' );
182     is( $foo->float,  10.5,  '... got the right float' );
183     is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
184     is_deeply(
185         $foo->hash,
186         { map { $_ => undef } ( 1 .. 10 ) },
187         '... got the right hash'
188     );
189
190     isa_ok( $foo->object, 'Foo' );
191     is( $foo->object->number, 2,
192         '... got the right number (in the embedded object)' );
193 }