test_regular_languages.py 3.2 KB

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