c44e43a9a59a1b792fa7325a95bf16af82439767
[gitmo/MooseX-Storage.git] / t / 102_io_storable_file.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 10;
7 use Test::Deep;
8 use File::Temp qw(tempdir);
9 use File::Spec::Functions;
10 my $dir = tempdir( CLEANUP => 1 );
11
12 BEGIN {
13     use_ok('MooseX::Storage');
14 }
15
16 {
17     package Foo;
18     use Moose;
19     use MooseX::Storage;
20     
21     with Storage(io => 'StorableFile');
22     
23     has 'number' => (is => 'ro', isa => 'Int');
24     has 'string' => (is => 'ro', isa => 'Str');
25     has 'float'  => (is => 'ro', isa => 'Num');        
26     has 'array'  => (is => 'ro', isa => 'ArrayRef');
27     has 'hash'   => (is => 'ro', isa => 'HashRef');    
28         has 'object' => (is => 'ro', isa => 'Object');    
29 }
30
31 my $file = catfile($dir,'temp.storable');
32
33 {
34     my $foo = Foo->new(
35         number => 10,
36         string => 'foo',
37         float  => 10.5,
38         array  => [ 1 .. 10 ],
39         hash   => { map { $_ => undef } (1 .. 10) },
40         object => Foo->new( number => 2 ),
41     );
42     isa_ok($foo, 'Foo');
43
44     $foo->store($file);
45 }
46
47 {
48     my $foo = Foo->load($file);
49     isa_ok($foo, 'Foo');
50
51     is($foo->number, 10, '... got the right number');
52     is($foo->string, 'foo', '... got the right string');
53     is($foo->float, 10.5, '... got the right float');
54     cmp_deeply($foo->array, [ 1 .. 10], '... got the right array');
55     cmp_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
56
57     isa_ok($foo->object, 'Foo');
58     is($foo->object->number, 2, '... got the right number (in the embedded object)');
59 }
60