Skip to content

Commit 790fff8

Browse files
alexisthedevtheosotr
authored andcommitted
Add description comments to unit tests
1 parent 93ce961 commit 790fff8

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

tests/test_typescript.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@
66

77

88
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
913
string_alias = ts_ast.TypeAliasDeclaration("Foo", tst.StringType()).get_type()
1014
number_alias = ts_ast.TypeAliasDeclaration("Bar", tst.NumberType()).get_type()
1115

@@ -19,6 +23,10 @@ def test_type_alias_with_literals():
1923

2024

2125
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"
2230
string_alias = ts_ast.TypeAliasDeclaration("Foo", tst.StringLiteralType("foo")).get_type()
2331
number_alias = ts_ast.TypeAliasDeclaration("Bar", tst.NumberLiteralType(5)).get_type()
2432

@@ -32,6 +40,11 @@ def test_type_alias_with_literals2():
3240

3341

3442
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
3548
union_1 = tst.UnionType([tst.NumberType(), tst.BooleanType()])
3649

3750
bar_lit = tst.StringLiteralType("bar")
@@ -46,11 +59,14 @@ def test_union_types_simple():
4659

4760

4861
def test_union_types_other_types():
62+
# Tests that types A, B are subtypes of A | B
4963
union = tst.UnionType([tst.NumberType(), tst.BooleanType()])
5064
assert tst.NumberType().is_subtype(union)
65+
assert tst.BooleanType().is_subtype(union)
5166

5267

5368
def test_union_type_assign():
69+
# Tests correct creation and assignment of union type
5470
union = tst.UnionType([tst.StringType(), tst.NumberType(), tst.BooleanType(), tst.ObjectType()])
5571
foo = tst.StringType()
5672

@@ -60,6 +76,8 @@ def test_union_type_assign():
6076

6177

6278
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.
6381
union1 = tst.UnionType([tst.NumberType(), tst.NullType()])
6482
union2 = tst.UnionType([tst.StringLiteralType("foo"), tst.NumberType()])
6583
t_param = tp.TypeParameter("T", bound=union2)
@@ -70,6 +88,7 @@ def test_union_type_param():
7088

7189

7290
def test_union_type_substitution():
91+
# Tests substitution of type parametes in union types
7392
type_param1 = tp.TypeParameter("T1")
7493
type_param2 = tp.TypeParameter("T2")
7594
type_param3 = tp.TypeParameter("T3")
@@ -86,6 +105,7 @@ def test_union_type_substitution():
86105

87106

88107
def test_union_type_substitution_type_var_bound():
108+
# Tests substitution of bounded type parameters in union types
89109
type_param1 = tp.TypeParameter("T1")
90110
type_param2 = tp.TypeParameter("T2", bound=type_param1)
91111
type_map = {type_param1: tst.StringType()}
@@ -101,6 +121,7 @@ def test_union_type_substitution_type_var_bound():
101121

102122

103123
def test_union_to_type_variable_free():
124+
# Tests the builtin method to-type-variable-free of union types
104125
type_param1 = tp.TypeParameter("T1")
105126
type_param2 = tp.TypeParameter("T2")
106127
foo = tp.TypeConstructor("Foo", [type_param1])
@@ -134,6 +155,7 @@ def test_union_type_unification_type_var():
134155
union = tst.UnionType([tst.StringType(), tst.StringLiteralType("foo")])
135156
type_param = tp.TypeParameter("T")
136157

158+
# Case 1: Unify a union with an unbounded type param
137159
type_var_map = tu.unify_types(union, type_param, tst.TypeScriptBuiltinFactory())
138160
assert len(type_var_map) == 1
139161
assert type_var_map == {type_param: union}
@@ -161,13 +183,19 @@ def test_union_type_unification():
161183
union2 = tst.UnionType([type_param, tst.NumberType(), tst.StringType()])
162184
assert union1.is_subtype(union2)
163185

186+
# Unify t1: 1410 | number | string
187+
# with t2: T | number | string
188+
# Result should be: {T: 1410}
164189
type_var_map = tu.unify_types(union1, union2, tst.TypeScriptBuiltinFactory())
165190
assert len(type_var_map) == 1
166191
assert type_var_map == {type_param: union1.types[0]}
167192

168193
type_param2 = tp.TypeParameter("G")
169194
union3 = tst.UnionType([type_param, type_param2, tst.StringLiteralType("foo")])
170195

196+
# Unify t1: 1410 | number | string
197+
# with t3: T | G | "foo".
198+
# Result should be: {T: number, G: string} or reversed.
171199
type_var_map = tu.unify_types(union1, union3, tst.TypeScriptBuiltinFactory())
172200
assert len(type_var_map) == 2
173201
assert type_param, type_param2 in type_var_map
@@ -178,15 +206,24 @@ def test_union_type_unification2():
178206
union = tst.UnionType([tst.NumberType(), tst.StringType()])
179207
assert tu.unify_types(tst.BooleanType(), union, tst.TypeScriptBuiltinFactory()) == {}
180208

209+
# Unify t1: number
210+
# with t2: number | T
211+
# Result should be: {T: number}
181212
t1 = tst.NumberType()
182213
t2 = tst.UnionType([tst.NumberType(), tp.TypeParameter("T")])
183214
res = tu.unify_types(t1, t2, tst.TypeScriptBuiltinFactory())
184215
assert len(res) == 1 and res[t2.types[1]] == t1
185216

217+
# Unify t1: number | string
218+
# with t2: number | T
219+
# Result should be: {T: string}
186220
t1 = tst.UnionType([tst.NumberType(), tst.StringType()])
187221
res = tu.unify_types(t1, t2, tst.TypeScriptBuiltinFactory())
188222
assert len(res) == 1 and res[t2.types[1]] == t1.types[1]
189223

224+
# Unify t1: number | "foo" | string
225+
# with t2: number | T
226+
# Result should be: {T: string}
190227
t1 = tst.UnionType([tst.NumberType(), tst.StringLiteralType("foo"), tst.StringType()])
191228
res = tu.unify_types(t1, t2, tst.TypeScriptBuiltinFactory())
192229
assert len(res) == 1 and res[t2.types[1]] == t1.types[2]

0 commit comments

Comments
 (0)