Test and fix keyutils._parse_keystring

This commit is contained in:
Florian Bruhin 2018-03-03 23:22:03 +01:00
parent 7f8508a367
commit 3c9e8ff9ab
2 changed files with 33 additions and 4 deletions

View File

@ -151,10 +151,14 @@ def _parse_keystring(keystr):
special = False
for c in keystr:
if c == '>':
assert special
if special:
yield _normalize_keystr(key)
key = ''
special = False
else:
yield '>'
for c in key:
yield 'Shift+' + c if c.isupper() else c
elif c == '<':
special = True
elif special:

View File

@ -138,6 +138,31 @@ def test_key_info_str(key, modifiers, expected):
assert str(keyutils.KeyInfo(key, modifiers)) == expected
@pytest.mark.parametrize('keystr, expected', [
('foo', "Could not parse 'foo': error"),
(None, "Could not parse keystring: error"),
])
def test_key_parse_error(keystr, expected):
exc = keyutils.KeyParseError(keystr, "error")
assert str(exc) == expected
@pytest.mark.parametrize('keystr, parts', [
('a', ['a']),
('ab', ['a', 'b']),
('a<', ['a', '<']),
('a>', ['a', '>']),
('<a', ['<', 'a']),
('>a', ['>', 'a']),
('aA', ['a', 'Shift+A']),
('a<Ctrl+a>b', ['a', 'ctrl+a', 'b']),
('<Ctrl+a>a', ['ctrl+a', 'a']),
('a<Ctrl+a>', ['a', 'ctrl+a']),
])
def test_parse_keystr(keystr, parts):
assert list(keyutils._parse_keystring(keystr)) == parts
@pytest.mark.parametrize('keystr, expected', [
('<Control-x>', keyutils.KeySequence(Qt.ControlModifier | Qt.Key_X)),
('<Meta-x>', keyutils.KeySequence(Qt.MetaModifier | Qt.Key_X)),