Deferred is done
[gitmo/MooseX-Storage.git] / t / 061_basic_deferred_w_io.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 21;
7
8 BEGIN {
9     use_ok('MooseX::Storage');
10 }
11
12 {
13     package Foo;
14     use Moose;
15     use MooseX::Storage;
16     
17     with 'MooseX::Storage::Deferred';
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.json';
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, { format => 'JSON', io => 'File' });
41 }
42
43 {
44     my $foo = Foo->load($file, { format => 'JSON', io => '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;
58 ok(!(-e $file), '... the file has been deleted');
59
60 {
61     my $foo = Foo->new(
62         number => 10,
63         string => 'foo',
64         float  => 10.5,
65         array  => [ 1 .. 10 ],
66         hash   => { map { $_ => undef } (1 .. 10) },
67         object => Foo->new( number => 2 ),
68     );
69     isa_ok($foo, 'Foo');
70
71     $foo->store($file, { format => 'JSON', io => 'AtomicFile' });
72 }
73
74 {
75     my $foo = Foo->load($file, { format => 'JSON', io => 'AtomicFile' });
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($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
83
84     isa_ok($foo->object, 'Foo');
85     is($foo->object->number, 2, '... got the right number (in the embedded object)');
86 }
87
88 unlink $file;
89 ok(!(-e $file), '... the file has been deleted');
90