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