X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F24_autobless.t;h=70ef1dfdc10b9dde151b988f83eeaa69e351dc4b;hb=345e7fd079ea47414f8d2601e47689a0dbd16c97;hp=e039b31220381cb7c009c0fc0dbf93cbe349bc37;hpb=95bbd935b78cf4efc580e3d89dde2b2bca9b17f8;p=dbsrgits%2FDBM-Deep.git diff --git a/t/24_autobless.t b/t/24_autobless.t index e039b31..70ef1df 100644 --- a/t/24_autobless.t +++ b/t/24_autobless.t @@ -7,19 +7,18 @@ use strict; sub foo { 'foo' }; } -use Test::More tests => 54; +use Test::More tests => 65; +use t::common qw( new_fh ); use_ok( 'DBM::Deep' ); -unlink 't/test.db'; +my ($fh, $filename) = new_fh(); { my $db = DBM::Deep->new( - file => "t/test.db", + file => $filename, + fh => $fh, autobless => 1, ); - if ($db->error()) { - die "ERROR: " . $db->error(); - } my $obj = bless { a => 1, @@ -27,6 +26,10 @@ unlink 't/test.db'; }, 'Foo'; $db->{blessed} = $obj; + is( $db->{blessed}{a}, 1 ); + is( $db->{blessed}{b}[0], 1 ); + is( $db->{blessed}{b}[1], 2 ); + is( $db->{blessed}{b}[2], 3 ); my $obj2 = bless [ { a => 'foo' }, @@ -34,22 +37,30 @@ unlink 't/test.db'; ], 'Foo'; $db->{blessed2} = $obj2; + is( $db->{blessed2}[0]{a}, 'foo' ); + is( $db->{blessed2}[1], '2' ); + $db->{unblessed} = {}; $db->{unblessed}{a} = 1; $db->{unblessed}{b} = []; $db->{unblessed}{b}[0] = 1; $db->{unblessed}{b}[1] = 2; $db->{unblessed}{b}[2] = 3; + + is( $db->{unblessed}{a}, 1 ); + is( $db->{unblessed}{b}[0], 1 ); + is( $db->{unblessed}{b}[1], 2 ); + is( $db->{unblessed}{b}[2], 3 ); + + $db->{blessed_long} = bless {}, 'a' x 1000; + $db->_get_self->_engine->storage->close( $db->_get_self ); } { my $db = DBM::Deep->new( - file => 't/test.db', + file => $filename, autobless => 1, ); - if ($db->error()) { - die "ERROR: " . $db->error(); - } my $obj = $db->{blessed}; isa_ok( $obj, 'Foo' ); @@ -62,9 +73,9 @@ unlink 't/test.db'; is( $obj->{b}[2], 3 ); my $obj2 = $db->{blessed2}; - isa_ok( $obj, 'Foo' ); - can_ok( $obj, 'export', 'foo' ); - ok( !$obj->can( 'STORE' ), "... but it cannot 'STORE'" ); + isa_ok( $obj2, 'Foo' ); + can_ok( $obj2, 'export', 'foo' ); + ok( !$obj2->can( 'STORE' ), "... but it cannot 'STORE'" ); is( $obj2->[0]{a}, 'foo' ); is( $obj2->[1], '2' ); @@ -76,17 +87,21 @@ unlink 't/test.db'; $obj->{c} = 'new'; is( $db->{blessed}{c}, 'new' ); + + isa_ok( $db->{blessed_long}, 'a' x 1000 ); + $db->_get_self->_engine->storage->close( $db->_get_self ); } { my $db = DBM::Deep->new( - file => 't/test.db', + file => $filename, autobless => 1, ); is( $db->{blessed}{c}, 'new' ); my $structure = $db->export(); - + use Data::Dumper;print Dumper $structure; + my $obj = $structure->{blessed}; isa_ok( $obj, 'Foo' ); can_ok( $obj, 'export', 'foo' ); @@ -98,9 +113,9 @@ unlink 't/test.db'; is( $obj->{b}[2], 3 ); my $obj2 = $structure->{blessed2}; - isa_ok( $obj, 'Foo' ); - can_ok( $obj, 'export', 'foo' ); - ok( !$obj->can( 'STORE' ), "... but it cannot 'STORE'" ); + isa_ok( $obj2, 'Foo' ); + can_ok( $obj2, 'export', 'foo' ); + ok( !$obj2->can( 'STORE' ), "... but it cannot 'STORE'" ); is( $obj2->[0]{a}, 'foo' ); is( $obj2->[1], '2' ); @@ -109,15 +124,14 @@ unlink 't/test.db'; is( $structure->{unblessed}{b}[0], 1 ); is( $structure->{unblessed}{b}[1], 2 ); is( $structure->{unblessed}{b}[2], 3 ); + $db->_get_self->_engine->storage->close( $db->_get_self ); } { my $db = DBM::Deep->new( - file => 't/test.db', + file => $filename, + autobless => 0, ); - if ($db->error()) { - die "ERROR: " . $db->error(); - } my $obj = $db->{blessed}; isa_ok( $obj, 'DBM::Deep' ); @@ -141,63 +155,60 @@ unlink 't/test.db'; is( $db->{unblessed}{b}[0], 1 ); is( $db->{unblessed}{b}[1], 2 ); is( $db->{unblessed}{b}[2], 3 ); + $db->_get_self->_engine->storage->close( $db->_get_self ); } { - unlink 't/test2.db'; - my $db = DBM::Deep->new( - file => "t/test2.db", - autobless => 1, - ); - if ($db->error()) { - die "ERROR: " . $db->error(); + my ($fh2, $filename2) = new_fh(); + { + my $db = DBM::Deep->new( + file => $filename2, + fh => $fh2, + autobless => 1, + ); + my $obj = bless { + a => 1, + b => [ 1 .. 3 ], + }, 'Foo'; + + $db->import( { blessed => $obj } ); + $db->_get_self->_engine->storage->close( $db->_get_self ); } - my $obj = bless { - a => 1, - b => [ 1 .. 3 ], - }, 'Foo'; - $db->import( { blessed => $obj } ); + { + my $db = DBM::Deep->new( + file => $filename2, + autobless => 1, + ); - undef $db; - - $db = DBM::Deep->new( - file => "t/test2.db", - autobless => 1, - ); - if ($db->error()) { - die "ERROR: " . $db->error(); + my $blessed = $db->{blessed}; + isa_ok( $blessed, 'Foo' ); + is( $blessed->{a}, 1 ); + $db->_get_self->_engine->storage->close( $db->_get_self ); } - - my $blessed = $db->{blessed}; - isa_ok( $blessed, 'Foo' ); - is( $blessed->{a}, 1 ); } { - ## - # test blessing hash into short named class (Foo), then re-blessing into - # longer named class (FooFoo) and replacing key in db file, then validating - # content after that point in file to check for corruption. - ## - unlink 't/test3.db'; + ## + # test blessing hash into short named class (Foo), then re-blessing into + # longer named class (FooFoo) and replacing key in db file, then validating + # content after that point in file to check for corruption. + ## + my ($fh3, $filename3) = new_fh(); my $db = DBM::Deep->new( - file => "t/test3.db", + file => $filename3, + fh => $fh3, autobless => 1, ); - if ($db->error()) { - die "ERROR: " . $db->error(); - } my $obj = bless {}, 'Foo'; $db->{blessed} = $obj; $db->{after} = "hello"; - + my $obj2 = bless {}, 'FooFoo'; - + $db->{blessed} = $obj2; is( $db->{after}, "hello" ); } -