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