test_emit.py 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. from __future__ import annotations
  2. import unittest
  3. from mypyc.codegen.emit import Emitter, EmitterContext
  4. from mypyc.ir.ops import BasicBlock, Register, Value
  5. from mypyc.ir.rtypes import int_rprimitive
  6. from mypyc.namegen import NameGenerator
  7. class TestEmitter(unittest.TestCase):
  8. def setUp(self) -> None:
  9. self.n = Register(int_rprimitive, "n")
  10. self.context = EmitterContext(NameGenerator([["mod"]]))
  11. def test_label(self) -> None:
  12. emitter = Emitter(self.context, {})
  13. assert emitter.label(BasicBlock(4)) == "CPyL4"
  14. def test_reg(self) -> None:
  15. names: dict[Value, str] = {self.n: "n"}
  16. emitter = Emitter(self.context, names)
  17. assert emitter.reg(self.n) == "cpy_r_n"
  18. def test_object_annotation(self) -> None:
  19. emitter = Emitter(self.context, {})
  20. assert emitter.object_annotation("hello, world", "line;") == " /* 'hello, world' */"
  21. assert (
  22. emitter.object_annotation(list(range(30)), "line;")
  23. == """\
  24. /* [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22,
  25. 23, 24, 25, 26, 27, 28, 29] */"""
  26. )
  27. def test_emit_line(self) -> None:
  28. emitter = Emitter(self.context, {})
  29. emitter.emit_line("line;")
  30. emitter.emit_line("a {")
  31. emitter.emit_line("f();")
  32. emitter.emit_line("}")
  33. assert emitter.fragments == ["line;\n", "a {\n", " f();\n", "}\n"]
  34. emitter = Emitter(self.context, {})
  35. emitter.emit_line("CPyStatics[0];", ann="hello, world")
  36. emitter.emit_line("CPyStatics[1];", ann=list(range(30)))
  37. assert emitter.fragments[0] == "CPyStatics[0]; /* 'hello, world' */\n"
  38. assert (
  39. emitter.fragments[1]
  40. == """\
  41. CPyStatics[1]; /* [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
  42. 21, 22, 23, 24, 25, 26, 27, 28, 29] */\n"""
  43. )