pre load class in class_type
[gitmo/Mouse.git] / t / 800_shikabased / 014-subtype-as.t
CommitLineData
1fbefea5 1use strict;
2use warnings;
3use Test::More tests => 6;
4use Scalar::Util qw/blessed/;
5
6{
7 package Obj1;
8 sub new { bless {}, shift };
9}
10{
11 package Obj2;
12 use overload '""' => sub { 'Ref' }, fallback => 1;
13 sub new { bless {}, shift };
14}
15
16{
17 package Foo;
18 use Mouse;
19 use Mouse::TypeRegistry;
20
21 subtype 'Type1' => as 'Str' => where { blessed($_) };
22 has str_obj => (
23 is => 'rw',
24 isa => 'Type1',
25 );
26
27 subtype 'Type2' => as 'Object' => where { $_ eq 'Ref' };
28 has obj_str => (
29 is => 'rw',
30 isa => 'Type2',
31 );
32}
33
34eval { Foo->new( str_obj => Obj1->new ) };
35like $@, qr/Attribute \(str_obj\) does not pass the type constraint because: Validation failed for 'Type1' failed with value Obj1=HASH/;
36eval { Foo->new( obj_str => Obj1->new ) };
37like $@, qr/Attribute \(obj_str\) does not pass the type constraint because: Validation failed for 'Type2' failed with value Obj1=HASH/;
38
39eval { Foo->new( str_obj => Obj2->new ) };
40like $@, qr/Attribute \(str_obj\) does not pass the type constraint because: Validation failed for 'Type1' failed with value Obj2=HASH/;
41
42eval { Foo->new( str_obj => 'Ref' ) };
43like $@, qr/Attribute \(str_obj\) does not pass the type constraint because: Validation failed for 'Type1' failed with value Ref/;
44
45my $f1 = eval { Foo->new( obj_str => Obj2->new ) };
46isa_ok $f1, 'Foo';
47is $f1->obj_str, 'Ref';