bdf1e7d1f4ce505784c0950f438e16d2b50e349d
[gitmo/MooseX-Storage.git] / t / 050_basic_storable.t
1 #!/usr/bin/perl
2 $|++;
3 use strict;
4 use warnings;
5
6 use Test::More tests => 11;
7 use Test::Deep;
8 use Storable;
9
10 BEGIN {
11     use_ok('MooseX::Storage');
12 }
13
14 {
15
16     package Foo;
17     use Moose;
18     use MooseX::Storage;
19
20     with Storage( 'format' => 'Storable' );
21
22     has 'number' => ( is => 'ro', isa => 'Int' );
23     has 'string' => ( is => 'ro', isa => 'Str' );
24     has 'float'  => ( is => 'ro', isa => 'Num' );
25     has 'array'  => ( is => 'ro', isa => 'ArrayRef' );
26     has 'hash'   => ( is => 'ro', isa => 'HashRef' );
27     has 'object' => ( is => 'ro', isa => 'Object' );
28 }
29
30 {
31     my $foo = Foo->new(
32         number => 10,
33         string => 'foo',
34         float  => 10.5,
35         array  => [ 1 .. 10 ],
36         hash   => { map { $_ => undef } ( 1 .. 10 ) },
37         object => Foo->new( number => 2 ),
38     );
39     isa_ok( $foo, 'Foo' );
40     
41     my $stored = $foo->freeze;
42
43     my $struct = Storable::thaw($stored);
44     cmp_deeply(
45         $struct,
46         {
47             '__CLASS__' => 'Foo',
48             'float'     => 10.5,
49             'number'    => 10,
50             'string'    => 'foo',           
51             'array'     => [ 1 .. 10],
52             'hash'      => { map { $_ => undef } 1 .. 10 },            
53             'object'    => {
54                 '__CLASS__' => 'Foo',
55                 'number' => 2
56             },
57         },
58         '... got the data struct we expected'
59     );
60 }
61
62 {
63     my $stored = Storable::nfreeze({
64         '__CLASS__' => 'Foo',
65         'float'     => 10.5,
66         'number'    => 10,
67         'string'    => 'foo',           
68         'array'     => [ 1 .. 10],
69         'hash'      => { map { $_ => undef } 1 .. 10 },            
70         'object'    => {
71             '__CLASS__' => 'Foo',
72             'number' => 2
73         },
74     });
75     
76     my $foo = Foo->thaw($stored);
77     isa_ok( $foo, 'Foo' );
78
79     is( $foo->number, 10,    '... got the right number' );
80     is( $foo->string, 'foo', '... got the right string' );
81     is( $foo->float,  10.5,  '... got the right float' );
82     cmp_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
83     cmp_deeply(
84         $foo->hash,
85         { map { $_ => undef } ( 1 .. 10 ) },
86         '... got the right hash'
87     );
88
89     isa_ok( $foo->object, 'Foo' );
90     is( $foo->object->number, 2,
91         '... got the right number (in the embedded object)' );
92 }
93