From: Jesse Luehrs Date: Thu, 16 Jun 2011 17:04:50 +0000 (-0500) Subject: make sure the correct delegation name is used in errors X-Git-Tag: 2.0102~10 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=53f4ad9f2ae455791a399329647c2ffc6f5ecfb4;p=gitmo%2FMoose.git make sure the correct delegation name is used in errors --- diff --git a/lib/Moose/Meta/Method/Accessor/Native/Hash/set.pm b/lib/Moose/Meta/Method/Accessor/Native/Hash/set.pm index 9bcbaba..7bbde96 100644 --- a/lib/Moose/Meta/Method/Accessor/Native/Hash/set.pm +++ b/lib/Moose/Meta/Method/Accessor/Native/Hash/set.pm @@ -34,7 +34,10 @@ around _inline_check_argument_count => sub { $self->$orig(@_), 'if (@_ % 2) {', $self->_inline_throw_error( - '"You must pass an even number of arguments to set"', + sprintf( + '"You must pass an even number of arguments to %s"', + $self->delegate_to_method, + ), ) . ';', '}', ); @@ -56,7 +59,10 @@ sub _inline_check_arguments { 'for (@keys_idx) {', 'if (!defined($_[$_])) {', $self->_inline_throw_error( - '"Hash keys passed to set must be defined"', + sprintf( + '"Hash keys passed to %s must be defined"', + $self->delegate_to_method, + ), ) . ';', '}', '}', diff --git a/t/native_traits/trait_hash.t b/t/native_traits/trait_hash.t index 36dd0be..a75a851 100644 --- a/t/native_traits/trait_hash.t +++ b/t/native_traits/trait_hash.t @@ -98,6 +98,18 @@ sub run_tests { ); }, undef, '... set the option okay' ); + like( + exception { $obj->set_option( foo => 'bar', 'baz' ) }, + qr/You must pass an even number of arguments to set/, + 'exception with odd number of arguments' + ); + + like( + exception { $obj->set_option( undef, 'bar' ) }, + qr/Hash keys passed to set must be defined/, + 'exception when using undef as a key' + ); + ok( $obj->is_defined('foo'), '... foo is defined' ); ok( !$obj->has_no_options, '... we have options' ); @@ -186,6 +198,12 @@ sub run_tests { 'error when calling accessor with no arguments' ); + like( + exception { $obj->option_accessor( undef, 'bar' ) }, + qr/Hash keys passed to accessor must be defined/, + 'exception when using undef as a key' + ); + is_deeply( $obj->options, { quantity => 4, size => 42 }, 'accessor as writer'