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