4 use Test::More tests => 11;
7 use File::Temp qw(tempdir);
8 use File::Spec::Functions;
9 my $dir = tempdir( CLEANUP => 1 );
12 use_ok('MooseX::Storage');
20 with Storage(io => 'StorableFile');
22 has 'number' => (is => 'ro', isa => 'Int');
23 has 'string' => (is => 'rw', isa => 'Str');
24 has 'float' => (is => 'ro', isa => 'Num');
25 has 'array' => (is => 'ro', isa => 'ArrayRef');
26 has 'hash' => (is => 'ro', isa => 'HashRef');
27 has 'object' => (is => 'ro', isa => 'Object');
29 ## add some custom freeze/thaw hooks here ...
32 my ( $class, $data ) = @_;
33 my $self = $class->unpack( $data );
34 $self->string("Hello World");
39 my ( $self, @args ) = @_;
40 my $data = $self->pack(@args);
41 $data->{string} = "HELLO WORLD";
47 my $file = catfile($dir,'temp.storable');
55 hash => { map { $_ => undef } (1 .. 10) },
56 object => Foo->new( number => 2 ),
62 # check our custom freeze hook fired ...
63 my $data = Storable::retrieve($file);
70 'string' => 'HELLO WORLD',
71 'array' => [ 1 .. 10],
72 'hash' => { map { $_ => undef } 1 .. 10 },
78 '... got the data struct we expected'
84 my $foo = Foo->load($file);
87 ## check our custom thaw hook fired
88 is($foo->string, 'Hello World', '... got the right string');
90 is($foo->number, 10, '... got the right number');
91 is($foo->float, 10.5, '... got the right float');
92 cmp_deeply($foo->array, [ 1 .. 10], '... got the right array');
93 cmp_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash');
95 isa_ok($foo->object, 'Foo');
96 is($foo->object->number, 2, '... got the right number (in the embedded object)');