We only need local $? if we inline calls to DEMOLISH
[gitmo/Moose.git] / t / type_constraints / normalize_type_name.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More;
7
8 use Moose::Util::TypeConstraints;
9
10 ## First, we check that the new regex parsing works
11
12 ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint(
13     'ArrayRef[Str]') => 'detected correctly';
14
15 is_deeply
16     [
17     Moose::Util::TypeConstraints::_parse_parameterized_type_constraint(
18         'ArrayRef[Str]')
19     ],
20     [ "ArrayRef", "Str" ] => 'Correctly parsed ArrayRef[Str]';
21
22 ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint(
23     'ArrayRef[Str  ]') => 'detected correctly';
24
25 is_deeply
26     [
27     Moose::Util::TypeConstraints::_parse_parameterized_type_constraint(
28         'ArrayRef[Str  ]')
29     ],
30     [ "ArrayRef", "Str" ] => 'Correctly parsed ArrayRef[Str  ]';
31
32 ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint(
33     'ArrayRef[  Str]') => 'detected correctly';
34
35 is_deeply
36     [
37     Moose::Util::TypeConstraints::_parse_parameterized_type_constraint(
38         'ArrayRef[  Str]')
39     ],
40     [ "ArrayRef", "Str" ] => 'Correctly parsed ArrayRef[  Str]';
41
42 ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint(
43     'ArrayRef[  Str  ]') => 'detected correctly';
44
45 is_deeply
46     [
47     Moose::Util::TypeConstraints::_parse_parameterized_type_constraint(
48         'ArrayRef[  Str  ]')
49     ],
50     [ "ArrayRef", "Str" ] => 'Correctly parsed ArrayRef[  Str  ]';
51
52 ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint(
53     'ArrayRef[  HashRef[Int]  ]') => 'detected correctly';
54
55 is_deeply
56     [
57     Moose::Util::TypeConstraints::_parse_parameterized_type_constraint(
58         'ArrayRef[  HashRef[Int]  ]')
59     ],
60     [ "ArrayRef", "HashRef[Int]" ] =>
61     'Correctly parsed ArrayRef[  HashRef[Int]  ]';
62
63 ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint(
64     'ArrayRef[  HashRef[Int  ]  ]') => 'detected correctly';
65
66 is_deeply
67     [
68     Moose::Util::TypeConstraints::_parse_parameterized_type_constraint(
69         'ArrayRef[  HashRef[Int  ]  ]')
70     ],
71     [ "ArrayRef", "HashRef[Int  ]" ] =>
72     'Correctly parsed ArrayRef[  HashRef[Int  ]  ]';
73
74 ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint(
75     'ArrayRef[Int|Str]') => 'detected correctly';
76
77 is_deeply
78     [
79     Moose::Util::TypeConstraints::_parse_parameterized_type_constraint(
80         'ArrayRef[Int|Str]')
81     ],
82     [ "ArrayRef", "Int|Str" ] => 'Correctly parsed ArrayRef[Int|Str]';
83
84 ok Moose::Util::TypeConstraints::_detect_parameterized_type_constraint(
85     'ArrayRef[ArrayRef[Int]|Str]') => 'detected correctly';
86
87 is_deeply
88     [
89     Moose::Util::TypeConstraints::_parse_parameterized_type_constraint(
90         'ArrayRef[ArrayRef[Int]|Str]')
91     ],
92     [ "ArrayRef", "ArrayRef[Int]|Str" ] =>
93     'Correctly parsed ArrayRef[ArrayRef[Int]|Str]';
94
95 ## creating names via subtype
96
97 ok my $r = Moose::Util::TypeConstraints->get_type_constraint_registry =>
98     'Got registry object';
99
100 ok my $subtype_a1
101     = subtype( 'subtype_a1' => as 'HashRef[Int]' ), => 'created subtype_a1';
102
103 ok my $subtype_a2
104     = subtype( 'subtype_a2' => as 'HashRef[  Int]' ), => 'created subtype_a2';
105
106 ok my $subtype_a3
107     = subtype( 'subtype_a2' => as 'HashRef[Int  ]' ), => 'created subtype_a2';
108
109 ok my $subtype_a4 = subtype( 'subtype_a2' => as 'HashRef[  Int  ]' ), =>
110     'created subtype_a2';
111
112 is $subtype_a1->parent->name, $subtype_a2->parent->name => 'names match';
113
114 is $subtype_a1->parent->name, $subtype_a3->parent->name => 'names match';
115
116 is $subtype_a1->parent->name, $subtype_a4->parent->name => 'names match';
117
118 ok my $subtype_b1 = subtype( 'subtype_b1' => as 'HashRef[Int|Str]' ), =>
119     'created subtype_b1';
120
121 ok my $subtype_b2 = subtype( 'subtype_b2' => as 'HashRef[Int | Str]' ), =>
122     'created subtype_b2';
123
124 ok my $subtype_b3 = subtype( 'subtype_b3' => as 'HashRef[Str|Int]' ), =>
125     'created subtype_b3';
126
127 is $subtype_b1->parent->name, $subtype_b2->parent->name => 'names match';
128
129 is $subtype_b1->parent->name, $subtype_b3->parent->name => 'names match';
130
131 is $subtype_b2->parent->name, $subtype_b3->parent->name => 'names match';
132
133 ## testing via add_constraint
134
135 ok my $union1 = Moose::Util::TypeConstraints::create_type_constraint_union(
136     'ArrayRef[Int|Str] | ArrayRef[Int | HashRef]') => 'Created Union1';
137
138 ok my $union2 = Moose::Util::TypeConstraints::create_type_constraint_union(
139     'ArrayRef[  Int|Str] | ArrayRef[Int | HashRef]') => 'Created Union2';
140
141 ok my $union3 = Moose::Util::TypeConstraints::create_type_constraint_union(
142     'ArrayRef[Int |Str   ] | ArrayRef[Int | HashRef  ]') => 'Created Union3';
143
144 is $union1->name, $union2->name, 'names match';
145
146 is $union1->name, $union3->name, 'names match';
147
148 is $union2->name, $union3->name, 'names match';
149
150 done_testing;