Unit tests and improvements for :bind/:unbind
This commit is contained in:
parent
353c10aee7
commit
07d0ea6a54
@ -223,8 +223,9 @@ class ConfigCommands:
|
||||
|
||||
"""qutebrowser commands related to the configuration."""
|
||||
|
||||
def __init__(self, config):
|
||||
def __init__(self, config, keyconfig):
|
||||
self._config = config
|
||||
self._keyconfig = keyconfig
|
||||
|
||||
@cmdutils.register(instance='config-commands', star_args_optional=True)
|
||||
@cmdutils.argument('win_id', win_id=True)
|
||||
@ -324,10 +325,10 @@ class ConfigCommands:
|
||||
"""
|
||||
if command is None:
|
||||
if utils.is_special_key(key):
|
||||
# key_instance.get_command does this, but we also need it
|
||||
# self._keyconfig.get_command does this, but we also need it
|
||||
# normalized for the output below
|
||||
key = utils.normalize_keystr(key)
|
||||
cmd = key_instance.get_command(key, mode)
|
||||
cmd = self._keyconfig.get_command(key, mode)
|
||||
if cmd is None:
|
||||
message.info("{} is unbound in {} mode".format(key, mode))
|
||||
else:
|
||||
@ -336,12 +337,13 @@ class ConfigCommands:
|
||||
return
|
||||
|
||||
try:
|
||||
key_instance.bind(key, command, mode=mode, force=force,
|
||||
save_yaml=True)
|
||||
self._keyconfig.bind(key, command, mode=mode, force=force,
|
||||
save_yaml=True)
|
||||
except configexc.DuplicateKeyError as e:
|
||||
raise cmdexc.CommandError(str(e) + " - use --force to override!")
|
||||
raise cmdexc.CommandError("bind: {} - use --force to override!"
|
||||
.format(e))
|
||||
except configexc.KeybindingError as e:
|
||||
raise cmdexc.CommandError(str(e))
|
||||
raise cmdexc.CommandError("bind: {}".format(e))
|
||||
|
||||
@cmdutils.register(instance='config-commands')
|
||||
def unbind(self, key, mode='normal'):
|
||||
@ -352,9 +354,9 @@ class ConfigCommands:
|
||||
mode: A mode to unbind the key in (default: `normal`).
|
||||
"""
|
||||
try:
|
||||
key_instance.unbind(key, mode=mode, save_yaml=True)
|
||||
self._keyconfig.unbind(key, mode=mode, save_yaml=True)
|
||||
except configexc.KeybindingError as e:
|
||||
raise cmdexc.CommandError(str(e))
|
||||
raise cmdexc.CommandError('unbind: {}'.format(e))
|
||||
|
||||
|
||||
class Config(QObject):
|
||||
@ -611,14 +613,14 @@ def init(parent=None):
|
||||
config.read_configdata()
|
||||
objreg.register('config', config)
|
||||
|
||||
config_commands = ConfigCommands(config)
|
||||
objreg.register('config-commands', config_commands)
|
||||
|
||||
global val, instance, key_instance
|
||||
val = ConfigContainer(config)
|
||||
instance = config
|
||||
key_instance = KeyConfig(config)
|
||||
|
||||
config_commands = ConfigCommands(config, key_instance)
|
||||
objreg.register('config-commands', config_commands)
|
||||
|
||||
for cf in _change_filters:
|
||||
cf.validate()
|
||||
config.read_yaml()
|
||||
|
@ -2,118 +2,7 @@
|
||||
|
||||
Feature: Keyboard input
|
||||
|
||||
Tests for :bind and :unbind, :clear-keychain and other keyboard input
|
||||
related things.
|
||||
|
||||
# :bind
|
||||
|
||||
Scenario: Binding a keychain
|
||||
When I run :bind test01 message-info test01
|
||||
And I press the keys "test01"
|
||||
Then the message "test01" should be shown
|
||||
|
||||
Scenario: Binding an invalid command
|
||||
When I run :bind test02 abcd
|
||||
Then the error "abcd: no such command" should be shown
|
||||
|
||||
Scenario: Binding with invalid mode.
|
||||
When I run :bind --mode abcd test03 message-info test03
|
||||
Then the error "Invalid mode abcd!" should be shown
|
||||
|
||||
Scenario: Binding with wrong mode
|
||||
When I run :bind --mode caret test13 completion-item-del
|
||||
Then the error "completion-item-del: This command is only allowed in command mode, not caret." should be shown
|
||||
|
||||
Scenario: Double-binding a key
|
||||
When I run :bind test04 message-info test04
|
||||
And I run :bind test04 message-info test04-2
|
||||
And I press the keys "test04"
|
||||
Then the error "Duplicate key test04 - use --force to override!" should be shown
|
||||
And the message "test04" should be shown
|
||||
|
||||
Scenario: Double-binding with --force
|
||||
When I run :bind test05 message-info test05
|
||||
And I run :bind --force test05 message-info test05-2
|
||||
And I press the keys "test05"
|
||||
Then the message "test05-2" should be shown
|
||||
|
||||
Scenario: Printing an unbound key
|
||||
When I run :bind test06
|
||||
Then the message "test06 is unbound in normal mode" should be shown
|
||||
|
||||
Scenario: Printing a bound key
|
||||
When I run :bind test07 message-info foo
|
||||
And I run :bind test07
|
||||
Then the message "test07 is bound to 'message-info foo' in normal mode" should be shown
|
||||
|
||||
Scenario: Printing a bound key in a given mode
|
||||
When I run :bind --mode=caret test08 message-info bar
|
||||
And I run :bind --mode=caret test08
|
||||
Then the message "test08 is bound to 'message-info bar' in caret mode" should be shown
|
||||
|
||||
Scenario: Binding special keys with differing case (issue 1544)
|
||||
When I run :bind <ctrl-test21> message-info test01
|
||||
And I run :bind <Ctrl-Test21> message-info test01
|
||||
Then the error "Duplicate key <ctrl+test21> - use --force to override!" should be shown
|
||||
|
||||
Scenario: Print a special binding with differing case (issue 1544)
|
||||
When I run :bind <Ctrl-Test22> message-info foo
|
||||
And I run :bind <ctrl-test22>
|
||||
Then the message "<ctrl+test22> is bound to 'message-info foo' in normal mode" should be shown
|
||||
|
||||
Scenario: Overriding a special binding with differing case (issue 816)
|
||||
When I run :bind <ctrl-test23> message-info foo
|
||||
And I run :bind --force <Ctrl-Test23> message-info bar
|
||||
And I run :bind <ctrl-test23>
|
||||
Then the message "<ctrl+test23> is bound to 'message-info bar' in normal mode" should be shown
|
||||
|
||||
## FIXME:conf
|
||||
|
||||
# Scenario: Binding to an alias
|
||||
# When I run :set aliases 'mib' 'message-info baz'
|
||||
# And I run :bind test25 mib
|
||||
# And I press the keys "test25"
|
||||
# Then the message "baz" should be shown
|
||||
|
||||
# Scenario: Printing a bound alias
|
||||
# When I run :set aliases 'mib' 'message-info baz'
|
||||
# And I run :bind <test26> mib
|
||||
# And I run :bind <test26>
|
||||
# Then the message "<test26> is bound to 'mib' in normal mode" should be shown
|
||||
|
||||
Scenario: Binding with an unsupported mode
|
||||
When I run :bind --mode=caret test27 rl-unix-filename-rubout
|
||||
Then the error "rl-unix-filename-rubout: This command is only allowed in command/prompt mode, not caret." should be shown
|
||||
|
||||
# :unbind
|
||||
|
||||
Scenario: Binding and unbinding a keychain
|
||||
When I run :bind test09 message-error test09
|
||||
And I wait for "Config option changed: *" in the log
|
||||
And I run :unbind test09
|
||||
And I wait for "Config option changed: *" in the log
|
||||
And I press the keys "test09"
|
||||
Then "test09" should not be logged
|
||||
|
||||
Scenario: Unbinding with invalid mode.
|
||||
When I run :unbind test10 abcd
|
||||
Then the error "Invalid mode abcd!" should be shown
|
||||
|
||||
Scenario: Unbinding with invalid keychain.
|
||||
When I run :unbind test11
|
||||
Then the error "Can't find binding 'test11' in section 'normal'!" should be shown
|
||||
|
||||
Scenario: Unbinding a built-in binding
|
||||
When I run :unbind o
|
||||
And I press the key "o"
|
||||
Then "Giving up with 'o', no matches" should be logged
|
||||
# maybe check it's unbound in the config?
|
||||
|
||||
Scenario: Binding and unbinding a special keychain with differing case (issue 1544)
|
||||
When I run :bind <ctrl-test24> message-error test09
|
||||
And I run :unbind <Ctrl-Test24>
|
||||
When I run :bind <ctrl-test24>
|
||||
Then the message "<ctrl+test24> is unbound in normal mode" should be shown
|
||||
Tests for :clear-keychain and other keyboard input related things.
|
||||
|
||||
# :clear-keychain
|
||||
|
||||
|
@ -36,6 +36,12 @@ def configdata_init():
|
||||
configdata.init()
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def keyconf(config_stub):
|
||||
config_stub.val.aliases = {}
|
||||
return config.KeyConfig(config_stub)
|
||||
|
||||
|
||||
class TestChangeFilter:
|
||||
|
||||
@pytest.mark.parametrize('option', ['foobar', 'tab', 'tabss', 'tabs.'])
|
||||
@ -175,19 +181,12 @@ class TestKeyConfig:
|
||||
'allowed in command mode, not normal.'):
|
||||
keyconf.bind('a', 'completion-item-del', mode='normal')
|
||||
|
||||
@pytest.mark.parametrize('as_default', [True, False])
|
||||
@pytest.mark.parametrize('force', [True, False])
|
||||
@pytest.mark.parametrize('key', ['a', '<Ctrl-X>'])
|
||||
def test_bind_duplicate(self, keyconf, config_stub, no_bindings,
|
||||
as_default, force, key):
|
||||
bindings = {'normal': {'a': 'nop', '<Ctrl+x>': 'nop'}}
|
||||
if as_default:
|
||||
config_stub.val.bindings.default = bindings
|
||||
config_stub.val.bindings.commands = no_bindings
|
||||
else:
|
||||
config_stub.val.bindings.default = no_bindings
|
||||
config_stub.val.bindings.commands = bindings
|
||||
|
||||
@pytest.mark.parametrize('key', ['a', '<Ctrl-X>', 'b'])
|
||||
def test_bind_duplicate(self, keyconf, config_stub, force, key):
|
||||
config_stub.val.bindings.default = {'normal': {'a': 'nop',
|
||||
'<Ctrl+x>': 'nop'}}
|
||||
config_stub.val.bindings.commands = {'normal': {'b': 'nop'}}
|
||||
if force:
|
||||
keyconf.bind(key, 'message-info foo', mode='normal', force=True)
|
||||
assert keyconf.get_command(key, 'normal') == 'message-info foo'
|
||||
@ -210,24 +209,21 @@ class TestKeyConfig:
|
||||
assert keyconf.get_bindings_for(mode)['a'] == command
|
||||
assert keyconf.get_command('a', mode) == command
|
||||
|
||||
@pytest.mark.parametrize('as_default', [True, False])
|
||||
@pytest.mark.parametrize('key, normalized', [
|
||||
('a', 'a'),
|
||||
('a', 'a'), # default bindings
|
||||
('b', 'b'), # custom bindings
|
||||
('<Ctrl-X>', '<ctrl+x>')
|
||||
])
|
||||
@pytest.mark.parametrize('mode', ['normal', 'caret'])
|
||||
def test_unbind(self, keyconf, config_stub, qtbot, no_bindings,
|
||||
as_default, key, normalized, mode):
|
||||
bindings = {
|
||||
def test_unbind(self, keyconf, config_stub, qtbot, key, normalized, mode):
|
||||
config_stub.val.bindings.default = {
|
||||
'normal': {'a': 'nop', '<ctrl+x>': 'nop'},
|
||||
'caret': {'a': 'nop', '<ctrl+x>': 'nop'},
|
||||
}
|
||||
if as_default:
|
||||
config_stub.val.bindings.default = bindings
|
||||
config_stub.val.bindings.commands = no_bindings
|
||||
else:
|
||||
config_stub.val.bindings.default = no_bindings
|
||||
config_stub.val.bindings.commands = bindings
|
||||
config_stub.val.bindings.commands = {
|
||||
'normal': {'b': 'nop'},
|
||||
'caret': {'b': 'nop'},
|
||||
}
|
||||
|
||||
with qtbot.wait_signal(config_stub.changed):
|
||||
keyconf.unbind(key, mode=mode)
|
||||
@ -235,12 +231,13 @@ class TestKeyConfig:
|
||||
assert keyconf.get_command(key, mode) is None
|
||||
|
||||
mode_bindings = config_stub.val.bindings.commands[mode]
|
||||
if as_default:
|
||||
default_bindings = config_stub.val.bindings.default
|
||||
assert default_bindings[mode] == bindings[mode]
|
||||
assert mode_bindings[normalized] is None
|
||||
else:
|
||||
if key == 'b':
|
||||
# Custom binding
|
||||
assert normalized not in mode_bindings
|
||||
else:
|
||||
default_bindings = config_stub.val.bindings.default
|
||||
assert default_bindings[mode] == {'a': 'nop', '<ctrl+x>': 'nop'}
|
||||
assert mode_bindings[normalized] is None
|
||||
|
||||
def test_unbind_unbound(self, keyconf, config_stub, no_bindings):
|
||||
"""Try unbinding a key which is not bound."""
|
||||
@ -305,11 +302,13 @@ def test_set_register_stylesheet(delete, stylesheet_param, update, qtbot,
|
||||
assert obj.rendered_stylesheet == expected
|
||||
|
||||
|
||||
class TestConfigCommands:
|
||||
class TestSetConfigCommand:
|
||||
|
||||
"""Tests for :set."""
|
||||
|
||||
@pytest.fixture
|
||||
def commands(self, config_stub):
|
||||
return config.ConfigCommands(config_stub)
|
||||
def commands(self, config_stub, keyconf):
|
||||
return config.ConfigCommands(config_stub, keyconf)
|
||||
|
||||
@pytest.fixture
|
||||
def tabbed_browser(self, stubs, win_registry):
|
||||
@ -463,3 +462,147 @@ class TestConfigCommands:
|
||||
commands.set(0, opt, 'green', 'magenta', 'blue', 'yellow')
|
||||
assert config_stub.get(opt) == expected
|
||||
assert config_stub._yaml.values[opt] == expected
|
||||
|
||||
|
||||
class TestBindConfigCommand:
|
||||
|
||||
"""Tests for :bind and :unbind."""
|
||||
|
||||
@pytest.fixture
|
||||
def commands(self, config_stub, keyconf):
|
||||
return config.ConfigCommands(config_stub, keyconf)
|
||||
|
||||
@pytest.fixture
|
||||
def no_bindings(self):
|
||||
"""Get a dict with no bindings."""
|
||||
return {'normal': {}}
|
||||
|
||||
@pytest.mark.parametrize('command', ['nop', 'nope'])
|
||||
def test_bind(self, commands, config_stub, no_bindings, keyconf, command):
|
||||
"""Simple :bind test (and aliases)."""
|
||||
config_stub.val.aliases = {'nope': 'nop'}
|
||||
config_stub.val.bindings.default = no_bindings
|
||||
config_stub.val.bindings.commands = no_bindings
|
||||
|
||||
commands.bind('a', command)
|
||||
assert keyconf.get_command('a', 'normal') == command
|
||||
yaml_bindings = config_stub._yaml.values['bindings.commands']['normal']
|
||||
assert yaml_bindings['a'] == command
|
||||
|
||||
@pytest.mark.parametrize('key, mode, expected', [
|
||||
# Simple
|
||||
('a', 'normal', "a is bound to 'message-info a' in normal mode"),
|
||||
# Alias
|
||||
('b', 'normal', "b is bound to 'mib' in normal mode"),
|
||||
# Custom binding
|
||||
('c', 'normal', "c is bound to 'message-info c' in normal mode"),
|
||||
# Special key
|
||||
('<Ctrl-X>', 'normal',
|
||||
"<ctrl+x> is bound to 'message-info C-x' in normal mode"),
|
||||
# unbound
|
||||
('x', 'normal', "x is unbound in normal mode"),
|
||||
# non-default mode
|
||||
('x', 'caret', "x is bound to 'nop' in caret mode"),
|
||||
])
|
||||
def test_bind_print(self, commands, config_stub, message_mock,
|
||||
key, mode, expected):
|
||||
""":bind key
|
||||
|
||||
Should print the binding.
|
||||
"""
|
||||
config_stub.val.aliases = {'mib': 'message-info b'}
|
||||
config_stub.val.bindings.default = {
|
||||
'normal': {'a': 'message-info a',
|
||||
'b': 'mib',
|
||||
'<Ctrl+x>': 'message-info C-x'},
|
||||
'caret': {'x': 'nop'}
|
||||
}
|
||||
config_stub.val.bindings.commands = {
|
||||
'normal': {'c': 'message-info c'}
|
||||
}
|
||||
|
||||
commands.bind(key, mode=mode)
|
||||
|
||||
msg = message_mock.getmsg(usertypes.MessageLevel.info)
|
||||
assert msg.text == expected
|
||||
|
||||
@pytest.mark.parametrize('command, mode, expected', [
|
||||
('foobar', 'normal', "bind: Invalid command: foobar"),
|
||||
('completion-item-del', 'normal',
|
||||
"bind: completion-item-del: This command is only allowed in "
|
||||
"command mode, not normal."),
|
||||
('nop', 'wrongmode', "bind: Invalid mode wrongmode!"),
|
||||
])
|
||||
def test_bind_invalid(self, commands, command, mode, expected):
|
||||
""":bind a foobar / :bind a completion-item-del
|
||||
|
||||
Should show an error.
|
||||
"""
|
||||
with pytest.raises(cmdexc.CommandError, match=expected):
|
||||
commands.bind('a', command, mode=mode)
|
||||
|
||||
@pytest.mark.parametrize('force', [True, False])
|
||||
@pytest.mark.parametrize('key', ['a', 'b', '<Ctrl-X>'])
|
||||
def test_bind_duplicate(self, commands, config_stub, keyconf, force, key):
|
||||
""":bind a key which already has been bound.
|
||||
|
||||
Also tests for https://github.com/qutebrowser/qutebrowser/issues/1544
|
||||
"""
|
||||
config_stub.val.bindings.default = {
|
||||
'normal': {'a': 'nop', '<Ctrl+x>': 'nop'}
|
||||
}
|
||||
config_stub.val.bindings.commands = {
|
||||
'normal': {'b': 'nop'},
|
||||
}
|
||||
|
||||
if force:
|
||||
commands.bind(key, 'message-info foo', mode='normal', force=True)
|
||||
assert keyconf.get_command(key, 'normal') == 'message-info foo'
|
||||
else:
|
||||
with pytest.raises(cmdexc.CommandError,
|
||||
match="bind: Duplicate key .* - use --force to "
|
||||
"override"):
|
||||
commands.bind(key, 'message-info foo', mode='normal')
|
||||
assert keyconf.get_command(key, 'normal') == 'nop'
|
||||
|
||||
@pytest.mark.parametrize('key, normalized', [
|
||||
('a', 'a'), # default bindings
|
||||
('b', 'b'), # custom bindings
|
||||
('c', 'c'), # :bind then :unbind
|
||||
('<Ctrl-X>', '<ctrl+x>') # normalized special binding
|
||||
])
|
||||
def test_unbind(self, commands, keyconf, config_stub, key, normalized):
|
||||
config_stub.val.bindings.default = {
|
||||
'normal': {'a': 'nop', '<ctrl+x>': 'nop'},
|
||||
'caret': {'a': 'nop', '<ctrl+x>': 'nop'},
|
||||
}
|
||||
config_stub.val.bindings.commands = {
|
||||
'normal': {'b': 'nop'},
|
||||
'caret': {'b': 'nop'},
|
||||
}
|
||||
if key == 'c':
|
||||
# Test :bind and :unbind
|
||||
commands.bind(key, 'nop')
|
||||
|
||||
commands.unbind(key)
|
||||
assert keyconf.get_command(key, 'normal') is None
|
||||
|
||||
yaml_bindings = config_stub._yaml.values['bindings.commands']['normal']
|
||||
if key in 'bc':
|
||||
# Custom binding
|
||||
assert normalized not in yaml_bindings
|
||||
else:
|
||||
assert yaml_bindings[normalized] is None
|
||||
|
||||
@pytest.mark.parametrize('key, mode, expected', [
|
||||
('foobar', 'normal',
|
||||
"unbind: Can't find binding 'foobar' in normal mode"),
|
||||
('x', 'wrongmode', "unbind: Invalid mode wrongmode!"),
|
||||
])
|
||||
def test_unbind_invalid(self, commands, key, mode, expected):
|
||||
""":unbind foobar / :unbind x wrongmode
|
||||
|
||||
Should show an error.
|
||||
"""
|
||||
with pytest.raises(cmdexc.CommandError, match=expected):
|
||||
commands.unbind(key, mode)
|
||||
|
Loading…
Reference in New Issue
Block a user