Merge branch 'master' of jules.scsys.co.uk:MooseX-Storage
[gitmo/MooseX-Storage.git] / t / 002_basic_io.t
CommitLineData
7aac8ce9 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
4fa64e86 6use Test::More;
cfd008fa 7use Test::TempDir;
8
5e5d4e28 9use File::Spec::Functions;
cfd008fa 10
11my $dir = tempdir;
4fa64e86 12
766ab81f 13BEGIN {
4fa64e86 14 eval "use JSON::Any";
766ab81f 15 plan skip_all => "JSON::Any is required for this test" if $@;
4fa64e86 16 plan tests => 10;
17 use_ok('MooseX::Storage');
18}
7aac8ce9 19
20{
21 package Foo;
22 use Moose;
23 use MooseX::Storage;
766ab81f 24
7aac8ce9 25 with Storage(
26 format => 'JSON',
27 io => 'File',
28 );
766ab81f 29
7aac8ce9 30 has 'number' => (is => 'ro', isa => 'Int');
31 has 'string' => (is => 'ro', isa => 'Str');
766ab81f 32 has 'float' => (is => 'ro', isa => 'Num');
7aac8ce9 33 has 'array' => (is => 'ro', isa => 'ArrayRef');
766ab81f 34 has 'hash' => (is => 'ro', isa => 'HashRef');
35 has 'object' => (is => 'ro', isa => 'Object');
7aac8ce9 36}
37
5e5d4e28 38my $file = catfile($dir, 'temp.json');
7aac8ce9 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