4
4
import src .ir .types as tp
5
5
import src .ir .type_utils as tu
6
6
7
+
7
8
def test_type_alias_with_literals ():
8
9
string_alias = ts_ast .TypeAliasDeclaration ("Foo" , tst .StringType ()).get_type ()
9
10
number_alias = ts_ast .TypeAliasDeclaration ("Bar" , tst .NumberType ()).get_type ()
@@ -16,6 +17,7 @@ def test_type_alias_with_literals():
16
17
assert number_lit .is_subtype (number_alias )
17
18
assert not number_alias .is_subtype (number_lit )
18
19
20
+
19
21
def test_type_alias_with_literals2 ():
20
22
string_alias = ts_ast .TypeAliasDeclaration ("Foo" , tst .StringLiteralType ("foo" )).get_type ()
21
23
number_alias = ts_ast .TypeAliasDeclaration ("Bar" , tst .NumberLiteralType (5 )).get_type ()
@@ -28,6 +30,7 @@ def test_type_alias_with_literals2():
28
30
assert string_alias .is_subtype (string_lit )
29
31
assert number_alias .is_subtype (number_lit )
30
32
33
+
31
34
def test_union_types_simple ():
32
35
union_1 = tst .UnionType ([tst .NumberType (), tst .BooleanType ()])
33
36
@@ -42,6 +45,11 @@ def test_union_types_simple():
42
45
assert union_1 .is_subtype (union_3 )
43
46
44
47
48
+ def test_union_types_other_types ():
49
+ union = tst .UnionType ([tst .NumberType (), tst .BooleanType ()])
50
+ assert tst .NumberType ().is_subtype (union )
51
+
52
+
45
53
def test_union_type_assign ():
46
54
union = tst .UnionType ([tst .StringType (), tst .NumberType (), tst .BooleanType (), tst .ObjectType ()])
47
55
foo = tst .StringType ()
@@ -130,6 +138,22 @@ def test_union_type_unification_type_var():
130
138
assert len (type_var_map ) == 1
131
139
assert type_var_map == {type_param : union }
132
140
141
+ # Case 2: unify a union with a bounded type param, which has an
142
+ # incompatible bound with the given union.
143
+ union = tst .UnionType ([tst .NumberType (), tst .StringType ()])
144
+ type_param = tp .TypeParameter ("T" , bound = tst .NumberType ())
145
+
146
+ type_var_map = tu .unify_types (union , type_param ,
147
+ tst .TypeScriptBuiltinFactory ())
148
+ assert type_var_map == {}
149
+
150
+
151
+ # Case 3: unify a union with a bounded type param, which has a compatible
152
+ # bound with the given union.
153
+ type_param = tp .TypeParameter ("T" , bound = union )
154
+ type_var_map = tu .unify_types (union , type_param ,
155
+ tst .TypeScriptBuiltinFactory ())
156
+ assert type_var_map == {type_param : union }
133
157
134
158
def test_union_type_unification ():
135
159
type_param = tp .TypeParameter ("T" )
@@ -149,6 +173,7 @@ def test_union_type_unification():
149
173
assert type_param , type_param2 in type_var_map
150
174
assert union1 .types [1 ], union1 .types [2 ] in type_var_map .values ()
151
175
176
+
152
177
def test_union_type_unification2 ():
153
178
union = tst .UnionType ([tst .NumberType (), tst .StringType ()])
154
179
assert tu .unify_types (tst .BooleanType (), union , tst .TypeScriptBuiltinFactory ()) == {}
0 commit comments