Clean up completer unit test.

Based on code review:

    - import modules, not classes
    - use methods, not lambdas for the mock command prompt class
    - use None rather than Mock for DUMB_SORT
    - autouse two fixtures and remove them from test signatures
This commit is contained in:
Ryan Roden-Corrent 2016-07-03 13:03:30 -04:00
parent 07edcce697
commit 13e8ed53d6
2 changed files with 58 additions and 52 deletions

View File

@ -376,10 +376,18 @@ class FakeStatusbarCommand(QObject):
super().__init__(parent) super().__init__(parent)
self._cursor_pos = 0 self._cursor_pos = 0
self._text = "" self._text = ""
self.cursorPosition = lambda: self._cursor_pos
self.text = lambda: self._text def cursorPosition(self):
self.prefix = lambda: self._text[0] return self._cursor_pos
self.setFocus = lambda: None
def text(self):
return self._text
def prefix(self):
return self._text[0]
def setFocus(self):
return None
def setText(self, x): def setText(self, x):
self._text = x self._text = x

View File

@ -19,20 +19,18 @@
"""Tests for the Completer Object.""" """Tests for the Completer Object."""
from unittest.mock import Mock
import pytest import pytest
from PyQt5.QtGui import QStandardItemModel from PyQt5.QtGui import QStandardItemModel
from qutebrowser.completion.completer import Completer from qutebrowser.completion import completer
from qutebrowser.utils.usertypes import Completion from qutebrowser.utils import usertypes
class FakeCompletionModel(QStandardItemModel): class FakeCompletionModel(QStandardItemModel):
"""Stub for a completion model.""" """Stub for a completion model."""
DUMB_SORT = Mock() DUMB_SORT = None
def __init__(self, kind, parent=None): def __init__(self, kind, parent=None):
super().__init__(parent) super().__init__(parent)
@ -46,43 +44,44 @@ def cmd(stubs):
@pytest.fixture @pytest.fixture
def completer(qtbot, cmd, config_stub): def completer_obj(qtbot, cmd, config_stub):
"""Create the completer used for testing.""" """Create the completer used for testing."""
config_stub.data = {'completion': {'auto-open': False}} config_stub.data = {'completion': {'auto-open': False}}
return Completer(cmd, 0) return completer.Completer(cmd, 0)
@pytest.fixture @pytest.fixture(autouse=True)
def instances(monkeypatch): def instances(monkeypatch):
"""Mock the instances module so get returns a fake completion model.""" """Mock the instances module so get returns a fake completion model."""
# populate a model for each completion type, with a nested structure for # populate a model for each completion type, with a nested structure for
# option and value completion # option and value completion
instances = {kind: FakeCompletionModel(kind) for kind in Completion} instances = {kind: FakeCompletionModel(kind)
instances[Completion.option] = { for kind in usertypes.Completion}
'general': FakeCompletionModel(Completion.option), instances[usertypes.Completion.option] = {
'general': FakeCompletionModel(usertypes.Completion.option),
} }
instances[Completion.value] = { instances[usertypes.Completion.value] = {
'general': { 'general': {
'ignore-case': FakeCompletionModel(Completion.value), 'ignore-case': FakeCompletionModel(usertypes.Completion.value),
} }
} }
monkeypatch.setattr('qutebrowser.completion.completer.instances', monkeypatch.setattr('qutebrowser.completion.completer.instances',
instances) instances)
return instances
@pytest.fixture @pytest.fixture(autouse=True)
def cmdutils_patch(monkeypatch, stubs): def cmdutils_patch(monkeypatch, stubs):
"""Patch the cmdutils module to provide fake commands.""" """Patch the cmdutils module to provide fake commands."""
cmds = { cmds = {
'set': [Completion.section, Completion.option, Completion.value], 'set': [usertypes.Completion.section, usertypes.Completion.option,
'help': [Completion.helptopic], usertypes.Completion.value],
'quickmark-load': [Completion.quickmark_by_name], 'help': [usertypes.Completion.helptopic],
'bookmark-load': [Completion.bookmark_by_url], 'quickmark-load': [usertypes.Completion.quickmark_by_name],
'open': [Completion.url], 'bookmark-load': [usertypes.Completion.bookmark_by_url],
'buffer': [Completion.tab], 'open': [usertypes.Completion.url],
'session-load': [Completion.sessions], 'buffer': [usertypes.Completion.tab],
'bind': [Completion.empty, Completion.command], 'session-load': [usertypes.Completion.sessions],
'bind': [usertypes.Completion.empty, usertypes.Completion.command],
} }
cmd_utils = stubs.FakeCmdUtils({ cmd_utils = stubs.FakeCmdUtils({
name: stubs.FakeCommand(completion=compl) name: stubs.FakeCommand(completion=compl)
@ -90,43 +89,42 @@ def cmdutils_patch(monkeypatch, stubs):
}) })
monkeypatch.setattr('qutebrowser.completion.completer.cmdutils', monkeypatch.setattr('qutebrowser.completion.completer.cmdutils',
cmd_utils) cmd_utils)
return cmd_utils
@pytest.mark.parametrize('txt, expected', [ @pytest.mark.parametrize('txt, expected', [
(':nope|', Completion.command), (':nope|', usertypes.Completion.command),
(':nope |', None), (':nope |', None),
(':set |', Completion.section), (':set |', usertypes.Completion.section),
(':set gen|', Completion.section), (':set gen|', usertypes.Completion.section),
(':set general |', Completion.option), (':set general |', usertypes.Completion.option),
(':set what |', None), (':set what |', None),
(':set general ignore-case |', Completion.value), (':set general ignore-case |', usertypes.Completion.value),
(':set general huh |', None), (':set general huh |', None),
(':help |', Completion.helptopic), (':help |', usertypes.Completion.helptopic),
(':quickmark-load |', Completion.quickmark_by_name), (':quickmark-load |', usertypes.Completion.quickmark_by_name),
(':bookmark-load |', Completion.bookmark_by_url), (':bookmark-load |', usertypes.Completion.bookmark_by_url),
(':open |', Completion.url), (':open |', usertypes.Completion.url),
(':buffer |', Completion.tab), (':buffer |', usertypes.Completion.tab),
(':session-load |', Completion.sessions), (':session-load |', usertypes.Completion.sessions),
(':bind |', Completion.empty), (':bind |', usertypes.Completion.empty),
(':bind <c-x> |', Completion.command), (':bind <c-x> |', usertypes.Completion.command),
(':bind <c-x> foo|', Completion.command), (':bind <c-x> foo|', usertypes.Completion.command),
(':bind <c-x>| foo', Completion.empty), (':bind <c-x>| foo', usertypes.Completion.empty),
(':set| general ', Completion.command), (':set| general ', usertypes.Completion.command),
(':|set general ', Completion.command), (':|set general ', usertypes.Completion.command),
(':set gene|ral ignore-case', Completion.section), (':set gene|ral ignore-case', usertypes.Completion.section),
(':|', Completion.command), (':|', usertypes.Completion.command),
(': |', Completion.command), (': |', usertypes.Completion.command),
(':bookmark-load |', Completion.bookmark_by_url), (':bookmark-load |', usertypes.Completion.bookmark_by_url),
]) ])
def test_update_completion(txt, expected, cmd, completer, instances, def test_update_completion(txt, expected, cmd, completer_obj,
cmdutils_patch, 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('|') cursor_pos = txt.index('|')
cmd.setText(txt.replace('|', '')) cmd.setText(txt.replace('|', ''))
cmd.setCursorPosition(cursor_pos) cmd.setCursorPosition(cursor_pos)
completer.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
else: else: