test_core.py 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. import unittest
  3. import re
  4. from gtts.tokenizer.core import (
  5. RegexBuilder,
  6. PreProcessorRegex,
  7. PreProcessorSub,
  8. Tokenizer,
  9. )
  10. # Tests based on classes usage examples
  11. # See class documentation for details
  12. class TestRegexBuilder(unittest.TestCase):
  13. def test_regexbuilder(self):
  14. rb = RegexBuilder("abc", lambda x: "{}".format(x))
  15. self.assertEqual(rb.regex, re.compile("a|b|c"))
  16. class TestPreProcessorRegex(unittest.TestCase):
  17. def test_preprocessorregex(self):
  18. pp = PreProcessorRegex("ab", lambda x: "{}".format(x), "c")
  19. self.assertEqual(len(pp.regexes), 2)
  20. self.assertEqual(pp.regexes[0].pattern, "a")
  21. self.assertEqual(pp.regexes[1].pattern, "b")
  22. class TestPreProcessorSub(unittest.TestCase):
  23. def test_proprocessorsub(self):
  24. sub_pairs = [("Mac", "PC"), ("Firefox", "Chrome")]
  25. pp = PreProcessorSub(sub_pairs)
  26. _in = "I use firefox on my mac"
  27. _out = "I use Chrome on my PC"
  28. self.assertEqual(pp.run(_in), _out)
  29. class TestTokenizer(unittest.TestCase):
  30. # tokenizer case 1
  31. def case1(self):
  32. return re.compile(r"\,")
  33. # tokenizer case 2
  34. def case2(self):
  35. return RegexBuilder("abc", lambda x: r"{}\.".format(x)).regex
  36. def test_tokenizer(self):
  37. t = Tokenizer([self.case1, self.case2])
  38. _in = "Hello, my name is Linda a. Call me Lin, b. I'm your friend"
  39. _out = ["Hello", " my name is Linda ", " Call me Lin", " ", " I'm your friend"]
  40. self.assertEqual(t.run(_in), _out)
  41. def test_bad_params_not_list(self):
  42. # original exception: TypeError
  43. with self.assertRaises(TypeError):
  44. Tokenizer(self.case1)
  45. def test_bad_params_not_callable(self):
  46. # original exception: TypeError
  47. with self.assertRaises(TypeError):
  48. Tokenizer([100])
  49. def test_bad_params_not_callable_returning_regex(self):
  50. # original exception: AttributeError
  51. def not_regex():
  52. return 1
  53. with self.assertRaises(TypeError):
  54. Tokenizer([not_regex])
  55. if __name__ == "__main__":
  56. unittest.main()