test_utils.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. # -*- coding: utf-8 -*-
  2. import pytest
  3. from gtts.utils import _minimize, _clean_tokens, _translate_url
  4. delim = " "
  5. Lmax = 10
  6. def test_ascii():
  7. _in = "Bacon ipsum dolor sit amet"
  8. _out = ["Bacon", "ipsum", "dolor sit", "amet"]
  9. assert _minimize(_in, delim, Lmax) == _out
  10. def test_ascii_no_delim():
  11. _in = "Baconipsumdolorsitametflankcornedbee"
  12. _out = ["Baconipsum", "dolorsitam", "etflankcor", "nedbee"]
  13. assert _minimize(_in, delim, Lmax) == _out
  14. def test_unicode():
  15. _in = u"这是一个三岁的小孩在讲述他从一系列照片里看到的东西。"
  16. _out = [u"这是一个三岁的小孩在", u"讲述他从一系列照片里", u"看到的东西。"]
  17. assert _minimize(_in, delim, Lmax) == _out
  18. def test_startwith_delim():
  19. _in = delim + "test"
  20. _out = ["test"]
  21. assert _minimize(_in, delim, Lmax) == _out
  22. def test_len_ascii():
  23. text = "Bacon ipsum dolor sit amet flank corned beef."
  24. assert len(text) == 45
  25. def test_len_unicode():
  26. text = u"但在一个重要的任务上"
  27. assert len(text) == 10
  28. def test_only_space_and_punc():
  29. _in = [",(:)?", "\t ", "\n"]
  30. _out = []
  31. assert _clean_tokens(_in) == _out
  32. def test_strip():
  33. _in = [" Bacon ", "& ", "ipsum\r", "."]
  34. _out = ["Bacon", "&", "ipsum"]
  35. assert _clean_tokens(_in) == _out
  36. def test_translate_url():
  37. _in = {"tld": "qwerty", "path": "asdf"}
  38. _out = "https://translate.google.qwerty/asdf"
  39. assert _translate_url(**_in) == _out
  40. if __name__ == "__main__":
  41. pytest.main(["-x", __file__])