From: Stevan Little Date: Tue, 3 Apr 2007 17:42:58 +0000 (+0000) Subject: reworking tests X-Git-Tag: 0_02~23 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-Storage.git;a=commitdiff_plain;h=ba5bba75ba0c4f8af075e95ce3b00e5376bedf5c reworking tests --- diff --git a/lib/MooseX/Storage/Engine.pm b/lib/MooseX/Storage/Engine.pm index cae2407..7f3bbb4 100644 --- a/lib/MooseX/Storage/Engine.pm +++ b/lib/MooseX/Storage/Engine.pm @@ -2,6 +2,12 @@ package MooseX::Storage::Engine; use Moose; +our $VERSION = '0.01'; + +# the class marker when +# serializing an object. +our $CLASS_MARKER = '__CLASS__'; + has 'storage' => ( is => 'rw', isa => 'HashRef', @@ -16,7 +22,7 @@ has 'class' => (is => 'rw', isa => 'Str'); sub collapse_object { my $self = shift; $self->map_attributes('collapse_attribute'); - $self->storage->{'__class__'} = $self->object->meta->name; + $self->storage->{$CLASS_MARKER} = $self->object->meta->name; return $self->storage; } @@ -81,9 +87,9 @@ sub map_attributes { my %OBJECT_HANDLERS = ( expand => sub { my $data = shift; - (exists $data->{'__class__'}) + (exists $data->{$CLASS_MARKER}) || confess "Serialized item has no class marker"; - $data->{'__class__'}->unpack($data); + $data->{$CLASS_MARKER}->unpack($data); }, collapse => sub { my $obj = shift; @@ -115,7 +121,7 @@ my %TYPES = ( my $array = shift; foreach my $i (0 .. $#{$array}) { next unless ref($array->[$i]) eq 'HASH' - && exists $array->[$i]->{'__class__'}; + && exists $array->[$i]->{$CLASS_MARKER}; $array->[$i] = $OBJECT_HANDLERS{expand}->($array->[$i]) } $array; @@ -138,7 +144,7 @@ my %TYPES = ( my $hash = shift; foreach my $k (keys %$hash) { next unless ref($hash->{$k}) eq 'HASH' - && exists $hash->{$k}->{'__class__'}; + && exists $hash->{$k}->{$CLASS_MARKER}; $hash->{$k} = $OBJECT_HANDLERS{expand}->($hash->{$k}) } $hash; diff --git a/t/001_basic.t b/t/001_basic.t index 06c343c..e75f51e 100644 --- a/t/001_basic.t +++ b/t/001_basic.t @@ -39,14 +39,14 @@ BEGIN { is_deeply( $foo->pack, { - __class__ => 'Foo', + __CLASS__ => 'Foo', number => 10, string => 'foo', float => 10.5, array => [ 1 .. 10 ], hash => { map { $_ => undef } ( 1 .. 10 ) }, object => { - __class__ => 'Foo', + __CLASS__ => 'Foo', number => 2 }, }, @@ -57,14 +57,14 @@ BEGIN { { my $foo = Foo->unpack( { - __class__ => 'Foo', + __CLASS__ => 'Foo', number => 10, string => 'foo', float => 10.5, array => [ 1 .. 10 ], hash => { map { $_ => undef } ( 1 .. 10 ) }, object => { - __class__ => 'Foo', + __CLASS__ => 'Foo', number => 2 }, } diff --git a/t/002_basic_w_subtypes.t b/t/002_basic_w_subtypes.t index 663dda4..e468ca8 100644 --- a/t/002_basic_w_subtypes.t +++ b/t/002_basic_w_subtypes.t @@ -70,14 +70,14 @@ cases. is_deeply( $foo->pack, { - __class__ => 'Foo', + __CLASS__ => 'Foo', number => 10, string => 'foo', float => 10.5, array => [ 1 .. 10 ], hash => { map { $_ => undef } ( 1 .. 10 ) }, object => { - __class__ => 'Foo', + __CLASS__ => 'Foo', number => 2 }, }, @@ -88,14 +88,14 @@ cases. { my $foo = Foo->unpack( { - __class__ => 'Foo', + __CLASS__ => 'Foo', number => 10, string => 'foo', float => 10.5, array => [ 1 .. 10 ], hash => { map { $_ => undef } ( 1 .. 10 ) }, object => { - __class__ => 'Foo', + __CLASS__ => 'Foo', number => 2 }, } diff --git a/t/003_basic_w_embedded_objects.t b/t/003_basic_w_embedded_objects.t index ecb0b76..7c69094 100644 --- a/t/003_basic_w_embedded_objects.t +++ b/t/003_basic_w_embedded_objects.t @@ -58,11 +58,11 @@ ArrayRef and HashRef type handlers. is_deeply( $foo->pack, { - __class__ => 'Foo', + __CLASS__ => 'Foo', bars => [ map { { - __class__ => 'Bar', + __CLASS__ => 'Bar', number => $_, } } (1 .. 10) @@ -75,11 +75,11 @@ ArrayRef and HashRef type handlers. { my $foo = Foo->unpack( { - __class__ => 'Foo', + __CLASS__ => 'Foo', bars => [ map { { - __class__ => 'Bar', + __CLASS__ => 'Bar', number => $_, } } (1 .. 10) @@ -104,11 +104,11 @@ ArrayRef and HashRef type handlers. is_deeply( $baz->pack, { - __class__ => 'Baz', + __CLASS__ => 'Baz', bars => { map { ($_ => { - __class__ => 'Bar', + __CLASS__ => 'Bar', number => $_, }) } (1 .. 10) @@ -121,11 +121,11 @@ ArrayRef and HashRef type handlers. { my $baz = Baz->unpack( { - __class__ => 'Baz', + __CLASS__ => 'Baz', bars => { map { ($_ => { - __class__ => 'Bar', + __CLASS__ => 'Bar', number => $_, }) } (1 .. 10) diff --git a/t/010_basic_json.t b/t/010_basic_json.t index 6a7d070..8c475e3 100644 --- a/t/010_basic_json.t +++ b/t/010_basic_json.t @@ -43,14 +43,14 @@ BEGIN { is_json( $json, - '{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__class__":"Foo"},"number":10,"__class__":"Foo","string":"foo"}', + '{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__CLASS__":"Foo"},"number":10,"__CLASS__":"Foo","string":"foo"}', '... got the right JSON' ); } { my $foo = Foo->thaw( - '{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__class__":"Foo"},"number":10,"__class__":"Foo","string":"foo"}' + '{"array":[1,2,3,4,5,6,7,8,9,10],"hash":{"6":null,"3":null,"7":null,"9":null,"2":null,"8":null,"1":null,"4":null,"10":null,"5":null},"float":10.5,"object":{"number":2,"__CLASS__":"Foo"},"number":10,"__CLASS__":"Foo","string":"foo"}' ); isa_ok( $foo, 'Foo' ); diff --git a/t/020_basic_yaml.t b/t/020_basic_yaml.t index 9b2339e..794f883 100644 --- a/t/020_basic_yaml.t +++ b/t/020_basic_yaml.t @@ -44,7 +44,7 @@ BEGIN { is( $yaml, q{--- -__class__: Foo +__CLASS__: Foo array: - 1 - 2 @@ -70,7 +70,7 @@ hash: 9: ~ number: 10 object: - __class__: Foo + __CLASS__: Foo number: 2 string: foo }, @@ -80,7 +80,7 @@ string: foo { my $foo = Foo->thaw(q{--- -__class__: Foo +__CLASS__: Foo array: - 1 - 2 @@ -106,7 +106,7 @@ hash: 9: ~ number: 10 object: - __class__: Foo + __CLASS__: Foo number: 2 string: foo }); diff --git a/t/021_basic_yaml_io.t b/t/021_basic_yaml_io.t deleted file mode 100644 index f666115..0000000 --- a/t/021_basic_yaml_io.t +++ /dev/null @@ -1,57 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; - -use Test::More no_plan => 1; - -BEGIN { - use_ok('MooseX::Storage'); -} - -{ - package Foo; - use Moose; - use MooseX::Storage; - - with Storage(format => 'YAML', io => 'File'); - - has 'number' => (is => 'ro', isa => 'Int'); - has 'string' => (is => 'ro', isa => 'Str'); - has 'float' => (is => 'ro', isa => 'Num'); - has 'array' => (is => 'ro', isa => 'ArrayRef'); - has 'hash' => (is => 'ro', isa => 'HashRef'); - has 'object' => (is => 'ro', isa => 'Object'); -} - -my $file = 'temp.yaml'; - -{ - my $foo = Foo->new( - number => 10, - string => 'foo', - float => 10.5, - array => [ 1 .. 10 ], - hash => { map { $_ => undef } (1 .. 10) }, - object => Foo->new( number => 2 ), - ); - isa_ok($foo, 'Foo'); - - $foo->store($file); -} - -{ - my $foo = Foo->load($file); - isa_ok($foo, 'Foo'); - - is($foo->number, 10, '... got the right number'); - is($foo->string, 'foo', '... got the right string'); - is($foo->float, 10.5, '... got the right float'); - is_deeply($foo->array, [ 1 .. 10], '... got the right array'); - is_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash'); - - isa_ok($foo->object, 'Foo'); - is($foo->object->number, 2, '... got the right number (in the embedded object)'); -} - -unlink $file; diff --git a/t/022_basic_yaml_io_atomic.t b/t/022_basic_yaml_io_atomic.t deleted file mode 100644 index 632def9..0000000 --- a/t/022_basic_yaml_io_atomic.t +++ /dev/null @@ -1,57 +0,0 @@ -#!/usr/bin/perl - -use strict; -use warnings; - -use Test::More no_plan => 1; - -BEGIN { - use_ok('MooseX::Storage'); -} - -{ - package Foo; - use Moose; - use MooseX::Storage; - - with Storage(format => 'YAML', io => 'AtomicFile'); - - has 'number' => (is => 'ro', isa => 'Int'); - has 'string' => (is => 'ro', isa => 'Str'); - has 'float' => (is => 'ro', isa => 'Num'); - has 'array' => (is => 'ro', isa => 'ArrayRef'); - has 'hash' => (is => 'ro', isa => 'HashRef'); - has 'object' => (is => 'ro', isa => 'Object'); -} - -my $file = 'temp.yaml'; - -{ - my $foo = Foo->new( - number => 10, - string => 'foo', - float => 10.5, - array => [ 1 .. 10 ], - hash => { map { $_ => undef } (1 .. 10) }, - object => Foo->new( number => 2 ), - ); - isa_ok($foo, 'Foo'); - - $foo->store($file); -} - -{ - my $foo = Foo->load($file); - isa_ok($foo, 'Foo'); - - is($foo->number, 10, '... got the right number'); - is($foo->string, 'foo', '... got the right string'); - is($foo->float, 10.5, '... got the right float'); - is_deeply($foo->array, [ 1 .. 10], '... got the right array'); - is_deeply($foo->hash, { map { $_ => undef } (1 .. 10) }, '... got the right hash'); - - isa_ok($foo->object, 'Foo'); - is($foo->object->number, 2, '... got the right number (in the embedded object)'); -} - -unlink $file; diff --git a/t/011_basic_json_io.t b/t/100_io.t similarity index 100% rename from t/011_basic_json_io.t rename to t/100_io.t diff --git a/t/012_basic_json_io_atomic.t b/t/101_io_atomic.t similarity index 100% rename from t/012_basic_json_io_atomic.t rename to t/101_io_atomic.t