Commit | Line | Data |
bf33d7c7 |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
cfee09ad |
6 | use Test::More; |
bf33d7c7 |
7 | |
8 | BEGIN { |
cfee09ad |
9 | eval "use IO::AtomicFile"; |
4fa64e86 |
10 | plan skip_all => "IO::AtomicFile is required for this test" if $@; |
11 | eval "use JSON::Any"; |
12 | plan skip_all => "JSON::Any is required for this test" if $@; |
cfee09ad |
13 | plan tests => 21; |
bf33d7c7 |
14 | use_ok('MooseX::Storage'); |
15 | } |
16 | |
17 | { |
18 | package Foo; |
19 | use Moose; |
20 | use MooseX::Storage; |
21 | |
22 | with 'MooseX::Storage::Deferred'; |
23 | |
24 | has 'number' => (is => 'ro', isa => 'Int'); |
25 | has 'string' => (is => 'ro', isa => 'Str'); |
26 | has 'float' => (is => 'ro', isa => 'Num'); |
27 | has 'array' => (is => 'ro', isa => 'ArrayRef'); |
28 | has 'hash' => (is => 'ro', isa => 'HashRef'); |
29 | has 'object' => (is => 'ro', isa => 'Object'); |
30 | } |
31 | |
32 | my $file = 'temp.json'; |
33 | |
34 | { |
35 | my $foo = Foo->new( |
36 | number => 10, |
37 | string => 'foo', |
38 | float => 10.5, |
39 | array => [ 1 .. 10 ], |
40 | hash => { map { $_ => undef } (1 .. 10) }, |
41 | object => Foo->new( number => 2 ), |
42 | ); |
43 | isa_ok($foo, 'Foo'); |
44 | |
45 | $foo->store($file, { format => 'JSON', io => 'File' }); |
46 | } |
47 | |
48 | { |
49 | my $foo = Foo->load($file, { format => 'JSON', io => 'File' }); |
50 | isa_ok($foo, 'Foo'); |
51 | |
52 | is($foo->number, 10, '... got the right number'); |
53 | is($foo->string, 'foo', '... got the right string'); |
54 | is($foo->float, 10.5, '... got the right float'); |
55 | is_deeply($foo->array, [ 1 .. 10], '... got the right array'); |
56 | is_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash'); |
57 | |
58 | isa_ok($foo->object, 'Foo'); |
59 | is($foo->object->number, 2, '... got the right number (in the embedded object)'); |
60 | } |
61 | |
62 | unlink $file; |
63 | ok(!(-e $file), '... the file has been deleted'); |
64 | |
65 | { |
66 | my $foo = Foo->new( |
67 | number => 10, |
68 | string => 'foo', |
69 | float => 10.5, |
70 | array => [ 1 .. 10 ], |
71 | hash => { map { $_ => undef } (1 .. 10) }, |
72 | object => Foo->new( number => 2 ), |
73 | ); |
74 | isa_ok($foo, 'Foo'); |
75 | |
76 | $foo->store($file, { format => 'JSON', io => 'AtomicFile' }); |
77 | } |
78 | |
79 | { |
80 | my $foo = Foo->load($file, { format => 'JSON', io => 'AtomicFile' }); |
81 | isa_ok($foo, 'Foo'); |
82 | |
83 | is($foo->number, 10, '... got the right number'); |
84 | is($foo->string, 'foo', '... got the right string'); |
85 | is($foo->float, 10.5, '... got the right float'); |
86 | is_deeply($foo->array, [ 1 .. 10], '... got the right array'); |
87 | is_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash'); |
88 | |
89 | isa_ok($foo->object, 'Foo'); |
90 | is($foo->object->number, 2, '... got the right number (in the embedded object)'); |
91 | } |
92 | |
93 | unlink $file; |
94 | ok(!(-e $file), '... the file has been deleted'); |
95 | |