Remove Test::TempDir
[gitmo/MooseX-Storage.git] / t / 061_basic_deferred_w_io.t
CommitLineData
bf33d7c7 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
cfee09ad 6use Test::More;
08d0f48e 7use File::Temp qw(tempdir);
5e5d4e28 8use File::Spec::Functions;
cfd008fa 9
10my $dir = tempdir;
bf33d7c7 11
0b173188 12use Test::Requires {
13 'IO::AtomicFile' => 0.01, # skip all if not installed
14 'JSON::Any' => 0.01,
15};
16
bf33d7c7 17BEGIN {
5e5d4e28 18 plan tests => 20;
bf33d7c7 19 use_ok('MooseX::Storage');
20}
21
22{
23 package Foo;
24 use Moose;
25 use MooseX::Storage;
b5f363ac 26
bf33d7c7 27 with 'MooseX::Storage::Deferred';
b5f363ac 28
bf33d7c7 29 has 'number' => (is => 'ro', isa => 'Int');
30 has 'string' => (is => 'ro', isa => 'Str');
b5f363ac 31 has 'float' => (is => 'ro', isa => 'Num');
bf33d7c7 32 has 'array' => (is => 'ro', isa => 'ArrayRef');
b5f363ac 33 has 'hash' => (is => 'ro', isa => 'HashRef');
34 has 'object' => (is => 'ro', isa => 'Object');
bf33d7c7 35}
36
5e5d4e28 37my $file = catfile($dir, 'temp.json');
bf33d7c7 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
67unlink $file;
68ok(!(-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
bf33d7c7 98