Children are now tied directly instead of copied. This makes code behave more as...
[dbsrgits/DBM-Deep.git] / t / 19_crossref.t
index 56f96de..aa49512 100644 (file)
@@ -2,46 +2,44 @@
 # DBM::Deep Test
 ##
 use strict;
-use Test::More tests => 5;
+use Test::More tests => 6;
+use Test::Exception;
+use File::Temp qw( tempfile tempdir );
+use Fcntl qw( :flock );
 
 use_ok( 'DBM::Deep' );
 
-unlink "t/test.db";
-my $db = DBM::Deep->new( "t/test.db" );
-if ($db->error()) {
-       die "ERROR: " . $db->error();
+my $dir = tempdir( CLEANUP => 1 );
+my ($fh2, $filename2) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
+flock $fh2, LOCK_UN;
+my $db2 = DBM::Deep->new( $filename2 );
+
+{
+    my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
+    flock $fh, LOCK_UN;
+    my $db = DBM::Deep->new( $filename );
+
+    ##
+    # Create structure in $db
+    ##
+    $db->import(
+        hash1 => {
+            subkey1 => "subvalue1",
+            subkey2 => "subvalue2",
+        }
+    );
+    is( $db->{hash1}{subkey1}, 'subvalue1', "Value imported correctly" );
+    is( $db->{hash1}{subkey2}, 'subvalue2', "Value imported correctly" );
+
+    ##
+    # Cross-ref nested hash accross DB objects
+    ##
+    throws_ok {
+        $db2->{copy} = $db->{hash1};
+    } qr/Cannot cross-reference\. Use export\(\) instead/, "cross-ref fails";
+    $db2->{copy} = $db->{hash1}->export;
 }
 
-unlink "t/test2.db";
-my $db2 = DBM::Deep->new( "t/test2.db" );
-if ($db2->error()) {
-       die "ERROR: " . $db2->error();
-}
-
-##
-# Create structure in $db
-##
-$db->import(
-       hash1 => {
-               subkey1 => "subvalue1",
-               subkey2 => "subvalue2"
-       }
-);
-
-is( $db->{hash1}{subkey1}, 'subvalue1', "Value imported correctly" );
-is( $db->{hash1}{subkey2}, 'subvalue2', "Value imported correctly" );
-
-##
-# Cross-ref nested hash accross DB objects
-##
-$db2->{copy} = $db->{hash1};
-
-##
-# close, delete $db
-##
-undef $db;
-unlink "t/test.db";
-
 ##
 # Make sure $db2 has copy of $db's hash structure
 ##