X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F800_shikabased%2F014-subtype-as.t;fp=t%2F800_shikabased%2F014-subtype-as.t;h=cf9e40e11fbf6f5b211d0c489e72940480e5101e;hb=1fbefea51e3234a987284705fc9c6cf34758a2a0;hp=0000000000000000000000000000000000000000;hpb=323adb6f1da08d840e1e37fd54da7c8001eb37f1;p=gitmo%2FMouse.git diff --git a/t/800_shikabased/014-subtype-as.t b/t/800_shikabased/014-subtype-as.t new file mode 100644 index 0000000..cf9e40e --- /dev/null +++ b/t/800_shikabased/014-subtype-as.t @@ -0,0 +1,47 @@ +use strict; +use warnings; +use Test::More tests => 6; +use Scalar::Util qw/blessed/; + +{ + package Obj1; + sub new { bless {}, shift }; +} +{ + package Obj2; + use overload '""' => sub { 'Ref' }, fallback => 1; + sub new { bless {}, shift }; +} + +{ + package Foo; + use Mouse; + use Mouse::TypeRegistry; + + subtype 'Type1' => as 'Str' => where { blessed($_) }; + has str_obj => ( + is => 'rw', + isa => 'Type1', + ); + + subtype 'Type2' => as 'Object' => where { $_ eq 'Ref' }; + has obj_str => ( + is => 'rw', + isa => 'Type2', + ); +} + +eval { Foo->new( str_obj => Obj1->new ) }; +like $@, qr/Attribute \(str_obj\) does not pass the type constraint because: Validation failed for 'Type1' failed with value Obj1=HASH/; +eval { Foo->new( obj_str => Obj1->new ) }; +like $@, qr/Attribute \(obj_str\) does not pass the type constraint because: Validation failed for 'Type2' failed with value Obj1=HASH/; + +eval { Foo->new( str_obj => Obj2->new ) }; +like $@, qr/Attribute \(str_obj\) does not pass the type constraint because: Validation failed for 'Type1' failed with value Obj2=HASH/; + +eval { Foo->new( str_obj => 'Ref' ) }; +like $@, qr/Attribute \(str_obj\) does not pass the type constraint because: Validation failed for 'Type1' failed with value Ref/; + +my $f1 = eval { Foo->new( obj_str => Obj2->new ) }; +isa_ok $f1, 'Foo'; +is $f1->obj_str, 'Ref';