TestStripLiterals.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. from Cython.Build.Dependencies import strip_string_literals
  2. from Cython.TestUtils import CythonTest
  3. class TestStripLiterals(CythonTest):
  4. def t(self, before, expected):
  5. actual, literals = strip_string_literals(before, prefix="_L")
  6. self.assertEqual(expected, actual)
  7. for key, value in literals.items():
  8. actual = actual.replace(key, value)
  9. self.assertEqual(before, actual)
  10. def test_empty(self):
  11. self.t("", "")
  12. def test_single_quote(self):
  13. self.t("'x'", "'_L1_'")
  14. def test_double_quote(self):
  15. self.t('"x"', '"_L1_"')
  16. def test_nested_quotes(self):
  17. self.t(""" '"' "'" """, """ '_L1_' "_L2_" """)
  18. def test_triple_quote(self):
  19. self.t(" '''a\n''' ", " '''_L1_''' ")
  20. def test_backslash(self):
  21. self.t(r"'a\'b'", "'_L1_'")
  22. self.t(r"'a\\'", "'_L1_'")
  23. self.t(r"'a\\\'b'", "'_L1_'")
  24. def test_unicode(self):
  25. self.t("u'abc'", "u'_L1_'")
  26. def test_raw(self):
  27. self.t(r"r'abc\\'", "r'_L1_'")
  28. def test_raw_unicode(self):
  29. self.t(r"ru'abc\\'", "ru'_L1_'")
  30. def test_comment(self):
  31. self.t("abc # foo", "abc #_L1_")
  32. def test_comment_and_quote(self):
  33. self.t("abc # 'x'", "abc #_L1_")
  34. self.t("'abc#'", "'_L1_'")
  35. def test_include(self):
  36. self.t("include 'a.pxi' # something here",
  37. "include '_L1_' #_L2_")
  38. def test_extern(self):
  39. self.t("cdef extern from 'a.h': # comment",
  40. "cdef extern from '_L1_': #_L2_")