TestStringEncoding.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # -*- coding: utf-8 -*-
  2. import sys
  3. import unittest
  4. import Cython.Compiler.StringEncoding as StringEncoding
  5. class StringEncodingTest(unittest.TestCase):
  6. """
  7. Test the StringEncoding module.
  8. """
  9. def test_string_contains_lone_surrogates(self):
  10. self.assertFalse(StringEncoding.string_contains_lone_surrogates(u"abc"))
  11. self.assertFalse(StringEncoding.string_contains_lone_surrogates(u"\uABCD"))
  12. self.assertFalse(StringEncoding.string_contains_lone_surrogates(u"\N{SNOWMAN}"))
  13. # This behaves differently in Py2 when freshly parsed and read from a .pyc file,
  14. # but it seems to be a marshalling bug in Py2, which doesn't hurt us in Cython.
  15. if sys.version_info[0] != 2:
  16. self.assertTrue(StringEncoding.string_contains_lone_surrogates(u"\uD800\uDFFF"))
  17. # In Py2 with 16bit Unicode, the following is indistinguishable from the 32bit character.
  18. obfuscated_surrogate_pair = (u"\uDFFF" + "\uD800")[::-1]
  19. if sys.version_info[0] == 2 and sys.maxunicode == 65565:
  20. self.assertFalse(StringEncoding.string_contains_lone_surrogates(obfuscated_surrogate_pair))
  21. else:
  22. self.assertTrue(StringEncoding.string_contains_lone_surrogates(obfuscated_surrogate_pair))
  23. self.assertTrue(StringEncoding.string_contains_lone_surrogates(u"\uD800"))
  24. self.assertTrue(StringEncoding.string_contains_lone_surrogates(u"\uDFFF"))
  25. self.assertTrue(StringEncoding.string_contains_lone_surrogates(u"\uDFFF\uD800"))
  26. self.assertTrue(StringEncoding.string_contains_lone_surrogates(u"\uD800x\uDFFF"))
  27. def test_string_contains_surrogates(self):
  28. self.assertFalse(StringEncoding.string_contains_surrogates(u"abc"))
  29. self.assertFalse(StringEncoding.string_contains_surrogates(u"\uABCD"))
  30. self.assertFalse(StringEncoding.string_contains_surrogates(u"\N{SNOWMAN}"))
  31. self.assertTrue(StringEncoding.string_contains_surrogates(u"\uD800"))
  32. self.assertTrue(StringEncoding.string_contains_surrogates(u"\uDFFF"))
  33. self.assertTrue(StringEncoding.string_contains_surrogates(u"\uD800\uDFFF"))
  34. self.assertTrue(StringEncoding.string_contains_surrogates(u"\uDFFF\uD800"))
  35. self.assertTrue(StringEncoding.string_contains_surrogates(u"\uD800x\uDFFF"))