test_truncate.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. """
  2. Tests for truncate functions
  3. """
  4. import sys
  5. import pytest
  6. import hypothesis as ht
  7. import hypothesis.strategies as htst
  8. from truncate import (
  9. truncate_funcs,
  10. truncate_by_concating,
  11. truncate_by_backing_up_bytes,
  12. )
  13. @pytest.mark.parametrize('truncate', truncate_funcs)
  14. class Test_manual:
  15. def test_ascii_uncut(self, truncate):
  16. s = "A normal string"
  17. assert s == truncate(s, sys.maxsize)
  18. def test_ascii_cut(self, truncate):
  19. s = "A normal string"
  20. max_len = 8
  21. assert s[0:max_len] == truncate(s, max_len)
  22. def test_unicode_uncut(self, truncate):
  23. s = "NO。121 TUCHENKONG VILLAGE"
  24. assert s == truncate(s, sys.maxsize)
  25. def test_unicode_cut(self, truncate):
  26. s = "NO。121 TUCHENKONG VILLAGE"
  27. max_bytes = 12
  28. max_chars = 10
  29. assert s[0:max_chars] == truncate(s, max_bytes)
  30. #
  31. # hypothesis stuff
  32. #
  33. ascii_abc = [chr(x) for x in range(128)]
  34. ht.settings.register_profile(
  35. 'base', max_examples=200, verbosity=ht.Verbosity.verbose
  36. )
  37. ht.settings.load_profile('base')
  38. @pytest.mark.parametrize('truncate', truncate_funcs)
  39. class Test_propbased:
  40. @ht.given(s=htst.text(alphabet=ascii_abc))
  41. def test_ascii_uncut(self, s, truncate):
  42. assert s == truncate(s, sys.maxsize)
  43. @ht.given(
  44. s=htst.text(alphabet=ascii_abc), max_len=htst.integers(min_value=0)
  45. )
  46. def test_ascii_cut(self, s, max_len, truncate):
  47. assert s[0:max_len] == truncate(s, max_len)
  48. @ht.given(s=htst.text())
  49. def test_unicode_uncut(self, s, truncate):
  50. assert s == truncate(s, sys.maxsize)
  51. @ht.given(
  52. s=htst.text(), max_len=htst.integers(min_value=0, max_value=10000)
  53. )
  54. def test_unicode_cut(self, s, max_len, truncate):
  55. t = truncate(s, max_len)
  56. assert s.startswith(t)
  57. @ht.given(s=htst.text(), max_len=htst.integers(min_value=0, max_value=10000))
  58. def test_propbased_assert_each_other(s, max_len):
  59. assert truncate_by_concating(s, max_len) == truncate_by_backing_up_bytes(
  60. s, max_len
  61. )