Need to ignore override and super in the method list
[gitmo/Mouse.git] / t / 800_shikabased / 802-mousex_types-isa-or.t
CommitLineData
6da1e936 1use strict;
2use warnings;
3use Test::More tests => 13;
4
5{
6 package Types;
7 use strict;
8 use warnings;
9 use MouseX::Types -declare => [qw/ Baz Type1 Type2 /];
10 use MouseX::Types::Mouse qw( ArrayRef );
11
3fa6f35d 12 type Baz, where { defined($_) && $_ eq 'Baz' };
6da1e936 13 coerce Baz, from ArrayRef, via { 'Baz' };
14
3fa6f35d 15 type Type1, where { defined($_) && $_ eq 'Name' };
6da1e936 16 coerce Type1, from 'Str', via { 'Names' };
17
3fa6f35d 18 type Type2, where { defined($_) && $_ eq 'Group' };
6da1e936 19 coerce Type2, from 'Str', via { 'Name' };
20
21}
22
23{
24 package Foo;
25 use Mouse;
26 use MouseX::Types::Mouse qw( Str Undef );
27 BEGIN { Types->import(qw( Baz Type1 )) }
28 has 'bar' => ( is => 'rw', isa => Str | Baz | Undef, coerce => 1 );
29}
30
31eval {
32 Foo->new( bar => +{} );
33};
34ok $@, 'not got an object';
35
36eval {
37 isa_ok(Foo->new( bar => undef ), 'Foo');
38};
39ok !$@, 'got an object 1';
40
41eval {
42 isa_ok(Foo->new( bar => 'foo' ), 'Foo');
43
44};
45ok !$@, 'got an object 2';
46
47
48my $f = Foo->new;
49eval {
50 $f->bar([]);
51};
52ok !$@;
53is $f->bar, 'Baz', 'bar is baz (coerce from ArrayRef)';
54
55eval {
56 $f->bar('hoge');
57};
58ok !$@;
59is $f->bar, 'hoge', 'bar is hoge';
60
61eval {
62 $f->bar(undef);
63};
64ok !$@;
65is $f->bar, undef, 'bar is undef';
66
67
68{
69 package Bar;
70 use Mouse;
71 BEGIN { Types->import(qw( Type1 Type2 )) }
72 has 'foo' => ( is => 'rw', isa => Type1 | Type2 , coerce => 1 );
73}
74
75my $foo = Bar->new( foo => 'aaa' );
76ok $foo, 'got an object 3';
77is $foo->foo, 'Name', 'foo is Name';