5 use Test::More tests => 32;
6 use t::common qw( new_fh );
10 my ($fh, $filename) = new_fh();
11 my $db = DBM::Deep->new( $filename );
16 $db->{key1} = "value1";
17 $db->{key2} = "value2";
19 my @keys_1 = sort keys %$db;
21 $db->{key3} = $db->{key1};
23 my @keys_2 = sort keys %$db;
24 is( @keys_2 + 0, @keys_1 + 1, "Correct number of keys" );
28 "Keys still match after circular reference is added",
31 $db->{key4} = { 'foo' => 'bar' };
32 $db->{key5} = $db->{key4};
33 $db->{key6} = $db->{key5};
35 my @keys_3 = sort keys %$db;
37 is( @keys_3 + 0, @keys_2 + 3, "Correct number of keys" );
39 [ @keys_2, 'key4', 'key5', 'key6', ],
41 "Keys still match after circular reference is added (@keys_3)",
45 # Insert circular reference
49 my @keys_4 = sort keys %$db;
51 is( @keys_4 + 0, @keys_3 + 1, "Correct number of keys" );
53 [ 'circle', @keys_3 ],
55 "Keys still match after circular reference is added",
59 # Make sure keys exist in both places
61 is( $db->{key1}, 'value1', "The value is there directly" );
62 is( $db->{circle}{key1}, 'value1', "The value is there in one loop of the circle" );
63 is( $db->{circle}{circle}{key1}, 'value1', "The value is there in two loops of the circle" );
64 is( $db->{circle}{circle}{circle}{key1}, 'value1', "The value is there in three loops of the circle" );
67 # Make sure changes are reflected in both places
69 $db->{key1} = "another value";
71 isnt( $db->{key3}, 'another value', "Simple scalars are copied by value" );
73 is( $db->{key1}, 'another value', "The value is there directly" );
74 is( $db->{circle}{key1}, 'another value', "The value is there in one loop of the circle" );
75 is( $db->{circle}{circle}{key1}, 'another value', "The value is there in two loops of the circle" );
76 is( $db->{circle}{circle}{circle}{key1}, 'another value', "The value is there in three loops of the circle" );
78 $db->{circle}{circle}{circle}{circle}{key1} = "circles";
80 is( $db->{key1}, 'circles', "The value is there directly" );
81 is( $db->{circle}{key1}, 'circles', "The value is there in one loop of the circle" );
82 is( $db->{circle}{circle}{key1}, 'circles', "The value is there in two loops of the circle" );
83 is( $db->{circle}{circle}{circle}{key1}, 'circles', "The value is there in three loops of the circle" );
85 is( $db->{key4}{foo}, 'bar' );
86 is( $db->{key5}{foo}, 'bar' );
87 is( $db->{key6}{foo}, 'bar' );
89 $db->{key4}{foo2} = 'bar2';
90 is( $db->{key4}{foo2}, 'bar2' );
91 is( $db->{key5}{foo2}, 'bar2' );
92 is( $db->{key6}{foo2}, 'bar2' );
94 $db->{key4}{foo3} = 'bar3';
95 is( $db->{key4}{foo3}, 'bar3' );
96 is( $db->{key5}{foo3}, 'bar3' );
97 is( $db->{key6}{foo3}, 'bar3' );
99 $db->{key4}{foo4} = 'bar4';
100 is( $db->{key4}{foo4}, 'bar4' );
101 is( $db->{key5}{foo4}, 'bar4' );
102 is( $db->{key6}{foo4}, 'bar4' );