From 3c9e8ff9ab6cec113682837348c77e963ec94256 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Sat, 3 Mar 2018 23:22:03 +0100 Subject: [PATCH] Test and fix keyutils._parse_keystring --- qutebrowser/keyinput/keyutils.py | 12 ++++++++---- tests/unit/keyinput/test_keyutils.py | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 4 deletions(-) diff --git a/qutebrowser/keyinput/keyutils.py b/qutebrowser/keyinput/keyutils.py index 3518e53dd..1741fbe33 100644 --- a/qutebrowser/keyinput/keyutils.py +++ b/qutebrowser/keyinput/keyutils.py @@ -151,10 +151,14 @@ def _parse_keystring(keystr): special = False for c in keystr: if c == '>': - assert special - yield _normalize_keystr(key) - key = '' - special = False + 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: diff --git a/tests/unit/keyinput/test_keyutils.py b/tests/unit/keyinput/test_keyutils.py index 2e595c19a..f8b8382cd 100644 --- a/tests/unit/keyinput/test_keyutils.py +++ b/tests/unit/keyinput/test_keyutils.py @@ -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']), + ('aA', ['a', 'Shift+A']), + ('ab', ['a', 'ctrl+a', 'b']), + ('a', ['ctrl+a', 'a']), + ('a', ['a', 'ctrl+a']), +]) +def test_parse_keystr(keystr, parts): + assert list(keyutils._parse_keystring(keystr)) == parts + + @pytest.mark.parametrize('keystr, expected', [ ('', keyutils.KeySequence(Qt.ControlModifier | Qt.Key_X)), ('', keyutils.KeySequence(Qt.MetaModifier | Qt.Key_X)),