remove whitespace
[gitmo/MooseX-Storage.git] / t / 003_basic_w_embedded_objects.t
1 use strict;
2 use warnings;
3
4 use Test::More tests => 47;
5 use Test::Deep;
6
7 BEGIN {
8     use_ok('MooseX::Storage');
9 }
10
11 =pod
12
13 This test checks the single level
14 expansion and collpasing of the
15 ArrayRef and HashRef type handlers.
16
17 =cut
18
19 {
20     package Bar;
21     use Moose;
22     use MooseX::Storage;
23
24     with Storage;
25
26     has 'number' => (is => 'ro', isa => 'Int');
27
28     package Foo;
29     use Moose;
30     use MooseX::Storage;
31
32     with Storage;
33
34     has 'bars' => (
35         is  => 'ro',
36         isa => 'ArrayRef'
37     );
38
39     package Baz;
40     use Moose;
41     use MooseX::Storage;
42
43     with Storage;
44
45     has 'bars' => (
46         is  => 'ro',
47         isa => 'HashRef'
48     );
49 }
50
51 {
52     my $foo = Foo->new(
53         bars => [ map { Bar->new(number => $_) } (1 .. 10) ]
54     );
55     isa_ok( $foo, 'Foo' );
56
57     cmp_deeply(
58         $foo->pack,
59         {
60             __CLASS__ => 'Foo',
61             bars      => [
62                 map {
63                   {
64                       __CLASS__ => 'Bar',
65                       number    => $_,
66                   }
67                 } (1 .. 10)
68             ],
69         },
70         '... got the right frozen class'
71     );
72 }
73
74 {
75     my $foo = Foo->unpack(
76         {
77             __CLASS__ => 'Foo',
78             bars      => [
79                 map {
80                   {
81                       __CLASS__ => 'Bar',
82                       number    => $_,
83                   }
84                 } (1 .. 10)
85             ],
86         }
87     );
88     isa_ok( $foo, 'Foo' );
89
90     foreach my $i (1 .. scalar @{$foo->bars}) {
91         isa_ok($foo->bars->[$i - 1], 'Bar');
92         is($foo->bars->[$i - 1]->number, $i, "... got the right number ($i) in the Bar in Foo");
93     }
94 }
95
96
97 {
98     my $baz = Baz->new(
99         bars => { map { ($_ => Bar->new(number => $_)) } (1 .. 10) }
100     );
101     isa_ok( $baz, 'Baz' );
102
103     cmp_deeply(
104         $baz->pack,
105         {
106             __CLASS__ => 'Baz',
107             bars      => {
108                 map {
109                   ($_ => {
110                       __CLASS__ => 'Bar',
111                       number    => $_,
112                   })
113                 } (1 .. 10)
114             },
115         },
116         '... got the right frozen class'
117     );
118 }
119
120 {
121     my $baz = Baz->unpack(
122         {
123             __CLASS__ => 'Baz',
124             bars      => {
125                 map {
126                   ($_ => {
127                       __CLASS__ => 'Bar',
128                       number    => $_,
129                   })
130                 } (1 .. 10)
131             },
132         }
133     );
134     isa_ok( $baz, 'Baz' );
135
136     foreach my $k (keys %{$baz->bars}) {
137         isa_ok($baz->bars->{$k}, 'Bar');
138         is($baz->bars->{$k}->number, $k, "... got the right number ($k) in the Bar in Baz");
139     }
140 }