X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F003_basic_w_embedded_objects.t;h=ccc7e14250a3c65bc4b4c0104729356075b6fe06;hb=619ab942be0a8bd8f530c57ca5b0c8d833cdc89b;hp=de33dff4ba6655aa2bae38447c79ea4dccc1f1a8;hpb=913d96ddbab15f9b2d870b8dba5bd8f8f11e36d1;p=gitmo%2FMooseX-Storage.git diff --git a/t/003_basic_w_embedded_objects.t b/t/003_basic_w_embedded_objects.t index de33dff..ccc7e14 100644 --- a/t/003_basic_w_embedded_objects.t +++ b/t/003_basic_w_embedded_objects.t @@ -3,12 +3,21 @@ use strict; use warnings; -use Test::More no_plan => 1; +use Test::More tests => 47; +use Test::Deep; BEGIN { use_ok('MooseX::Storage'); } +=pod + +This test checks the single level +expansion and collpasing of the +ArrayRef and HashRef type handlers. + +=cut + { package Bar; use Moose; @@ -16,7 +25,7 @@ BEGIN { with Storage; - has 'baz' => (is => 'ro', isa => 'Int'); + has 'number' => (is => 'ro', isa => 'Int'); package Foo; use Moose; @@ -28,23 +37,34 @@ BEGIN { is => 'ro', isa => 'ArrayRef' ); + + package Baz; + use Moose; + use MooseX::Storage; + + with Storage; + + has 'bars' => ( + is => 'ro', + isa => 'HashRef' + ); } { my $foo = Foo->new( - bars => [ map { Bar->new(baz => $_) } (1 .. 10) ] + bars => [ map { Bar->new(number => $_) } (1 .. 10) ] ); isa_ok( $foo, 'Foo' ); - is_deeply( + cmp_deeply( $foo->pack, { - __class__ => 'Foo', + __CLASS__ => 'Foo', bars => [ map { { - __class__ => 'Bar', - baz => $_, + __CLASS__ => 'Bar', + number => $_, } } (1 .. 10) ], @@ -56,12 +76,12 @@ BEGIN { { my $foo = Foo->unpack( { - __class__ => 'Foo', + __CLASS__ => 'Foo', bars => [ map { { - __class__ => 'Bar', - baz => $_, + __CLASS__ => 'Bar', + number => $_, } } (1 .. 10) ], @@ -71,6 +91,52 @@ BEGIN { foreach my $i (1 .. scalar @{$foo->bars}) { isa_ok($foo->bars->[$i - 1], 'Bar'); - is($foo->bars->[$i - 1]->baz, $i, "... got the right baz ($i) in the Bar in Foo"); + is($foo->bars->[$i - 1]->number, $i, "... got the right number ($i) in the Bar in Foo"); + } +} + + +{ + my $baz = Baz->new( + bars => { map { ($_ => Bar->new(number => $_)) } (1 .. 10) } + ); + isa_ok( $baz, 'Baz' ); + + cmp_deeply( + $baz->pack, + { + __CLASS__ => 'Baz', + bars => { + map { + ($_ => { + __CLASS__ => 'Bar', + number => $_, + }) + } (1 .. 10) + }, + }, + '... got the right frozen class' + ); +} + +{ + my $baz = Baz->unpack( + { + __CLASS__ => 'Baz', + bars => { + map { + ($_ => { + __CLASS__ => 'Bar', + number => $_, + }) + } (1 .. 10) + }, + } + ); + isa_ok( $baz, 'Baz' ); + + foreach my $k (keys %{$baz->bars}) { + isa_ok($baz->bars->{$k}, 'Bar'); + is($baz->bars->{$k}->number, $k, "... got the right number ($k) in the Bar in Baz"); } }