do not skip any tests in author mode - die instead!
[gitmo/MooseX-Storage.git] / t / 103_io_storable_file_custom.t
CommitLineData
124c2ba5 1use strict;
2use warnings;
3
4use Test::More tests => 11;
619ab942 5use Test::Deep;
124c2ba5 6use Storable ();
5e5d4e28 7use File::Temp qw(tempdir);
8use File::Spec::Functions;
9my $dir = tempdir( CLEANUP => 1 );
124c2ba5 10
11BEGIN {
12 use_ok('MooseX::Storage');
13}
14
15{
16 package Foo;
17 use Moose;
18 use MooseX::Storage;
c2dae5d8 19
124c2ba5 20 with Storage(io => 'StorableFile');
c2dae5d8 21
124c2ba5 22 has 'number' => (is => 'ro', isa => 'Int');
23 has 'string' => (is => 'rw', isa => 'Str');
c2dae5d8 24 has 'float' => (is => 'ro', isa => 'Num');
124c2ba5 25 has 'array' => (is => 'ro', isa => 'ArrayRef');
c2dae5d8 26 has 'hash' => (is => 'ro', isa => 'HashRef');
8b2ba857 27 has 'object' => (is => 'ro', isa => 'Object');
28
29 ## add some custom freeze/thaw hooks here ...
30
124c2ba5 31 sub thaw {
32 my ( $class, $data ) = @_;
33 my $self = $class->unpack( $data );
34 $self->string("Hello World");
35 $self;
36 }
37
38 sub freeze {
39 my ( $self, @args ) = @_;
40 my $data = $self->pack(@args);
41 $data->{string} = "HELLO WORLD";
42 $data;
43 }
44
45}
46
5e5d4e28 47my $file = catfile($dir,'temp.storable');
124c2ba5 48
49{
50 my $foo = Foo->new(
51 number => 10,
52 string => 'foo',
53 float => 10.5,
54 array => [ 1 .. 10 ],
55 hash => { map { $_ => undef } (1 .. 10) },
8b2ba857 56 object => Foo->new( number => 2 ),
124c2ba5 57 );
58 isa_ok($foo, 'Foo');
59
60 $foo->store($file);
c2dae5d8 61
124c2ba5 62 # check our custom freeze hook fired ...
63 my $data = Storable::retrieve($file);
619ab942 64 cmp_deeply(
124c2ba5 65 $data,
66 {
67 '__CLASS__' => 'Foo',
68 'float' => 10.5,
69 'number' => 10,
c2dae5d8 70 'string' => 'HELLO WORLD',
124c2ba5 71 'array' => [ 1 .. 10],
c2dae5d8 72 'hash' => { map { $_ => undef } 1 .. 10 },
124c2ba5 73 'object' => {
74 '__CLASS__' => 'Foo',
75 'number' => 2
76 },
77 },
78 '... got the data struct we expected'
c2dae5d8 79 );
80
124c2ba5 81}
82
83{
84 my $foo = Foo->load($file);
85 isa_ok($foo, 'Foo');
86
87 ## check our custom thaw hook fired
88 is($foo->string, 'Hello World', '... got the right string');
89
90 is($foo->number, 10, '... got the right number');
91 is($foo->float, 10.5, '... got the right float');
619ab942 92 cmp_deeply($foo->array, [ 1 .. 10], '... got the right array');
93 cmp_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
124c2ba5 94
95 isa_ok($foo->object, 'Foo');
96 is($foo->object->number, 2, '... got the right number (in the embedded object)');
97}
98