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