Use helper method for cmd prompt text testing.
In test_completer, introduce two helper methods to reduce duplicate code for handling fake cmd prompt text that uses '|' as a placeholder.
This commit is contained in:
parent
27d635a394
commit
4c9417ac6e
@ -96,6 +96,28 @@ def cmdutils_patch(monkeypatch, stubs):
|
|||||||
cmd_utils)
|
cmd_utils)
|
||||||
|
|
||||||
|
|
||||||
|
def _set_cmd_prompt(cmd, txt):
|
||||||
|
"""Set the command prompt's text and cursor position.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
cmd: The command prompt object.
|
||||||
|
txt: The prompt text, using | as a placeholder for the cursor position.
|
||||||
|
"""
|
||||||
|
cmd.setText(txt.replace('|', ''))
|
||||||
|
cmd.setCursorPosition(txt.index('|'))
|
||||||
|
|
||||||
|
|
||||||
|
def _validate_cmd_prompt(cmd, txt):
|
||||||
|
"""Interpret fake command prompt text using | as the cursor placeholder.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
cmd: The command prompt object.
|
||||||
|
txt: The prompt text, using | as a placeholder for the cursor position.
|
||||||
|
"""
|
||||||
|
assert cmd.cursorPosition() == txt.index('|')
|
||||||
|
assert cmd.text() == txt.replace('|', '')
|
||||||
|
|
||||||
|
|
||||||
@pytest.mark.parametrize('txt, expected', [
|
@pytest.mark.parametrize('txt, expected', [
|
||||||
(':nope|', usertypes.Completion.command),
|
(':nope|', usertypes.Completion.command),
|
||||||
(':nope |', None),
|
(':nope |', None),
|
||||||
@ -139,9 +161,7 @@ def test_update_completion(txt, expected, cmd, completer_obj,
|
|||||||
completion_widget_stub):
|
completion_widget_stub):
|
||||||
"""Test setting the completion widget's model based on command text."""
|
"""Test setting the completion widget's model based on command text."""
|
||||||
# this test uses | as a placeholder for the current cursor position
|
# this test uses | as a placeholder for the current cursor position
|
||||||
cursor_pos = txt.index('|')
|
_set_cmd_prompt(cmd, txt)
|
||||||
cmd.setText(txt.replace('|', ''))
|
|
||||||
cmd.setCursorPosition(cursor_pos)
|
|
||||||
completer_obj.update_completion()
|
completer_obj.update_completion()
|
||||||
if expected is None:
|
if expected is None:
|
||||||
assert not completion_widget_stub.set_model.called
|
assert not completion_widget_stub.set_model.called
|
||||||
@ -193,14 +213,8 @@ def test_selection_changed(before, newtxt, count, quick_complete, after,
|
|||||||
selection = unittest.mock.Mock()
|
selection = unittest.mock.Mock()
|
||||||
selection.indexes = unittest.mock.Mock(return_value=indexes)
|
selection.indexes = unittest.mock.Mock(return_value=indexes)
|
||||||
completion_widget_stub.model = unittest.mock.Mock(return_value=model)
|
completion_widget_stub.model = unittest.mock.Mock(return_value=model)
|
||||||
before_pos = before.index('|')
|
_set_cmd_prompt(cmd, before)
|
||||||
after_pos = after.index('|')
|
|
||||||
before_txt = before.replace('|', '')
|
|
||||||
after_txt = after.replace('|', '')
|
|
||||||
cmd.setText(before_txt)
|
|
||||||
cmd.setCursorPosition(before_pos)
|
|
||||||
completer_obj.update_cursor_part()
|
completer_obj.update_cursor_part()
|
||||||
completer_obj.selection_changed(selection, None)
|
completer_obj.selection_changed(selection, None)
|
||||||
model.data.assert_called_with(indexes[0])
|
model.data.assert_called_with(indexes[0])
|
||||||
assert cmd.text() == after_txt
|
_validate_cmd_prompt(cmd, after)
|
||||||
assert cmd.cursorPosition() == after_pos
|
|
||||||
|
Loading…
Reference in New Issue
Block a user