test_regular_languages.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. from __future__ import annotations
  2. from prompt_toolkit.completion import CompleteEvent, Completer, Completion
  3. from prompt_toolkit.contrib.regular_languages import compile
  4. from prompt_toolkit.contrib.regular_languages.compiler import Match, Variables
  5. from prompt_toolkit.contrib.regular_languages.completion import GrammarCompleter
  6. from prompt_toolkit.document import Document
  7. def test_simple_match():
  8. g = compile("hello|world")
  9. m = g.match("hello")
  10. assert isinstance(m, Match)
  11. m = g.match("world")
  12. assert isinstance(m, Match)
  13. m = g.match("somethingelse")
  14. assert m is None
  15. def test_variable_varname():
  16. """
  17. Test `Variable` with varname.
  18. """
  19. g = compile("((?P<varname>hello|world)|test)")
  20. m = g.match("hello")
  21. variables = m.variables()
  22. assert isinstance(variables, Variables)
  23. assert variables.get("varname") == "hello"
  24. assert variables["varname"] == "hello"
  25. m = g.match("world")
  26. variables = m.variables()
  27. assert isinstance(variables, Variables)
  28. assert variables.get("varname") == "world"
  29. assert variables["varname"] == "world"
  30. m = g.match("test")
  31. variables = m.variables()
  32. assert isinstance(variables, Variables)
  33. assert variables.get("varname") is None
  34. assert variables["varname"] is None
  35. def test_prefix():
  36. """
  37. Test `match_prefix`.
  38. """
  39. g = compile(r"(hello\ world|something\ else)")
  40. m = g.match_prefix("hello world")
  41. assert isinstance(m, Match)
  42. m = g.match_prefix("he")
  43. assert isinstance(m, Match)
  44. m = g.match_prefix("")
  45. assert isinstance(m, Match)
  46. m = g.match_prefix("som")
  47. assert isinstance(m, Match)
  48. m = g.match_prefix("hello wor")
  49. assert isinstance(m, Match)
  50. m = g.match_prefix("no-match")
  51. assert m.trailing_input().start == 0
  52. assert m.trailing_input().stop == len("no-match")
  53. m = g.match_prefix("hellotest")
  54. assert m.trailing_input().start == len("hello")
  55. assert m.trailing_input().stop == len("hellotest")
  56. def test_completer():
  57. class completer1(Completer):
  58. def get_completions(self, document, complete_event):
  59. yield Completion(f"before-{document.text}-after", -len(document.text))
  60. yield Completion(f"before-{document.text}-after-B", -len(document.text))
  61. class completer2(Completer):
  62. def get_completions(self, document, complete_event):
  63. yield Completion(f"before2-{document.text}-after2", -len(document.text))
  64. yield Completion(f"before2-{document.text}-after2-B", -len(document.text))
  65. # Create grammar. "var1" + "whitespace" + "var2"
  66. g = compile(r"(?P<var1>[a-z]*) \s+ (?P<var2>[a-z]*)")
  67. # Test 'get_completions()'
  68. completer = GrammarCompleter(g, {"var1": completer1(), "var2": completer2()})
  69. completions = list(
  70. completer.get_completions(Document("abc def", len("abc def")), CompleteEvent())
  71. )
  72. assert len(completions) == 2
  73. assert completions[0].text == "before2-def-after2"
  74. assert completions[0].start_position == -3
  75. assert completions[1].text == "before2-def-after2-B"
  76. assert completions[1].start_position == -3