added key to _get_subloc after figuring out the correct unpack magic
[dbsrgits/DBM-Deep.git] / t / 16_circular.t
CommitLineData
ffed8b01 1##
2# DBM::Deep Test
3##
4use strict;
1df992b3 5use Test::More tests => 19;
2a81bf9e 6use File::Temp qw( tempfile tempdir );
ffed8b01 7
8use_ok( 'DBM::Deep' );
9
2a81bf9e 10my $dir = tempdir( CLEANUP => 1 );
11my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
12my $db = DBM::Deep->new( $filename );
ffed8b01 13
14##
15# put/get simple keys
16##
17$db->{key1} = "value1";
18$db->{key2} = "value2";
19
1df992b3 20my @keys_1 = sort keys %$db;
21
22$db->{key3} = $db->{key1};
23
24my @keys_2 = sort keys %$db;
25is( @keys_2 + 0, @keys_1 + 1, "Correct number of keys" );
26is_deeply(
27 [ @keys_1, 'key3' ],
28 [ @keys_2 ],
29 "Keys still match after circular reference is added",
30);
31
32$db->{key4} = {};
75be6413 33$db->{key5} = $db->{key4};
1df992b3 34
35my @keys_3 = sort keys %$db;
1df992b3 36
75be6413 37TODO: {
38 local $TODO = "Need to fix how internal references are stored";
39 is( @keys_3 + 0, @keys_2 + 2, "Correct number of keys" );
40 is_deeply(
41 [ @keys_2, 'key4', 'key5' ],
42 [ @keys_3 ],
43 "Keys still match after circular reference is added (@keys_3)",
44 );
ffed8b01 45
75be6413 46 ##
47 # Insert circular reference
48 ##
49 $db->{circle} = $db;
1df992b3 50
75be6413 51 my @keys_4 = sort keys %$db;
52 print "@keys_4\n";
53
54 is( @keys_4 + 0, @keys_3 + 1, "Correct number of keys" );
55 is_deeply(
56 [ '[base]', @keys_3 ],
57 [ @keys_4 ],
58 "Keys still match after circular reference is added",
59 );
60}
1df992b3 61
ffed8b01 62##
63# Make sure keys exist in both places
64##
65is( $db->{key1}, 'value1', "The value is there directly" );
66is( $db->{circle}{key1}, 'value1', "The value is there in one loop of the circle" );
67is( $db->{circle}{circle}{key1}, 'value1', "The value is there in two loops of the circle" );
68is( $db->{circle}{circle}{circle}{key1}, 'value1', "The value is there in three loops of the circle" );
69
70##
71# Make sure changes are reflected in both places
72##
73$db->{key1} = "another value";
74
75is( $db->{key1}, 'another value', "The value is there directly" );
76is( $db->{circle}{key1}, 'another value', "The value is there in one loop of the circle" );
77is( $db->{circle}{circle}{key1}, 'another value', "The value is there in two loops of the circle" );
78is( $db->{circle}{circle}{circle}{key1}, 'another value', "The value is there in three loops of the circle" );
79
80$db->{circle}{circle}{circle}{circle}{key1} = "circles";
81
82is( $db->{key1}, 'circles', "The value is there directly" );
83is( $db->{circle}{key1}, 'circles', "The value is there in one loop of the circle" );
84is( $db->{circle}{circle}{key1}, 'circles', "The value is there in two loops of the circle" );
85is( $db->{circle}{circle}{circle}{key1}, 'circles', "The value is there in three loops of the circle" );