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