feb01757d24203484ece192aa5f9fd3fd53c6b58
[gitmo/MooseX-Storage.git] / t / 102_io_storable_file.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 10;
5 use Test::Deep;
6 use File::Temp qw(tempdir);
7 use File::Spec::Functions;
8 my $dir = tempdir( CLEANUP => 1 );
9
10 BEGIN {
11     use_ok('MooseX::Storage');
12 }
13
14 {
15     package Foo;
16     use Moose;
17     use MooseX::Storage;
18
19     with Storage(io => 'StorableFile');
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 my $file = catfile($dir,'temp.storable');
30
31 {
32     my $foo = Foo->new(
33         number => 10,
34         string => 'foo',
35         float  => 10.5,
36         array  => [ 1 .. 10 ],
37         hash   => { map { $_ => undef } (1 .. 10) },
38         object => Foo->new( number => 2 ),
39     );
40     isa_ok($foo, 'Foo');
41
42     $foo->store($file);
43 }
44
45 {
46     my $foo = Foo->load($file);
47     isa_ok($foo, 'Foo');
48
49     is($foo->number, 10, '... got the right number');
50     is($foo->string, 'foo', '... got the right string');
51     is($foo->float, 10.5, '... got the right float');
52     cmp_deeply($foo->array, [ 1 .. 10], '... got the right array');
53     cmp_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
54
55     isa_ok($foo->object, 'Foo');
56     is($foo->object->number, 2, '... got the right number (in the embedded object)');
57 }
58