Initial patch from Bruno for implementing a ::Storage::Format::XML
[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 => 12;
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 'boolean' => ( is => 'ro', isa => 'Bool' );
23     has 'float'   => ( is => 'ro', isa => 'Num' );
24     has 'array'   => ( is => 'ro', isa => 'ArrayRef' );
25     has 'hash'    => ( is => 'ro', isa => 'HashRef' );
26     has 'object'  => ( is => 'ro', isa => 'Foo' );
27 }
28
29 {
30     my $foo = Foo->new(
31         number  => 10,
32         string  => 'foo',
33         boolean => 1,
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     is_deeply(
42         $foo->pack,
43         {
44             __CLASS__ => 'Foo',
45             number    => 10,
46             string    => 'foo',
47             boolean   => 1,
48             float     => 10.5,
49             array     => [ 1 .. 10 ],
50             hash      => { map { $_ => undef } ( 1 .. 10 ) },
51             object    => { 
52                             __CLASS__ => 'Foo',                
53                             number    => 2 
54                          },            
55         },
56         '... got the right frozen class'
57     );
58 }
59
60 {
61     my $foo = Foo->unpack(
62         {
63             __CLASS__ => 'Foo',
64             number    => 10,
65             string    => 'foo',
66             boolean   => 1,
67             float     => 10.5,
68             array     => [ 1 .. 10 ],
69             hash      => { map { $_ => undef } ( 1 .. 10 ) },
70             object    => { 
71                             __CLASS__ => 'Foo',                
72                             number    => 2 
73                          },            
74         }        
75     );
76     isa_ok( $foo, 'Foo' );
77
78     is( $foo->number, 10,    '... got the right number' );
79     is( $foo->string, 'foo', '... got the right string' );
80     ok( $foo->boolean,       '... got the right boolean' );
81     is( $foo->float,  10.5,  '... got the right float' );
82     is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
83     is_deeply(
84         $foo->hash,
85         { map { $_ => undef } ( 1 .. 10 ) },
86         '... got the right hash'
87     );
88
89     isa_ok( $foo->object, 'Foo' );
90     is( $foo->object->number, 2,
91         '... got the right number (in the embedded object)' );
92 }