File: 003-isa_or.t

package info (click to toggle)
libmousex-types-perl 0.06-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 308 kB
  • sloc: perl: 2,122; makefile: 2
file content (77 lines) | stat: -rw-r--r-- 1,483 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
use strict;
use warnings;
use Test::More tests => 13;

BEGIN{
    package Types;
    use strict;
    use warnings;
    use MouseX::Types -declare => [qw/ Baz Type1 Type2 /];
    use MouseX::Types::Mouse qw( ArrayRef );

    type Baz, where { defined($_) && $_ eq 'Baz' };
    coerce Baz, from ArrayRef, via { 'Baz' };

    type Type1, where { defined($_) && $_ eq 'Name' };
    coerce Type1, from 'Str', via { 'Names' };

    type Type2, where { defined($_) && $_ eq 'Group' };
    coerce Type2, from 'Str', via { 'Name' };

}

{   
    package Foo;
    use Mouse;
    use MouseX::Types::Mouse qw( Str Undef );
    BEGIN { Types->import(qw( Baz Type1 )) }
    has 'bar' => ( is => 'rw', isa => Str | Baz | Undef, coerce => 1 );
}

eval {
    Foo->new( bar => +{} );
};
ok $@, 'not got an object';

eval {
    isa_ok(Foo->new( bar => undef ), 'Foo');
};
ok !$@, 'got an object 1';

eval {
    isa_ok(Foo->new( bar => 'foo' ), 'Foo');

};
ok !$@, 'got an object 2';


my $f = Foo->new;
eval {
    $f->bar([]);
};
ok !$@;
is $f->bar, 'Baz', 'bar is baz (coerce from ArrayRef)';

eval {
    $f->bar('hoge');
};
ok !$@;
is $f->bar, 'hoge', 'bar is hoge';

eval {
    $f->bar(undef);
};
ok !$@;
is $f->bar, undef, 'bar is undef';


{   
    package Bar;
    use Mouse;
    BEGIN { Types->import(qw( Type1 Type2 )) }
    has 'foo' => ( is => 'rw', isa => Type1 | Type2 , coerce => 1 );
}

my $foo = Bar->new( foo => 'aaa' );
ok $foo, 'got an object 3';
is $foo->foo, 'Name', 'foo is Name';