reformat Changes to make CPAN::Changes::Spec and metacpan parsing happy
[gitmo/MooseX-Storage.git] / t / 060_basic_deferred.t
1 $|++;
2 use strict;
3 use warnings;
4
5 use Test::More;
6 use Test::Deep;
7 use Storable;
8
9 use Test::Requires {
10     'Test::Deep::JSON' => 0, # skip all if not installed
11     'JSON::Any' => 0.01,
12     'YAML::Any' => 0.01,
13 };
14
15 BEGIN {
16     plan tests => 30;
17     use_ok('MooseX::Storage');
18 }
19
20 diag('Using implementation: ', YAML::Any->implementation);
21
22 {
23     package Foo;
24     use Moose;
25     use MooseX::Storage;
26
27     with 'MooseX::Storage::Deferred';
28
29     has 'number' => ( is => 'ro', isa => 'Int' );
30     has 'string' => ( is => 'ro', isa => 'Str' );
31     has 'float'  => ( is => 'ro', isa => 'Num' );
32     has 'array'  => ( is => 'ro', isa => 'ArrayRef' );
33     has 'hash'   => ( is => 'ro', isa => 'HashRef' );
34     has 'object' => ( is => 'ro', isa => 'Object' );
35 }
36
37 {
38     my $foo = Foo->new(
39         number => 10,
40         string => 'foo',
41         float  => 10.5,
42         array  => [ 1 .. 10 ],
43         hash   => { map { $_ => undef } ( 1 .. 10 ) },
44         object => Foo->new( number => 2 ),
45     );
46     isa_ok( $foo, 'Foo' );
47
48     my $json = $foo->freeze({ 'format' => 'JSON' });
49
50     cmp_deeply(
51         $json,
52         json({
53             number => 10,
54             string => 'foo',
55             float  => 10.5,
56             array  => [ 1 .. 10 ],
57             hash   => { map { $_ => undef } ( 1 .. 10 ) },
58             __CLASS__ => 'Foo',
59             object => {
60                 number => 2,
61                 __CLASS__ => 'Foo'
62             },
63         }),
64         '... got the right JSON'
65     );
66 }
67
68 {
69     my $foo = Foo->thaw(
70         '{"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"}',
71         { 'format' => 'JSON' }
72     );
73     isa_ok( $foo, 'Foo' );
74
75     is( $foo->number, 10,    '... got the right number' );
76     is( $foo->string, 'foo', '... got the right string' );
77     is( $foo->float,  10.5,  '... got the right float' );
78     cmp_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
79     cmp_deeply(
80         $foo->hash,
81         { map { $_ => undef } ( 1 .. 10 ) },
82         '... got the right hash'
83     );
84
85     isa_ok( $foo->object, 'Foo' );
86     is( $foo->object->number, 2,
87         '... got the right number (in the embedded object)' );
88 }
89
90 {
91     my $foo = Foo->new(
92         number => 10,
93         string => 'foo',
94         float  => 10.5,
95         array  => [ 1 .. 10 ],
96         hash   => { map { $_ => undef } ( 1 .. 10 ) },
97         object => Foo->new( number => 2 ),
98     );
99     isa_ok( $foo, 'Foo' );
100
101     my $stored = $foo->freeze({ 'format' => 'Storable' });
102
103     my $struct = Storable::thaw($stored);
104     cmp_deeply(
105         $struct,
106         {
107             '__CLASS__' => 'Foo',
108             'float'     => 10.5,
109             'number'    => 10,
110             'string'    => 'foo',
111             'array'     => [ 1 .. 10],
112             'hash'      => { map { $_ => undef } 1 .. 10 },
113             'object'    => {
114                 '__CLASS__' => 'Foo',
115                 'number' => 2
116             },
117         },
118         '... got the data struct we expected'
119     );
120 }
121
122 {
123     my $stored = Storable::nfreeze({
124         '__CLASS__' => 'Foo',
125         'float'     => 10.5,
126         'number'    => 10,
127         'string'    => 'foo',
128         'array'     => [ 1 .. 10],
129         'hash'      => { map { $_ => undef } 1 .. 10 },
130         'object'    => {
131             '__CLASS__' => 'Foo',
132             'number' => 2
133         },
134     });
135
136     my $foo = Foo->thaw($stored, { 'format' => 'Storable' });
137     isa_ok( $foo, 'Foo' );
138
139     is( $foo->number, 10,    '... got the right number' );
140     is( $foo->string, 'foo', '... got the right string' );
141     is( $foo->float,  10.5,  '... got the right float' );
142     cmp_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
143     cmp_deeply(
144         $foo->hash,
145         { map { $_ => undef } ( 1 .. 10 ) },
146         '... got the right hash'
147     );
148
149     isa_ok( $foo->object, 'Foo' );
150     is( $foo->object->number, 2,
151         '... got the right number (in the embedded object)' );
152 }
153
154 {
155     my $foo = Foo->new(
156         number => 10,
157         string => 'foo',
158         float  => 10.5,
159         array  => [ 1 .. 10 ],
160         hash   => { map { $_ => undef } ( 1 .. 10 ) },
161         object => Foo->new( number => 2 ),
162     );
163     isa_ok( $foo, 'Foo' );
164
165     my $yaml = $foo->freeze({ 'format' => 'YAML' });
166
167     my $bar = Foo->thaw( $yaml, { 'format' => 'YAML' } );
168     isa_ok( $bar, 'Foo' );
169
170     is( $bar->number, 10,    '... got the right number' );
171     is( $bar->string, 'foo', '... got the right string' );
172     is( $bar->float,  10.5,  '... got the right float' );
173     cmp_deeply( $bar->array, [ 1 .. 10 ], '... got the right array' );
174     cmp_deeply(
175         $bar->hash,
176         { map { $_ => undef } ( 1 .. 10 ) },
177         '... got the right hash'
178     );
179
180     isa_ok( $bar->object, 'Foo' );
181     is( $bar->object->number, 2,
182         '... got the right number (in the embedded object)' );
183 }
184