6
6
7
7
8
8
def test_type_alias_with_literals ():
9
+ # Tests subtyping relations between a string alias and string literal
10
+ # and between a number alias and a number literal.
11
+ # - (type Foo = string) with literal "foo"
12
+ # - (type Bar = number) with literal 5
9
13
string_alias = ts_ast .TypeAliasDeclaration ("Foo" , tst .StringType ()).get_type ()
10
14
number_alias = ts_ast .TypeAliasDeclaration ("Bar" , tst .NumberType ()).get_type ()
11
15
@@ -19,6 +23,10 @@ def test_type_alias_with_literals():
19
23
20
24
21
25
def test_type_alias_with_literals2 ():
26
+ # Tests subtyping relation between a literal alias
27
+ # and their corresponding literal type.
28
+ # - (type Foo = "foo") with literal "foo"
29
+ # - (type Bar = "bar") with literal "bar"
22
30
string_alias = ts_ast .TypeAliasDeclaration ("Foo" , tst .StringLiteralType ("foo" )).get_type ()
23
31
number_alias = ts_ast .TypeAliasDeclaration ("Bar" , tst .NumberLiteralType (5 )).get_type ()
24
32
@@ -32,6 +40,11 @@ def test_type_alias_with_literals2():
32
40
33
41
34
42
def test_union_types_simple ():
43
+ # Tests subtyping relation between union types
44
+ # and the types in their union.
45
+ # - number | boolean
46
+ # - boolean | "bar"
47
+ # - boolean | number
35
48
union_1 = tst .UnionType ([tst .NumberType (), tst .BooleanType ()])
36
49
37
50
bar_lit = tst .StringLiteralType ("bar" )
@@ -46,11 +59,14 @@ def test_union_types_simple():
46
59
47
60
48
61
def test_union_types_other_types ():
62
+ # Tests that types A, B are subtypes of A | B
49
63
union = tst .UnionType ([tst .NumberType (), tst .BooleanType ()])
50
64
assert tst .NumberType ().is_subtype (union )
65
+ assert tst .BooleanType ().is_subtype (union )
51
66
52
67
53
68
def test_union_type_assign ():
69
+ # Tests correct creation and assignment of union type
54
70
union = tst .UnionType ([tst .StringType (), tst .NumberType (), tst .BooleanType (), tst .ObjectType ()])
55
71
foo = tst .StringType ()
56
72
@@ -60,6 +76,8 @@ def test_union_type_assign():
60
76
61
77
62
78
def test_union_type_param ():
79
+ # Tests that union type bounds of type parameters do not
80
+ # conflict with the sybtyping relations between the two.
63
81
union1 = tst .UnionType ([tst .NumberType (), tst .NullType ()])
64
82
union2 = tst .UnionType ([tst .StringLiteralType ("foo" ), tst .NumberType ()])
65
83
t_param = tp .TypeParameter ("T" , bound = union2 )
@@ -70,6 +88,7 @@ def test_union_type_param():
70
88
71
89
72
90
def test_union_type_substitution ():
91
+ # Tests substitution of type parametes in union types
73
92
type_param1 = tp .TypeParameter ("T1" )
74
93
type_param2 = tp .TypeParameter ("T2" )
75
94
type_param3 = tp .TypeParameter ("T3" )
@@ -86,6 +105,7 @@ def test_union_type_substitution():
86
105
87
106
88
107
def test_union_type_substitution_type_var_bound ():
108
+ # Tests substitution of bounded type parameters in union types
89
109
type_param1 = tp .TypeParameter ("T1" )
90
110
type_param2 = tp .TypeParameter ("T2" , bound = type_param1 )
91
111
type_map = {type_param1 : tst .StringType ()}
@@ -101,6 +121,7 @@ def test_union_type_substitution_type_var_bound():
101
121
102
122
103
123
def test_union_to_type_variable_free ():
124
+ # Tests the builtin method to-type-variable-free of union types
104
125
type_param1 = tp .TypeParameter ("T1" )
105
126
type_param2 = tp .TypeParameter ("T2" )
106
127
foo = tp .TypeConstructor ("Foo" , [type_param1 ])
@@ -134,6 +155,7 @@ def test_union_type_unification_type_var():
134
155
union = tst .UnionType ([tst .StringType (), tst .StringLiteralType ("foo" )])
135
156
type_param = tp .TypeParameter ("T" )
136
157
158
+ # Case 1: Unify a union with an unbounded type param
137
159
type_var_map = tu .unify_types (union , type_param , tst .TypeScriptBuiltinFactory ())
138
160
assert len (type_var_map ) == 1
139
161
assert type_var_map == {type_param : union }
@@ -161,13 +183,19 @@ def test_union_type_unification():
161
183
union2 = tst .UnionType ([type_param , tst .NumberType (), tst .StringType ()])
162
184
assert union1 .is_subtype (union2 )
163
185
186
+ # Unify t1: 1410 | number | string
187
+ # with t2: T | number | string
188
+ # Result should be: {T: 1410}
164
189
type_var_map = tu .unify_types (union1 , union2 , tst .TypeScriptBuiltinFactory ())
165
190
assert len (type_var_map ) == 1
166
191
assert type_var_map == {type_param : union1 .types [0 ]}
167
192
168
193
type_param2 = tp .TypeParameter ("G" )
169
194
union3 = tst .UnionType ([type_param , type_param2 , tst .StringLiteralType ("foo" )])
170
195
196
+ # Unify t1: 1410 | number | string
197
+ # with t3: T | G | "foo".
198
+ # Result should be: {T: number, G: string} or reversed.
171
199
type_var_map = tu .unify_types (union1 , union3 , tst .TypeScriptBuiltinFactory ())
172
200
assert len (type_var_map ) == 2
173
201
assert type_param , type_param2 in type_var_map
@@ -178,15 +206,24 @@ def test_union_type_unification2():
178
206
union = tst .UnionType ([tst .NumberType (), tst .StringType ()])
179
207
assert tu .unify_types (tst .BooleanType (), union , tst .TypeScriptBuiltinFactory ()) == {}
180
208
209
+ # Unify t1: number
210
+ # with t2: number | T
211
+ # Result should be: {T: number}
181
212
t1 = tst .NumberType ()
182
213
t2 = tst .UnionType ([tst .NumberType (), tp .TypeParameter ("T" )])
183
214
res = tu .unify_types (t1 , t2 , tst .TypeScriptBuiltinFactory ())
184
215
assert len (res ) == 1 and res [t2 .types [1 ]] == t1
185
216
217
+ # Unify t1: number | string
218
+ # with t2: number | T
219
+ # Result should be: {T: string}
186
220
t1 = tst .UnionType ([tst .NumberType (), tst .StringType ()])
187
221
res = tu .unify_types (t1 , t2 , tst .TypeScriptBuiltinFactory ())
188
222
assert len (res ) == 1 and res [t2 .types [1 ]] == t1 .types [1 ]
189
223
224
+ # Unify t1: number | "foo" | string
225
+ # with t2: number | T
226
+ # Result should be: {T: string}
190
227
t1 = tst .UnionType ([tst .NumberType (), tst .StringLiteralType ("foo" ), tst .StringType ()])
191
228
res = tu .unify_types (t1 , t2 , tst .TypeScriptBuiltinFactory ())
192
229
assert len (res ) == 1 and res [t2 .types [1 ]] == t1 .types [2 ]
0 commit comments