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