From: Matt S Trout Date: Wed, 3 Sep 2008 15:28:50 +0000 (+0000) Subject: fix types with stringifyable TC objects X-Git-Tag: 0.57~12 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=9c434902c00a8e890cdc68655b5209a06439db02;p=gitmo%2FMoose.git fix types with stringifyable TC objects --- diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index ab1561d..129f8be 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -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; } diff --git a/t/040_type_constraints/010_misc_type_tests.t b/t/040_type_constraints/010_misc_type_tests.t index 8e7a1cf..1aa0ba5 100644 --- a/t/040_type_constraints/010_misc_type_tests.t +++ b/t/040_type_constraints/010_misc_type_tests.t @@ -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";