From aedf5b930a7c833e374c92e31c944f2dece23448 Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Mon, 11 Jul 2016 20:54:16 -0400 Subject: [PATCH] Test Completer.schedule_completion_update. update_completion is only used internally, so instead test the real public entry point which is schedule_completion_update. This required mocking out QTimer to fire immediately so the test didn't have to do flaky artificial delays. --- tests/helpers/stubs.py | 22 ++++++++++++++++++++++ tests/unit/completion/test_completer.py | 6 ++++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/tests/helpers/stubs.py b/tests/helpers/stubs.py index 419d6bd78..7808b5816 100644 --- a/tests/helpers/stubs.py +++ b/tests/helpers/stubs.py @@ -381,6 +381,28 @@ class FakeTimer(QObject): return self._started +class InstaTimer(QObject): + + """Stub for a QTimer that fires instantly on start(). + + Useful to test a time-based event without inserting an artificial delay. + """ + + timeout = pyqtSignal() + + def __init__(self, parent=None): + super().__init__(parent) + + def start(self): + self.timeout.emit() + + def setSingleShot(self, yes): + pass + + def setInterval(self, interval): + pass + + class FakeConfigType: """A stub to provide valid_values for typ attribute of a SettingValue.""" diff --git a/tests/unit/completion/test_completer.py b/tests/unit/completion/test_completer.py index bfb1f0718..34537330c 100644 --- a/tests/unit/completion/test_completer.py +++ b/tests/unit/completion/test_completer.py @@ -48,8 +48,10 @@ def cmd(stubs, qtbot): @pytest.fixture -def completer_obj(qtbot, cmd, config_stub): +def completer_obj(qtbot, cmd, 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) @@ -162,7 +164,7 @@ def test_update_completion(txt, expected, cmd, completer_obj, """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) - completer_obj.update_completion() + completer_obj.schedule_completion_update() if expected is None: assert not completion_widget_stub.set_model.called else: