diff options
author | Jonathan Slenders <jonathan@slenders.be> | 2017-03-26 19:05:01 +0200 |
---|---|---|
committer | Jonathan Slenders <jonathan@slenders.be> | 2017-03-26 19:05:01 +0200 |
commit | 96a6435558251aa68e603cd7ca8b0388678ac557 (patch) | |
tree | c900a5a3192f6d7b57b7a9fbc3346f1851794ebe | |
parent | ce49e8495bc998bdbae99ba6f1f601ed70c52e55 (diff) |
Fixes previous_key_sequences: make a copy of key_sequences before calling the handler + initialize to an empty list.
-rw-r--r-- | prompt_toolkit/key_binding/input_processor.py | 4 | ||||
-rw-r--r-- | tests/test_key_binding.py | 42 |
2 files changed, 44 insertions, 2 deletions
diff --git a/prompt_toolkit/key_binding/input_processor.py b/prompt_toolkit/key_binding/input_processor.py index a57e3aa3..51a31108 100644 --- a/prompt_toolkit/key_binding/input_processor.py +++ b/prompt_toolkit/key_binding/input_processor.py @@ -93,7 +93,7 @@ class InputProcessor(object): self.reset() def reset(self): - self._previous_key_sequence = None + self._previous_key_sequence = [] self._previous_handler = None self._process_coroutine = self._process() @@ -173,7 +173,7 @@ class InputProcessor(object): # Exact matches found, call handler. if not is_prefix_of_longer_match and matches: - self._call_handler(matches[-1], key_sequence=buffer) + self._call_handler(matches[-1], key_sequence=buffer[:]) del buffer[:] # Keep reference. # No match found. diff --git a/tests/test_key_binding.py b/tests/test_key_binding.py index 04f6b5f8..d706c444 100644 --- a/tests/test_key_binding.py +++ b/tests/test_key_binding.py @@ -96,3 +96,45 @@ def test_common_prefix(processor, handlers): processor.process_keys() assert handlers.called == ['control_x', 'control_d'] + + +def test_previous_key_sequence(processor, handlers): + """ + test whether we receive the correct previous_key_sequence. + """ + events = [] + def handler(event): + events.append(event) + + # Build registry. + registry = Registry() + registry.add_binding('a', 'a')(handler) + registry.add_binding('b', 'b')(handler) + processor = InputProcessor(registry, lambda: None) + + # Create processor and feed keys. + processor.feed(KeyPress('a', 'a')) + processor.feed(KeyPress('a', 'a')) + processor.feed(KeyPress('b', 'b')) + processor.feed(KeyPress('b', 'b')) + processor.process_keys() + + # Test. + assert len(events) == 2 + assert len(events[0].key_sequence) == 2 + assert events[0].key_sequence[0].key == 'a' + assert events[0].key_sequence[0].data == 'a' + assert events[0].key_sequence[1].key == 'a' + assert events[0].key_sequence[1].data == 'a' + assert events[0].previous_key_sequence == [] + + assert len(events[1].key_sequence) == 2 + assert events[1].key_sequence[0].key == 'b' + assert events[1].key_sequence[0].data == 'b' + assert events[1].key_sequence[1].key == 'b' + assert events[1].key_sequence[1].data == 'b' + assert len(events[1].previous_key_sequence) == 2 + assert events[1].previous_key_sequence[0].key == 'a' + assert events[1].previous_key_sequence[0].data == 'a' + assert events[1].previous_key_sequence[1].key == 'a' + assert events[1].previous_key_sequence[1].data == 'a' |