11 eval "use Test::JSON";
12 plan skip_all => "Test::JSON is required for this test" if $@;
14 plan skip_all => "JSON::Any is required for this test" if $@;
16 plan skip_all => "YAML::Any is required for this test" if $@;
18 use_ok('MooseX::Storage');
21 diag('Using implementation: ', YAML::Any->implementation);
29 with 'MooseX::Storage::Deferred';
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' );
45 hash => { map { $_ => undef } ( 1 .. 10 ) },
46 object => Foo->new( number => 2 ),
48 isa_ok( $foo, 'Foo' );
50 my $json = $foo->freeze({ 'format' => 'JSON' });
52 is_valid_json($json, '.. this is valid 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'
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"}',
64 { 'format' => 'JSON' }
66 isa_ok( $foo, 'Foo' );
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' );
74 { map { $_ => undef } ( 1 .. 10 ) },
75 '... got the right hash'
78 isa_ok( $foo->object, 'Foo' );
79 is( $foo->object->number, 2,
80 '... got the right number (in the embedded object)' );
89 hash => { map { $_ => undef } ( 1 .. 10 ) },
90 object => Foo->new( number => 2 ),
92 isa_ok( $foo, 'Foo' );
94 my $stored = $foo->freeze({ 'format' => 'Storable' });
96 my $struct = Storable::thaw($stored);
100 '__CLASS__' => 'Foo',
104 'array' => [ 1 .. 10],
105 'hash' => { map { $_ => undef } 1 .. 10 },
107 '__CLASS__' => 'Foo',
111 '... got the data struct we expected'
116 my $stored = Storable::nfreeze({
117 '__CLASS__' => 'Foo',
121 'array' => [ 1 .. 10],
122 'hash' => { map { $_ => undef } 1 .. 10 },
124 '__CLASS__' => 'Foo',
129 my $foo = Foo->thaw($stored, { 'format' => 'Storable' });
130 isa_ok( $foo, 'Foo' );
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' );
138 { map { $_ => undef } ( 1 .. 10 ) },
139 '... got the right hash'
142 isa_ok( $foo->object, 'Foo' );
143 is( $foo->object->number, 2,
144 '... got the right number (in the embedded object)' );
152 array => [ 1 .. 10 ],
153 hash => { map { $_ => undef } ( 1 .. 10 ) },
154 object => Foo->new( number => 2 ),
156 isa_ok( $foo, 'Foo' );
158 my $yaml = $foo->freeze({ 'format' => 'YAML' });
160 my $bar = Foo->thaw( $yaml, { 'format' => 'YAML' } );
161 isa_ok( $bar, 'Foo' );
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' );
169 { map { $_ => undef } ( 1 .. 10 ) },
170 '... got the right hash'
173 isa_ok( $bar->object, 'Foo' );
174 is( $bar->object->number, 2,
175 '... got the right number (in the embedded object)' );