Commit | Line | Data |
1f3074ea |
1 | #!/usr/bin/perl |
4fa64e86 |
2 | |
1f3074ea |
3 | $|++; |
4 | use strict; |
5 | use warnings; |
6 | |
cfee09ad |
7 | use Test::More; |
1f3074ea |
8 | use Storable; |
1f3074ea |
9 | |
10 | BEGIN { |
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 | |
f5f21f1d |
21 | diag('Using implementation: ', YAML::Any->implementation); |
22 | |
1f3074ea |
23 | { |
24 | |
25 | package Foo; |
26 | use Moose; |
27 | use MooseX::Storage; |
28 | |
29 | with 'MooseX::Storage::Deferred'; |
30 | |
31 | has 'number' => ( is => 'ro', isa => 'Int' ); |
32 | has 'string' => ( is => 'ro', isa => 'Str' ); |
33 | has 'float' => ( is => 'ro', isa => 'Num' ); |
34 | has 'array' => ( is => 'ro', isa => 'ArrayRef' ); |
35 | has 'hash' => ( is => 'ro', isa => 'HashRef' ); |
36 | has 'object' => ( is => 'ro', isa => 'Object' ); |
37 | } |
38 | |
39 | { |
40 | my $foo = Foo->new( |
41 | number => 10, |
42 | string => 'foo', |
43 | float => 10.5, |
44 | array => [ 1 .. 10 ], |
45 | hash => { map { $_ => undef } ( 1 .. 10 ) }, |
46 | object => Foo->new( number => 2 ), |
47 | ); |
48 | isa_ok( $foo, 'Foo' ); |
49 | |
50 | my $json = $foo->freeze({ 'format' => 'JSON' }); |
51 | |
52 | is_valid_json($json, '.. this is valid JSON'); |
53 | |
54 | is_json( |
55 | $json, |
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 | '... got the right JSON' |
58 | ); |
59 | } |
60 | |
61 | { |
62 | my $foo = Foo->thaw( |
63 | '{"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 |
64 | { 'format' => 'JSON' } |
1f3074ea |
65 | ); |
66 | isa_ok( $foo, 'Foo' ); |
67 | |
68 | is( $foo->number, 10, '... got the right number' ); |
69 | is( $foo->string, 'foo', '... got the right string' ); |
70 | is( $foo->float, 10.5, '... got the right float' ); |
71 | is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' ); |
72 | is_deeply( |
73 | $foo->hash, |
74 | { map { $_ => undef } ( 1 .. 10 ) }, |
75 | '... got the right hash' |
76 | ); |
77 | |
78 | isa_ok( $foo->object, 'Foo' ); |
79 | is( $foo->object->number, 2, |
80 | '... got the right number (in the embedded object)' ); |
81 | } |
82 | |
83 | { |
84 | my $foo = Foo->new( |
85 | number => 10, |
86 | string => 'foo', |
87 | float => 10.5, |
88 | array => [ 1 .. 10 ], |
89 | hash => { map { $_ => undef } ( 1 .. 10 ) }, |
90 | object => Foo->new( number => 2 ), |
91 | ); |
92 | isa_ok( $foo, 'Foo' ); |
b5f363ac |
93 | |
1f3074ea |
94 | my $stored = $foo->freeze({ 'format' => 'Storable' }); |
95 | |
96 | my $struct = Storable::thaw($stored); |
97 | is_deeply( |
98 | $struct, |
99 | { |
100 | '__CLASS__' => 'Foo', |
101 | 'float' => 10.5, |
102 | 'number' => 10, |
b5f363ac |
103 | 'string' => 'foo', |
1f3074ea |
104 | 'array' => [ 1 .. 10], |
b5f363ac |
105 | 'hash' => { map { $_ => undef } 1 .. 10 }, |
1f3074ea |
106 | 'object' => { |
107 | '__CLASS__' => 'Foo', |
108 | 'number' => 2 |
109 | }, |
110 | }, |
111 | '... got the data struct we expected' |
112 | ); |
113 | } |
114 | |
115 | { |
116 | my $stored = Storable::nfreeze({ |
117 | '__CLASS__' => 'Foo', |
118 | 'float' => 10.5, |
119 | 'number' => 10, |
b5f363ac |
120 | 'string' => 'foo', |
1f3074ea |
121 | 'array' => [ 1 .. 10], |
b5f363ac |
122 | 'hash' => { map { $_ => undef } 1 .. 10 }, |
1f3074ea |
123 | 'object' => { |
124 | '__CLASS__' => 'Foo', |
125 | 'number' => 2 |
126 | }, |
127 | }); |
b5f363ac |
128 | |
1f3074ea |
129 | my $foo = Foo->thaw($stored, { 'format' => 'Storable' }); |
130 | isa_ok( $foo, 'Foo' ); |
131 | |
132 | is( $foo->number, 10, '... got the right number' ); |
133 | is( $foo->string, 'foo', '... got the right string' ); |
134 | is( $foo->float, 10.5, '... got the right float' ); |
135 | is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' ); |
136 | is_deeply( |
137 | $foo->hash, |
138 | { map { $_ => undef } ( 1 .. 10 ) }, |
139 | '... got the right hash' |
140 | ); |
141 | |
142 | isa_ok( $foo->object, 'Foo' ); |
143 | is( $foo->object->number, 2, |
144 | '... got the right number (in the embedded object)' ); |
145 | } |
146 | |
147 | { |
148 | my $foo = Foo->new( |
149 | number => 10, |
150 | string => 'foo', |
151 | float => 10.5, |
152 | array => [ 1 .. 10 ], |
153 | hash => { map { $_ => undef } ( 1 .. 10 ) }, |
154 | object => Foo->new( number => 2 ), |
155 | ); |
156 | isa_ok( $foo, 'Foo' ); |
157 | |
158 | my $yaml = $foo->freeze({ 'format' => 'YAML' }); |
159 | |
f16b5740 |
160 | my $bar = Foo->thaw( $yaml, { 'format' => 'YAML' } ); |
161 | isa_ok( $bar, 'Foo' ); |
1f3074ea |
162 | |
f16b5740 |
163 | is( $bar->number, 10, '... got the right number' ); |
164 | is( $bar->string, 'foo', '... got the right string' ); |
165 | is( $bar->float, 10.5, '... got the right float' ); |
166 | is_deeply( $bar->array, [ 1 .. 10 ], '... got the right array' ); |
1f3074ea |
167 | is_deeply( |
f16b5740 |
168 | $bar->hash, |
1f3074ea |
169 | { map { $_ => undef } ( 1 .. 10 ) }, |
170 | '... got the right hash' |
171 | ); |
172 | |
f16b5740 |
173 | isa_ok( $bar->object, 'Foo' ); |
174 | is( $bar->object->number, 2, |
1f3074ea |
175 | '... got the right number (in the embedded object)' ); |
176 | } |
177 | |