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