this is a shorter way of specifying the gitmo repository properly
[gitmo/MooseX-Storage.git] / t / 101_io_atomic.t
CommitLineData
e64b7302 1use strict;
2use warnings;
3
8d8356bb 4use Test::More;
619ab942 5use Test::Deep;
5e5d4e28 6use File::Temp qw(tempdir);
7use File::Spec::Functions;
8my $dir = tempdir( CLEANUP => 1 );
e64b7302 9
0b173188 10use Test::Requires {
11 'JSON::Any' => 0.01, # skip all if not installed
12 'IO::AtomicFile' => 0.01,
13};
14
e64b7302 15BEGIN {
8d8356bb 16 plan tests => 10;
e64b7302 17 use_ok('MooseX::Storage');
18}
19
20{
21 package Foo;
22 use Moose;
23 use MooseX::Storage;
c2dae5d8 24
e64b7302 25 with Storage(format => 'JSON', io => 'AtomicFile');
c2dae5d8 26
e64b7302 27 has 'number' => (is => 'ro', isa => 'Int');
28 has 'string' => (is => 'ro', isa => 'Str');
c2dae5d8 29 has 'float' => (is => 'ro', isa => 'Num');
e64b7302 30 has 'array' => (is => 'ro', isa => 'ArrayRef');
c2dae5d8 31 has 'hash' => (is => 'ro', isa => 'HashRef');
8b2ba857 32 has 'object' => (is => 'ro', isa => 'Object');
e64b7302 33}
34
5e5d4e28 35my $file = catfile($dir,'temp.json');
e64b7302 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) },
8b2ba857 44 object => Foo->new( number => 2 ),
e64b7302 45 );
46 isa_ok($foo, 'Foo');
47
48 $foo->store($file);
49}
50
51{
52 my $foo = Foo->load($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');
619ab942 58 cmp_deeply($foo->array, [ 1 .. 10], '... got the right array');
59 cmp_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
e64b7302 60
61 isa_ok($foo->object, 'Foo');
62 is($foo->object->number, 2, '... got the right number (in the embedded object)');
63}
64