| 1 |
import unittest |
| 2 |
|
| 3 |
from mypyc.ir.class_ir import ClassIR |
| 4 |
from mypyc.ir.rtypes import ( |
| 5 |
RInstance, |
| 6 |
RTuple, |
| 7 |
RTypeVar, |
| 8 |
RUnion, |
| 9 |
RVec, |
| 10 |
int_rprimitive, |
| 11 |
str_rprimitive, |
| 12 |
void_rtype, |
| 13 |
) |
| 14 |
from mypyc.rt_expandtype import expand_rtype |
| 15 |
|
| 16 |
|
| 17 |
class TestExpandRType(unittest.TestCase): |
| 18 |
def test_trivial(self) -> None: |
| 19 |
assert expand_rtype(str_rprimitive, []) == str_rprimitive |
| 20 |
assert expand_rtype(str_rprimitive, [int_rprimitive]) == str_rprimitive |
| 21 |
assert expand_rtype(void_rtype, []) == void_rtype |
| 22 |
|
| 23 |
def test_instance(self) -> None: |
| 24 |
inst = RInstance(ClassIR("A", "__main__")) |
| 25 |
assert expand_rtype(inst, [int_rprimitive]) == inst |
| 26 |
|
| 27 |
def test_simple_expansion(self) -> None: |
| 28 |
assert expand_rtype(RTypeVar(0), [str_rprimitive]) == str_rprimitive |
| 29 |
|
| 30 |
def test_tuple_expansion(self) -> None: |
| 31 |
assert expand_rtype( |
| 32 |
RTuple([RTypeVar(0), RTypeVar(1)]), [str_rprimitive, int_rprimitive] |
| 33 |
) == RTuple([str_rprimitive, int_rprimitive]) |
| 34 |
|
| 35 |
def test_union_expansion(self) -> None: |
| 36 |
assert expand_rtype( |
| 37 |
RUnion([RTypeVar(0), RTypeVar(1)]), [str_rprimitive, int_rprimitive] |
| 38 |
) == RUnion([str_rprimitive, int_rprimitive]) |
| 39 |
|
| 40 |
def test_vec_expansion(self) -> None: |
| 41 |
assert expand_rtype(RVec(RTypeVar(0)), [str_rprimitive]) == RVec(str_rprimitive) |
| 42 |
|
| 43 |
def test_nested_expansion(self) -> None: |
| 44 |
typ = RUnion([RTuple([RVec(RTypeVar(0)), RTypeVar(1)]), RVec(RVec(RTypeVar(0)))]) |
| 45 |
expected = RUnion( |
| 46 |
[RTuple([RVec(str_rprimitive), int_rprimitive]), RVec(RVec(str_rprimitive))] |
| 47 |
) |
| 48 |
assert expand_rtype(typ, [str_rprimitive, int_rprimitive]) == expected |