diff --git a/tests/helpers/fixtures.py b/tests/helpers/fixtures.py index 0fd890fa0..7454b46e4 100644 --- a/tests/helpers/fixtures.py +++ b/tests/helpers/fixtures.py @@ -275,6 +275,16 @@ def completion_widget_stub(win_registry): objreg.delete('completion', scope='window', window=0) +@pytest.yield_fixture +def status_command_stub(stubs, qtbot, win_registry): + """Fixture which provides a fake status-command object.""" + cmd = stubs.StatusBarCommandStub() + objreg.register('status-command', cmd, scope='window', window=0) + qtbot.addWidget(cmd) + yield cmd + objreg.delete('status-command', scope='window', window=0) + + @pytest.fixture(scope='session') def stubs(): """Provide access to stub objects useful for testing.""" diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py index 7808b5816..5027f301b 100644 --- a/tests/helpers/stubs.py +++ b/tests/helpers/stubs.py @@ -413,7 +413,7 @@ class FakeConfigType: self.complete = lambda: [(val, '') for val in valid_values] -class FakeStatusbarCommand(QLineEdit): +class StatusBarCommandStub(QLineEdit): """Stub for the statusbar command prompt.""" diff --git a/tests/unit/completion/test_completer.py b/tests/unit/completion/test_completer.py index d87d241f3..9335bbf70 100644 --- a/tests/unit/completion/test_completer.py +++ b/tests/unit/completion/test_completer.py @@ -40,20 +40,12 @@ class FakeCompletionModel(QStandardItemModel): @pytest.fixture -def cmd(stubs, qtbot): - """Create the statusbar command prompt the completer uses.""" - cmd = stubs.FakeStatusbarCommand() - qtbot.addWidget(cmd) - return cmd - - -@pytest.fixture -def completer_obj(qtbot, cmd, config_stub, monkeypatch, stubs): +def completer_obj(qtbot, status_command_stub, config_stub, monkeypatch, stubs): """Create the completer used for testing.""" monkeypatch.setattr('qutebrowser.completion.completer.QTimer', stubs.InstaTimer) config_stub.data = {'completion': {'auto-open': False}} - return completer.Completer(cmd, 0) + return completer.Completer(status_command_stub, 0) @pytest.fixture(autouse=True) @@ -159,11 +151,11 @@ def _validate_cmd_prompt(cmd, txt): (':set -t -p |', usertypes.Completion.section), (':open -- |', None), ]) -def test_update_completion(txt, expected, cmd, completer_obj, +def test_update_completion(txt, expected, status_command_stub, completer_obj, completion_widget_stub): """Test setting the completion widget's model based on command text.""" # this test uses | as a placeholder for the current cursor position - _set_cmd_prompt(cmd, txt) + _set_cmd_prompt(status_command_stub, txt) completer_obj.schedule_completion_update() if expected is None: assert not completion_widget_stub.set_model.called @@ -174,19 +166,19 @@ def test_update_completion(txt, expected, cmd, completer_obj, assert arg.srcmodel.kind == expected -def test_completion_item_prev(completer_obj, cmd, completion_widget_stub, - config_stub, qtbot): +def test_completion_item_prev(completer_obj, status_command_stub, + completion_widget_stub, config_stub, qtbot): """Test that completion_item_prev emits next_prev_item.""" - cmd.setText(':') + status_command_stub.setText(':') with qtbot.waitSignal(completer_obj.next_prev_item) as blocker: completer_obj.completion_item_prev() assert blocker.args == [True] -def test_completion_item_next(completer_obj, cmd, completion_widget_stub, - config_stub, qtbot): +def test_completion_item_next(completer_obj, status_command_stub, + completion_widget_stub, config_stub, qtbot): """Test that completion_item_next emits next_prev_item.""" - cmd.setText(':') + status_command_stub.setText(':') with qtbot.waitSignal(completer_obj.next_prev_item) as blocker: completer_obj.completion_item_next() assert blocker.args == [False] @@ -202,8 +194,8 @@ def test_completion_item_next(completer_obj, cmd, completion_widget_stub, (':foo |', None, True, 1, ":foo |"), ]) def test_selection_changed(before, newtxt, count, quick_complete, after, - completer_obj, cmd, completion_widget_stub, - config_stub): + completer_obj, status_command_stub, + completion_widget_stub, config_stub): """Test that change_completed_part modifies the cmd text properly. The | represents the current cursor position in the cmd prompt. @@ -218,9 +210,9 @@ def test_selection_changed(before, newtxt, count, quick_complete, after, selection = unittest.mock.Mock() selection.indexes = unittest.mock.Mock(return_value=indexes) completion_widget_stub.model = unittest.mock.Mock(return_value=model) - _set_cmd_prompt(cmd, before) + _set_cmd_prompt(status_command_stub, before) # schedule_completion_update is needed to pick up the cursor position completer_obj.schedule_completion_update() completer_obj.selection_changed(selection, None) model.data.assert_called_with(indexes[0]) - _validate_cmd_prompt(cmd, after) + _validate_cmd_prompt(status_command_stub, after)