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