X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F040_type_constraints%2F020_class_type_constraint.t;h=0ae13485b41fc5dfa98778a9c75f146ba1e6d02c;hb=e018ef42963899146ba9b3b2140cfdb2906d8ce5;hp=628dd19317cc25e668644cd2fc58ed687b07a4e5;hpb=28412c0b280c30cfd1eac1579b9f5953b5e1850e;p=gitmo%2FMoose.git diff --git a/t/040_type_constraints/020_class_type_constraint.t b/t/040_type_constraints/020_class_type_constraint.t index 628dd19..0ae1348 100644 --- a/t/040_type_constraints/020_class_type_constraint.t +++ b/t/040_type_constraints/020_class_type_constraint.t @@ -3,10 +3,11 @@ use strict; use warnings; -use Test::More tests => 7; +use Test::More; +use Test::Exception; BEGIN { - use_ok('Moose::Util::TypeConstraints'); + use_ok('Moose::Util::TypeConstraints'); } { @@ -20,17 +21,46 @@ BEGIN { use Moose; extends qw(Bar Gorch); + } +lives_ok { class_type 'Beep' } 'class_type keywork works'; +lives_ok { class_type('Boop', message { "${_} is not a Boop" }) } + 'class_type keywork works with message'; + my $type = find_type_constraint("Foo"); +is( $type->class, "Foo", "class attribute" ); + ok( $type->is_subtype_of("Gorch"), "subtype of gorch" ); ok( $type->is_subtype_of("Bar"), "subtype of bar" ); ok( $type->is_subtype_of("Object"), "subtype of Object" ); +ok( !$type->is_subtype_of("ThisTypeDoesNotExist"), "not subtype of undefined type" ); +ok( !$type->is_a_type_of("ThisTypeDoesNotExist"), "not type of undefined type" ); + ok( find_type_constraint("Bar")->check(Foo->new), "Foo passes Bar" ); ok( find_type_constraint("Bar")->check(Bar->new), "Bar passes Bar" ); ok( !find_type_constraint("Gorch")->check(Bar->new), "but Bar doesn't pass Gorch"); +ok( find_type_constraint("Beep")->check( bless {} => 'Beep' ), "Beep passes Beep" ); +my $boop = find_type_constraint("Boop"); +ok( $boop->has_message, 'Boop has a message'); +my $error = $boop->get_message(Foo->new); +like( $error, qr/is not a Boop/, 'boop gives correct error message'); + + +ok( $type->equals($type), "equals self" ); +ok( $type->equals(Moose::Meta::TypeConstraint::Class->new( name => "__ANON__", class => "Foo" )), "equals anon constraint of same value" ); +ok( $type->equals(Moose::Meta::TypeConstraint::Class->new( name => "Oink", class => "Foo" )), "equals differently named constraint of same value" ); +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" ); + +{ + my $regexp_type = Moose::Meta::TypeConstraint::Class->new(name => 'Regexp', class => 'Regexp'); + ok(!$regexp_type->check(qr//), 'a Regexp is not an instance of a class, even tho perl pretends it is'); +} + +done_testing;