test_key_binding.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. from __future__ import unicode_literals
  2. from prompt_toolkit.key_binding.input_processor import InputProcessor, KeyPress
  3. from prompt_toolkit.key_binding.registry import Registry
  4. from prompt_toolkit.keys import Keys
  5. import pytest
  6. class Handlers(object):
  7. def __init__(self):
  8. self.called = []
  9. def __getattr__(self, name):
  10. def func(event):
  11. self.called.append(name)
  12. return func
  13. @pytest.fixture
  14. def handlers():
  15. return Handlers()
  16. @pytest.fixture
  17. def registry(handlers):
  18. registry = Registry()
  19. registry.add_binding(
  20. Keys.ControlX, Keys.ControlC)(handlers.controlx_controlc)
  21. registry.add_binding(Keys.ControlX)(handlers.control_x)
  22. registry.add_binding(Keys.ControlD)(handlers.control_d)
  23. registry.add_binding(
  24. Keys.ControlSquareClose, Keys.Any)(handlers.control_square_close_any)
  25. return registry
  26. @pytest.fixture
  27. def processor(registry):
  28. return InputProcessor(registry, lambda: None)
  29. def test_feed_simple(processor, handlers):
  30. processor.feed(KeyPress(Keys.ControlX, '\x18'))
  31. processor.feed(KeyPress(Keys.ControlC, '\x03'))
  32. processor.process_keys()
  33. assert handlers.called == ['controlx_controlc']
  34. def test_feed_several(processor, handlers):
  35. # First an unknown key first.
  36. processor.feed(KeyPress(Keys.ControlQ, ''))
  37. processor.process_keys()
  38. assert handlers.called == []
  39. # Followed by a know key sequence.
  40. processor.feed(KeyPress(Keys.ControlX, ''))
  41. processor.feed(KeyPress(Keys.ControlC, ''))
  42. processor.process_keys()
  43. assert handlers.called == ['controlx_controlc']
  44. # Followed by another unknown sequence.
  45. processor.feed(KeyPress(Keys.ControlR, ''))
  46. processor.feed(KeyPress(Keys.ControlS, ''))
  47. # Followed again by a know key sequence.
  48. processor.feed(KeyPress(Keys.ControlD, ''))
  49. processor.process_keys()
  50. assert handlers.called == ['controlx_controlc', 'control_d']
  51. def test_control_square_closed_any(processor, handlers):
  52. processor.feed(KeyPress(Keys.ControlSquareClose, ''))
  53. processor.feed(KeyPress('C', 'C'))
  54. processor.process_keys()
  55. assert handlers.called == ['control_square_close_any']
  56. def test_common_prefix(processor, handlers):
  57. # Sending Control_X should not yet do anything, because there is
  58. # another sequence starting with that as well.
  59. processor.feed(KeyPress(Keys.ControlX, ''))
  60. processor.process_keys()
  61. assert handlers.called == []
  62. # When another key is pressed, we know that we did not meant the longer
  63. # "ControlX ControlC" sequence and the callbacks are called.
  64. processor.feed(KeyPress(Keys.ControlD, ''))
  65. processor.process_keys()
  66. assert handlers.called == ['control_x', 'control_d']
  67. def test_previous_key_sequence(processor, handlers):
  68. """
  69. test whether we receive the correct previous_key_sequence.
  70. """
  71. events = []
  72. def handler(event):
  73. events.append(event)
  74. # Build registry.
  75. registry = Registry()
  76. registry.add_binding('a', 'a')(handler)
  77. registry.add_binding('b', 'b')(handler)
  78. processor = InputProcessor(registry, lambda: None)
  79. # Create processor and feed keys.
  80. processor.feed(KeyPress('a', 'a'))
  81. processor.feed(KeyPress('a', 'a'))
  82. processor.feed(KeyPress('b', 'b'))
  83. processor.feed(KeyPress('b', 'b'))
  84. processor.process_keys()
  85. # Test.
  86. assert len(events) == 2
  87. assert len(events[0].key_sequence) == 2
  88. assert events[0].key_sequence[0].key == 'a'
  89. assert events[0].key_sequence[0].data == 'a'
  90. assert events[0].key_sequence[1].key == 'a'
  91. assert events[0].key_sequence[1].data == 'a'
  92. assert events[0].previous_key_sequence == []
  93. assert len(events[1].key_sequence) == 2
  94. assert events[1].key_sequence[0].key == 'b'
  95. assert events[1].key_sequence[0].data == 'b'
  96. assert events[1].key_sequence[1].key == 'b'
  97. assert events[1].key_sequence[1].data == 'b'
  98. assert len(events[1].previous_key_sequence) == 2
  99. assert events[1].previous_key_sequence[0].key == 'a'
  100. assert events[1].previous_key_sequence[0].data == 'a'
  101. assert events[1].previous_key_sequence[1].key == 'a'
  102. assert events[1].previous_key_sequence[1].data == 'a'