2 use warnings FATAL => 'all';
6 use t::common qw( new_dbm );
7 use Scalar::Util qw( reftype );
11 my $dbm_factory = new_dbm();
12 while ( my $dbm_maker = $dbm_factory->() ) {
13 my $db = $dbm_maker->();
18 $db->{key1} = "value1";
19 is( $db->get("key1"), "value1", "get() works with hash assignment" );
20 is( $db->fetch("key1"), "value1", "... fetch() works with hash assignment" );
21 is( $db->{key1}, "value1", "... and hash-access also works" );
23 $db->put("key2", undef);
24 is( $db->get("key2"), undef, "get() works with put()" );
25 is( $db->fetch("key2"), undef, "... fetch() works with put()" );
26 is( $db->{key2}, undef, "... and hash-access also works" );
28 $db->store( "0", "value3" );
29 is( $db->get("0"), "value3", "get() works with store()" );
30 is( $db->fetch("0"), "value3", "... fetch() works with put()" );
31 is( $db->{0}, 'value3', "... and hash-access also works" );
33 # Verify that the keyval pairs are still correct.
34 is( $db->{key1}, "value1", "Key1 is still correct" );
35 is( $db->{key2}, undef, "Key2 is still correct" );
36 is( $db->{0}, 'value3', "Key3 is still correct" );
38 ok( $db->exists("key1"), "exists() function works" );
39 ok( exists $db->{key2}, "exists() works against tied hash" );
41 ok( !exists $db->{key4}, "exists() function works for keys that aren't there" );
42 is( $db->{key4}, undef, "Autovivified key4" );
43 ok( exists $db->{key4}, "Autovivified key4 now exists" );
46 ok( !exists $db->{key4}, "And key4 doesn't exists anymore" );
48 # Keys will be done via an iterator that keeps a breadcrumb trail of the last
49 # key it provided. There will also be an "edit revision number" on the
50 # reference so that resetting the iterator can be done.
52 # Q: How do we make sure that the iterator is unique? Is it supposed to be?
57 is( scalar keys %$db, 3, "keys() works against tied hash" );
63 while ( my ($key, $value) = each %$db ) {
64 $temphash->{$key} = $value;
67 is( $temphash->{key1}, 'value1', "First key copied successfully using tied interface" );
68 is( $temphash->{key2}, undef, "Second key copied successfully" );
69 is( $temphash->{0}, 'value3', "Third key copied successfully" );
72 my $key = $db->first_key();
73 while (defined $key) {
74 $temphash->{$key} = $db->get($key);
75 $key = $db->next_key($key);
78 is( $temphash->{key1}, 'value1', "First key copied successfully using OO interface" );
79 is( $temphash->{key2}, undef, "Second key copied successfully" );
80 is( $temphash->{0}, 'value3', "Third key copied successfully" );
85 is( delete $db->{key2}, undef, "delete through tied inteface works" );
86 is( $db->delete("key1"), 'value1', "delete through OO inteface works" );
87 is( $db->{0}, 'value3', "The other key is still there" );
88 ok( !exists $db->{key1}, "key1 doesn't exist" );
89 ok( !exists $db->{key2}, "key2 doesn't exist" );
91 is( scalar keys %$db, 1, "After deleting two keys, 1 remains" );
96 ok( $db->clear(), "clear() returns true" );
98 is( scalar keys %$db, 0, "After clear(), everything is removed" );
103 $db->put("key1", "value1");
104 is( $db->get("key1"), "value1", "Assignment still works" );
106 $db->put("key1", "value2");
107 is( $db->get("key1"), "value2", "... and replacement works" );
109 $db->put("key1", "value222222222222222222222222");
110 is( $db->get("key1"), "value222222222222222222222222", "We set a value before closing the file" );
113 # Make sure DB still works after closing / opening
116 $db = $dbm_maker->();
117 is( $db->get("key1"), "value222222222222222222222222", "The value we set is still there after closure" );
120 # Make sure keys are still fetchable after replacing values
121 # with smaller ones (bug found by John Cardenas, DBM::Deep 0.93)
124 $db->put("key1", "long value here");
125 $db->put("key2", "longer value here");
127 $db->put("key1", "short value");
128 $db->put("key2", "shorter v");
130 my $first_key = $db->first_key();
131 my $next_key = $db->next_key($first_key);
134 (($first_key eq "key1") || ($first_key eq "key2")) &&
135 (($next_key eq "key1") || ($next_key eq "key2")) &&
136 ($first_key ne $next_key)
137 ,"keys() still works if you replace long values with shorter ones"
140 # Test autovivification
141 $db->{unknown}{bar} = 1;
142 ok( $db->{unknown}, 'Autovivified hash exists' );
143 is( reftype($db->{unknown}), 'HASH', "... and it's a HASH" );
144 cmp_ok( $db->{unknown}{bar}, '==', 1, 'And the value stored is there' );
149 } qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key";
153 } qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key";
157 } qr/Cannot use an undefined hash key/, "STORE fails on an undefined key";
160 $db->store(undef, undef);
161 } qr/Cannot use an undefined hash key/, "STORE fails on an undefined key";
165 } qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key";
169 } qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key";
173 } qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key";
177 } qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key";
181 # RT# 50541 (reported by Peter Scott)
182 # clear() leaves one key unless there's only one
183 my $dbm_factory = new_dbm();
184 while ( my $dbm_maker = $dbm_factory->() ) {
185 my $db = $dbm_maker->();
188 $db->{critical} = { };
191 cmp_ok( scalar(keys( %$db )), '==', 3, "Have 3 keys" );
195 cmp_ok( scalar(keys( %$db )), '==', 0, "clear clears everything" );