Add .gitignore
[gitmo/MooseX-Storage.git] / t / 003_basic_w_embedded_objects.t
CommitLineData
913d96dd 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
8d8356bb 6use Test::More tests => 47;
913d96dd 7
8BEGIN {
9 use_ok('MooseX::Storage');
10}
11
95f31c36 12=pod
13
14This test checks the single level
15expansion and collpasing of the
16ArrayRef and HashRef type handlers.
17
18=cut
19
913d96dd 20{
21 package Bar;
22 use Moose;
23 use MooseX::Storage;
24
25 with Storage;
26
95f31c36 27 has 'number' => (is => 'ro', isa => 'Int');
913d96dd 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 );
95f31c36 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 );
913d96dd 50}
51
52{
53 my $foo = Foo->new(
95f31c36 54 bars => [ map { Bar->new(number => $_) } (1 .. 10) ]
913d96dd 55 );
56 isa_ok( $foo, 'Foo' );
57
58 is_deeply(
59 $foo->pack,
60 {
ba5bba75 61 __CLASS__ => 'Foo',
913d96dd 62 bars => [
63 map {
64 {
ba5bba75 65 __CLASS__ => 'Bar',
95f31c36 66 number => $_,
913d96dd 67 }
68 } (1 .. 10)
69 ],
70 },
71 '... got the right frozen class'
72 );
73}
74
75{
76 my $foo = Foo->unpack(
77 {
ba5bba75 78 __CLASS__ => 'Foo',
913d96dd 79 bars => [
80 map {
81 {
ba5bba75 82 __CLASS__ => 'Bar',
95f31c36 83 number => $_,
913d96dd 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');
95f31c36 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 {
ba5bba75 107 __CLASS__ => 'Baz',
95f31c36 108 bars => {
109 map {
110 ($_ => {
ba5bba75 111 __CLASS__ => 'Bar',
95f31c36 112 number => $_,
113 })
114 } (1 .. 10)
115 },
116 },
117 '... got the right frozen class'
118 );
119}
120
121{
122 my $baz = Baz->unpack(
123 {
ba5bba75 124 __CLASS__ => 'Baz',
95f31c36 125 bars => {
126 map {
127 ($_ => {
ba5bba75 128 __CLASS__ => 'Bar',
95f31c36 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");
913d96dd 140 }
141}