test_writer.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import unittest
  2. from src.writer import write_statements
  3. from src.statement import Statement as S, Block as B
  4. class TestWriter(unittest.TestCase):
  5. def setUp(self):
  6. self.foo = S('command', 'move', '$regA', '$regB')
  7. self.bar = S('command', 'addu', '$regC', '$regA', '$regB')
  8. def tearDown(self):
  9. del self.foo
  10. del self.bar
  11. def test_writer_one(self):
  12. output = write_statements([self.foo])
  13. expect = "\tmove\t$regA,$regB\n"
  14. self.assertEqual(output, expect)
  15. def test_writer_longname(self):
  16. command = S('command', 'movemovemove', '$regA', '$regB')
  17. output = write_statements([command])
  18. expect = "\tmovemovemove $regA,$regB\n"
  19. self.assertEqual(output, expect)
  20. def test_writer_several(self):
  21. output = write_statements([self.foo, self.bar, self.foo])
  22. expect = "\tmove\t$regA,$regB\n" \
  23. + "\taddu\t$regC,$regA,$regB\n" \
  24. + "\tmove\t$regA,$regB\n"
  25. self.assertEqual(output, expect)
  26. def test_writer_with_label(self):
  27. label = S('label', '$L1')
  28. output = write_statements([self.foo, label, self.bar])
  29. expect = "\tmove\t$regA,$regB\n" \
  30. + "$L1:\n" \
  31. + "\taddu\t$regC,$regA,$regB\n"
  32. self.assertEqual(output, expect)
  33. def test_writer_with_comment(self):
  34. comment = S('comment', 'tralala')
  35. output = write_statements([self.foo, comment, self.bar])
  36. expect = "\tmove\t$regA,$regB\n" \
  37. + "\n#tralala\n\n" \
  38. + "\taddu\t$regC,$regA,$regB\n"
  39. self.assertEqual(output, expect)
  40. def test_writer_with_comment_non_tabbed(self):
  41. directive = S('comment', 'tralala')
  42. output = write_statements([directive, self.foo, self.bar])
  43. expect = "\n#tralala\n\n" \
  44. + "\tmove\t$regA,$regB\n" \
  45. + "\taddu\t$regC,$regA,$regB\n"
  46. self.assertEqual(output, expect)
  47. def test_writer_with_inlinecomment(self):
  48. self.foo.options['comment'] = 'tralala'
  49. output = write_statements([self.foo, self.bar])
  50. expect = "\tmove\t$regA,$regB" \
  51. + "\t\t#tralala\n" \
  52. + "\taddu\t$regC,$regA,$regB\n"
  53. self.assertEqual(output, expect)
  54. def test_writer_with_directive(self):
  55. directive = S('directive', '.tralala trololo')
  56. output = write_statements([self.foo, directive, self.bar])
  57. expect = "\tmove\t$regA,$regB\n" \
  58. + "\t.tralala trololo\n" \
  59. + "\taddu\t$regC,$regA,$regB\n"
  60. self.assertEqual(output, expect)