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