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