11 use MooseX::Types -declare => [qw( MyUnionType Test1 Test2 Test3 MyStr )];
12 use MooseX::Types::Moose qw(Str Int Item Object);
14 subtype MyUnionType, as Str|'Int';
15 subtype MyStr, as Str;
17 eval { coerce MyStr, from Item, via {"$_"} };
20 Test::More::ok !$@, 'types are not mutated by union with a string type';
23 as Int | 'ArrayRef[Int]';
25 Test::More::ok Test1->check(1), '1 is an Int';
26 Test::More::ok !Test1->check('a'), 'a is not an Int';
27 Test::More::ok Test1->check([1, 2, 3]), 'Passes ArrayRef';
28 Test::More::ok !Test1->check([1, 'a', 3]), 'Fails ArrayRef with a letter';
29 Test::More::ok !Test1->check({a=>1}), 'fails wrong ref type';
33 as Int | 'IDONTEXIST';
38 Test::More::ok $@, 'Got an error for bad Type';
39 Test::More::like $check, qr/IDONTEXIST is not a type constraint/, 'correct error';
41 my $obj = subtype Test3,
42 as Int | 'ArrayRef[Int]' | Object;
44 Test::More::ok Test3->check(1), '1 is an Int';
45 Test::More::ok !Test3->check('a'), 'a is not an Int';
46 Test::More::ok Test3->check([1, 2, 3]), 'Passes ArrayRef';
47 Test::More::ok !Test3->check([1, 'a', 3]), 'Fails ArrayRef with a letter';
48 Test::More::ok !Test3->check({a=>1}), 'fails wrong ref type';
49 Test::More::ok Test3->check($obj), 'Union allows Object';