From: Justin Hunter Date: Mon, 19 Oct 2009 16:33:44 +0000 (-0700) Subject: add some checks to methods that require args X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9c3bb078b74a5885e36a595637783e9985985ea1;p=gitmo%2FMoose.git add some checks to methods that require args --- diff --git a/lib/Moose/Meta/Attribute/Native/MethodProvider/Hash.pm b/lib/Moose/Meta/Attribute/Native/MethodProvider/Hash.pm index 460e428..8abe318 100644 --- a/lib/Moose/Meta/Attribute/Native/MethodProvider/Hash.pm +++ b/lib/Moose/Meta/Attribute/Native/MethodProvider/Hash.pm @@ -7,12 +7,18 @@ our $AUTHORITY = 'cpan:STEVAN'; sub exists : method { my ( $attr, $reader, $writer ) = @_; - return sub { CORE::exists $reader->( $_[0] )->{ $_[1] } ? 1 : 0 }; + return sub { + $attr->associated_class->throw_error('One argument expected') if @_ == 1; + CORE::exists $reader->( $_[0] )->{ $_[1] } ? 1 : 0 + }; } sub defined : method { my ( $attr, $reader, $writer ) = @_; - return sub { CORE::defined $reader->( $_[0] )->{ $_[1] } ? 1 : 0 }; + return sub { + $attr->associated_class->throw_error('One argument expected') if @_ == 1; + CORE::defined $reader->( $_[0] )->{ $_[1] } ? 1 : 0 + }; } sub get : method { @@ -23,6 +29,7 @@ sub get : method { } else { my ( $self, @keys ) = @_; + $attr->associated_class->throw_error('One or more arguments expected') unless @keys; @{ $reader->($self) }{@keys}; } }; @@ -171,6 +178,7 @@ sub delete : method { my ( $attr, $reader, $writer ) = @_; return sub { my $hashref = $reader->(shift); + $attr->associated_class->throw_error('One or more arguments expected') unless @_; CORE::delete @{$hashref}{@_}; }; } diff --git a/t/070_native_traits/203_trait_hash.t b/t/070_native_traits/203_trait_hash.t index a7d77f4..962f80d 100644 --- a/t/070_native_traits/203_trait_hash.t +++ b/t/070_native_traits/203_trait_hash.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 46; +use Test::More tests => 50; use Test::Exception; use Test::Moose 'does_ok'; @@ -54,6 +54,10 @@ is( $stuff->num_options, 0, '... we have no options' ); is_deeply( $stuff->options, {}, '... no options yet' ); ok( !$stuff->has_option('foo'), '... we have no foo option' ); +dies_ok { + $stuff->has_option; +} +'... could not determinne exists without a key'; lives_ok { $stuff->set_option( foo => 'bar' ); @@ -61,6 +65,10 @@ lives_ok { '... set the option okay'; ok( $stuff->is_defined('foo'), '... foo is defined' ); +dies_ok { + $stuff->is_defined +} +'... could not determine defined without a key'; ok( !$stuff->has_no_options, '... we have options' ); is( $stuff->num_options, 1, '... we have 1 option(s)' ); @@ -103,6 +111,11 @@ lives_ok { } '... deleted multiple option okay'; +dies_ok { + $stuff->delete_option; +} +'... deleted with no option fails ok'; + is( $stuff->num_options, 1, '... we have 1 option(s)' ); is_deeply( $stuff->options, { foo => 'bar' }, '... got more options now' ); @@ -136,6 +149,11 @@ dies_ok { } '... bad constructor params'; +dies_ok { + $stuff->get_option; +} +'... could not get an element without a key'; + ## test the meta my $options = $stuff->meta->get_attribute('options');