From: gfx Date: Thu, 24 Sep 2009 03:01:55 +0000 (+0900) Subject: Fix class_type X-Git-Tag: 0.35~33^2~7 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a497c7d3c518bbecf930e3f17d7a75b9bf84fa2f;p=gitmo%2FMouse.git Fix class_type --- diff --git a/lib/Mouse/Util/TypeConstraints.pm b/lib/Mouse/Util/TypeConstraints.pm index 9755115..8694163 100644 --- a/lib/Mouse/Util/TypeConstraints.pm +++ b/lib/Mouse/Util/TypeConstraints.pm @@ -6,7 +6,8 @@ use base 'Exporter'; use Carp (); use Scalar::Util qw/blessed looks_like_number openhandle/; -use Mouse::Util; +use Mouse::Util qw(does_role); +use Mouse::Meta::Module; # class_of use Mouse::Meta::TypeConstraint; our @EXPORT = qw( @@ -215,10 +216,11 @@ sub class_type { if ($conf && $conf->{class}) { # No, you're using this wrong warn "class_type() should be class_type(ClassName). Perhaps you're looking for subtype $name => as '$conf->{class}'?"; - subtype($name, as => $conf->{class}); - } else { - subtype( - $name => where => sub { $_->isa($name) } + subtype $name, as => $conf->{class}; + } + else { + subtype $name => ( + where => sub { blessed($_) && $_->isa($name) }, ); } } @@ -226,11 +228,8 @@ sub class_type { sub role_type { my($name, $conf) = @_; my $role = $conf->{role}; - subtype( - $name => where => sub { - return unless defined $_ && ref($_) && $_->isa('Mouse::Object'); - $_->meta->does_role($role); - } + subtype $name => ( + $name => where => sub { does_role($_, $role) }, ); } diff --git a/t/043-parameterized-type.t b/t/043-parameterized-type.t index 8c20411..a7eae99 100644 --- a/t/043-parameterized-type.t +++ b/t/043-parameterized-type.t @@ -1,11 +1,19 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 9; +use Test::More tests => 16; use Test::Exception; { { + package My::Role; + use Mouse::Role; + + package My::Class; + use Mouse; + + with 'My::Role'; + package Foo; use Mouse; @@ -19,10 +27,20 @@ use Test::Exception; isa => 'ArrayRef[Int]', ); - has 'complex' => ( - is => 'rw', + has complex => ( + is => 'rw', isa => 'ArrayRef[HashRef[Int]]' ); + + has my_class => ( + is => 'rw', + isa => 'ArrayRef[My::Class]', + ); + + has my_role => ( + is => 'rw', + isa => 'ArrayRef[My::Role]', + ); }; ok(Foo->meta->has_attribute('foo')); @@ -36,6 +54,14 @@ use Test::Exception; is_deeply($foo->foo(), $hash, "foo is a proper hash"); is_deeply($foo->bar(), $array, "bar is a proper array"); is_deeply($foo->complex(), $complex, "complex is a proper ... structure"); + + $foo->my_class([My::Class->new]); + is ref($foo->my_class), 'ARRAY'; + isa_ok $foo->my_class->[0], 'My::Class'; + + $foo->my_role([My::Class->new]); + is ref($foo->my_role), 'ARRAY'; + } "Parameterized constraints work"; # check bad args @@ -50,6 +76,21 @@ use Test::Exception; throws_ok { Foo->new( complex => [ { a => 1, b => 1 }, { c => "d", e => "f" } ] ) } qr/Attribute \(complex\) does not pass the type constraint because: Validation failed for 'ArrayRef\[HashRef\[Int\]\]' failed with value/, "Bad args for complex types throws an exception"; + + throws_ok { + Foo->new( my_class => [ 10 ] ); + } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' failed with value/; + throws_ok { + Foo->new( my_class => [ {foo => 'bar'} ] ); + } qr/Attribute \(my_class\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Class\]' failed with value/; + + + throws_ok { + Foo->new( my_role => [ 20 ] ); + } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' failed with value/; + throws_ok { + Foo->new( my_role => [ {foo => 'bar'} ] ); + } qr/Attribute \(my_role\) does not pass the type constraint because: Validation failed for 'ArrayRef\[My::Role\]' failed with value/; } {