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