Standardized test incantations
[dbsrgits/DBM-Deep.git] / t / 24_autobless.t
index 93fe857..2126749 100644 (file)
@@ -7,118 +7,205 @@ use strict;
     sub foo { 'foo' };
 }
 
-use Test::More tests => 26;
+use Test::More tests => 65;
+use t::common qw( new_fh );
 
 use_ok( 'DBM::Deep' );
 
-unlink 't/test.db';
-my $db = DBM::Deep->new(
-    file     => "t/test.db",
-    autobless => 1,
-);
-if ($db->error()) {
-       die "ERROR: " . $db->error();
+my ($fh, $filename) = new_fh();
+{
+    my $db = DBM::Deep->new(
+        file     => $filename,
+        autobless => 1,
+    );
+
+    my $obj = bless {
+        a => 1,
+        b => [ 1 .. 3 ],
+    }, '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' },
+        2,
+    ], '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 $obj = bless {
-    a => 1,
-    b => [ 1 .. 3 ],
-}, 'Foo';
+{
+    my $db = DBM::Deep->new(
+        file     => $filename,
+        autobless => 1,
+    );
 
-$db->{blessed} = $obj;
+    my $obj = $db->{blessed};
+    isa_ok( $obj, 'Foo' );
+    can_ok( $obj, 'export', 'foo' );
+    ok( !$obj->can( 'STORE' ), "... but it cannot 'STORE'" );
 
-$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( $obj->{a}, 1 );
+    is( $obj->{b}[0], 1 );
+    is( $obj->{b}[1], 2 );
+    is( $obj->{b}[2], 3 );
 
-undef $db;
+    my $obj2 = $db->{blessed2};
+    isa_ok( $obj2, 'Foo' );
+    can_ok( $obj2, 'export', 'foo' );
+    ok( !$obj2->can( 'STORE' ), "... but it cannot 'STORE'" );
 
-my $db2 = DBM::Deep->new(
-    file     => 't/test.db',
-    autobless => 1,
-);
-if ($db2->error()) {
-       die "ERROR: " . $db2->error();
-}
+    is( $obj2->[0]{a}, 'foo' );
+    is( $obj2->[1], '2' );
 
-my $obj2 = $db2->{blessed};
-isa_ok( $obj2, 'Foo' );
-can_ok( $obj2, 'export', 'foo' );
-ok( !$obj2->can( 'STORE' ), "... but it cannot 'STORE'" );
-
-is( $obj2->{a}, 1 );
-is( $obj2->{b}[0], 1 );
-is( $obj2->{b}[1], 2 );
-is( $obj2->{b}[2], 3 );
-
-is( $db2->{unblessed}{a}, 1 );
-is( $db2->{unblessed}{b}[0], 1 );
-is( $db2->{unblessed}{b}[1], 2 );
-is( $db2->{unblessed}{b}[2], 3 );
-
-TODO: {
-    todo_skip "_copy_node() doesn't work with autobless", 1;
-    my $structure = $db2->export();
-    ok( 1 );
+    is( $db->{unblessed}{a}, 1 );
+    is( $db->{unblessed}{b}[0], 1 );
+    is( $db->{unblessed}{b}[1], 2 );
+    is( $db->{unblessed}{b}[2], 3 );
+
+    $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 $db3 = DBM::Deep->new(
-    file     => 't/test.db',
-);
-if ($db3->error()) {
-       die "ERROR: " . $db3->error();
+{
+    my $db = DBM::Deep->new(
+        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' );
+    ok( !$obj->can( 'STORE' ), "... but it cannot 'STORE'" );
+
+    is( $obj->{a}, 1 );
+    is( $obj->{b}[0], 1 );
+    is( $obj->{b}[1], 2 );
+    is( $obj->{b}[2], 3 );
+
+    my $obj2 = $structure->{blessed2};
+    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' );
+
+    is( $structure->{unblessed}{a}, 1 );
+    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 $obj3 = $db3->{blessed};
-isa_ok( $obj3, 'DBM::Deep' );
-can_ok( $obj3, 'export', 'STORE' );
-ok( !$obj3->can( 'foo' ), "... but it cannot 'foo'" );
+{
+    my $db = DBM::Deep->new(
+        file     => $filename,
+        autobless => 0,
+    );
 
-is( $obj3->{a}, 1 );
-is( $obj3->{b}[0], 1 );
-is( $obj3->{b}[1], 2 );
-is( $obj3->{b}[2], 3 );
+    my $obj = $db->{blessed};
+    isa_ok( $obj, 'DBM::Deep' );
+    can_ok( $obj, 'export', 'STORE' );
+    ok( !$obj->can( 'foo' ), "... but it cannot 'foo'" );
+
+    is( $obj->{a}, 1 );
+    is( $obj->{b}[0], 1 );
+    is( $obj->{b}[1], 2 );
+    is( $obj->{b}[2], 3 );
+
+    my $obj2 = $db->{blessed2};
+    isa_ok( $obj2, 'DBM::Deep' );
+    can_ok( $obj2, 'export', 'STORE' );
+    ok( !$obj2->can( 'foo' ), "... but it cannot 'foo'" );
+
+    is( $obj2->[0]{a}, 'foo' );
+    is( $obj2->[1], '2' );
+
+    is( $db->{unblessed}{a}, 1 );
+    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 );
+}
 
-is( $db3->{unblessed}{a}, 1 );
-is( $db3->{unblessed}{b}[0], 1 );
-is( $db3->{unblessed}{b}[1], 2 );
-is( $db3->{unblessed}{b}[2], 3 );
+{
+    my ($fh2, $filename2) = new_fh();
+    {
+        my $db = DBM::Deep->new(
+            file     => $filename2,
+            autobless => 1,
+        );
+        my $obj = bless {
+            a => 1,
+            b => [ 1 .. 3 ],
+        }, 'Foo';
+
+        $db->import( { blessed => $obj } );
+        $db->_get_self->_engine->storage->close( $db->_get_self );
+    }
 
-undef $db;
-undef $db2;
-undef $db3;
+    {
+        my $db = DBM::Deep->new(
+            file     => $filename2,
+            autobless => 1,
+        );
+
+        my $blessed = $db->{blessed};
+        isa_ok( $blessed, 'Foo' );
+        is( $blessed->{a}, 1 );
+        $db->_get_self->_engine->storage->close( $db->_get_self );
+    }
+}
 
 {
-    unlink 't/test.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/test.db",
+        file     => $filename3,
         autobless => 1,
     );
-    if ($db->error()) {
-        die "ERROR: " . $db->error();
-    }
 
-    my $obj = bless {
-        a => 1,
-        b => [ 1 .. 3 ],
-    }, 'Foo';
+    my $obj = bless {}, 'Foo';
 
-    $db->import( { blessed => $obj } );
+    $db->{blessed} = $obj;
+    $db->{after} = "hello";
 
-    undef $db;
+    my $obj2 = bless {}, 'FooFoo';
 
-    $db = DBM::Deep->new(
-        file     => "t/test.db",
-        autobless => 1,
-    );
-    if ($db->error()) {
-        die "ERROR: " . $db->error();
-    }
+    $db->{blessed} = $obj2;
 
-    my $blessed = $db->{blessed};
-    isa_ok( $blessed, 'Foo' );
-    is( $blessed->{a}, 1 );
+    is( $db->{after}, "hello" );
 }