5 use Test::More tests => 32;
6 use File::Temp qw( tempfile tempdir );
10 my $dir = tempdir( CLEANUP => 1 );
11 my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
12 my $db = DBM::Deep->new( $filename );
17 $db->{key1} = "value1";
18 $db->{key2} = "value2";
20 my @keys_1 = sort keys %$db;
22 $db->{key3} = $db->{key1};
24 my @keys_2 = sort keys %$db;
25 is( @keys_2 + 0, @keys_1 + 1, "Correct number of keys" );
29 "Keys still match after circular reference is added",
32 $db->{key4} = { 'foo' => 'bar' };
33 $db->{key5} = $db->{key4};
34 $db->{key6} = $db->{key5};
36 my @keys_3 = sort keys %$db;
38 is( @keys_3 + 0, @keys_2 + 3, "Correct number of keys" );
40 [ @keys_2, 'key4', 'key5', 'key6', ],
42 "Keys still match after circular reference is added (@keys_3)",
46 # Insert circular reference
50 my @keys_4 = sort keys %$db;
52 is( @keys_4 + 0, @keys_3 + 1, "Correct number of keys" );
54 [ 'circle', @keys_3 ],
56 "Keys still match after circular reference is added",
60 # Make sure keys exist in both places
62 is( $db->{key1}, 'value1', "The value is there directly" );
63 is( $db->{circle}{key1}, 'value1', "The value is there in one loop of the circle" );
64 is( $db->{circle}{circle}{key1}, 'value1', "The value is there in two loops of the circle" );
65 is( $db->{circle}{circle}{circle}{key1}, 'value1', "The value is there in three loops of the circle" );
68 # Make sure changes are reflected in both places
70 $db->{key1} = "another value";
72 isnt( $db->{key3}, 'another value', "Simple scalars are copied by value" );
74 is( $db->{key1}, 'another value', "The value is there directly" );
75 is( $db->{circle}{key1}, 'another value', "The value is there in one loop of the circle" );
76 is( $db->{circle}{circle}{key1}, 'another value', "The value is there in two loops of the circle" );
77 is( $db->{circle}{circle}{circle}{key1}, 'another value', "The value is there in three loops of the circle" );
79 $db->{circle}{circle}{circle}{circle}{key1} = "circles";
81 is( $db->{key1}, 'circles', "The value is there directly" );
82 is( $db->{circle}{key1}, 'circles', "The value is there in one loop of the circle" );
83 is( $db->{circle}{circle}{key1}, 'circles', "The value is there in two loops of the circle" );
84 is( $db->{circle}{circle}{circle}{key1}, 'circles', "The value is there in three loops of the circle" );
86 is( $db->{key4}{foo}, 'bar' );
87 is( $db->{key5}{foo}, 'bar' );
88 is( $db->{key6}{foo}, 'bar' );
90 $db->{key4}{foo2} = 'bar2';
91 is( $db->{key4}{foo2}, 'bar2' );
92 is( $db->{key5}{foo2}, 'bar2' );
93 is( $db->{key6}{foo2}, 'bar2' );
95 $db->{key4}{foo3} = 'bar3';
96 is( $db->{key4}{foo3}, 'bar3' );
97 is( $db->{key5}{foo3}, 'bar3' );
98 is( $db->{key6}{foo3}, 'bar3' );
100 $db->{key4}{foo4} = 'bar4';
101 is( $db->{key4}{foo4}, 'bar4' );
102 is( $db->{key5}{foo4}, 'bar4' );
103 is( $db->{key6}{foo4}, 'bar4' );