Initial patch from Bruno for implementing a ::Storage::Format::XML
[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 File::Temp qw(tempdir);
8 use File::Spec::Functions;
9 my $dir = tempdir( CLEANUP => 1 );
10
11 BEGIN {
12     use_ok('MooseX::Storage');
13 }
14
15 {
16     package Foo;
17     use Moose;
18     use MooseX::Storage;
19     
20     with Storage(io => 'StorableFile');
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 my $file = catfile($dir,'temp.storable');
31
32 {
33     my $foo = Foo->new(
34         number => 10,
35         string => 'foo',
36         float  => 10.5,
37         array  => [ 1 .. 10 ],
38         hash   => { map { $_ => undef } (1 .. 10) },
39         object => Foo->new( number => 2 ),
40     );
41     isa_ok($foo, 'Foo');
42
43     $foo->store($file);
44 }
45
46 {
47     my $foo = Foo->load($file);
48     isa_ok($foo, 'Foo');
49
50     is($foo->number, 10, '... got the right number');
51     is($foo->string, 'foo', '... got the right string');
52     is($foo->float, 10.5, '... got the right float');
53     is_deeply($foo->array, [ 1 .. 10], '... got the right array');
54     is_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
55
56     isa_ok($foo->object, 'Foo');
57     is($foo->object->number, 2, '... got the right number (in the embedded object)');
58 }
59