Add assert_valid() to Meta::TypeConstraint
[gitmo/Mouse.git] / t / 040_type_constraints / failing / 014_type_notation_parser.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 41;
7
8 BEGIN {
9     use_ok("Mouse::Util::TypeConstraints");
10 }
11
12 =pod
13
14 This is a good candidate for LectroTest
15 Volunteers welcome :)
16
17 =cut
18
19 ## check the containers
20
21 ok(Mouse::Util::TypeConstraints::_detect_parameterized_type_constraint($_),
22    '... this correctly detected a container (' . $_ . ')')
23     for (
24     'ArrayRef[Foo]',
25     'ArrayRef[Foo | Int]',
26     'ArrayRef[ArrayRef[Int]]',
27     'ArrayRef[ArrayRef[Int | Foo]]',
28     'ArrayRef[ArrayRef[Int|Str]]',
29 );
30
31 ok(!Mouse::Util::TypeConstraints::_detect_parameterized_type_constraint($_),
32    '... this correctly detected a non-container (' . $_ . ')')
33     for (
34     'ArrayRef[]',
35     'ArrayRef[Foo]Bar',
36 );
37
38 {
39     my %split_tests = (
40         'ArrayRef[Foo]'                 => [ 'ArrayRef', 'Foo' ],
41         'ArrayRef[Foo | Int]'           => [ 'ArrayRef', 'Foo | Int' ],
42         'ArrayRef[Foo|Int]'             => [ 'ArrayRef', 'Foo|Int' ],
43         # these will get processed with recusion,
44         # so we only need to detect it once
45         'ArrayRef[ArrayRef[Int]]'       => [ 'ArrayRef', 'ArrayRef[Int]' ],
46         'ArrayRef[ArrayRef[Int | Foo]]' => [ 'ArrayRef', 'ArrayRef[Int | Foo]' ],
47         'ArrayRef[ArrayRef[Int|Str]]'   => [ 'ArrayRef', 'ArrayRef[Int|Str]' ],
48     );
49
50     is_deeply(
51         [ Mouse::Util::TypeConstraints::_parse_parameterized_type_constraint($_) ],
52         $split_tests{$_},
53         '... this correctly split the container (' . $_ . ')'
54     ) for keys %split_tests;
55 }
56
57 ## now for the unions
58
59 ok(Mouse::Util::TypeConstraints::_detect_type_constraint_union($_),
60    '... this correctly detected union (' . $_ . ')')
61     for (
62     'Int | Str',
63     'Int|Str',
64     'ArrayRef[Foo] | Int',
65     'ArrayRef[Foo]|Int',
66     'Int | ArrayRef[Foo]',
67     'Int|ArrayRef[Foo]',
68     'ArrayRef[Foo | Int] | Str',
69     'ArrayRef[Foo|Int]|Str',
70     'Str | ArrayRef[Foo | Int]',
71     'Str|ArrayRef[Foo|Int]',
72     'Some|Silly|Name|With|Pipes | Int',
73     'Some|Silly|Name|With|Pipes|Int',
74 );
75
76 ok(!Mouse::Util::TypeConstraints::_detect_type_constraint_union($_),
77    '... this correctly detected a non-union (' . $_ . ')')
78     for (
79     'Int',
80     'ArrayRef[Foo | Int]',
81     'ArrayRef[Foo|Int]',
82 );
83
84 {
85     my %split_tests = (
86         'Int | Str'                        => [ 'Int', 'Str' ],
87         'Int|Str'                          => [ 'Int', 'Str' ],
88         'ArrayRef[Foo] | Int'              => [ 'ArrayRef[Foo]', 'Int' ],
89         'ArrayRef[Foo]|Int'                => [ 'ArrayRef[Foo]', 'Int' ],
90         'Int | ArrayRef[Foo]'              => [ 'Int', 'ArrayRef[Foo]' ],
91         'Int|ArrayRef[Foo]'                => [ 'Int', 'ArrayRef[Foo]' ],
92         'ArrayRef[Foo | Int] | Str'        => [ 'ArrayRef[Foo | Int]', 'Str' ],
93         'ArrayRef[Foo|Int]|Str'            => [ 'ArrayRef[Foo|Int]', 'Str' ],
94         'Str | ArrayRef[Foo | Int]'        => [ 'Str', 'ArrayRef[Foo | Int]' ],
95         'Str|ArrayRef[Foo|Int]'            => [ 'Str', 'ArrayRef[Foo|Int]' ],
96         'Some|Silly|Name|With|Pipes | Int' => [ 'Some', 'Silly', 'Name', 'With', 'Pipes', 'Int' ],
97         'Some|Silly|Name|With|Pipes|Int'   => [ 'Some', 'Silly', 'Name', 'With', 'Pipes', 'Int' ],
98     );
99
100     is_deeply(
101         [ Mouse::Util::TypeConstraints::_parse_type_constraint_union($_) ],
102         $split_tests{$_},
103         '... this correctly split the union (' . $_ . ')'
104     ) for keys %split_tests;
105 }