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