fix types with stringifyable TC objects
Matt S Trout [Wed, 3 Sep 2008 15:28:50 +0000 (15:28 +0000)]
lib/Moose/Util/TypeConstraints.pm
t/040_type_constraints/010_misc_type_tests.t

index ab1561d..129f8be 100644 (file)
@@ -213,21 +213,18 @@ sub find_or_create_does_type_constraint ($) {
 
 sub find_or_parse_type_constraint ($) {
     my $type_constraint_name = shift;
-
-    return $REGISTRY->get_type_constraint($type_constraint_name)
-        if $REGISTRY->has_type_constraint($type_constraint_name);
-
     my $constraint;
-
-    if (_detect_type_constraint_union($type_constraint_name)) {
+    
+    if ($constraint = find_type_constraint($type_constraint_name)) {
+        return $constraint;
+    } elsif (_detect_type_constraint_union($type_constraint_name)) {
         $constraint = create_type_constraint_union($type_constraint_name);
-    }
-    elsif (_detect_parameterized_type_constraint($type_constraint_name)) {
+    } elsif (_detect_parameterized_type_constraint($type_constraint_name)) {     
         $constraint = create_parameterized_type_constraint($type_constraint_name);
     } else {
         return;
     }
-
+    
     $REGISTRY->add_type_constraint($constraint);
     return $constraint;
 }
index 8e7a1cf..1aa0ba5 100644 (file)
@@ -3,7 +3,7 @@
 use strict;
 use warnings;
 
-use Test::More tests => 3;
+use Test::More tests => 8;
 use Test::Exception;
 
 BEGIN {
@@ -17,4 +17,30 @@ lives_ok {
 } '... create bare subtype fine';
 
 my $numb3rs = find_type_constraint('Numb3rs');
-isa_ok($numb3rs, 'Moose::Meta::TypeConstraint');
\ No newline at end of file
+isa_ok($numb3rs, 'Moose::Meta::TypeConstraint');
+
+# subtype with unions
+
+{
+    package Test::Moose::Meta::TypeConstraint::Union;
+    
+    use overload '""' => sub { 'Broken|Test' }, fallback => 1;
+    use Moose;
+    
+    extends 'Moose::Meta::TypeConstraint';
+}
+
+ok my $dummy_instance = Test::Moose::Meta::TypeConstraint::Union->new
+ => "Created Instance";
+
+isa_ok $dummy_instance, 'Test::Moose::Meta::TypeConstraint::Union'
+ => 'isa correct type';
+
+is "$dummy_instance", "Broken|Test"
+ => "Got expected stringification result";
+ok my $subtype1 = subtype('New1', as $dummy_instance)
+ => "made a subtype";
+ok my $subtype2 = subtype('New2', as $subtype1)
+ => "made another subtype";