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