From: Yuval Kogman <nothingmuch@woobling.org>
Date: Sat, 17 May 2008 20:01:09 +0000 (+0000)
Subject: Subtypes of parametrizables are parametrizable themselves
X-Git-Tag: 0_55~168
X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=36dbd1056e16ddb57ba6e461d3f5548c8c8863cb;p=gitmo%2FMoose.git

Subtypes of parametrizables are parametrizable themselves
---

diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm
index bf13b12..6c6ab16 100644
--- a/lib/Moose/Util/TypeConstraints.pm
+++ b/lib/Moose/Util/TypeConstraints.pm
@@ -383,9 +383,15 @@ sub _create_type_constraint ($$$;$$) {
                  if defined $type;
     }
 
-    $parent = find_or_parse_type_constraint($parent) if defined $parent;
+    my $class = "Moose::Meta::TypeConstraint";
 
-    my $constraint = Moose::Meta::TypeConstraint->new(
+    # FIXME should probably not be a special case
+    # FIXME also support metaclass/traits in TCs
+    if ( defined $parent and $parent = find_or_parse_type_constraint($parent) ) {
+        $class = "Moose::Meta::TypeConstraint::Parameterizable" if $parent->isa("Moose::Meta::TypeConstraint::Parameterizable");
+    }
+
+    my $constraint = $class->new(
         name               => $name || '__ANON__',
         package_defined_in => $pkg_defined_in,
 
diff --git a/t/040_type_constraints/016_subtyping_parameterized_types.t b/t/040_type_constraints/016_subtyping_parameterized_types.t
index 5c94290..221cf6a 100644
--- a/t/040_type_constraints/016_subtyping_parameterized_types.t
+++ b/t/040_type_constraints/016_subtyping_parameterized_types.t
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 21;
+use Test::More tests => 30;
 use Test::Exception;
 
 BEGIN {
@@ -60,3 +60,28 @@ lives_ok {
     ok(!$t->check({ one => "ONE", two => "TWO" }), '... validated it correctly');
 }
 
+lives_ok {
+    subtype 'MyNonSpecialHash'
+        => as "HashRef"
+        => where { keys %$_ == 3 };
+};
+
+{
+    my $t = find_type_constraint('MyNonSpecialHash');
+
+    isa_ok($t, 'Moose::Meta::TypeConstraint');
+    isa_ok($t, 'Moose::Meta::TypeConstraint::Parameterizable');
+
+    ok( $t->check({ one => 1, two => "foo", three => [] }), "validated" );
+    ok( !$t->check({ one => 1 }), "failed" );
+}
+
+{
+    my $t = Moose::Util::TypeConstraints::find_or_parse_type_constraint('MyNonSpecialHash[Int]');
+
+    isa_ok($t, 'Moose::Meta::TypeConstraint');
+
+    ok( $t->check({ one => 1, two => 2, three => 3 }), "validated" );
+    ok( !$t->check({ one => 1, two => "foo", three => [] }), "failed" );
+    ok( !$t->check({ one => 1 }), "failed" );
+}