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

View File

@ -79,6 +79,13 @@ class TestReadConfig:
with pytest.raises(ValueError): with pytest.raises(ValueError):
kp.read_config() 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): def test_read_config_valid(self):
"""Test reading config.""" """Test reading config."""
kp = basekeyparser.BaseKeyParser(0, supports_count=True, kp = basekeyparser.BaseKeyParser(0, supports_count=True,