Commit | Line | Data |
124c2ba5 |
1 | #!/usr/bin/perl |
2 | $|++; |
3 | use strict; |
4 | use warnings; |
5 | |
6 | use Test::More tests => 11; |
619ab942 |
7 | use Test::Deep; |
124c2ba5 |
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' ); |
b5f363ac |
40 | |
124c2ba5 |
41 | my $stored = $foo->freeze; |
42 | |
43 | my $struct = Storable::thaw($stored); |
619ab942 |
44 | cmp_deeply( |
124c2ba5 |
45 | $struct, |
46 | { |
47 | '__CLASS__' => 'Foo', |
48 | 'float' => 10.5, |
49 | 'number' => 10, |
b5f363ac |
50 | 'string' => 'foo', |
124c2ba5 |
51 | 'array' => [ 1 .. 10], |
b5f363ac |
52 | 'hash' => { map { $_ => undef } 1 .. 10 }, |
124c2ba5 |
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, |
b5f363ac |
67 | 'string' => 'foo', |
124c2ba5 |
68 | 'array' => [ 1 .. 10], |
b5f363ac |
69 | 'hash' => { map { $_ => undef } 1 .. 10 }, |
124c2ba5 |
70 | 'object' => { |
71 | '__CLASS__' => 'Foo', |
72 | 'number' => 2 |
73 | }, |
74 | }); |
b5f363ac |
75 | |
124c2ba5 |
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' ); |
619ab942 |
82 | cmp_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' ); |
83 | cmp_deeply( |
124c2ba5 |
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 | |