From dcf89f7a2889698b4a45e28c157581350bcdd2b2 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 29 Dec 2017 16:10:12 +0100 Subject: [PATCH] Fix KeyConfig._prepare --- qutebrowser/config/config.py | 11 +++++------ tests/unit/config/test_config.py | 19 +++++++++---------- 2 files changed, 14 insertions(+), 16 deletions(-) diff --git a/qutebrowser/config/config.py b/qutebrowser/config/config.py index 4d83db409..9ac05c7d6 100644 --- a/qutebrowser/config/config.py +++ b/qutebrowser/config/config.py @@ -134,12 +134,11 @@ class KeyConfig: self._config = config def _prepare(self, key, mode): - """Make sure the given mode exists and normalize the key.""" + """Make sure the given mode exists.""" # Catch old usage of this code assert isinstance(key, keyutils.KeySequence), key if mode not in configdata.DATA['bindings.default'].default: raise configexc.KeybindingError("Invalid mode {}!".format(mode)) - return key def get_bindings_for(self, mode): """Get the combined bindings for the given mode.""" @@ -169,7 +168,7 @@ class KeyConfig: def get_command(self, key, mode, default=False): """Get the command for a given key (or None).""" - key = self._prepare(key, mode) + self._prepare(key, mode) if default: bindings = dict(val.bindings.default[mode]) else: @@ -183,7 +182,7 @@ class KeyConfig: "Can't add binding '{}' with empty command in {} " 'mode'.format(key, mode)) - key = self._prepare(key, mode) + self._prepare(key, mode) log.keyboard.vdebug("Adding binding {} -> {} in mode {}.".format( key, command, mode)) @@ -195,7 +194,7 @@ class KeyConfig: def bind_default(self, key, *, mode='normal', save_yaml=False): """Restore a default keybinding.""" - key = self._prepare(key, mode) + self._prepare(key, mode) bindings_commands = self._config.get_obj('bindings.commands') try: @@ -207,7 +206,7 @@ class KeyConfig: def unbind(self, key, *, mode='normal', save_yaml=False): """Unbind the given key in the given mode.""" - key = self._prepare(key, mode) + self._prepare(key, mode) bindings_commands = self._config.get_obj('bindings.commands') diff --git a/tests/unit/config/test_config.py b/tests/unit/config/test_config.py index d8bf73700..964391154 100644 --- a/tests/unit/config/test_config.py +++ b/tests/unit/config/test_config.py @@ -29,6 +29,7 @@ from PyQt5.QtGui import QColor from qutebrowser.config import config, configdata, configexc, configfiles from qutebrowser.utils import usertypes from qutebrowser.misc import objects +from qutebrowser.keyinput import keyutils @pytest.fixture(autouse=True) @@ -98,18 +99,16 @@ class TestKeyConfig: """Get a dict with no bindings.""" return {'normal': {}} - @pytest.mark.parametrize('key, expected', [ - ('A', 'A'), - ('', ''), - ]) - def test_prepare_valid(self, key_config_stub, key, expected): - """Make sure prepare normalizes the key.""" - assert key_config_stub._prepare(key, 'normal') == expected - - def test_prepare_invalid(self, key_config_stub): + def test_prepare_invalid_mode(self, key_config_stub): """Make sure prepare checks the mode.""" + seq = keyutils.KeySequence('x') with pytest.raises(configexc.KeybindingError): - assert key_config_stub._prepare('x', 'abnormal') + assert key_config_stub._prepare(seq, 'abnormal') + + def test_prepare_invalid_type(self, key_config_stub): + """Make sure prepare checks the type.""" + with pytest.raises(AssertionError): + assert key_config_stub._prepare('x', 'normal') @pytest.mark.parametrize('commands, expected', [ # Unbinding default key