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