merged
John Napiorkowski [Tue, 28 Dec 2010 17:17:21 +0000 (12:17 -0500)]
Changes
dist.ini
lib/MooseX/Meta/TypeConstraint/Structured.pm
t/bug-is-subtype.t [new file with mode: 0644]

diff --git a/Changes b/Changes
index 798103e..0ea7aec 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,6 +1,11 @@
 Revision history for MooseX-Types-Structured
 
-{{NEXT}}
+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
         - The test suite now uses Test::Fatal instead of Test::Exception (Karen
           Etheridge).
 
index cffd919..20d6f63 100644 (file)
--- a/dist.ini
+++ b/dist.ini
@@ -1,5 +1,5 @@
 name    = MooseX-Types-Structured
-version = 0.24
+version = 0.25
 author  = John Napiorkowski <jjnapiork@cpan.org>
 author  = Florian Ragwitz <rafl@debian.org>
 author  = Yuval Kogman <nothingmuch@woobling.org>
index 0db946f..a770be0 100644 (file)
@@ -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 (file)
index 0000000..996cb0c
--- /dev/null
@@ -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;