test_inputstream.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from __future__ import annotations
  2. import pytest
  3. from prompt_toolkit.input.vt100_parser import Vt100Parser
  4. from prompt_toolkit.keys import Keys
  5. class _ProcessorMock:
  6. def __init__(self):
  7. self.keys = []
  8. def feed_key(self, key_press):
  9. self.keys.append(key_press)
  10. @pytest.fixture
  11. def processor():
  12. return _ProcessorMock()
  13. @pytest.fixture
  14. def stream(processor):
  15. return Vt100Parser(processor.feed_key)
  16. def test_control_keys(processor, stream):
  17. stream.feed("\x01\x02\x10")
  18. assert len(processor.keys) == 3
  19. assert processor.keys[0].key == Keys.ControlA
  20. assert processor.keys[1].key == Keys.ControlB
  21. assert processor.keys[2].key == Keys.ControlP
  22. assert processor.keys[0].data == "\x01"
  23. assert processor.keys[1].data == "\x02"
  24. assert processor.keys[2].data == "\x10"
  25. def test_arrows(processor, stream):
  26. stream.feed("\x1b[A\x1b[B\x1b[C\x1b[D")
  27. assert len(processor.keys) == 4
  28. assert processor.keys[0].key == Keys.Up
  29. assert processor.keys[1].key == Keys.Down
  30. assert processor.keys[2].key == Keys.Right
  31. assert processor.keys[3].key == Keys.Left
  32. assert processor.keys[0].data == "\x1b[A"
  33. assert processor.keys[1].data == "\x1b[B"
  34. assert processor.keys[2].data == "\x1b[C"
  35. assert processor.keys[3].data == "\x1b[D"
  36. def test_escape(processor, stream):
  37. stream.feed("\x1bhello")
  38. assert len(processor.keys) == 1 + len("hello")
  39. assert processor.keys[0].key == Keys.Escape
  40. assert processor.keys[1].key == "h"
  41. assert processor.keys[0].data == "\x1b"
  42. assert processor.keys[1].data == "h"
  43. def test_special_double_keys(processor, stream):
  44. stream.feed("\x1b[1;3D") # Should both send escape and left.
  45. assert len(processor.keys) == 2
  46. assert processor.keys[0].key == Keys.Escape
  47. assert processor.keys[1].key == Keys.Left
  48. assert processor.keys[0].data == "\x1b[1;3D"
  49. assert processor.keys[1].data == ""
  50. def test_flush_1(processor, stream):
  51. # Send left key in two parts without flush.
  52. stream.feed("\x1b")
  53. stream.feed("[D")
  54. assert len(processor.keys) == 1
  55. assert processor.keys[0].key == Keys.Left
  56. assert processor.keys[0].data == "\x1b[D"
  57. def test_flush_2(processor, stream):
  58. # Send left key with a 'Flush' in between.
  59. # The flush should make sure that we process everything before as-is,
  60. # with makes the first part just an escape character instead.
  61. stream.feed("\x1b")
  62. stream.flush()
  63. stream.feed("[D")
  64. assert len(processor.keys) == 3
  65. assert processor.keys[0].key == Keys.Escape
  66. assert processor.keys[1].key == "["
  67. assert processor.keys[2].key == "D"
  68. assert processor.keys[0].data == "\x1b"
  69. assert processor.keys[1].data == "["
  70. assert processor.keys[2].data == "D"
  71. def test_meta_arrows(processor, stream):
  72. stream.feed("\x1b\x1b[D")
  73. assert len(processor.keys) == 2
  74. assert processor.keys[0].key == Keys.Escape
  75. assert processor.keys[1].key == Keys.Left
  76. def test_control_square_close(processor, stream):
  77. stream.feed("\x1dC")
  78. assert len(processor.keys) == 2
  79. assert processor.keys[0].key == Keys.ControlSquareClose
  80. assert processor.keys[1].key == "C"
  81. def test_invalid(processor, stream):
  82. # Invalid sequence that has at two characters in common with other
  83. # sequences.
  84. stream.feed("\x1b[*")
  85. assert len(processor.keys) == 3
  86. assert processor.keys[0].key == Keys.Escape
  87. assert processor.keys[1].key == "["
  88. assert processor.keys[2].key == "*"
  89. def test_cpr_response(processor, stream):
  90. stream.feed("a\x1b[40;10Rb")
  91. assert len(processor.keys) == 3
  92. assert processor.keys[0].key == "a"
  93. assert processor.keys[1].key == Keys.CPRResponse
  94. assert processor.keys[2].key == "b"
  95. def test_cpr_response_2(processor, stream):
  96. # Make sure that the newline is not included in the CPR response.
  97. stream.feed("\x1b[40;1R\n")
  98. assert len(processor.keys) == 2
  99. assert processor.keys[0].key == Keys.CPRResponse
  100. assert processor.keys[1].key == Keys.ControlJ