better test for coercing parameterized type, now passes
Rafael Kitover [Sat, 22 Aug 2009 15:49:09 +0000 (11:49 -0400)]
Changes
lib/MooseX/Types.pm
t/21_coerce_parameterized_types.t

diff --git a/Changes b/Changes
index 65c96ee..412812d 100644 (file)
--- a/Changes
+++ b/Changes
@@ -1,5 +1,7 @@
 Revision history for MooseX-Types
 
+        - fix coercions on parameterized types (Rafael Kitover)
+
 0.17    Tue Aug 18 02:32:31 EDT 2009
         - Documentation typo fix (Dave Rolsky).
         - Stop blowing up in has_available_type_export if the introspected code
index 4516728..9528d36 100644 (file)
@@ -425,9 +425,16 @@ it with @args.
 =cut
 
 sub create_arged_type_constraint {
-    my ($class, $name, @args) = @_;  
-    my $type_constraint = Moose::Util::TypeConstraints::find_or_create_type_constraint("$name");
-    return $type_constraint->parameterize(@args);
+    my ($class, $name, $arg) = @_;  
+
+    my $container_tc =
+       Moose::Util::TypeConstraints::find_or_create_type_constraint("$name");
+    my $contained_tc =
+       Moose::Util::TypeConstraints::find_or_create_type_constraint("$arg");
+
+    my $tc_name = $container_tc->name . '[' . $contained_tc->name .  ']';
+
+    return Moose::Util::TypeConstraints::find_or_create_type_constraint($tc_name);
 }
 
 =head2 create_base_type_constraint ($name)
index dbbd5d2..08df149 100644 (file)
@@ -7,32 +7,36 @@ use Test::More tests => 2;
 
 BEGIN {
     package TypeLib;
-    use MooseX::Types -declare => [qw/MyType ArrayRefOfMyType/];
-    use MooseX::Types::Moose qw/ArrayRef Str/;
+    use MooseX::Types -declare => [qw/MyChar MyDigit ArrayRefOfMyCharOrDigit/];
+    use MooseX::Types::Moose qw/ArrayRef Str Int/;
 
-    subtype MyType, as Str, where {
+    subtype MyChar, as Str, where {
        length == 1
     };
 
-    coerce ArrayRef[MyType], from Str, via {
+    subtype MyDigit, as Int, where {
+       length == 1
+    };
+
+    coerce ArrayRef[MyChar|MyDigit], from Str, via {
        [split //]
     };
 
 # same thing with an explicit subtype
-    subtype ArrayRefOfMyType, as ArrayRef[MyType];
+    subtype ArrayRefOfMyCharOrDigit, as ArrayRef[MyChar|MyDigit];
 
-    coerce ArrayRefOfMyType, from Str, via {
+    coerce ArrayRefOfMyCharOrDigit, from Str, via {
        [split //]
     };
 }
 {
     package AClass;
     use Moose;
-    BEGIN { TypeLib->import(qw/MyType ArrayRefOfMyType/) };
+    BEGIN { TypeLib->import(qw/MyChar MyDigit ArrayRefOfMyCharOrDigit/) };
     use MooseX::Types::Moose 'ArrayRef';
 
-    has parameterized => (is => 'rw', isa => ArrayRef[MyType], coerce => 1);
-    has subtype => (is => 'rw', isa => ArrayRefOfMyType, coerce => 1);
+    has parameterized => (is => 'rw', isa => ArrayRef[MyChar|MyDigit], coerce => 1);
+    has subtype => (is => 'rw', isa => ArrayRefOfMyCharOrDigit, coerce => 1);
 }
 
 my $instance = AClass->new;