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