adding support for Deferred
[gitmo/MooseX-Storage.git] / t / 060_basic_deferred.t
1 #!/usr/bin/perl
2 $|++;
3 use strict;
4 use warnings;
5
6 use Test::More tests => 33;
7 use Storable;
8 use Test::JSON;
9 use Test::YAML::Valid;
10
11 BEGIN {
12     $ENV{JSON_ANY_ORDER} = qw(JSON);
13     use_ok('MooseX::Storage');
14 }
15
16 {
17
18     package Foo;
19     use Moose;
20     use MooseX::Storage;
21
22     with 'MooseX::Storage::Deferred';
23
24     has 'number' => ( is => 'ro', isa => 'Int' );
25     has 'string' => ( is => 'ro', isa => 'Str' );
26     has 'float'  => ( is => 'ro', isa => 'Num' );
27     has 'array'  => ( is => 'ro', isa => 'ArrayRef' );
28     has 'hash'   => ( is => 'ro', isa => 'HashRef' );
29     has 'object' => ( is => 'ro', isa => 'Object' );
30 }
31
32 {
33     my $foo = Foo->new(
34         number => 10,
35         string => 'foo',
36         float  => 10.5,
37         array  => [ 1 .. 10 ],
38         hash   => { map { $_ => undef } ( 1 .. 10 ) },
39         object => Foo->new( number => 2 ),
40     );
41     isa_ok( $foo, 'Foo' );
42
43     my $json = $foo->freeze({ 'format' => 'JSON' });
44
45     is_valid_json($json, '.. this is valid JSON');
46
47     is_json(
48         $json,
49 '{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__CLASS__":"Foo"},"number":10,"__CLASS__":"Foo","string":"foo"}',
50         '... got the right JSON'
51     );
52 }
53
54 {
55     my $foo = Foo->thaw(
56         '{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__CLASS__":"Foo"},"number":10,"__CLASS__":"Foo","string":"foo"}',
57         { 'format' => 'JSON' } 
58     );
59     isa_ok( $foo, 'Foo' );
60
61     is( $foo->number, 10,    '... got the right number' );
62     is( $foo->string, 'foo', '... got the right string' );
63     is( $foo->float,  10.5,  '... got the right float' );
64     is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
65     is_deeply(
66         $foo->hash,
67         { map { $_ => undef } ( 1 .. 10 ) },
68         '... got the right hash'
69     );
70
71     isa_ok( $foo->object, 'Foo' );
72     is( $foo->object->number, 2,
73         '... got the right number (in the embedded object)' );
74 }
75
76 {
77     my $foo = Foo->new(
78         number => 10,
79         string => 'foo',
80         float  => 10.5,
81         array  => [ 1 .. 10 ],
82         hash   => { map { $_ => undef } ( 1 .. 10 ) },
83         object => Foo->new( number => 2 ),
84     );
85     isa_ok( $foo, 'Foo' );
86     
87     my $stored = $foo->freeze({ 'format' => 'Storable' });
88
89     my $struct = Storable::thaw($stored);
90     is_deeply(
91         $struct,
92         {
93             '__CLASS__' => 'Foo',
94             'float'     => 10.5,
95             'number'    => 10,
96             'string'    => 'foo',           
97             'array'     => [ 1 .. 10],
98             'hash'      => { map { $_ => undef } 1 .. 10 },            
99             'object'    => {
100                 '__CLASS__' => 'Foo',
101                 'number' => 2
102             },
103         },
104         '... got the data struct we expected'
105     );
106 }
107
108 {
109     my $stored = Storable::nfreeze({
110         '__CLASS__' => 'Foo',
111         'float'     => 10.5,
112         'number'    => 10,
113         'string'    => 'foo',           
114         'array'     => [ 1 .. 10],
115         'hash'      => { map { $_ => undef } 1 .. 10 },            
116         'object'    => {
117             '__CLASS__' => 'Foo',
118             'number' => 2
119         },
120     });
121     
122     my $foo = Foo->thaw($stored, { 'format' => 'Storable' });
123     isa_ok( $foo, 'Foo' );
124
125     is( $foo->number, 10,    '... got the right number' );
126     is( $foo->string, 'foo', '... got the right string' );
127     is( $foo->float,  10.5,  '... got the right float' );
128     is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
129     is_deeply(
130         $foo->hash,
131         { map { $_ => undef } ( 1 .. 10 ) },
132         '... got the right hash'
133     );
134
135     isa_ok( $foo->object, 'Foo' );
136     is( $foo->object->number, 2,
137         '... got the right number (in the embedded object)' );
138 }
139
140 {
141     my $foo = Foo->new(
142         number => 10,
143         string => 'foo',
144         float  => 10.5,
145         array  => [ 1 .. 10 ],
146         hash   => { map { $_ => undef } ( 1 .. 10 ) },
147         object => Foo->new( number => 2 ),
148     );
149     isa_ok( $foo, 'Foo' );
150
151     my $yaml = $foo->freeze({ 'format' => 'YAML' });
152
153     yaml_string_ok( $yaml, '... we got valid YAML out of it' );
154
155     is(
156         $yaml,
157         q{--- 
158 __CLASS__: Foo
159 array: 
160   - 1
161   - 2
162   - 3
163   - 4
164   - 5
165   - 6
166   - 7
167   - 8
168   - 9
169   - 10
170 float: 10.5
171 hash: 
172   1: ~
173   10: ~
174   2: ~
175   3: ~
176   4: ~
177   5: ~
178   6: ~
179   7: ~
180   8: ~
181   9: ~
182 number: 10
183 object: 
184   __CLASS__: Foo
185   number: 2
186 string: foo
187 },
188         '... got the same YAML'
189     );
190
191 }
192
193 {
194     my $foo = Foo->thaw(
195         q{--- 
196 __CLASS__: Foo
197 array: 
198   - 1
199   - 2
200   - 3
201   - 4
202   - 5
203   - 6
204   - 7
205   - 8
206   - 9
207   - 10
208 float: 10.5
209 hash: 
210   1: ~
211   10: ~
212   2: ~
213   3: ~
214   4: ~
215   5: ~
216   6: ~
217   7: ~
218   8: ~
219   9: ~
220 number: 10
221 object: 
222   __CLASS__: Foo
223   number: 2
224 string: foo
225 }, { 'format' => 'YAML' }
226     );
227     isa_ok( $foo, 'Foo' );
228
229     is( $foo->number, 10,    '... got the right number' );
230     is( $foo->string, 'foo', '... got the right string' );
231     is( $foo->float,  10.5,  '... got the right float' );
232     is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
233     is_deeply(
234         $foo->hash,
235         { map { $_ => undef } ( 1 .. 10 ) },
236         '... got the right hash'
237     );
238
239     isa_ok( $foo->object, 'Foo' );
240     is( $foo->object->number, 2,
241         '... got the right number (in the embedded object)' );
242 }
243