Refactor read_config for easier testing

This commit is contained in:
Alexander Cogneau 2015-08-25 12:04:22 +02:00
parent 05eb9bd08c
commit 09161faca5
2 changed files with 22 additions and 11 deletions

View File

@ -326,17 +326,21 @@ class BaseKeyParser(QObject):
self.special_bindings = {}
keyconfparser = objreg.get('key-config')
for (key, cmd) in keyconfparser.get_bindings_for(modename).items():
if not cmd:
continue
elif key.startswith('<') and key.endswith('>'):
keystr = utils.normalize_keystr(key[1:-1])
self.special_bindings[keystr] = cmd
elif self._supports_chains:
self.bindings[key] = cmd
elif self._warn_on_keychains:
log.keyboard.warning(
"Ignoring keychain '{}' in mode '{}' because "
"keychains are not supported there.".format(key, modename))
if cmd:
self._parse_key_command(modename, key, cmd)
def _parse_key_command(self, modename, key, cmd):
"""Parse the keys and their command and store them in the object."""
if key.startswith('<') and key.endswith('>'):
keystr = utils.normalize_keystr(key[1:-1])
self.special_bindings[keystr] = cmd
elif self._supports_chains:
self.bindings[key] = cmd
elif self._warn_on_keychains:
log.keyboard.warning(
"Ignoring keychain '{}' in mode '{}' because "
"keychains are not supported there."
.format(key, modename))
def execute(self, cmdstr, keytype, count=None):
"""Handle a completed keychain.

View File

@ -79,6 +79,13 @@ class TestReadConfig:
with pytest.raises(ValueError):
kp.read_config()
def test_read_config_no_modename(self):
"""Test reading config with _modename set."""
kp = basekeyparser.BaseKeyParser(0, supports_chains=True)
kp._modename = "normal"
kp.read_config(modename=None)
assert 'a' in kp.bindings
def test_read_config_valid(self):
"""Test reading config."""
kp = basekeyparser.BaseKeyParser(0, supports_count=True,