From: Tomas Doran Date: Thu, 23 Dec 2010 15:47:00 +0000 (+0000) Subject: Fix error reporting in duck_type X-Git-Tag: 2.0009~16 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4887f7e33128975b3b05e5cd2f83daced21d1cd7;p=gitmo%2FMoose.git Fix error reporting in duck_type --- diff --git a/Changes b/Changes index 6384434..d859284 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,11 @@ for, noteworthy changes. {{$NEXT}} + [BUG FIXES] + + * duck_type type constraints now report reasonable errors when given + something which isn't an instance of an object. (t0m) + 2.0008 Thu, Jun 16, 2011 [BUG FIXES] diff --git a/lib/Moose/Meta/TypeConstraint/DuckType.pm b/lib/Moose/Meta/TypeConstraint/DuckType.pm index 2878eed..b9bfbab 100644 --- a/lib/Moose/Meta/TypeConstraint/DuckType.pm +++ b/lib/Moose/Meta/TypeConstraint/DuckType.pm @@ -89,6 +89,8 @@ sub get_message { return $self->SUPER::get_message(@_); } + return $self->SUPER::get_message($value) unless blessed($value); + my @methods = grep { !$value->can($_) } @{ $self->methods }; my $class = blessed $value; return $class diff --git a/t/type_constraints/duck_types.t b/t/type_constraints/duck_types.t index cc12852..f0763c8 100644 --- a/t/type_constraints/duck_types.t +++ b/t/type_constraints/duck_types.t @@ -76,4 +76,11 @@ is( exception { DucktypeTest->new( duck => RubberDuck->new ) }, undef, 'the Rubb # try with the other constraint form is( exception { DucktypeTest->new( other_swan => Swan->new ) }, undef, 'but a Swan can honk' ); +my $re = qr/Validation failed for 'DuckType' with value/; + +like( exception { DucktypeTest->new( duck => undef ) }, $re, 'Exception for undef' ); +like( exception { DucktypeTest->new( duck => [] ) }, $re, 'Exception for arrayref' ); +like( exception { DucktypeTest->new( duck => {} ) }, $re, 'Exception for hashref' ); +like( exception { DucktypeTest->new( duck => \'foo' ) }, $re, 'Exception for scalar ref' ); + done_testing;