Skip to content

Commit 123d20a

Browse files
alexisthedevtheosotr
authored andcommitted
Add unit tests for union type unification
1 parent 291397b commit 123d20a

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

tests/test_typescript.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import src.ir.typescript_types as tst
22
import src.ir.typescript_ast as ts_ast
33
import src.ir.types as tp
4+
import src.ir.type_utils as tu
45

56
def test_type_alias_with_literals():
67
string_alias = ts_ast.TypeAliasDeclaration("Foo", tst.StringType()).get_type()
@@ -118,3 +119,30 @@ def test_union_to_type_variable_free():
118119
foo_n = union_n.types[0]
119120
assert foo_n.type_args[0] == bar.new(
120121
[tp.WildCardType(tst.NumberType(), variance=tp.Covariant)])
122+
123+
124+
def test_union_type_unification_type_var():
125+
union = tst.UnionType([tst.StringType(), tst.StringLiteralType("foo")])
126+
type_param = tp.TypeParameter("T")
127+
128+
type_var_map = tu.unify_types(union, type_param, tst.TypeScriptBuiltinFactory())
129+
assert len(type_var_map) == 1
130+
assert type_var_map == {type_param: union}
131+
132+
133+
def test_unify_two_union_types():
134+
type_param = tp.TypeParameter("T")
135+
union1 = tst.UnionType([tst.NumberLiteralType(1410), tst.NumberType(), tst.StringType()])
136+
union2 = tst.UnionType([type_param, tst.NumberType(), tst.StringType()])
137+
138+
type_var_map = tu.unify_types(union1, union2, tst.TypeScriptBuiltinFactory())
139+
assert len(type_var_map) == 1
140+
assert type_var_map == {type_param: union1.types[0]}
141+
142+
type_param2 = tp.TypeParameter("G")
143+
union3 = tst.UnionType([type_param, type_param2, tst.StringLiteralType("foo")])
144+
145+
type_var_map = tu.unify_types(union1, union3, tst.TypeScriptBuiltinFactory())
146+
assert len(type_var_map) == 2
147+
assert type_var_map == {type_param: union1.types[0],
148+
type_param2: union1.types[1]}

0 commit comments

Comments
 (0)