Add more words to the spelling whitelist
[gitmo/Moose.git] / t / type_constraints / class_type_constraint.t
index d47cbae..419ab58 100644 (file)
@@ -58,4 +58,59 @@ ok( $type->equals(Moose::Meta::TypeConstraint::Class->new( name => "Oink", class
 ok( !$type->equals(Moose::Meta::TypeConstraint::Class->new( name => "__ANON__", class => "Bar" )), "doesn't equal other anon constraint" );
 ok( $type->is_subtype_of(Moose::Meta::TypeConstraint::Class->new( name => "__ANON__", class => "Bar" )), "subtype of other anon constraint" );
 
+{
+    package Parent;
+    sub parent { }
+}
+
+{
+    package Child;
+    use base 'Parent';
+}
+
+{
+    my $parent = Moose::Meta::TypeConstraint::Class->new(
+        name  => 'Parent',
+        class => 'Parent',
+    );
+    ok($parent->is_a_type_of('Parent'));
+    ok(!$parent->is_subtype_of('Parent'));
+    ok($parent->is_a_type_of($parent));
+    ok(!$parent->is_subtype_of($parent));
+
+    my $child = Moose::Meta::TypeConstraint::Class->new(
+        name  => 'Child',
+        class => 'Child',
+    );
+    ok($child->is_a_type_of('Child'));
+    ok(!$child->is_subtype_of('Child'));
+    ok($child->is_a_type_of($child));
+    ok(!$child->is_subtype_of($child));
+    ok($child->is_a_type_of('Parent'));
+    ok($child->is_subtype_of('Parent'));
+    ok($child->is_a_type_of($parent));
+    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;