adding simple checksum role
[gitmo/MooseX-Storage.git] / t / 030_with_checksum.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 6;
7 use Test::Exception;
8 use Test::Deep;
9
10 BEGIN {
11     use_ok('MooseX::Storage');
12 }
13
14 {
15
16     package Foo;
17     use Moose;
18     use MooseX::Storage;
19
20     with Storage(base => 'WithChecksum');
21
22     has 'number' => ( is => 'ro', isa => 'Int' );
23     has 'string' => ( is => 'ro', 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 => 'Foo' );
28 }
29
30 {
31     my $foo = Foo->new(
32         number => 10,
33         string => 'foo',
34         float  => 10.5,
35         array  => [ 1 .. 10 ],
36         hash   => { map { $_ => undef } ( 1 .. 10 ) },
37         object => Foo->new( number => 2 ),
38     );
39     isa_ok( $foo, 'Foo' );
40     
41     my $packed = $foo->pack;
42     
43     cmp_deeply(
44         $packed,
45         {
46             __CLASS__ => 'Foo',
47             checksum  => re('[0-9a-f]+'),
48             number    => 10,
49             string    => 'foo',
50             float     => 10.5,
51             array     => [ 1 .. 10 ],
52             hash      => { map { $_ => undef } ( 1 .. 10 ) },
53             object    => { 
54                             __CLASS__ => 'Foo', 
55                             checksum  => re('[0-9a-f]+'),               
56                             number    => 2 
57                          },            
58         },
59         '... got the right frozen class'
60     );
61
62     my $foo2;
63     lives_ok {
64         $foo2 = Foo->unpack($packed);
65     } '... unpacked okay';
66     isa_ok($foo2, 'Foo');
67     
68     cmp_deeply(
69         $foo2->pack,
70         {
71             __CLASS__ => 'Foo',
72             checksum  => re('[0-9a-f]+'),
73             number    => 10,
74             string    => 'foo',
75             float     => 10.5,
76             array     => [ 1 .. 10 ],
77             hash      => { map { $_ => undef } ( 1 .. 10 ) },
78             object    => { 
79                             __CLASS__ => 'Foo', 
80                             checksum  => re('[0-9a-f]+'),               
81                             number    => 2 
82                          },            
83         },
84         '... got the right frozen class'
85     );    
86     
87 }
88