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 { |
cfee09ad |
11 | eval "use Test::JSON; use Test::YAML::Valid;"; |
4fa64e86 |
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 $@; |
cfee09ad |
15 | plan tests => 33; |
1f3074ea |
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 | is( |
159 | $yaml, |
160 | q{--- |
161 | __CLASS__: Foo |
162 | array: |
163 | - 1 |
164 | - 2 |
165 | - 3 |
166 | - 4 |
167 | - 5 |
168 | - 6 |
169 | - 7 |
170 | - 8 |
171 | - 9 |
172 | - 10 |
173 | float: 10.5 |
174 | hash: |
175 | 1: ~ |
176 | 10: ~ |
177 | 2: ~ |
178 | 3: ~ |
179 | 4: ~ |
180 | 5: ~ |
181 | 6: ~ |
182 | 7: ~ |
183 | 8: ~ |
184 | 9: ~ |
185 | number: 10 |
186 | object: |
187 | __CLASS__: Foo |
188 | number: 2 |
189 | string: foo |
190 | }, |
191 | '... got the same YAML' |
192 | ); |
193 | |
194 | } |
195 | |
196 | { |
197 | my $foo = Foo->thaw( |
198 | q{--- |
199 | __CLASS__: Foo |
200 | array: |
201 | - 1 |
202 | - 2 |
203 | - 3 |
204 | - 4 |
205 | - 5 |
206 | - 6 |
207 | - 7 |
208 | - 8 |
209 | - 9 |
210 | - 10 |
211 | float: 10.5 |
212 | hash: |
213 | 1: ~ |
214 | 10: ~ |
215 | 2: ~ |
216 | 3: ~ |
217 | 4: ~ |
218 | 5: ~ |
219 | 6: ~ |
220 | 7: ~ |
221 | 8: ~ |
222 | 9: ~ |
223 | number: 10 |
224 | object: |
225 | __CLASS__: Foo |
226 | number: 2 |
227 | string: foo |
228 | }, { 'format' => 'YAML' } |
229 | ); |
230 | isa_ok( $foo, 'Foo' ); |
231 | |
232 | is( $foo->number, 10, '... got the right number' ); |
233 | is( $foo->string, 'foo', '... got the right string' ); |
234 | is( $foo->float, 10.5, '... got the right float' ); |
235 | is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' ); |
236 | is_deeply( |
237 | $foo->hash, |
238 | { map { $_ => undef } ( 1 .. 10 ) }, |
239 | '... got the right hash' |
240 | ); |
241 | |
242 | isa_ok( $foo->object, 'Foo' ); |
243 | is( $foo->object->number, 2, |
244 | '... got the right number (in the embedded object)' ); |
245 | } |
246 | |