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