use Test::Requires in tests
[gitmo/MooseX-Storage.git] / t / 060_basic_deferred.t
1 #!/usr/bin/perl
2
3 $|++;
4 use strict;
5 use warnings;
6
7 use Test::More;
8 use Storable;
9
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
16 BEGIN {
17     plan tests => 31;
18     use_ok('MooseX::Storage');
19 }
20
21 diag('Using implementation: ', YAML::Any->implementation);
22
23 {
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"}',
63         { 'format' => 'JSON' } 
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' );
92     
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,
102             'string'    => 'foo',           
103             'array'     => [ 1 .. 10],
104             'hash'      => { map { $_ => undef } 1 .. 10 },            
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,
119         'string'    => 'foo',           
120         'array'     => [ 1 .. 10],
121         'hash'      => { map { $_ => undef } 1 .. 10 },            
122         'object'    => {
123             '__CLASS__' => 'Foo',
124             'number' => 2
125         },
126     });
127     
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
159     my $bar = Foo->thaw( $yaml, { 'format' => 'YAML' } );
160     isa_ok( $bar, 'Foo' );
161
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' );
166     is_deeply(
167         $bar->hash,
168         { map { $_ => undef } ( 1 .. 10 ) },
169         '... got the right hash'
170     );
171
172     isa_ok( $bar->object, 'Foo' );
173     is( $bar->object->number, 2,
174         '... got the right number (in the embedded object)' );
175 }
176