From: Tomas Doran Date: Thu, 15 Dec 2011 17:02:07 +0000 (+0000) Subject: Fix bug with multiple class_type statements. X-Git-Tag: 2.0500~90 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=0f9a0b95605b8f5d8bec1c42bc84dad667c4a0ed;p=gitmo%2FMoose.git Fix bug with multiple class_type statements. We would previously override the existing class_type with a new one, thereby losing any coercions that were defined on the previous class_type. This bug was introduced in commit 6f49644548 --- diff --git a/Changes b/Changes index e8c8e1d..f9f9ccb 100644 --- a/Changes +++ b/Changes @@ -3,6 +3,13 @@ for, noteworthy changes. {{$NEXT}} + [BUG FIXES] + + * Re-declaring a class_type constraint that has been already declared + now just returns the original type constraint, rather than replacing + the original constraint and ergo losing any coercions that were on + the original constraint. (t0m) + 2.0402 Sat, Feb 04, 2012 [OTHER] diff --git a/lib/Moose/Util/TypeConstraints.pm b/lib/Moose/Util/TypeConstraints.pm index 81f4d9f..bcd9f97 100644 --- a/lib/Moose/Util/TypeConstraints.pm +++ b/lib/Moose/Util/TypeConstraints.pm @@ -165,6 +165,9 @@ sub create_class_type_constraint { . " and cannot be created again in " . $pkg_defined_in ) } + else { + return $type; + } } my %options = ( diff --git a/t/type_constraints/class_type_constraint.t b/t/type_constraints/class_type_constraint.t index 082f7f9..419ab58 100644 --- a/t/type_constraints/class_type_constraint.t +++ b/t/type_constraints/class_type_constraint.t @@ -92,4 +92,25 @@ ok( $type->is_subtype_of(Moose::Meta::TypeConstraint::Class->new( name => "__ANO ok($child->is_subtype_of($parent)); } +{ + my $type; + is( exception { $type = class_type 'MyExampleClass' }, undef, 'Make initial class_type' ); + coerce 'MyExampleClass', from 'Str', via { bless {}, 'MyExampleClass' }; + # We test class_type keeping the existing type (not making a new one) here. + is( exception { is(class_type('MyExampleClass'), $type, 're-running class_type gives same type') }, undef, 'No exception making duplicate class_type' );; + + # Next define a class which needs this type and it's original coercion + # Note this has to be after the 2nd class_type call to test the bug as M::M::Attribute grabs + # the type constraint which is there at the time the attribute decleration runs. + { + package HoldsExample; + use Moose; + + has foo => ( isa => 'MyExampleClass', is => 'ro', coerce => 1, required => 1 ); + no Moose; + } + + is( exception { isa_ok(HoldsExample->new(foo => "bar")->foo, 'MyExampleClass') }, undef, 'class_type coercion works' ); +} + done_testing;