aa495129f01fa69d696ee12ecae83940dcadccaa
[dbsrgits/DBM-Deep.git] / t / 19_crossref.t
1 ##
2 # DBM::Deep Test
3 ##
4 use strict;
5 use Test::More tests => 6;
6 use Test::Exception;
7 use File::Temp qw( tempfile tempdir );
8 use Fcntl qw( :flock );
9
10 use_ok( 'DBM::Deep' );
11
12 my $dir = tempdir( CLEANUP => 1 );
13 my ($fh2, $filename2) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
14 flock $fh2, LOCK_UN;
15 my $db2 = DBM::Deep->new( $filename2 );
16
17 {
18     my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
19     flock $fh, LOCK_UN;
20     my $db = DBM::Deep->new( $filename );
21
22     ##
23     # Create structure in $db
24     ##
25     $db->import(
26         hash1 => {
27             subkey1 => "subvalue1",
28             subkey2 => "subvalue2",
29         }
30     );
31     is( $db->{hash1}{subkey1}, 'subvalue1', "Value imported correctly" );
32     is( $db->{hash1}{subkey2}, 'subvalue2', "Value imported correctly" );
33
34     ##
35     # Cross-ref nested hash accross DB objects
36     ##
37     throws_ok {
38         $db2->{copy} = $db->{hash1};
39     } qr/Cannot cross-reference\. Use export\(\) instead/, "cross-ref fails";
40     $db2->{copy} = $db->{hash1}->export;
41 }
42
43 ##
44 # Make sure $db2 has copy of $db's hash structure
45 ##
46 is( $db2->{copy}{subkey1}, 'subvalue1', "Value copied correctly" );
47 is( $db2->{copy}{subkey2}, 'subvalue2', "Value copied correctly" );