Commit | Line | Data |
913d96dd |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
8d8356bb |
6 | use Test::More tests => 47; |
913d96dd |
7 | |
8 | BEGIN { |
9 | use_ok('MooseX::Storage'); |
10 | } |
11 | |
95f31c36 |
12 | =pod |
13 | |
b5f363ac |
14 | This test checks the single level |
15 | expansion and collpasing of the |
95f31c36 |
16 | ArrayRef and HashRef type handlers. |
17 | |
18 | =cut |
19 | |
913d96dd |
20 | { |
21 | package Bar; |
22 | use Moose; |
23 | use MooseX::Storage; |
24 | |
25 | with Storage; |
b5f363ac |
26 | |
95f31c36 |
27 | has 'number' => (is => 'ro', isa => 'Int'); |
b5f363ac |
28 | |
913d96dd |
29 | package Foo; |
30 | use Moose; |
31 | use MooseX::Storage; |
32 | |
b5f363ac |
33 | with Storage; |
913d96dd |
34 | |
b5f363ac |
35 | has 'bars' => ( |
36 | is => 'ro', |
37 | isa => 'ArrayRef' |
913d96dd |
38 | ); |
b5f363ac |
39 | |
95f31c36 |
40 | package Baz; |
41 | use Moose; |
42 | use MooseX::Storage; |
43 | |
b5f363ac |
44 | with Storage; |
95f31c36 |
45 | |
b5f363ac |
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' ); |
b5f363ac |
57 | |
913d96dd |
58 | is_deeply( |
59 | $foo->pack, |
60 | { |
ba5bba75 |
61 | __CLASS__ => 'Foo', |
b5f363ac |
62 | bars => [ |
913d96dd |
63 | map { |
64 | { |
ba5bba75 |
65 | __CLASS__ => 'Bar', |
95f31c36 |
66 | number => $_, |
b5f363ac |
67 | } |
913d96dd |
68 | } (1 .. 10) |
b5f363ac |
69 | ], |
913d96dd |
70 | }, |
71 | '... got the right frozen class' |
72 | ); |
73 | } |
74 | |
75 | { |
76 | my $foo = Foo->unpack( |
77 | { |
ba5bba75 |
78 | __CLASS__ => 'Foo', |
b5f363ac |
79 | bars => [ |
913d96dd |
80 | map { |
81 | { |
ba5bba75 |
82 | __CLASS__ => 'Bar', |
95f31c36 |
83 | number => $_, |
b5f363ac |
84 | } |
913d96dd |
85 | } (1 .. 10) |
b5f363ac |
86 | ], |
87 | } |
913d96dd |
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' ); |
b5f363ac |
103 | |
95f31c36 |
104 | is_deeply( |
105 | $baz->pack, |
106 | { |
ba5bba75 |
107 | __CLASS__ => 'Baz', |
95f31c36 |
108 | bars => { |
109 | map { |
110 | ($_ => { |
ba5bba75 |
111 | __CLASS__ => 'Bar', |
95f31c36 |
112 | number => $_, |
b5f363ac |
113 | }) |
95f31c36 |
114 | } (1 .. 10) |
b5f363ac |
115 | }, |
95f31c36 |
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 => $_, |
b5f363ac |
130 | }) |
95f31c36 |
131 | } (1 .. 10) |
b5f363ac |
132 | }, |
133 | } |
95f31c36 |
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 | } |