fix is_subtype_of for unregistered class types
Jesse Luehrs [Sun, 18 Sep 2011 00:25:13 +0000 (19:25 -0500)]
Changes
lib/Moose/Meta/TypeConstraint/Class.pm
t/type_constraints/class_type_constraint.t

diff --git a/Changes b/Changes
index f1c2e19..586e22c 100644 (file)
--- a/Changes
+++ b/Changes
@@ -58,6 +58,10 @@ for, noteworthy changes.
     'does' parameter to attribute construction will now register the type. This
     means that it cannot later be redefined as something else. (doy)
 
+  * $class_type->is_subtype_of no longer returns true if passed the name of the
+    class that the class type represents when the class type wasn't registered.
+    (doy)
+
   [OTHER]
 
   * The Class::MOP::load_class and Class::MOP::is_class_loaded subroutines are
index 5d66bc6..1be980c 100644 (file)
@@ -63,7 +63,13 @@ sub equals {
 
     my $other = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
 
-    return unless defined $other;
+    if (!defined($other)) {
+        if (!ref($type_or_name)) {
+            return $self->class eq $type_or_name;
+        }
+        return;
+    }
+
     return unless $other->isa(__PACKAGE__);
 
     return $self->class eq $other->class;
@@ -72,9 +78,7 @@ sub equals {
 sub is_a_type_of {
     my ($self, $type_or_name) = @_;
 
-    my $type = Moose::Util::TypeConstraints::find_type_constraint($type_or_name);
-
-    ($self->equals($type) || $self->is_subtype_of($type_or_name));
+    ($self->equals($type_or_name) || $self->is_subtype_of($type_or_name));
 }
 
 sub is_subtype_of {
@@ -85,7 +89,9 @@ sub is_subtype_of {
     if ( not defined $type ) {
         if ( not ref $type_or_name_or_class ) {
             # it might be a class
-            return 1 if $self->class->isa( $type_or_name_or_class );
+            my $class = $self->class;
+            return 1 if $class ne $type_or_name_or_class
+                     && $class->isa( $type_or_name_or_class );
         }
         return;
     }
index d47cbae..082f7f9 100644 (file)
@@ -58,4 +58,38 @@ ok( $type->equals(Moose::Meta::TypeConstraint::Class->new( name => "Oink", class
 ok( !$type->equals(Moose::Meta::TypeConstraint::Class->new( name => "__ANON__", class => "Bar" )), "doesn't equal other anon constraint" );
 ok( $type->is_subtype_of(Moose::Meta::TypeConstraint::Class->new( name => "__ANON__", class => "Bar" )), "subtype of other anon constraint" );
 
+{
+    package Parent;
+    sub parent { }
+}
+
+{
+    package Child;
+    use base 'Parent';
+}
+
+{
+    my $parent = Moose::Meta::TypeConstraint::Class->new(
+        name  => 'Parent',
+        class => 'Parent',
+    );
+    ok($parent->is_a_type_of('Parent'));
+    ok(!$parent->is_subtype_of('Parent'));
+    ok($parent->is_a_type_of($parent));
+    ok(!$parent->is_subtype_of($parent));
+
+    my $child = Moose::Meta::TypeConstraint::Class->new(
+        name  => 'Child',
+        class => 'Child',
+    );
+    ok($child->is_a_type_of('Child'));
+    ok(!$child->is_subtype_of('Child'));
+    ok($child->is_a_type_of($child));
+    ok(!$child->is_subtype_of($child));
+    ok($child->is_a_type_of('Parent'));
+    ok($child->is_subtype_of('Parent'));
+    ok($child->is_a_type_of($parent));
+    ok($child->is_subtype_of($parent));
+}
+
 done_testing;