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