6 use File::Temp qw(tempdir);
7 use File::Spec::Functions;
12 'IO::AtomicFile' => 0.01, # skip all if not installed
18 use_ok('MooseX::Storage');
26 with 'MooseX::Storage::Deferred';
28 has 'number' => (is => 'ro', isa => 'Int');
29 has 'string' => (is => 'ro', isa => 'Str');
30 has 'float' => (is => 'ro', isa => 'Num');
31 has 'array' => (is => 'ro', isa => 'ArrayRef');
32 has 'hash' => (is => 'ro', isa => 'HashRef');
33 has 'object' => (is => 'ro', isa => 'Object');
36 my $file = catfile($dir, 'temp.json');
44 hash => { map { $_ => undef } (1 .. 10) },
45 object => Foo->new( number => 2 ),
49 $foo->store($file, { format => 'JSON', io => 'File' });
53 my $foo = Foo->load($file, { format => 'JSON', io => 'File' });
56 is($foo->number, 10, '... got the right number');
57 is($foo->string, 'foo', '... got the right string');
58 is($foo->float, 10.5, '... got the right float');
59 cmp_deeply($foo->array, [ 1 .. 10], '... got the right array');
60 cmp_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
62 isa_ok($foo->object, 'Foo');
63 is($foo->object->number, 2, '... got the right number (in the embedded object)');
67 ok(!(-e $file), '... the file has been deleted');
75 hash => { map { $_ => undef } (1 .. 10) },
76 object => Foo->new( number => 2 ),
80 $foo->store($file, { format => 'JSON', io => 'AtomicFile' });
84 my $foo = Foo->load($file, { format => 'JSON', io => 'AtomicFile' });
87 is($foo->number, 10, '... got the right number');
88 is($foo->string, 'foo', '... got the right string');
89 is($foo->float, 10.5, '... got the right float');
90 cmp_deeply($foo->array, [ 1 .. 10], '... got the right array');
91 cmp_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
93 isa_ok($foo->object, 'Foo');
94 is($foo->object->number, 2, '... got the right number (in the embedded object)');