From: rkinyon Date: Thu, 18 Jan 2007 15:15:51 +0000 (+0000) Subject: r14847@rob-kinyons-computer: rob | 2007-01-17 22:13:19 -0500 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=845d37e8e71e2117c9f500fbd1c4c85b20d593f4;p=dbsrgits%2FDBM-Deep.git r14847@rob-kinyons-computer: rob | 2007-01-17 22:13:19 -0500 Added gatekeepers to array methods to verify only legal keys are used --- diff --git a/lib/DBM/Deep/Array.pm b/lib/DBM/Deep/Array.pm index 817aaa1..fe96af0 100644 --- a/lib/DBM/Deep/Array.pm +++ b/lib/DBM/Deep/Array.pm @@ -30,6 +30,7 @@ sub _import { return 1; } + sub TIEARRAY { my $class = shift; my $args = $class->_get_args( @_ ); @@ -54,7 +55,9 @@ sub FETCH { } } } - else { + elsif ( $key ne 'length' ) { + $self->unlock; + DBM::Deep->_throw_error( "Cannot use '$key' as an array index." ); } my $rv = $self->SUPER::FETCH( $key ); @@ -82,8 +85,12 @@ sub STORE { $key += $size } } + elsif ( $key ne 'length' ) { + $self->unlock; + DBM::Deep->_throw_error( "Cannot use '$key' as an array index." ); + } - my $rv = $self->SUPER::STORE( $key, $value, ($key eq 'length' ? undef : $key) ); + my $rv = $self->SUPER::STORE( $key, $value ); if ( $idx_is_numeric ) { $size = $self->FETCHSIZE unless defined $size; @@ -112,6 +119,10 @@ sub EXISTS { } } } + elsif ( $key ne 'length' ) { + $self->unlock; + DBM::Deep->_throw_error( "Cannot use '$key' as an array index." ); + } my $rv = $self->SUPER::EXISTS( $key ); @@ -136,11 +147,15 @@ sub DELETE { } } } + elsif ( $key ne 'length' ) { + $self->unlock; + DBM::Deep->_throw_error( "Cannot use '$key' as an array index." ); + } my $rv = $self->SUPER::DELETE( $key ); if ($rv && $key == $size - 1) { - $self->STORESIZE( $key, ($key eq 'length' ? undef : $key) ); + $self->STORESIZE( $key ); } $self->unlock; @@ -337,7 +352,7 @@ sub SPLICE { return wantarray ? @old_elements : $old_elements[-1]; } -# We don't need to define it, yet. +# We don't need to populate it, yet. # It will be useful, though, when we split out HASH and ARRAY sub EXTEND { ## diff --git a/t/04_array.t b/t/04_array.t index 300a63b..a77fd70 100644 --- a/t/04_array.t +++ b/t/04_array.t @@ -2,7 +2,7 @@ # DBM::Deep Test ## use strict; -use Test::More tests => 108; +use Test::More tests => 112; use Test::Exception; use t::common qw( new_fh ); @@ -200,8 +200,25 @@ $db->[0] = [ 1 .. 3 ]; $db->[1] = { a => 'foo' }; is( $db->[0]->length, 3, "Reuse of same space with array successful" ); is( $db->[1]->fetch('a'), 'foo', "Reuse of same space with hash successful" ); -# Test autovivification +# Test autovivification $db->[9999]{bar} = 1; ok( $db->[9999] ); cmp_ok( $db->[9999]{bar}, '==', 1 ); + +# Test failures +throws_ok { + $db->fetch( 'foo' ); +} qr/Cannot use 'foo' as an array index/, "FETCH fails on an illegal key"; + +throws_ok { + $db->store( 'foo', 'bar' ); +} qr/Cannot use 'foo' as an array index/, "STORE fails on an illegal key"; + +throws_ok { + $db->delete( 'foo' ); +} qr/Cannot use 'foo' as an array index/, "STORE fails on an illegal key"; + +throws_ok { + $db->exists( 'foo' ); +} qr/Cannot use 'foo' as an array index/, "STORE fails on an illegal key";