TestStringIOTree.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. import unittest
  2. from Cython import StringIOTree as stringtree
  3. code = """
  4. cdef int spam # line 1
  5. cdef ham():
  6. a = 1
  7. b = 2
  8. c = 3
  9. d = 4
  10. def eggs():
  11. pass
  12. cpdef bacon():
  13. print spam
  14. print 'scotch'
  15. print 'tea?'
  16. print 'or coffee?' # line 16
  17. """
  18. linemap = dict(enumerate(code.splitlines()))
  19. class TestStringIOTree(unittest.TestCase):
  20. def setUp(self):
  21. self.tree = stringtree.StringIOTree()
  22. def test_markers(self):
  23. assert not self.tree.allmarkers()
  24. def test_insertion(self):
  25. self.write_lines((1, 2, 3))
  26. line_4_to_6_insertion_point = self.tree.insertion_point()
  27. self.write_lines((7, 8))
  28. line_9_to_13_insertion_point = self.tree.insertion_point()
  29. self.write_lines((14, 15, 16))
  30. line_4_insertion_point = line_4_to_6_insertion_point.insertion_point()
  31. self.write_lines((5, 6), tree=line_4_to_6_insertion_point)
  32. line_9_to_12_insertion_point = (
  33. line_9_to_13_insertion_point.insertion_point())
  34. self.write_line(13, tree=line_9_to_13_insertion_point)
  35. self.write_line(4, tree=line_4_insertion_point)
  36. self.write_line(9, tree=line_9_to_12_insertion_point)
  37. line_10_insertion_point = line_9_to_12_insertion_point.insertion_point()
  38. self.write_line(11, tree=line_9_to_12_insertion_point)
  39. self.write_line(10, tree=line_10_insertion_point)
  40. self.write_line(12, tree=line_9_to_12_insertion_point)
  41. self.assertEqual(self.tree.allmarkers(), list(range(1, 17)))
  42. self.assertEqual(code.strip(), self.tree.getvalue().strip())
  43. def write_lines(self, linenos, tree=None):
  44. for lineno in linenos:
  45. self.write_line(lineno, tree=tree)
  46. def write_line(self, lineno, tree=None):
  47. if tree is None:
  48. tree = self.tree
  49. tree.markers.append(lineno)
  50. tree.write(linemap[lineno] + '\n')