foo
[gitmo/MooseX-Storage.git] / t / 101_io_atomic.t
CommitLineData
e64b7302 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
6use Test::More no_plan => 1;
7
8BEGIN {
9 use_ok('MooseX::Storage');
10}
11
12{
13 package Foo;
14 use Moose;
15 use MooseX::Storage;
16
17 with Storage(format => 'JSON', io => 'AtomicFile');
18
19 has 'number' => (is => 'ro', isa => 'Int');
20 has 'string' => (is => 'ro', isa => 'Str');
21 has 'float' => (is => 'ro', isa => 'Num');
22 has 'array' => (is => 'ro', isa => 'ArrayRef');
23 has 'hash' => (is => 'ro', isa => 'HashRef');
24 has 'object' => (is => 'ro', isa => 'Object');
25}
26
27my $file = 'temp.json';
28
29{
30 my $foo = Foo->new(
31 number => 10,
32 string => 'foo',
33 float => 10.5,
34 array => [ 1 .. 10 ],
35 hash => { map { $_ => undef } (1 .. 10) },
36 object => Foo->new( number => 2 ),
37 );
38 isa_ok($foo, 'Foo');
39
40 $foo->store($file);
41}
42
43{
44 my $foo = Foo->load($file);
45 isa_ok($foo, 'Foo');
46
47 is($foo->number, 10, '... got the right number');
48 is($foo->string, 'foo', '... got the right string');
49 is($foo->float, 10.5, '... got the right float');
50 is_deeply($foo->array, [ 1 .. 10], '... got the right array');
51 is_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
52
53 isa_ok($foo->object, 'Foo');
54 is($foo->object->number, 2, '... got the right number (in the embedded object)');
55}
56
57unlink $file;