bump to latest version of bundle
[gitmo/MooseX-Storage.git] / t / 060_basic_deferred.t
CommitLineData
1f3074ea 1$|++;
2use strict;
3use warnings;
4
cfee09ad 5use Test::More;
619ab942 6use Test::Deep;
1f3074ea 7use Storable;
1f3074ea 8
0b173188 9use Test::Requires {
880d376a 10 'Test::Deep::JSON' => 0, # skip all if not installed
0b173188 11 'JSON::Any' => 0.01,
12 'YAML::Any' => 0.01,
13};
14
1f3074ea 15BEGIN {
880d376a 16 plan tests => 30;
1f3074ea 17 use_ok('MooseX::Storage');
18}
19
f5f21f1d 20diag('Using implementation: ', YAML::Any->implementation);
21
1f3074ea 22{
1f3074ea 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
880d376a 50 cmp_deeply(
1f3074ea 51 $json,
880d376a 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 }),
1f3074ea 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"}',
c2dae5d8 71 { 'format' => 'JSON' }
1f3074ea 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' );
619ab942 78 cmp_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
79 cmp_deeply(
1f3074ea 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' );
c2dae5d8 100
1f3074ea 101 my $stored = $foo->freeze({ 'format' => 'Storable' });
102
103 my $struct = Storable::thaw($stored);
619ab942 104 cmp_deeply(
1f3074ea 105 $struct,
106 {
107 '__CLASS__' => 'Foo',
108 'float' => 10.5,
109 'number' => 10,
c2dae5d8 110 'string' => 'foo',
1f3074ea 111 'array' => [ 1 .. 10],
c2dae5d8 112 'hash' => { map { $_ => undef } 1 .. 10 },
1f3074ea 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,
c2dae5d8 127 'string' => 'foo',
1f3074ea 128 'array' => [ 1 .. 10],
c2dae5d8 129 'hash' => { map { $_ => undef } 1 .. 10 },
1f3074ea 130 'object' => {
131 '__CLASS__' => 'Foo',
132 'number' => 2
133 },
134 });
c2dae5d8 135
1f3074ea 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' );
619ab942 142 cmp_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
143 cmp_deeply(
1f3074ea 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
f16b5740 167 my $bar = Foo->thaw( $yaml, { 'format' => 'YAML' } );
168 isa_ok( $bar, 'Foo' );
1f3074ea 169
f16b5740 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' );
619ab942 173 cmp_deeply( $bar->array, [ 1 .. 10 ], '... got the right array' );
174 cmp_deeply(
f16b5740 175 $bar->hash,
1f3074ea 176 { map { $_ => undef } ( 1 .. 10 ) },
177 '... got the right hash'
178 );
179
f16b5740 180 isa_ok( $bar->object, 'Foo' );
181 is( $bar->object->number, 2,
1f3074ea 182 '... got the right number (in the embedded object)' );
183}
184