Skip to content

Commit 3e06d6f

Browse files
alexisthedevtheosotr
authored andcommitted
Add more unit tests for union type substitution
1 parent ff26ffe commit 3e06d6f

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

tests/test_typescript.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,48 @@ def test_union_type_substitution():
7373

7474
assert ptype.types[1].type_args[0] == tst.NumberType()
7575
assert ptype.types[1].type_args[1] == type_param4
76+
77+
78+
def test_union_type_substitution_type_var_bound():
79+
type_param1 = tp.TypeParameter("T1")
80+
type_param2 = tp.TypeParameter("T2", bound=type_param1)
81+
type_map = {type_param1: tst.StringType()}
82+
83+
union = tst.UnionType([tst.NumberType(), type_param2])
84+
ptype_union = tp.substitute_type(union, type_map)
85+
ptype = ptype_union.types[1]
86+
87+
88+
assert ptype.name == type_param2.name
89+
assert ptype.variance == type_param2.variance
90+
assert ptype.bound == tst.StringType()
91+
92+
93+
def test_union_to_type_variable_free():
94+
type_param1 = tp.TypeParameter("T1")
95+
type_param2 = tp.TypeParameter("T2")
96+
foo = tp.TypeConstructor("Foo", [type_param1])
97+
foo_t = foo.new([type_param2])
98+
union = tst.UnionType([foo_t, tst.StringLiteralType("bar")])
99+
100+
union_n = union.to_type_variable_free(tst.TypeScriptBuiltinFactory())
101+
foo_n = union_n.types[0]
102+
assert foo_n.type_args[0] == tp.WildCardType(tst.ObjectType(), variance=tp.Covariant)
103+
104+
type_param2.bound = tst.NumberType()
105+
foo_t = foo.new([type_param2])
106+
union = tst.UnionType([foo_t, tst.NumberLiteralType(43)])
107+
108+
union_n = union.to_type_variable_free(tst.TypeScriptBuiltinFactory())
109+
foo_n = union_n.types[0]
110+
assert foo_n.type_args[0] == tp.WildCardType(tst.NumberType(), variance=tp.Covariant)
111+
112+
bar = tp.TypeConstructor("Bar", [tp.TypeParameter("T")])
113+
bar_p = bar.new([type_param2])
114+
foo_t = foo.new([bar_p])
115+
union = tst.UnionType([foo_t, tst.NumberType(), tst.StringType(), tst.AliasType(tst.StringLiteralType("foobar"))])
116+
117+
union_n = union.to_type_variable_free(tst.TypeScriptBuiltinFactory())
118+
foo_n = union_n.types[0]
119+
assert foo_n.type_args[0] == bar.new(
120+
[tp.WildCardType(tst.NumberType(), variance=tp.Covariant)])

0 commit comments

Comments
 (0)