From 069d7b26a28d2922ae1277e301cee927606fdf08 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Tue, 19 May 2015 07:44:45 +0200 Subject: [PATCH] pytest: Use common fixture for fake_keyconfig. --- tests/keyinput/conftest.py | 45 ++++++++++++++++++++++++++++ tests/keyinput/test_basekeyparser.py | 21 +------------ tests/keyinput/test_modeparsers.py | 15 ++-------- 3 files changed, 49 insertions(+), 32 deletions(-) create mode 100644 tests/keyinput/conftest.py diff --git a/tests/keyinput/conftest.py b/tests/keyinput/conftest.py new file mode 100644 index 000000000..614017876 --- /dev/null +++ b/tests/keyinput/conftest.py @@ -0,0 +1,45 @@ +# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: + +# Copyright 2015 Florian Bruhin (The Compiler) : +# +# 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 . + +"""pytest fixtures for tests.keyinput.""" + +import pytest + +from unittest import mock +from qutebrowser.utils import objreg + + +BINDINGS = {'test': {'': 'ctrla', + 'a': 'a', + 'ba': 'ba', + 'ax': 'ax', + 'ccc': 'ccc', + '0': '0'}, + 'test2': {'foo': 'bar', '': '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') diff --git a/tests/keyinput/test_basekeyparser.py b/tests/keyinput/test_basekeyparser.py index 0b960b242..e8f7f9325 100644 --- a/tests/keyinput/test_basekeyparser.py +++ b/tests/keyinput/test_basekeyparser.py @@ -29,31 +29,12 @@ from PyQt5.QtCore import Qt import pytest from qutebrowser.keyinput import basekeyparser -from qutebrowser.utils import objreg, log +from qutebrowser.utils import log CONFIG = {'input': {'timeout': 100}} -BINDINGS = {'test': {'': 'ctrla', - 'a': 'a', - 'ba': 'ba', - 'ax': 'ax', - 'ccc': 'ccc', - '0': '0'}, - 'test2': {'foo': 'bar', '': '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 def mock_timer(monkeypatch, stubs): """Mock the Timer class used by the usertypes module with a stub.""" diff --git a/tests/keyinput/test_modeparsers.py b/tests/keyinput/test_modeparsers.py index 05444892c..47789b5b4 100644 --- a/tests/keyinput/test_modeparsers.py +++ b/tests/keyinput/test_modeparsers.py @@ -19,25 +19,18 @@ """Tests for mode parsers.""" +from unittest import mock + from PyQt5.QtCore import Qt -from unittest import mock import pytest from qutebrowser.keyinput import modeparsers -from qutebrowser.utils import objreg 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: """Tests for NormalKeyParser. @@ -49,7 +42,7 @@ class TestsNormalKeyParser: # pylint: disable=protected-access @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.""" monkeypatch.setattr( 'qutebrowser.keyinput.basekeyparser.usertypes.Timer', @@ -58,11 +51,9 @@ class TestsNormalKeyParser: monkeypatch.setattr('qutebrowser.keyinput.modeparsers.config', config_stub) - objreg.register('key-config', fake_keyconfig) self.kp = modeparsers.NormalKeyParser(0) self.kp.execute = mock.Mock() yield - objreg.delete('key-config') def test_keychain(self, fake_keyevent_factory): """Test valid keychain."""