From: Tomas Doran Date: Thu, 23 Dec 2010 15:47:00 +0000 (+0000) Subject: Fix error reporting in duck_type X-Git-Tag: 2.0103~25 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=6ea852f8c8a254271541fef9ec233b436a85cdb9;p=gitmo%2FMoose.git Fix error reporting in duck_type --- diff --git a/Changes b/Changes index df3ff64..3e2e1ec 100644 --- a/Changes +++ b/Changes @@ -8,6 +8,11 @@ for, noteworthy changes. * Several things that have been deprecated for a while have been removed. See the 2.0000 section in Moose::Manual::Delta for details. + [BUG FIXES] + + * duck_type type constraints now report reasonable errors when given + something which isn't an instance of an object. (t0m) + 2.0102 Sat, Jun 18, 2011 [ENHANCEMENTS] diff --git a/lib/Moose/Meta/TypeConstraint/DuckType.pm b/lib/Moose/Meta/TypeConstraint/DuckType.pm index 0a8ef8a..8957156 100644 --- a/lib/Moose/Meta/TypeConstraint/DuckType.pm +++ b/lib/Moose/Meta/TypeConstraint/DuckType.pm @@ -98,6 +98,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; $class ||= $value; 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;