Fixed the tests.
This commit is contained in:
@@ -540,12 +540,15 @@ class Controller(object):
|
||||
for command, func in controller.character_map.copy().items():
|
||||
if isinstance(command, Command):
|
||||
for key in keymap.get(command):
|
||||
if key in controller.character_map:
|
||||
val = keymap.parse(key)
|
||||
# Check if the key is already programmed to trigger a
|
||||
# different function.
|
||||
if controller.character_map.get(val, func) != func:
|
||||
raise exceptions.ConfigError(
|
||||
'Invalid configuration, cannot bind the `%s`'
|
||||
' key to two different commands in the `%s`'
|
||||
' context' % (key, self.__class__.__name__))
|
||||
controller.character_map[key] = func
|
||||
controller.character_map[val] = func
|
||||
|
||||
def trigger(self, char, *args, **kwargs):
|
||||
|
||||
@@ -621,7 +624,7 @@ class KeyMap(object):
|
||||
for command, keys in bindings.items():
|
||||
if not isinstance(command, Command):
|
||||
command = Command(command)
|
||||
self._keymap[command] = [self._parse_key(key) for key in keys]
|
||||
self._keymap[command] = keys
|
||||
|
||||
def get(self, command):
|
||||
if not isinstance(command, Command):
|
||||
@@ -633,7 +636,7 @@ class KeyMap(object):
|
||||
'Invalid configuration, `%s` key undefined' % command.val)
|
||||
|
||||
@staticmethod
|
||||
def _parse_key(key):
|
||||
def parse(key):
|
||||
"""
|
||||
Parse a key represented by a string and return its character code.
|
||||
"""
|
||||
|
||||
@@ -44,7 +44,7 @@ class SubscriptionPage(Page):
|
||||
self.subreddit_data = self.content.get(self.nav.absolute_index)
|
||||
self.active = False
|
||||
|
||||
@SubscriptionController.register(Command('SUBSCRIPTION_CLOSE'))
|
||||
@SubscriptionController.register(Command('SUBSCRIPTION_EXIT'))
|
||||
def close_subscriptions(self):
|
||||
"Close subscriptions and return to the subreddit page"
|
||||
|
||||
|
||||
@@ -115,8 +115,8 @@ def test_config_from_file():
|
||||
config.update(**fargs)
|
||||
config.keymap.set_bindings(fbindings)
|
||||
assert config.config == args
|
||||
assert config.keymap.get('REFRESH') == [ord('r'), 269]
|
||||
assert config.keymap.get('UPVOTE') == []
|
||||
assert config.keymap.get('REFRESH') == ['r', '<KEY_F5>']
|
||||
assert config.keymap.get('UPVOTE') == ['']
|
||||
|
||||
|
||||
def test_config_refresh_token():
|
||||
|
||||
@@ -276,7 +276,7 @@ def test_objects_controller_command():
|
||||
assert 'ControllerA' in six.text_type(e)
|
||||
|
||||
# Reset the character map
|
||||
ControllerA.character_map = {Command('REFRESH'): int, Command('UPVOTE'):int}
|
||||
ControllerA.character_map = {Command('REFRESH'): 0, Command('UPVOTE'): 0}
|
||||
|
||||
# All commands must be defined in the keymap
|
||||
keymap = KeyMap({'REFRESH': [0x10]})
|
||||
@@ -310,12 +310,12 @@ def test_objects_keymap():
|
||||
}
|
||||
|
||||
keymap = KeyMap(bindings)
|
||||
assert keymap.get(Command('REFRESH')) == [97, 18, 10, 259]
|
||||
assert keymap.get(Command('REFRESH')) == ['a', 0x12, '<LF>', '<KEY_UP>']
|
||||
assert keymap.get(Command('exit')) == []
|
||||
assert keymap.get('upvote') == [98, 269]
|
||||
assert keymap.get('upvote') == ['b', '<KEY_F5>']
|
||||
with pytest.raises(exceptions.ConfigError) as e:
|
||||
keymap.get('downvote')
|
||||
assert 'Command(DOWNVOTE)' in six.text_type(e)
|
||||
assert 'DOWNVOTE' in six.text_type(e)
|
||||
|
||||
# Updating the bindings wipes out the old ones
|
||||
bindings = {'refresh': ['a', 0x12, '<LF>', '<KEY_UP>']}
|
||||
@@ -323,11 +323,17 @@ def test_objects_keymap():
|
||||
assert keymap.get('refresh')
|
||||
with pytest.raises(exceptions.ConfigError) as e:
|
||||
keymap.get('upvote')
|
||||
assert 'Command(UPVOTE)' in six.text_type(e)
|
||||
assert 'UPVOTE' in six.text_type(e)
|
||||
|
||||
# Strings should be parsed correctly into keys
|
||||
assert KeyMap.parse('a') == 97
|
||||
assert KeyMap.parse(0x12) == 18
|
||||
assert KeyMap.parse('<LF>') == 10
|
||||
assert KeyMap.parse('<KEY_UP>') == 259
|
||||
assert KeyMap.parse('<KEY_F5>') == 269
|
||||
for key in ('', None, '<lf>', '<DNS>', '<KEY_UD>', '♬'):
|
||||
with pytest.raises(exceptions.ConfigError) as e:
|
||||
keymap.set_bindings({'refresh': [key]})
|
||||
keymap.parse(key)
|
||||
assert six.text_type(key) in six.text_type(e)
|
||||
|
||||
|
||||
|
||||
@@ -38,7 +38,7 @@ def test_page_logged_in(terminal):
|
||||
def test_page_unauthenticated(reddit, terminal, config, oauth):
|
||||
|
||||
page = Page(reddit, terminal, config, oauth)
|
||||
page.controller = PageController(page)
|
||||
page.controller = PageController(page, keymap=config.keymap)
|
||||
with mock.patch.object(page, 'refresh_content'), \
|
||||
mock.patch.object(page, 'content'), \
|
||||
mock.patch.object(page, 'nav'), \
|
||||
@@ -104,7 +104,7 @@ def test_page_unauthenticated(reddit, terminal, config, oauth):
|
||||
def test_page_authenticated(reddit, terminal, config, oauth, refresh_token):
|
||||
|
||||
page = Page(reddit, terminal, config, oauth)
|
||||
page.controller = PageController(page)
|
||||
page.controller = PageController(page, keymap=config.keymap)
|
||||
config.refresh_token = refresh_token
|
||||
|
||||
# Login
|
||||
|
||||
Reference in New Issue
Block a user