From: Dave Rolsky Date: Thu, 2 Jul 2009 20:42:53 +0000 (-0500) Subject: Simplify the logic to throw an error on an undef or ! blessed delegatee. X-Git-Tag: 0.86~1^2~5 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6148c16743ccbfbe259f2b93f239ac5775a218e0;p=gitmo%2FMoose.git Simplify the logic to throw an error on an undef or ! blessed delegatee. Add a test for undef in the delegatee slot. --- diff --git a/lib/Moose/Meta/Method/Delegation.pm b/lib/Moose/Meta/Method/Delegation.pm index aa146f4..04cab20 100644 --- a/lib/Moose/Meta/Method/Delegation.pm +++ b/lib/Moose/Meta/Method/Delegation.pm @@ -79,24 +79,23 @@ sub _initialize_body { $self->{body} = sub { my $instance = shift; my $proxy = $instance->$accessor(); - ( defined $proxy ) - || $self->throw_error( - "Cannot delegate $handle_name to $method_to_call because " - . "the value of " - . $self->associated_attribute->name - . " is not defined", - method_name => $method_to_call, - object => $instance - ); - ( blessed $proxy ) - || $self->throw_error( - "Cannot delegate $handle_name to $method_to_call because " - . "the value of " - . $self->associated_attribute->name - . " is not an object (got '$proxy')", - method_name => $method_to_call, - object => $instance + + my $error + = !defined $proxy ? ' is not defined' + : !blessed $proxy ? qq{ is not an object (got '$proxy')} + : undef; + + if ($error) { + $self->throw_error( + "Cannot delegate $handle_name to $method_to_call because " + . "the value of " + . $self->associated_attribute->name + . $error, + method_name => $method_to_call, + object => $instance ); + } + $proxy->$method_to_call(@_); }; } diff --git a/t/020_attributes/010_attribute_delegation.t b/t/020_attributes/010_attribute_delegation.t index 84890b9..eb05ac7 100644 --- a/t/020_attributes/010_attribute_delegation.t +++ b/t/020_attributes/010_attribute_delegation.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 89; +use Test::More tests => 90; use Test::Exception; @@ -413,7 +413,11 @@ is($car->stop, 'Engine::stop', '... got the right value from ->stop'); # Make sure that a useful error message is thrown when the delegation target is # not an object { - my $i = Bar->new(foo => []); - throws_ok { $i->foo_bar } qr/is not an object \(got 'ARRAY/, + my $i = Bar->new(foo => undef); + throws_ok { $i->foo_bar } qr/is not defined/, + 'useful error from unblessed reference'; + + my $j = Bar->new(foo => []); + throws_ok { $j->foo_bar } qr/is not an object \(got 'ARRAY/, 'useful error from unblessed reference'; }