From: John Napiorkowski Date: Tue, 28 Dec 2010 17:15:13 +0000 (-0500) Subject: fix for bug and prep for cpan X-Git-Tag: 0.25~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?p=gitmo%2FMooseX-Types-Structured.git;a=commitdiff_plain;h=8c18714858905491191a133640ad8fab1be876f0 fix for bug and prep for cpan --- diff --git a/Changes b/Changes index 8bc9dbd..0a45e20 100644 --- a/Changes +++ b/Changes @@ -1,5 +1,12 @@ Revision history for MooseX-Types-Structured +0.25 28 December 2010 + - fixed bug where ->is_subtype_of dies meaninglessly when the type we + are trying to check is not a type we can find. This makes our + handling consistent with core Moose. Also changed ->equals and + ->is_a_type_of to be consistent. + - Added test case for above + 0.24 16 November 2010 - Added some performance enhancing caching code (phaeton) diff --git a/dist.ini b/dist.ini index 3e66564..882e33c 100644 --- a/dist.ini +++ b/dist.ini @@ -1,5 +1,5 @@ name = MooseX-Types-Structured -version = 0.24 +version = 0.25 author = John Napiorkowski author = Florian Ragwitz author = Yuval Kogman diff --git a/lib/MooseX/Meta/TypeConstraint/Structured.pm b/lib/MooseX/Meta/TypeConstraint/Structured.pm index 0db946f..a770be0 100644 --- a/lib/MooseX/Meta/TypeConstraint/Structured.pm +++ b/lib/MooseX/Meta/TypeConstraint/Structured.pm @@ -197,7 +197,8 @@ Override the base class behavior. sub equals { my ( $self, $type_or_name ) = @_; - my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); + my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name) + or return; return unless $other->isa(__PACKAGE__); @@ -210,7 +211,8 @@ sub equals { sub is_a_type_of { my ( $self, $type_or_name ) = @_; - my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); + my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name) + or return; if ( $other->isa(__PACKAGE__) and @{ $other->type_constraints || [] }) { if ( $self->parent->is_a_type_of($other->parent) ) { @@ -228,7 +230,8 @@ sub is_a_type_of { sub is_subtype_of { my ( $self, $type_or_name ) = @_; - my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name); + my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name) + or return; if ( $other->isa(__PACKAGE__) ) { if ( $other->type_constraints and $self->type_constraints ) { if ( $self->parent->is_a_type_of($other->parent) ) { diff --git a/t/bug-is-subtype.t b/t/bug-is-subtype.t new file mode 100644 index 0000000..996cb0c --- /dev/null +++ b/t/bug-is-subtype.t @@ -0,0 +1,85 @@ +use strict; +use warnings; +use Test::More; + +## Bug report was that if calling ->is_subtype on crap (not a type, etc) you +## get a not very helpful error message. Fix was to make crap just return +## boolean false to make this like the rest of Moose type constraints. I am +## not convinced this is good, but at least is consistent. +# +# I also changed ->equals and ->is_a_type_of to be consistent + +{ + package moosex::types::structured::bug_is_subtype; + + use Moose; + use MooseX::Types -declare => [qw/ ThingType /]; + use MooseX::Types::Moose qw/ Int Str /; + use MooseX::Types::Structured qw/ Dict /; + + subtype ThingType, as Dict [ id => Int, name => Str, ]; + has thing => ( is => 'ro', isa => ThingType, ); +} + +ok my $test = moosex::types::structured::bug_is_subtype->new, + 'created class'; + +is( + moosex::types::structured::bug_is_subtype::ThingType, + 'moosex::types::structured::bug_is_subtype::ThingType', + 'correct type', +); + +use MooseX::Types::Moose 'HashRef'; + +is( + HashRef, + 'HashRef', + 'correct type', +); + +ok( + moosex::types::structured::bug_is_subtype::ThingType->is_subtype_of(HashRef), + 'is a subtype', +); + +ok( + !moosex::types::structured::bug_is_subtype::ThingType + ->is_subtype_of(moosex::types::structured::bug_is_subtype::ThingType), + 'is not a subtype', +); + +ok( + !moosex::types::structured::bug_is_subtype::ThingType + ->is_subtype_of('SomeCrap'), + 'is not a subtype', +); + +sub SomeCrap {} + +ok( + !moosex::types::structured::bug_is_subtype::ThingType + ->is_subtype_of(SomeCrap), + 'is not a subtype', +); + +ok( + !moosex::types::structured::bug_is_subtype::ThingType + ->is_subtype_of(undef), + 'is not a subtype', +); + +ok( + !moosex::types::structured::bug_is_subtype::ThingType + ->equal(undef), + 'is not a subtype', +); + +ok( + !moosex::types::structured::bug_is_subtype::ThingType + ->is_a_type_of(undef), + 'is not a subtype', +); + + +done_testing;