faf41ebf6544b869b5404b5a0d4e4e2981ff6323
[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 => 14;
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     has 'union'   => ( is => 'ro', isa => 'ArrayRef|Str' );
28     has 'union2'   => ( is => 'ro', isa => 'ArrayRef|Str' );
29 }
30
31 {
32     my $foo = Foo->new(
33         number  => 10,
34         string  => 'foo',
35         boolean => 1,
36         float   => 10.5,
37         array   => [ 1 .. 10 ],
38         hash    => { map { $_ => undef } ( 1 .. 10 ) },
39         object  => Foo->new( number => 2 ),
40         union   => [ 1, 2, 3 ],
41         union2  => 'A String'
42     );
43     isa_ok( $foo, 'Foo' );
44
45     is_deeply(
46         $foo->pack,
47         {
48             __CLASS__ => 'Foo',
49             number    => 10,
50             string    => 'foo',
51             boolean   => 1,
52             float     => 10.5,
53             array     => [ 1 .. 10 ],
54             hash      => { map { $_ => undef } ( 1 .. 10 ) },
55             object    => {
56                             __CLASS__ => 'Foo',
57                             number    => 2
58                          },
59             union     => [ 1, 2, 3 ],
60             union2    => 'A String'
61         },
62         '... got the right frozen class'
63     );
64 }
65
66 {
67     my $foo = Foo->unpack(
68         {
69             __CLASS__ => 'Foo',
70             number    => 10,
71             string    => 'foo',
72             boolean   => 1,
73             float     => 10.5,
74             array     => [ 1 .. 10 ],
75             hash      => { map { $_ => undef } ( 1 .. 10 ) },
76             object    => {
77                             __CLASS__ => 'Foo',
78                             number    => 2
79                          },
80             union     => [ 1, 2, 3 ],
81             union2    => 'A String'
82         }
83     );
84     isa_ok( $foo, 'Foo' );
85
86     is( $foo->number, 10,    '... got the right number' );
87     is( $foo->string, 'foo', '... got the right string' );
88     ok( $foo->boolean,       '... got the right boolean' );
89     is( $foo->float,  10.5,  '... got the right float' );
90     is_deeply( $foo->array, [ 1 .. 10 ], '... got the right array' );
91     is_deeply(
92         $foo->hash,
93         { map { $_ => undef } ( 1 .. 10 ) },
94         '... got the right hash'
95     );
96
97     isa_ok( $foo->object, 'Foo' );
98     is( $foo->object->number, 2,
99         '... got the right number (in the embedded object)' );
100     is_deeply( $foo->union, [ 1 .. 3 ], '... got the right array (in the union)' );
101     is( $foo->union2,  'A String',  '... got the right string (in the union)' );
102 }