allow inlining if the type has been subsequently defined
[gitmo/MooseX-Types.git] / t / 20_union_with_string_type.t
1 #!/usr/bin/env perl
2 use strict;
3 use warnings;
4
5 use Test::More;
6
7 my $exception;
8 {
9     package TypeLib;
10
11     use MooseX::Types -declare => [qw( MyUnionType Test1 Test2 Test3 MyStr )];
12     use MooseX::Types::Moose qw(Str Int Item Object);
13
14     subtype MyUnionType, as Str|'Int';
15     subtype MyStr, as Str;
16
17     eval { coerce MyStr, from Item, via {"$_"} };
18     my $exception = $@;
19
20         Test::More::ok !$@, 'types are not mutated by union with a string type';
21
22         subtype Test1, 
23           as Int | 'ArrayRef[Int]';
24         
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';
30
31         eval {
32         subtype Test2, 
33          as Int | 'IDONTEXIST';
34         };
35
36         my $check = $@;
37
38         Test::More::ok $@, 'Got an error for bad Type'; 
39         Test::More::like $check,  qr/IDONTEXIST is not a type constraint/,  'correct error';
40
41         my $obj = subtype Test3, 
42           as Int | 'ArrayRef[Int]' | Object;
43
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';
50 }
51
52 done_testing();