use tempfiles in the test suite
[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;
7 use File::Temp qw(tempdir);
8 use File::Spec::Functions;
9 my $dir = tempdir( CLEANUP => 1 );
10
11 BEGIN {
12     eval "use IO::AtomicFile";
13     plan skip_all => "IO::AtomicFile is required for this test" if $@;   
14     eval "use JSON::Any";
15     plan skip_all => "JSON::Any is required for this test" if $@;         
16     plan tests => 20;
17     use_ok('MooseX::Storage');
18 }
19
20 {
21     package Foo;
22     use Moose;
23     use MooseX::Storage;
24     
25     with 'MooseX::Storage::Deferred';
26     
27     has 'number' => (is => 'ro', isa => 'Int');
28     has 'string' => (is => 'ro', isa => 'Str');
29     has 'float'  => (is => 'ro', isa => 'Num');        
30     has 'array'  => (is => 'ro', isa => 'ArrayRef');
31     has 'hash'   => (is => 'ro', isa => 'HashRef');    
32         has 'object' => (is => 'ro', isa => 'Object');    
33 }
34
35 my $file = catfile($dir, 'temp.json');
36
37 {
38     my $foo = Foo->new(
39         number => 10,
40         string => 'foo',
41         float  => 10.5,
42         array  => [ 1 .. 10 ],
43         hash   => { map { $_ => undef } (1 .. 10) },
44         object => Foo->new( number => 2 ),
45     );
46     isa_ok($foo, 'Foo');
47
48     $foo->store($file, { format => 'JSON', io => 'File' });
49 }
50
51 {
52     my $foo = Foo->load($file, { format => 'JSON', io => 'File' });
53     isa_ok($foo, 'Foo');
54
55     is($foo->number, 10, '... got the right number');
56     is($foo->string, 'foo', '... got the right string');
57     is($foo->float, 10.5, '... got the right float');
58     is_deeply($foo->array, [ 1 .. 10], '... got the right array');
59     is_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
60
61     isa_ok($foo->object, 'Foo');
62     is($foo->object->number, 2, '... got the right number (in the embedded object)');
63 }
64
65 unlink $file;
66 ok(!(-e $file), '... the file has been deleted');
67
68 {
69     my $foo = Foo->new(
70         number => 10,
71         string => 'foo',
72         float  => 10.5,
73         array  => [ 1 .. 10 ],
74         hash   => { map { $_ => undef } (1 .. 10) },
75         object => Foo->new( number => 2 ),
76     );
77     isa_ok($foo, 'Foo');
78
79     $foo->store($file, { format => 'JSON', io => 'AtomicFile' });
80 }
81
82 {
83     my $foo = Foo->load($file, { format => 'JSON', io => 'AtomicFile' });
84     isa_ok($foo, 'Foo');
85
86     is($foo->number, 10, '... got the right number');
87     is($foo->string, 'foo', '... got the right string');
88     is($foo->float, 10.5, '... got the right float');
89     is_deeply($foo->array, [ 1 .. 10], '... got the right array');
90     is_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
91
92     isa_ok($foo->object, 'Foo');
93     is($foo->object->number, 2, '... got the right number (in the embedded object)');
94 }
95
96