pytest: Use common fixture for fake_keyconfig.

This commit is contained in:
Florian Bruhin 2015-05-19 07:44:45 +02:00
parent cc88451003
commit 069d7b26a2
3 changed files with 49 additions and 32 deletions

View File

@ -0,0 +1,45 @@
# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et:
# Copyright 2015 Florian Bruhin (The Compiler) <mail@qutebrowser.org>:
#
# This file is part of qutebrowser.
#
# qutebrowser is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# qutebrowser is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with qutebrowser. If not, see <http://www.gnu.org/licenses/>.
"""pytest fixtures for tests.keyinput."""
import pytest
from unittest import mock
from qutebrowser.utils import objreg
BINDINGS = {'test': {'<Ctrl-a>': 'ctrla',
'a': 'a',
'ba': 'ba',
'ax': 'ax',
'ccc': 'ccc',
'0': '0'},
'test2': {'foo': 'bar', '<Ctrl+X>': 'ctrlx'},
'normal': {'a': 'a', 'ba': 'ba'}}
@pytest.yield_fixture
def fake_keyconfig():
"""Create a mock of a KeyConfiguration and register it into objreg."""
fake_keyconfig = mock.Mock(spec=['get_bindings_for'])
fake_keyconfig.get_bindings_for.side_effect = lambda s: BINDINGS[s]
objreg.register('key-config', fake_keyconfig)
yield
objreg.delete('key-config')

View File

@ -29,31 +29,12 @@ from PyQt5.QtCore import Qt
import pytest import pytest
from qutebrowser.keyinput import basekeyparser from qutebrowser.keyinput import basekeyparser
from qutebrowser.utils import objreg, log from qutebrowser.utils import log
CONFIG = {'input': {'timeout': 100}} CONFIG = {'input': {'timeout': 100}}
BINDINGS = {'test': {'<Ctrl-a>': 'ctrla',
'a': 'a',
'ba': 'ba',
'ax': 'ax',
'ccc': 'ccc',
'0': '0'},
'test2': {'foo': 'bar', '<Ctrl+X>': 'ctrlx'}}
@pytest.yield_fixture
def fake_keyconfig():
"""Create a mock of a KeyConfiguration and register it into objreg."""
fake_keyconfig = mock.Mock(spec=['get_bindings_for'])
fake_keyconfig.get_bindings_for.side_effect = lambda s: BINDINGS[s]
objreg.register('key-config', fake_keyconfig)
yield
objreg.delete('key-config')
@pytest.fixture @pytest.fixture
def mock_timer(monkeypatch, stubs): def mock_timer(monkeypatch, stubs):
"""Mock the Timer class used by the usertypes module with a stub.""" """Mock the Timer class used by the usertypes module with a stub."""

View File

@ -19,25 +19,18 @@
"""Tests for mode parsers.""" """Tests for mode parsers."""
from unittest import mock
from PyQt5.QtCore import Qt from PyQt5.QtCore import Qt
from unittest import mock
import pytest import pytest
from qutebrowser.keyinput import modeparsers from qutebrowser.keyinput import modeparsers
from qutebrowser.utils import objreg
CONFIG = {'input': {'partial-timeout': 100}} CONFIG = {'input': {'partial-timeout': 100}}
BINDINGS = {'normal': {'a': 'a', 'ba': 'ba'}}
fake_keyconfig = mock.Mock(spec=['get_bindings_for'])
fake_keyconfig.get_bindings_for.side_effect = lambda s: BINDINGS[s]
class TestsNormalKeyParser: class TestsNormalKeyParser:
"""Tests for NormalKeyParser. """Tests for NormalKeyParser.
@ -49,7 +42,7 @@ class TestsNormalKeyParser:
# pylint: disable=protected-access # pylint: disable=protected-access
@pytest.yield_fixture(autouse=True) @pytest.yield_fixture(autouse=True)
def setup(self, monkeypatch, stubs, config_stub): def setup(self, monkeypatch, stubs, config_stub, fake_keyconfig):
"""Set up mocks and read the test config.""" """Set up mocks and read the test config."""
monkeypatch.setattr( monkeypatch.setattr(
'qutebrowser.keyinput.basekeyparser.usertypes.Timer', 'qutebrowser.keyinput.basekeyparser.usertypes.Timer',
@ -58,11 +51,9 @@ class TestsNormalKeyParser:
monkeypatch.setattr('qutebrowser.keyinput.modeparsers.config', monkeypatch.setattr('qutebrowser.keyinput.modeparsers.config',
config_stub) config_stub)
objreg.register('key-config', fake_keyconfig)
self.kp = modeparsers.NormalKeyParser(0) self.kp = modeparsers.NormalKeyParser(0)
self.kp.execute = mock.Mock() self.kp.execute = mock.Mock()
yield yield
objreg.delete('key-config')
def test_keychain(self, fake_keyevent_factory): def test_keychain(self, fake_keyevent_factory):
"""Test valid keychain.""" """Test valid keychain."""