Commit | Line | Data |
bf33d7c7 |
1 | use strict; |
2 | use warnings; |
3 | |
cfee09ad |
4 | use Test::More; |
619ab942 |
5 | use Test::Deep; |
08d0f48e |
6 | use File::Temp qw(tempdir); |
5e5d4e28 |
7 | use File::Spec::Functions; |
cfd008fa |
8 | |
9 | my $dir = tempdir; |
bf33d7c7 |
10 | |
0b173188 |
11 | use Test::Requires { |
12 | 'IO::AtomicFile' => 0.01, # skip all if not installed |
13 | 'JSON::Any' => 0.01, |
14 | }; |
15 | |
bf33d7c7 |
16 | BEGIN { |
5e5d4e28 |
17 | plan tests => 20; |
bf33d7c7 |
18 | use_ok('MooseX::Storage'); |
19 | } |
20 | |
21 | { |
22 | package Foo; |
23 | use Moose; |
24 | use MooseX::Storage; |
c2dae5d8 |
25 | |
bf33d7c7 |
26 | with 'MooseX::Storage::Deferred'; |
c2dae5d8 |
27 | |
bf33d7c7 |
28 | has 'number' => (is => 'ro', isa => 'Int'); |
29 | has 'string' => (is => 'ro', isa => 'Str'); |
c2dae5d8 |
30 | has 'float' => (is => 'ro', isa => 'Num'); |
bf33d7c7 |
31 | has 'array' => (is => 'ro', isa => 'ArrayRef'); |
c2dae5d8 |
32 | has 'hash' => (is => 'ro', isa => 'HashRef'); |
8b2ba857 |
33 | has 'object' => (is => 'ro', isa => 'Object'); |
bf33d7c7 |
34 | } |
35 | |
5e5d4e28 |
36 | my $file = catfile($dir, 'temp.json'); |
bf33d7c7 |
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) }, |
8b2ba857 |
45 | object => Foo->new( number => 2 ), |
bf33d7c7 |
46 | ); |
47 | isa_ok($foo, 'Foo'); |
48 | |
49 | $foo->store($file, { format => 'JSON', io => 'File' }); |
50 | } |
51 | |
52 | { |
53 | my $foo = Foo->load($file, { format => 'JSON', io => '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'); |
619ab942 |
59 | cmp_deeply($foo->array, [ 1 .. 10], '... got the right array'); |
60 | cmp_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash'); |
bf33d7c7 |
61 | |
62 | isa_ok($foo->object, 'Foo'); |
63 | is($foo->object->number, 2, '... got the right number (in the embedded object)'); |
64 | } |
65 | |
66 | unlink $file; |
67 | ok(!(-e $file), '... the file has been deleted'); |
68 | |
69 | { |
70 | my $foo = Foo->new( |
71 | number => 10, |
72 | string => 'foo', |
73 | float => 10.5, |
74 | array => [ 1 .. 10 ], |
75 | hash => { map { $_ => undef } (1 .. 10) }, |
8b2ba857 |
76 | object => Foo->new( number => 2 ), |
bf33d7c7 |
77 | ); |
78 | isa_ok($foo, 'Foo'); |
79 | |
80 | $foo->store($file, { format => 'JSON', io => 'AtomicFile' }); |
81 | } |
82 | |
83 | { |
84 | my $foo = Foo->load($file, { format => 'JSON', io => 'AtomicFile' }); |
85 | isa_ok($foo, 'Foo'); |
86 | |
87 | is($foo->number, 10, '... got the right number'); |
88 | is($foo->string, 'foo', '... got the right string'); |
89 | is($foo->float, 10.5, '... got the right float'); |
619ab942 |
90 | cmp_deeply($foo->array, [ 1 .. 10], '... got the right array'); |
91 | cmp_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash'); |
bf33d7c7 |
92 | |
93 | isa_ok($foo->object, 'Foo'); |
94 | is($foo->object->number, 2, '... got the right number (in the embedded object)'); |
95 | } |
96 | |
bf33d7c7 |
97 | |