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