|
| 1 | +import pytest |
| 2 | +from IPython.terminal.shortcuts import _apply_autosuggest |
| 3 | + |
| 4 | +from unittest.mock import Mock |
| 5 | + |
| 6 | + |
| 7 | +def make_event(text, cursor, suggestion): |
| 8 | + event = Mock() |
| 9 | + event.current_buffer = Mock() |
| 10 | + event.current_buffer.suggestion = Mock() |
| 11 | + event.current_buffer.cursor_position = cursor |
| 12 | + event.current_buffer.suggestion.text = suggestion |
| 13 | + event.current_buffer.document = Mock() |
| 14 | + event.current_buffer.document.get_end_of_line_position = Mock(return_value=0) |
| 15 | + event.current_buffer.document.text = text |
| 16 | + event.current_buffer.document.cursor_position = cursor |
| 17 | + return event |
| 18 | + |
| 19 | + |
| 20 | +@pytest.mark.parametrize( |
| 21 | + "text, cursor, suggestion, called", |
| 22 | + [ |
| 23 | + ("123456", 6, "123456789", True), |
| 24 | + ("123456", 3, "123456789", False), |
| 25 | + ("123456 \n789", 6, "123456789", True), |
| 26 | + ], |
| 27 | +) |
| 28 | +def test_autosuggest_at_EOL(text, cursor, suggestion, called): |
| 29 | + """ |
| 30 | + test that autosuggest is only applied at end of line. |
| 31 | + """ |
| 32 | + |
| 33 | + event = make_event(text, cursor, suggestion) |
| 34 | + event.current_buffer.insert_text = Mock() |
| 35 | + _apply_autosuggest(event) |
| 36 | + if called: |
| 37 | + event.current_buffer.insert_text.assert_called() |
| 38 | + else: |
| 39 | + event.current_buffer.insert_text.assert_not_called() |
| 40 | + # event.current_buffer.document.get_end_of_line_position.assert_called() |
0 commit comments