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