X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F02_hash.t;h=6e9972a3941706748270b33cca5aa8deb9258784;hb=6e78585cfdedc2754b455639da5cc877d7f25cab;hp=3f1969fb5f53f0eebb611d2f8af7c110f1bf6a78;hpb=075910edd08e4ee071dcf8b0abbde2ac00cc0daa;p=dbsrgits%2FDBM-Deep.git diff --git a/t/02_hash.t b/t/02_hash.t index 3f1969f..6e9972a 100644 --- a/t/02_hash.t +++ b/t/02_hash.t @@ -2,16 +2,17 @@ # DBM::Deep Test ## use strict; -use Test::More tests => 44; +use Test::More tests => 49; use Test::Exception; +use t::common qw( new_fh ); use_ok( 'DBM::Deep' ); -unlink "t/test.db"; -my $db = DBM::Deep->new( "t/test.db" ); -if ($db->error()) { - die "ERROR: " . $db->error(); -} +my ($fh, $filename) = new_fh(); +my $db = DBM::Deep->new( + file => $filename, + fh => $fh, +); ## # put/get key @@ -31,9 +32,27 @@ is( $db->get("key3"), "value3", "get() works with store()" ); is( $db->fetch("key3"), "value3", "... fetch() works with put()" ); is( $db->{key3}, 'value3', "... and hash-access also works" ); +# Verify that the keyval pairs are still correct. +is( $db->{key1}, "value1", "Key1 is still correct" ); +is( $db->{key2}, undef, "Key2 is still correct" ); +is( $db->{key3}, 'value3', "Key3 is still correct" ); + ok( $db->exists("key1"), "exists() function works" ); ok( exists $db->{key2}, "exists() works against tied hash" ); +ok( !exists $db->{key4}, "exists() function works for keys that aren't there" ); +is( $db->{key4}, undef, "Autovivified key4" ); +ok( exists $db->{key4}, "Autovivified key4 now exists" ); + +delete $db->{key4}; +ok( !exists $db->{key4}, "And key4 doesn't exists anymore" ); + +# Keys will be done via an iterator that keeps a breadcrumb trail of the last +# key it provided. There will also be an "edit revision number" on the +# reference so that resetting the iterator can be done. +# +# Q: How do we make sure that the iterator is unique? Is it supposed to be? + ## # count keys ## @@ -44,7 +63,7 @@ is( scalar keys %$db, 3, "keys() works against tied hash" ); ## my $temphash = {}; while ( my ($key, $value) = each %$db ) { - $temphash->{$key} = $value; + $temphash->{$key} = $value; } is( $temphash->{key1}, 'value1', "First key copied successfully using tied interface" ); @@ -54,8 +73,8 @@ is( $temphash->{key3}, 'value3', "Third key copied successfully" ); $temphash = {}; my $key = $db->first_key(); while ($key) { - $temphash->{$key} = $db->get($key); - $key = $db->next_key($key); + $temphash->{$key} = $db->get($key); + $key = $db->next_key($key); } is( $temphash->{key1}, 'value1', "First key copied successfully using OO interface" ); @@ -65,11 +84,11 @@ is( $temphash->{key3}, 'value3', "Third key copied successfully" ); ## # delete keys ## -TODO: { - local $TODO = "Delete should return the deleted value"; - is( delete $db->{key1}, 'value1', "delete through tied inteface works" ); - is( $db->delete("key2"), undef, "delete through OO inteface works" ); -} +is( delete $db->{key2}, undef, "delete through tied inteface works" ); +is( $db->delete("key1"), 'value1', "delete through OO inteface works" ); +is( $db->{key3}, 'value3', "The other key is still there" ); +ok( !exists $db->{key1}, "key1 doesn't exist" ); +ok( !exists $db->{key2}, "key2 doesn't exist" ); is( scalar keys %$db, 1, "After deleting two keys, 1 remains" ); @@ -90,17 +109,17 @@ $db->put("key1", "value2"); is( $db->get("key1"), "value2", "... and replacement works" ); $db->put("key1", "value222222222222222222222222"); - is( $db->get("key1"), "value222222222222222222222222", "We set a value before closing the file" ); ## # Make sure DB still works after closing / opening ## undef $db; -$db = DBM::Deep->new( "t/test.db" ); -if ($db->error()) { - die "ERROR: " . $db->error(); -} +open $fh, '+<', $filename; +$db = DBM::Deep->new( + file => $filename, + fh => $fh, +); is( $db->get("key1"), "value222222222222222222222222", "The value we set is still there after closure" ); ## @@ -118,67 +137,47 @@ my $first_key = $db->first_key(); my $next_key = $db->next_key($first_key); ok( - (($first_key eq "key1") || ($first_key eq "key2")) && - (($next_key eq "key1") || ($next_key eq "key2")) && - ($first_key ne $next_key) + (($first_key eq "key1") || ($first_key eq "key2")) && + (($next_key eq "key1") || ($next_key eq "key2")) && + ($first_key ne $next_key) ,"keys() still works if you replace long values with shorter ones" ); -# These tests verify that the array methods cannot be called on hashtypes. -# They will be removed once the ARRAY and HASH types are refactored into their own classes. - -throws_ok { - $db->splice(); -} qr/SPLICE method only supported for arrays/, "Cannot call splice on a hash type"; - -throws_ok { - $db->SPLICE(); -} qr/SPLICE method only supported for arrays/, "Cannot call SPLICE on a hash type"; - -throws_ok { - $db->length(); -} qr/FETCHSIZE method only supported for arrays/, "Cannot call length on a hash type"; - -throws_ok { - $db->FETCHSIZE(); -} qr/FETCHSIZE method only supported for arrays/, "Cannot call FETCHSIZE on a hash type"; - -throws_ok { - $db->STORESIZE(); -} qr/STORESIZE method only supported for arrays/, "Cannot call STORESIZE on a hash type"; +# Test autovivification +$db->{unknown}{bar} = 1; +ok( $db->{unknown}, 'Autovivified hash exists' ); +cmp_ok( $db->{unknown}{bar}, '==', 1, 'And the value stored is there' ); +# Test failures throws_ok { - $db->POP(); -} qr/POP method only supported for arrays/, "Cannot call POP on a hash type"; + $db->fetch(); +} qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key"; throws_ok { - $db->pop(); -} qr/POP method only supported for arrays/, "Cannot call pop on a hash type"; + $db->fetch(undef); +} qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key"; throws_ok { - $db->PUSH(); -} qr/PUSH method only supported for arrays/, "Cannot call PUSH on a hash type"; + $db->store(); +} qr/Cannot use an undefined hash key/, "STORE fails on an undefined key"; throws_ok { - $db->push(); -} qr/PUSH method only supported for arrays/, "Cannot call push on a hash type"; + $db->store(undef, undef); +} qr/Cannot use an undefined hash key/, "STORE fails on an undefined key"; throws_ok { - $db->SHIFT(); -} qr/SHIFT method only supported for arrays/, "Cannot call SHIFT on a hash type"; + $db->delete(); +} qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key"; throws_ok { - $db->shift(); -} qr/SHIFT method only supported for arrays/, "Cannot call shift on a hash type"; + $db->delete(undef); +} qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key"; throws_ok { - $db->UNSHIFT(); -} qr/UNSHIFT method only supported for arrays/, "Cannot call UNSHIFT on a hash type"; + $db->exists(); +} qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key"; throws_ok { - $db->unshift(); -} qr/UNSHIFT method only supported for arrays/, "Cannot call unshift on a hash type"; + $db->exists(undef); +} qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key"; -ok( $db->error, "We have an error ..." ); -$db->clear_error(); -ok( !$db->error(), "... and we cleared the error" );