From c63d16e2eaa3af45d49c7d44d1ff2711636d78b0 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Thu, 5 Oct 2017 11:09:07 +0200 Subject: [PATCH] tests: Get rid of duplicate key config stubs --- tests/unit/config/conftest.py | 29 ----------- tests/unit/config/test_config.py | 63 ++++++++++++------------ tests/unit/config/test_configcommands.py | 22 +++++---- 3 files changed, 43 insertions(+), 71 deletions(-) delete mode 100644 tests/unit/config/conftest.py diff --git a/tests/unit/config/conftest.py b/tests/unit/config/conftest.py deleted file mode 100644 index 89767b786..000000000 --- a/tests/unit/config/conftest.py +++ /dev/null @@ -1,29 +0,0 @@ -# vim: ft=python fileencoding=utf-8 sts=4 sw=4 et: -# Copyright 2017 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 . - -"""Fixtures needed in various config test files.""" - -import pytest - -from qutebrowser.config import config - - -@pytest.fixture -def keyconf(config_stub): - config_stub.val.aliases = {} - return config.KeyConfig(config_stub) diff --git a/tests/unit/config/test_config.py b/tests/unit/config/test_config.py index a46e8e3de..e42aca1a8 100644 --- a/tests/unit/config/test_config.py +++ b/tests/unit/config/test_config.py @@ -93,11 +93,6 @@ class TestChangeFilter: class TestKeyConfig: - @pytest.fixture - def keyconf(self, config_stub): - config_stub.val.aliases = {} - return config.KeyConfig(config_stub) - @pytest.fixture def no_bindings(self): """Get a dict with no bindings.""" @@ -107,14 +102,14 @@ class TestKeyConfig: ('A', 'A'), ('', ''), ]) - def test_prepare_valid(self, keyconf, key, expected): + def test_prepare_valid(self, key_config_stub, key, expected): """Make sure prepare normalizes the key.""" - assert keyconf._prepare(key, 'normal') == expected + assert key_config_stub._prepare(key, 'normal') == expected - def test_prepare_invalid(self, keyconf): + def test_prepare_invalid(self, key_config_stub): """Make sure prepare checks the mode.""" with pytest.raises(configexc.KeybindingError): - assert keyconf._prepare('x', 'abnormal') + assert key_config_stub._prepare('x', 'abnormal') @pytest.mark.parametrize('commands, expected', [ # Unbinding default key @@ -126,7 +121,8 @@ class TestKeyConfig: # Unbinding unknown key ({'x': None}, {'a': 'message-info foo', 'b': 'message-info bar'}), ]) - def test_get_bindings_for_and_get_command(self, keyconf, config_stub, + def test_get_bindings_for_and_get_command(self, key_config_stub, + config_stub, commands, expected): orig_default_bindings = {'normal': {'a': 'message-info foo', 'b': 'message-info bar'}, @@ -139,18 +135,19 @@ class TestKeyConfig: 'register': {}} config_stub.val.bindings.default = copy.deepcopy(orig_default_bindings) config_stub.val.bindings.commands = {'normal': commands} - bindings = keyconf.get_bindings_for('normal') + bindings = key_config_stub.get_bindings_for('normal') # Make sure the code creates a copy and doesn't modify the setting assert config_stub.val.bindings.default == orig_default_bindings assert bindings == expected for key, command in expected.items(): - assert keyconf.get_command(key, 'normal') == command + assert key_config_stub.get_command(key, 'normal') == command - def test_get_command_unbound(self, keyconf, config_stub, no_bindings): + def test_get_command_unbound(self, key_config_stub, config_stub, + no_bindings): config_stub.val.bindings.default = no_bindings config_stub.val.bindings.commands = no_bindings - assert keyconf.get_command('foobar', 'normal') is None + assert key_config_stub.get_command('foobar', 'normal') is None @pytest.mark.parametrize('bindings, expected', [ # Simple @@ -166,46 +163,47 @@ class TestKeyConfig: ({'a': 'message-info foo ;; message-info bar'}, {'message-info foo': ['a'], 'message-info bar': ['a']}), ]) - def test_get_reverse_bindings_for(self, keyconf, config_stub, no_bindings, - bindings, expected): + def test_get_reverse_bindings_for(self, key_config_stub, config_stub, + no_bindings, bindings, expected): config_stub.val.bindings.default = no_bindings config_stub.val.bindings.commands = {'normal': bindings} - assert keyconf.get_reverse_bindings_for('normal') == expected + assert key_config_stub.get_reverse_bindings_for('normal') == expected @pytest.mark.parametrize('key', ['a', '', 'b']) - def test_bind_duplicate(self, keyconf, config_stub, key): + def test_bind_duplicate(self, key_config_stub, config_stub, key): config_stub.val.bindings.default = {'normal': {'a': 'nop', '': 'nop'}} config_stub.val.bindings.commands = {'normal': {'b': 'nop'}} - keyconf.bind(key, 'message-info foo', mode='normal') - assert keyconf.get_command(key, 'normal') == 'message-info foo' + key_config_stub.bind(key, 'message-info foo', mode='normal') + assert key_config_stub.get_command(key, 'normal') == 'message-info foo' @pytest.mark.parametrize('mode', ['normal', 'caret']) @pytest.mark.parametrize('command', [ 'message-info foo', 'nop ;; wq', # https://github.com/qutebrowser/qutebrowser/issues/3002 ]) - def test_bind(self, keyconf, config_stub, qtbot, no_bindings, + def test_bind(self, key_config_stub, config_stub, qtbot, no_bindings, mode, command): config_stub.val.bindings.default = no_bindings config_stub.val.bindings.commands = no_bindings with qtbot.wait_signal(config_stub.changed): - keyconf.bind('a', command, mode=mode) + key_config_stub.bind('a', command, mode=mode) assert config_stub.val.bindings.commands[mode]['a'] == command - assert keyconf.get_bindings_for(mode)['a'] == command - assert keyconf.get_command('a', mode) == command + assert key_config_stub.get_bindings_for(mode)['a'] == command + assert key_config_stub.get_command('a', mode) == command - def test_bind_mode_changing(self, keyconf, config_stub, no_bindings): + def test_bind_mode_changing(self, key_config_stub, config_stub, + no_bindings): """Make sure we can bind to a command which changes the mode. https://github.com/qutebrowser/qutebrowser/issues/2989 """ config_stub.val.bindings.default = no_bindings config_stub.val.bindings.commands = no_bindings - keyconf.bind('a', 'set-cmd-text :nop ;; rl-beginning-of-line', - mode='normal') + key_config_stub.bind('a', 'set-cmd-text :nop ;; rl-beginning-of-line', + mode='normal') @pytest.mark.parametrize('key, normalized', [ ('a', 'a'), # default bindings @@ -213,7 +211,8 @@ class TestKeyConfig: ('', '') ]) @pytest.mark.parametrize('mode', ['normal', 'caret', 'prompt']) - def test_unbind(self, keyconf, config_stub, qtbot, key, normalized, mode): + def test_unbind(self, key_config_stub, config_stub, qtbot, + key, normalized, mode): default_bindings = { 'normal': {'a': 'nop', '': 'nop'}, 'caret': {'a': 'nop', '': 'nop'}, @@ -228,9 +227,9 @@ class TestKeyConfig: } with qtbot.wait_signal(config_stub.changed): - keyconf.unbind(key, mode=mode) + key_config_stub.unbind(key, mode=mode) - assert keyconf.get_command(key, mode) is None + assert key_config_stub.get_command(key, mode) is None mode_bindings = config_stub.val.bindings.commands[mode] if key == 'b' and mode != 'prompt': @@ -241,13 +240,13 @@ class TestKeyConfig: assert default_bindings[mode] == old_default_bindings[mode] assert mode_bindings[normalized] is None - def test_unbind_unbound(self, keyconf, config_stub, no_bindings): + def test_unbind_unbound(self, key_config_stub, config_stub, no_bindings): """Try unbinding a key which is not bound.""" config_stub.val.bindings.default = no_bindings config_stub.val.bindings.commands = no_bindings with pytest.raises(configexc.KeybindingError, match="Can't find binding 'foobar' in normal mode"): - keyconf.unbind('foobar', mode='normal') + key_config_stub.unbind('foobar', mode='normal') class TestConfig: diff --git a/tests/unit/config/test_configcommands.py b/tests/unit/config/test_configcommands.py index 96517d81c..21ec040f7 100644 --- a/tests/unit/config/test_configcommands.py +++ b/tests/unit/config/test_configcommands.py @@ -31,8 +31,8 @@ from qutebrowser.misc import objects @pytest.fixture -def commands(config_stub, keyconf): - return configcommands.ConfigCommands(config_stub, keyconf) +def commands(config_stub, key_config_stub): + return configcommands.ConfigCommands(config_stub, key_config_stub) class TestSet: @@ -353,10 +353,10 @@ class TestWritePy: """Tests for :config-write-py.""" - def test_custom(self, commands, config_stub, keyconf, tmpdir): + def test_custom(self, commands, config_stub, key_config_stub, tmpdir): confpy = tmpdir / 'config.py' config_stub.val.content.javascript.enabled = True - keyconf.bind(',x', 'message-info foo', mode='normal') + key_config_stub.bind(',x', 'message-info foo', mode='normal') commands.config_write_py(str(confpy)) @@ -408,14 +408,15 @@ class TestBind: return {'normal': {}} @pytest.mark.parametrize('command', ['nop', 'nope']) - def test_bind(self, commands, config_stub, no_bindings, keyconf, command): + def test_bind(self, commands, config_stub, no_bindings, key_config_stub, + command): """Simple :bind test (and aliases).""" config_stub.val.aliases = {'nope': 'nop'} config_stub.val.bindings.default = no_bindings config_stub.val.bindings.commands = no_bindings commands.bind('a', command) - assert keyconf.get_command('a', 'normal') == command + assert key_config_stub.get_command('a', 'normal') == command yaml_bindings = config_stub._yaml['bindings.commands']['normal'] assert yaml_bindings['a'] == command @@ -466,7 +467,7 @@ class TestBind: commands.bind('a', 'nop', mode='wrongmode') @pytest.mark.parametrize('key', ['a', 'b', '']) - def test_bind_duplicate(self, commands, config_stub, keyconf, key): + def test_bind_duplicate(self, commands, config_stub, key_config_stub, key): """Run ':bind' with a key which already has been bound.'. Also tests for https://github.com/qutebrowser/qutebrowser/issues/1544 @@ -479,7 +480,7 @@ class TestBind: } commands.bind(key, 'message-info foo', mode='normal') - assert keyconf.get_command(key, 'normal') == 'message-info foo' + assert key_config_stub.get_command(key, 'normal') == 'message-info foo' def test_bind_none(self, commands, config_stub): config_stub.val.bindings.commands = None @@ -495,7 +496,8 @@ class TestBind: ('c', 'c'), # :bind then :unbind ('', '') # normalized special binding ]) - def test_unbind(self, commands, keyconf, config_stub, key, normalized): + def test_unbind(self, commands, key_config_stub, config_stub, + key, normalized): config_stub.val.bindings.default = { 'normal': {'a': 'nop', '': 'nop'}, 'caret': {'a': 'nop', '': 'nop'}, @@ -509,7 +511,7 @@ class TestBind: commands.bind(key, 'nop') commands.unbind(key) - assert keyconf.get_command(key, 'normal') is None + assert key_config_stub.get_command(key, 'normal') is None yaml_bindings = config_stub._yaml['bindings.commands']['normal'] if key in 'bc':