Standardized test incantations
[dbsrgits/DBM-Deep.git] / t / 16_circular.t
CommitLineData
ffed8b01 1##
2# DBM::Deep Test
3##
4use strict;
1cff45d7 5use Test::More tests => 32;
fde3db1a 6use t::common qw( new_fh );
ffed8b01 7
8use_ok( 'DBM::Deep' );
9
fde3db1a 10my ($fh, $filename) = new_fh();
33d7395d 11my $db = DBM::Deep->new( $filename );
ffed8b01 12
13##
14# put/get simple keys
15##
16$db->{key1} = "value1";
17$db->{key2} = "value2";
18
1df992b3 19my @keys_1 = sort keys %$db;
20
21$db->{key3} = $db->{key1};
22
23my @keys_2 = sort keys %$db;
24is( @keys_2 + 0, @keys_1 + 1, "Correct number of keys" );
25is_deeply(
26 [ @keys_1, 'key3' ],
27 [ @keys_2 ],
28 "Keys still match after circular reference is added",
29);
30
8db25060 31$db->{key4} = { 'foo' => 'bar' };
75be6413 32$db->{key5} = $db->{key4};
8db25060 33$db->{key6} = $db->{key5};
1df992b3 34
35my @keys_3 = sort keys %$db;
1df992b3 36
8db25060 37is( @keys_3 + 0, @keys_2 + 3, "Correct number of keys" );
38is_deeply(
39 [ @keys_2, 'key4', 'key5', 'key6', ],
40 [ @keys_3 ],
41 "Keys still match after circular reference is added (@keys_3)",
42);
43
44##
45# Insert circular reference
46##
47$db->{circle} = $db;
48
49my @keys_4 = sort keys %$db;
50
51is( @keys_4 + 0, @keys_3 + 1, "Correct number of keys" );
52is_deeply(
53 [ 'circle', @keys_3 ],
54 [ @keys_4 ],
55 "Keys still match after circular reference is added",
56);
1df992b3 57
ffed8b01 58##
59# Make sure keys exist in both places
60##
61is( $db->{key1}, 'value1', "The value is there directly" );
62is( $db->{circle}{key1}, 'value1', "The value is there in one loop of the circle" );
63is( $db->{circle}{circle}{key1}, 'value1', "The value is there in two loops of the circle" );
64is( $db->{circle}{circle}{circle}{key1}, 'value1', "The value is there in three loops of the circle" );
65
66##
67# Make sure changes are reflected in both places
68##
69$db->{key1} = "another value";
70
eea0d863 71isnt( $db->{key3}, 'another value', "Simple scalars are copied by value" );
72
ffed8b01 73is( $db->{key1}, 'another value', "The value is there directly" );
74is( $db->{circle}{key1}, 'another value', "The value is there in one loop of the circle" );
75is( $db->{circle}{circle}{key1}, 'another value', "The value is there in two loops of the circle" );
76is( $db->{circle}{circle}{circle}{key1}, 'another value', "The value is there in three loops of the circle" );
77
78$db->{circle}{circle}{circle}{circle}{key1} = "circles";
79
80is( $db->{key1}, 'circles', "The value is there directly" );
81is( $db->{circle}{key1}, 'circles', "The value is there in one loop of the circle" );
82is( $db->{circle}{circle}{key1}, 'circles', "The value is there in two loops of the circle" );
83is( $db->{circle}{circle}{circle}{key1}, 'circles', "The value is there in three loops of the circle" );
8db25060 84
85is( $db->{key4}{foo}, 'bar' );
86is( $db->{key5}{foo}, 'bar' );
87is( $db->{key6}{foo}, 'bar' );
88
89$db->{key4}{foo2} = 'bar2';
90is( $db->{key4}{foo2}, 'bar2' );
91is( $db->{key5}{foo2}, 'bar2' );
92is( $db->{key6}{foo2}, 'bar2' );
93
94$db->{key4}{foo3} = 'bar3';
95is( $db->{key4}{foo3}, 'bar3' );
96is( $db->{key5}{foo3}, 'bar3' );
97is( $db->{key6}{foo3}, 'bar3' );
98
99$db->{key4}{foo4} = 'bar4';
100is( $db->{key4}{foo4}, 'bar4' );
101is( $db->{key5}{foo4}, 'bar4' );
102is( $db->{key6}{foo4}, 'bar4' );
103