TestCodeWriter.py 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. from Cython.TestUtils import CythonTest
  2. class TestCodeWriter(CythonTest):
  3. # CythonTest uses the CodeWriter heavily, so do some checking by
  4. # roundtripping Cython code through the test framework.
  5. # Note that this test is dependent upon the normal Cython parser
  6. # to generate the input trees to the CodeWriter. This save *a lot*
  7. # of time; better to spend that time writing other tests than perfecting
  8. # this one...
  9. # Whitespace is very significant in this process:
  10. # - always newline on new block (!)
  11. # - indent 4 spaces
  12. # - 1 space around every operator
  13. def t(self, codestr):
  14. self.assertCode(codestr, self.fragment(codestr).root)
  15. def test_print(self):
  16. self.t(u"""
  17. print x, y
  18. print x + y ** 2
  19. print x, y, z,
  20. """)
  21. def test_if(self):
  22. self.t(u"if x:\n pass")
  23. def test_ifelifelse(self):
  24. self.t(u"""
  25. if x:
  26. pass
  27. elif y:
  28. pass
  29. elif z + 34 ** 34 - 2:
  30. pass
  31. else:
  32. pass
  33. """)
  34. def test_def(self):
  35. self.t(u"""
  36. def f(x, y, z):
  37. pass
  38. def f(x = 34, y = 54, z):
  39. pass
  40. """)
  41. def test_longness_and_signedness(self):
  42. self.t(u"def f(unsigned long long long long long int y):\n pass")
  43. def test_signed_short(self):
  44. self.t(u"def f(signed short int y):\n pass")
  45. def test_typed_args(self):
  46. self.t(u"def f(int x, unsigned long int y):\n pass")
  47. def test_cdef_var(self):
  48. self.t(u"""
  49. cdef int hello
  50. cdef int hello = 4, x = 3, y, z
  51. """)
  52. def test_for_loop(self):
  53. self.t(u"""
  54. for x, y, z in f(g(h(34) * 2) + 23):
  55. print x, y, z
  56. else:
  57. print 43
  58. """)
  59. def test_inplace_assignment(self):
  60. self.t(u"x += 43")
  61. def test_attribute(self):
  62. self.t(u"a.x")
  63. if __name__ == "__main__":
  64. import unittest
  65. unittest.main()