Add YAML implementation tests
[gitmo/MooseX-Storage.git] / t / 060_basic_deferred.t
CommitLineData
1f3074ea 1#!/usr/bin/perl
4fa64e86 2
1f3074ea 3$|++;
4use strict;
5use warnings;
6
cfee09ad 7use Test::More;
1f3074ea 8use Storable;
1f3074ea 9
10BEGIN {
f16b5740 11 eval "use Test::JSON";
12 plan skip_all => "Test::JSON is required for this test" if $@;
4fa64e86 13 eval "use JSON::Any";
f16b5740 14 plan skip_all => "JSON::Any is required for this test" if $@;
15 eval "use YAML::Any";
16 plan skip_all => "YAML::Any is required for this test" if $@;
17 plan tests => 31;
1f3074ea 18 use_ok('MooseX::Storage');
19}
20
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 is_valid_json($json, '.. this is valid JSON');
51
52 is_json(
53 $json,
54'{"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"}',
55 '... got the right JSON'
56 );
57}
58
59{
60 my $foo = Foo->thaw(
61 '{"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"}',
b5f363ac 62 { 'format' => 'JSON' }
1f3074ea 63 );
64 isa_ok( $foo, 'Foo' );
65
66 is( $foo->number, 10, '... got the right number' );
67 is( $foo->string, 'foo', '... got the right string' );
68 is( $foo->float, 10.5, '... got the right float' );
69 is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
70 is_deeply(
71 $foo->hash,
72 { map { $_ => undef } ( 1 .. 10 ) },
73 '... got the right hash'
74 );
75
76 isa_ok( $foo->object, 'Foo' );
77 is( $foo->object->number, 2,
78 '... got the right number (in the embedded object)' );
79}
80
81{
82 my $foo = Foo->new(
83 number => 10,
84 string => 'foo',
85 float => 10.5,
86 array => [ 1 .. 10 ],
87 hash => { map { $_ => undef } ( 1 .. 10 ) },
88 object => Foo->new( number => 2 ),
89 );
90 isa_ok( $foo, 'Foo' );
b5f363ac 91
1f3074ea 92 my $stored = $foo->freeze({ 'format' => 'Storable' });
93
94 my $struct = Storable::thaw($stored);
95 is_deeply(
96 $struct,
97 {
98 '__CLASS__' => 'Foo',
99 'float' => 10.5,
100 'number' => 10,
b5f363ac 101 'string' => 'foo',
1f3074ea 102 'array' => [ 1 .. 10],
b5f363ac 103 'hash' => { map { $_ => undef } 1 .. 10 },
1f3074ea 104 'object' => {
105 '__CLASS__' => 'Foo',
106 'number' => 2
107 },
108 },
109 '... got the data struct we expected'
110 );
111}
112
113{
114 my $stored = Storable::nfreeze({
115 '__CLASS__' => 'Foo',
116 'float' => 10.5,
117 'number' => 10,
b5f363ac 118 'string' => 'foo',
1f3074ea 119 'array' => [ 1 .. 10],
b5f363ac 120 'hash' => { map { $_ => undef } 1 .. 10 },
1f3074ea 121 'object' => {
122 '__CLASS__' => 'Foo',
123 'number' => 2
124 },
125 });
b5f363ac 126
1f3074ea 127 my $foo = Foo->thaw($stored, { 'format' => 'Storable' });
128 isa_ok( $foo, 'Foo' );
129
130 is( $foo->number, 10, '... got the right number' );
131 is( $foo->string, 'foo', '... got the right string' );
132 is( $foo->float, 10.5, '... got the right float' );
133 is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
134 is_deeply(
135 $foo->hash,
136 { map { $_ => undef } ( 1 .. 10 ) },
137 '... got the right hash'
138 );
139
140 isa_ok( $foo->object, 'Foo' );
141 is( $foo->object->number, 2,
142 '... got the right number (in the embedded object)' );
143}
144
145{
146 my $foo = Foo->new(
147 number => 10,
148 string => 'foo',
149 float => 10.5,
150 array => [ 1 .. 10 ],
151 hash => { map { $_ => undef } ( 1 .. 10 ) },
152 object => Foo->new( number => 2 ),
153 );
154 isa_ok( $foo, 'Foo' );
155
156 my $yaml = $foo->freeze({ 'format' => 'YAML' });
157
f16b5740 158 my $bar = Foo->thaw( $yaml, { 'format' => 'YAML' } );
159 isa_ok( $bar, 'Foo' );
1f3074ea 160
f16b5740 161 is( $bar->number, 10, '... got the right number' );
162 is( $bar->string, 'foo', '... got the right string' );
163 is( $bar->float, 10.5, '... got the right float' );
164 is_deeply( $bar->array, [ 1 .. 10 ], '... got the right array' );
1f3074ea 165 is_deeply(
f16b5740 166 $bar->hash,
1f3074ea 167 { map { $_ => undef } ( 1 .. 10 ) },
168 '... got the right hash'
169 );
170
f16b5740 171 isa_ok( $bar->object, 'Foo' );
172 is( $bar->object->number, 2,
1f3074ea 173 '... got the right number (in the embedded object)' );
174}
175