e0a90a0bbd149c0848b215894c4feb6195bb58ba
[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
8 BEGIN {
9     use_ok('MooseX::Storage');
10 }
11
12 {
13     package Foo;
14     use Moose;
15     use MooseX::Storage;
16     
17     with Storage(io => 'StorableFile');
18     
19     has 'number' => (is => 'ro', isa => 'Int');
20     has 'string' => (is => 'ro', isa => 'Str');
21     has 'float'  => (is => 'ro', isa => 'Num');        
22     has 'array'  => (is => 'ro', isa => 'ArrayRef');
23     has 'hash'   => (is => 'ro', isa => 'HashRef');    
24         has 'object' => (is => 'ro', isa => 'Object');    
25 }
26
27 my $file = 'temp.storable';
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     $foo->store($file);
41 }
42
43 {
44     my $foo = Foo->load($file);
45     isa_ok($foo, 'Foo');
46
47     is($foo->number, 10, '... got the right number');
48     is($foo->string, 'foo', '... got the right string');
49     is($foo->float, 10.5, '... got the right float');
50     is_deeply($foo->array, [ 1 .. 10], '... got the right array');
51     is_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
52
53     isa_ok($foo->object, 'Foo');
54     is($foo->object->number, 2, '... got the right number (in the embedded object)');
55 }
56
57 unlink $file;