|
1 | 1 | import src.ir.typescript_types as tst
|
2 | 2 | import src.ir.typescript_ast as ts_ast
|
3 | 3 | import src.ir.types as tp
|
| 4 | +import src.ir.type_utils as tu |
4 | 5 |
|
5 | 6 | def test_type_alias_with_literals():
|
6 | 7 | string_alias = ts_ast.TypeAliasDeclaration("Foo", tst.StringType()).get_type()
|
@@ -118,3 +119,30 @@ def test_union_to_type_variable_free():
|
118 | 119 | foo_n = union_n.types[0]
|
119 | 120 | assert foo_n.type_args[0] == bar.new(
|
120 | 121 | [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