From: rkinyon Date: Thu, 18 Jan 2007 15:16:39 +0000 (+0000) Subject: r14849@rob-kinyons-computer: rob | 2007-01-17 22:19:30 -0500 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=c7a4e071084617ff0de22eb2f5e8ecc4e0e06b59;p=dbsrgits%2FDBM-Deep.git r14849@rob-kinyons-computer: rob | 2007-01-17 22:19:30 -0500 Added tets for undefined and missing hash keys --- diff --git a/lib/DBM/Deep/Hash.pm b/lib/DBM/Deep/Hash.pm index 461bd2f..4b3d1d4 100644 --- a/lib/DBM/Deep/Hash.pm +++ b/lib/DBM/Deep/Hash.pm @@ -41,6 +41,7 @@ sub TIEHASH { sub FETCH { my $self = shift->_get_self; + DBM::Deep->_throw_error( "Cannot use an undefined hash key." ) unless defined $_[0]; my $key = ($self->_storage->{filter_store_key}) ? $self->_storage->{filter_store_key}->($_[0]) : $_[0]; @@ -50,6 +51,7 @@ sub FETCH { sub STORE { my $self = shift->_get_self; + DBM::Deep->_throw_error( "Cannot use an undefined hash key." ) unless defined $_[0]; my $key = ($self->_storage->{filter_store_key}) ? $self->_storage->{filter_store_key}->($_[0]) : $_[0]; @@ -60,6 +62,7 @@ sub STORE { sub EXISTS { my $self = shift->_get_self; + DBM::Deep->_throw_error( "Cannot use an undefined hash key." ) unless defined $_[0]; my $key = ($self->_storage->{filter_store_key}) ? $self->_storage->{filter_store_key}->($_[0]) : $_[0]; @@ -69,6 +72,7 @@ sub EXISTS { sub DELETE { my $self = shift->_get_self; + DBM::Deep->_throw_error( "Cannot use an undefined hash key." ) unless defined $_[0]; my $key = ($self->_storage->{filter_store_key}) ? $self->_storage->{filter_store_key}->($_[0]) : $_[0]; diff --git a/t/02_hash.t b/t/02_hash.t index 78ee2cb..59495ff 100644 --- a/t/02_hash.t +++ b/t/02_hash.t @@ -2,7 +2,7 @@ # DBM::Deep Test ## use strict; -use Test::More tests => 41; +use Test::More tests => 49; use Test::Exception; use t::common qw( new_fh ); @@ -140,3 +140,37 @@ ok( $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->fetch(); +} qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key"; + +throws_ok { + $db->fetch(undef); +} qr/Cannot use an undefined hash key/, "FETCH fails on an undefined key"; + +throws_ok { + $db->store(); +} qr/Cannot use an undefined hash key/, "STORE fails on an undefined key"; + +throws_ok { + $db->store(undef, undef); +} qr/Cannot use an undefined hash key/, "STORE fails on an undefined key"; + +throws_ok { + $db->delete(); +} qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key"; + +throws_ok { + $db->delete(undef); +} qr/Cannot use an undefined hash key/, "DELETE fails on an undefined key"; + +throws_ok { + $db->exists(); +} qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key"; + +throws_ok { + $db->exists(undef); +} qr/Cannot use an undefined hash key/, "EXISTS fails on an undefined key"; + diff --git a/t/04_array.t b/t/04_array.t index 341e53e..a3f9ce3 100644 --- a/t/04_array.t +++ b/t/04_array.t @@ -221,21 +221,21 @@ throws_ok { throws_ok { $db->store(); -} qr/Cannot use an undefined array index/, "FETCH fails on an undefined key"; +} qr/Cannot use an undefined array index/, "STORE fails on an undefined key"; throws_ok { $db->delete( 'foo' ); -} qr/Cannot use 'foo' as an array index/, "STORE fails on an illegal key"; +} qr/Cannot use 'foo' as an array index/, "DELETE fails on an illegal key"; throws_ok { $db->delete(); -} qr/Cannot use an undefined array index/, "FETCH fails on an undefined key"; +} qr/Cannot use an undefined array index/, "DELETE fails on an undefined key"; throws_ok { $db->exists( 'foo' ); -} qr/Cannot use 'foo' as an array index/, "STORE fails on an illegal key"; +} qr/Cannot use 'foo' as an array index/, "EXISTS fails on an illegal key"; throws_ok { $db->exists(); -} qr/Cannot use an undefined array index/, "FETCH fails on an undefined key"; +} qr/Cannot use an undefined array index/, "EXISTS fails on an undefined key"; diff --git a/t/TODO b/t/TODO index 9042281..4e7ae62 100644 --- a/t/TODO +++ b/t/TODO @@ -28,11 +28,6 @@ This is to verify that the appropriate errors are thrown For some reason, $c doesn't seem to be undefinable in _copy_value. Maybe this means that the bless()ing should occur iff Cisa('DBM::Deep')>? -=item * OO Array access with illegal keys - -There's a ton of tests that can be written here to verify the gatekeepers in -the array methods. - =item * Splice =over 4