9c91a885fd587db81635b4c7f75a4a7727d8cd93
[gitmo/MooseX-Storage.git] / t / 100_io.t
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Test::Deep;
6 use File::Temp qw(tempdir);
7 use File::Spec::Functions;
8 my $dir = tempdir;
9
10 use Test::Requires {
11     'JSON::Any' => 0.01, # skip all if not installed
12 };
13
14 BEGIN {
15     plan tests => 10;
16     use_ok('MooseX::Storage');
17 }
18
19 {
20     package Foo;
21     use Moose;
22     use MooseX::Storage;
23
24     with Storage(format => 'JSON', io => 'File');
25
26     has 'number' => (is => 'ro', isa => 'Int');
27     has 'string' => (is => 'ro', isa => 'Str');
28     has 'float'  => (is => 'ro', isa => 'Num');
29     has 'array'  => (is => 'ro', isa => 'ArrayRef');
30     has 'hash'   => (is => 'ro', isa => 'HashRef');
31         has 'object' => (is => 'ro', isa => 'Object');
32 }
33
34 my $file = catfile( $dir, 'temp.json' );
35
36 {
37     my $foo = Foo->new(
38         number => 10,
39         string => 'foo',
40         float  => 10.5,
41         array  => [ 1 .. 10 ],
42         hash   => { map { $_ => undef } (1 .. 10) },
43         object => Foo->new( number => 2 ),
44     );
45     isa_ok($foo, 'Foo');
46
47     $foo->store($file);
48 }
49
50 {
51     my $foo = Foo->load($file);
52     isa_ok($foo, 'Foo');
53
54     is($foo->number, 10, '... got the right number');
55     is($foo->string, 'foo', '... got the right string');
56     is($foo->float, 10.5, '... got the right float');
57     cmp_deeply($foo->array, [ 1 .. 10], '... got the right array');
58     cmp_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
59
60     isa_ok($foo->object, 'Foo');
61     is($foo->object->number, 2, '... got the right number (in the embedded object)');
62 }
63