Skip tests for strict constructor on Moose
[gitmo/Mouse.git] / t / 001_mouse / 814-subtype-as.t
CommitLineData
1fbefea5 1use strict;
2use warnings;
2f665925 3use Test::More tests => 12;
1fbefea5 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;
3b46bd49 19 use Mouse::Util::TypeConstraints;
1fbefea5 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 );
2f665925 32
33 subtype 'Type3' => as 'Object';
34 has as_only => (
35 is => 'rw',
36 isa => 'Type3',
37 );
38
39
3fa6f35d 40 type 'Type4';
2f665925 41 has any => (
42 is => 'rw',
43 isa => 'Type4',
44 );
1fbefea5 45}
46
47eval { Foo->new( str_obj => Obj1->new ) };
48like $@, qr/Attribute \(str_obj\) does not pass the type constraint because: Validation failed for 'Type1' failed with value Obj1=HASH/;
49eval { Foo->new( obj_str => Obj1->new ) };
50like $@, qr/Attribute \(obj_str\) does not pass the type constraint because: Validation failed for 'Type2' failed with value Obj1=HASH/;
51
52eval { Foo->new( str_obj => Obj2->new ) };
53like $@, qr/Attribute \(str_obj\) does not pass the type constraint because: Validation failed for 'Type1' failed with value Obj2=HASH/;
54
55eval { Foo->new( str_obj => 'Ref' ) };
56like $@, qr/Attribute \(str_obj\) does not pass the type constraint because: Validation failed for 'Type1' failed with value Ref/;
57
58my $f1 = eval { Foo->new( obj_str => Obj2->new ) };
59isa_ok $f1, 'Foo';
60is $f1->obj_str, 'Ref';
2f665925 61
62my $f2 = eval { Foo->new( as_only => Obj1->new ) };
63isa_ok $f2, 'Foo';
64is ref($f2->as_only), 'Obj1';
65
66my $f3 = eval { Foo->new( any => Obj1->new ) };
684db121 67die $@ if $@;
2f665925 68isa_ok $f3, 'Foo';
69is ref($f3->any), 'Obj1';
70
71my $f4 = eval { Foo->new( any => 'YATTA' ) };
72isa_ok $f4, 'Foo';
73is $f4->any, 'YATTA';