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