From: rkinyon Date: Tue, 21 Feb 2006 19:57:10 +0000 (+0000) Subject: Cleared up some more TODO tests X-Git-Tag: 0-97~29 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=e1b265cc1bea88e4a5f67ff274ec9e40b83fc559;p=dbsrgits%2FDBM-Deep.git Cleared up some more TODO tests --- diff --git a/lib/DBM/Deep/Array.pm b/lib/DBM/Deep/Array.pm index 1a6b549..34d6a8c 100644 --- a/lib/DBM/Deep/Array.pm +++ b/lib/DBM/Deep/Array.pm @@ -4,6 +4,8 @@ use strict; use base 'DBM::Deep'; +use Scalar::Util (); + sub _get_self { eval { tied( @{$_[0]} ) } || $_[0] } @@ -20,9 +22,15 @@ sub TIEARRAY { } $args = {@_}; } - #XXX This use of ref() is bad and is a bug - elsif (ref($_[0])) { $args = $_[0]; } - else { $args = { file => shift }; } + elsif ( my $type = Scalar::Util::reftype($_[0]) ) { + if ( $type ne 'HASH' ) { + $class->_throw_error( "Not a hashref in TIEARRAY" ); + } + $args = $_[0]; + } + else { + $args = { file => shift }; + } $args->{type} = $class->TYPE_ARRAY; diff --git a/lib/DBM/Deep/Hash.pm b/lib/DBM/Deep/Hash.pm index e315a5d..baceab1 100644 --- a/lib/DBM/Deep/Hash.pm +++ b/lib/DBM/Deep/Hash.pm @@ -20,8 +20,12 @@ sub TIEHASH { } $args = {@_}; } - #XXX This use of ref() is bad and is a bug - elsif (ref($_[0])) { $args = $_[0]; } + elsif ( my $type = Scalar::Util::reftype($_[0]) ) { + if ( $type ne 'HASH' ) { + $class->_throw_error( "Not a hashref in TIEHASH" ); + } + $args = $_[0]; + } else { $args = { file => shift }; } $args->{type} = $class->TYPE_HASH; diff --git a/t/20_tie.t b/t/20_tie.t index c6e3a2a..151e28a 100644 --- a/t/20_tie.t +++ b/t/20_tie.t @@ -70,40 +70,15 @@ use_ok( 'DBM::Deep' ); is( $db->{type}, DBM::Deep->TYPE_ARRAY, "TIE_ARRAY sets the correct type" ); } -# These are testing the naive use of ref() within TIEHASH and TIEARRAY. -# They should be doing (Scalar::Util::reftype($_[0]) eq 'HASH') and then -# erroring out if it's not. -TODO: { - todo_skip( "Naive use of {\@_}", 1 ); - unlink "t/test.db"; - my %hash; - my $db = tie %hash, 'DBM::Deep', [ - file => 't/test.db', - ]; - - if ($db->error()) { - print "ERROR: " . $db->error(); - ok(0); - exit(0); - } - else { ok(1); } -} - -TODO: { - todo_skip( "Naive use of {\@_}", 1 ); - unlink "t/test.db"; - my @array; - my $db = tie @array, 'DBM::Deep', [ - file => 't/test.db', - ]; +unlink "t/test.db"; +throws_ok { + tie my %hash, 'DBM::Deep', [ file => 't/test.db' ]; +} qr/Not a hashref/, "Passing an arrayref to TIEHASH fails"; - if ($db->error()) { - print "ERROR: " . $db->error(); - ok(0); - exit(0); - } - else { ok(1); } -} +unlink "t/test.db"; +throws_ok { + tie my @array, 'DBM::Deep', [ file => 't/test.db' ]; +} qr/Not a hashref/, "Passing an arrayref to TIEARRAY fails"; unlink "t/test.db"; throws_ok {