Fix KeyConfig._prepare

This commit is contained in:
Florian Bruhin 2017-12-29 16:10:12 +01:00
parent a145497c65
commit dcf89f7a28
2 changed files with 14 additions and 16 deletions

View File

@ -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')

View File

@ -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'),
('<Ctrl-X>', '<ctrl+x>'),
])
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