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