test_shortcuts.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. from prompt_toolkit.shortcuts import _split_multiline_prompt
  2. from prompt_toolkit.token import Token
  3. def test_split_multiline_prompt():
  4. # Test 1: no newlines:
  5. tokens = [(Token, 'ab')]
  6. has_before_tokens, before, first_input_line = _split_multiline_prompt(lambda cli: tokens)
  7. assert has_before_tokens(None) is False
  8. assert before(None) == []
  9. assert first_input_line(None) == [
  10. (Token, 'a'),
  11. (Token, 'b'),
  12. ]
  13. # Test 1: multiple lines.
  14. tokens = [(Token, 'ab\ncd\nef')]
  15. has_before_tokens, before, first_input_line = _split_multiline_prompt(lambda cli: tokens)
  16. assert has_before_tokens(None) is True
  17. assert before(None) == [
  18. (Token, 'a'),
  19. (Token, 'b'),
  20. (Token, '\n'),
  21. (Token, 'c'),
  22. (Token, 'd'),
  23. ]
  24. assert first_input_line(None) == [
  25. (Token, 'e'),
  26. (Token, 'f'),
  27. ]
  28. # Edge case 1: starting with a newline.
  29. tokens = [(Token, '\nab')]
  30. has_before_tokens, before, first_input_line = _split_multiline_prompt(lambda cli: tokens)
  31. assert has_before_tokens(None) is True
  32. assert before(None) == []
  33. assert first_input_line(None) == [
  34. (Token, 'a'),
  35. (Token, 'b')
  36. ]
  37. # Edge case 2: starting with two newlines.
  38. tokens = [(Token, '\n\nab')]
  39. has_before_tokens, before, first_input_line = _split_multiline_prompt(lambda cli: tokens)
  40. assert has_before_tokens(None) is True
  41. assert before(None) == [(Token, '\n')]
  42. assert first_input_line(None) == [
  43. (Token, 'a'),
  44. (Token, 'b')
  45. ]