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