implement to subtype's as is moose way
[gitmo/Mouse.git] / t / 800_shikabased / 014-subtype-as.t
1 use strict;
2 use warnings;
3 use Test::More tests => 6;
4 use 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
34 eval { Foo->new( str_obj => Obj1->new ) };
35 like $@, qr/Attribute \(str_obj\) does not pass the type constraint because: Validation failed for 'Type1' failed with value Obj1=HASH/;
36 eval { Foo->new( obj_str => Obj1->new ) };
37 like $@, qr/Attribute \(obj_str\) does not pass the type constraint because: Validation failed for 'Type2' failed with value Obj1=HASH/;
38
39 eval { Foo->new( str_obj => Obj2->new ) };
40 like $@, qr/Attribute \(str_obj\) does not pass the type constraint because: Validation failed for 'Type1' failed with value Obj2=HASH/;
41
42 eval { Foo->new( str_obj => 'Ref' ) };
43 like $@, qr/Attribute \(str_obj\) does not pass the type constraint because: Validation failed for 'Type1' failed with value Ref/;
44
45 my $f1 = eval { Foo->new( obj_str => Obj2->new ) };
46 isa_ok $f1, 'Foo';
47 is $f1->obj_str, 'Ref';