changed made sure I properly delegate parsing the type constraint parameter string...
[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 => 37;
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 ## creating names via subtype
80
81 ok my $r = Moose::Util::TypeConstraints->get_type_constraint_registry
82  => 'Got registry object';
83
84 ok my $subtype_a1 = subtype('subtype_a1' => as 'HashRef[Int]'),
85  => 'created subtype_a1';
86         
87 ok my $subtype_a2 = subtype('subtype_a2' => as 'HashRef[  Int]'),
88  => 'created subtype_a2';
89
90 ok my $subtype_a3 = subtype('subtype_a2' => as 'HashRef[Int  ]'),
91  => 'created subtype_a2';
92
93 ok my $subtype_a4 = subtype('subtype_a2' => as 'HashRef[  Int  ]'),
94  => 'created subtype_a2';
95
96 is $subtype_a1->parent->name, $subtype_a2->parent->name
97  =>'names match';
98
99 is $subtype_a1->parent->name, $subtype_a3->parent->name
100  =>'names match';
101  
102 is $subtype_a1->parent->name, $subtype_a4->parent->name
103  =>'names match';
104
105 ok my $subtype_b1 = subtype('subtype_b1' => as 'HashRef[Int|Str]'),
106  => 'created subtype_b1';
107  
108 ok my $subtype_b2 = subtype('subtype_b2' => as 'HashRef[Int | Str]'),
109  => 'created subtype_b2';
110
111 ok my $subtype_b3 = subtype('subtype_b2' => as 'HashRef[Str|Int]'),
112  => 'created subtype_b3';
113
114 is $subtype_b1->parent->name, $subtype_b2->parent->name
115  =>'names match';
116
117 is $subtype_b1->parent->name, $subtype_b3->parent->name
118  =>'names match';
119  
120 is $subtype_b2->parent->name, $subtype_b3->parent->name
121  =>'names match';
122
123 ## testing via add_constraint
124
125 ok my $union1 = Moose::Util::TypeConstraints::create_type_constraint_union(
126         'ArrayRef[Int|Str] | ArrayRef[Int | HashRef]') => 'Created Union1';
127
128 ok my $union2 = Moose::Util::TypeConstraints::create_type_constraint_union(
129         'ArrayRef[  Int|Str] | ArrayRef[Int | HashRef]') => 'Created Union2';
130     
131 ok my $union3 = Moose::Util::TypeConstraints::create_type_constraint_union(
132         'ArrayRef[Int |Str   ] | ArrayRef[Int | HashRef  ]') => 'Created Union3'; 
133  
134 is $union1->name, $union2->name,
135  'names match';
136
137 is $union1->name, $union3->name,
138  'names match';
139  
140 is $union2->name, $union3->name,
141  'names match';
142  
143  
144  
145