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