Fixed the tests.

This commit is contained in:
Michael Lazar
2016-02-09 23:42:07 -08:00
parent 0de3033414
commit d08b9f47be
5 changed files with 24 additions and 15 deletions

View File

@@ -540,12 +540,15 @@ class Controller(object):
for command, func in controller.character_map.copy().items(): for command, func in controller.character_map.copy().items():
if isinstance(command, Command): if isinstance(command, Command):
for key in keymap.get(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( raise exceptions.ConfigError(
'Invalid configuration, cannot bind the `%s`' 'Invalid configuration, cannot bind the `%s`'
' key to two different commands in the `%s`' ' key to two different commands in the `%s`'
' context' % (key, self.__class__.__name__)) ' context' % (key, self.__class__.__name__))
controller.character_map[key] = func controller.character_map[val] = func
def trigger(self, char, *args, **kwargs): def trigger(self, char, *args, **kwargs):
@@ -621,7 +624,7 @@ class KeyMap(object):
for command, keys in bindings.items(): for command, keys in bindings.items():
if not isinstance(command, Command): if not isinstance(command, Command):
command = Command(command) command = Command(command)
self._keymap[command] = [self._parse_key(key) for key in keys] self._keymap[command] = keys
def get(self, command): def get(self, command):
if not isinstance(command, Command): if not isinstance(command, Command):
@@ -633,7 +636,7 @@ class KeyMap(object):
'Invalid configuration, `%s` key undefined' % command.val) 'Invalid configuration, `%s` key undefined' % command.val)
@staticmethod @staticmethod
def _parse_key(key): def parse(key):
""" """
Parse a key represented by a string and return its character code. Parse a key represented by a string and return its character code.
""" """

View File

@@ -44,7 +44,7 @@ class SubscriptionPage(Page):
self.subreddit_data = self.content.get(self.nav.absolute_index) self.subreddit_data = self.content.get(self.nav.absolute_index)
self.active = False self.active = False
@SubscriptionController.register(Command('SUBSCRIPTION_CLOSE')) @SubscriptionController.register(Command('SUBSCRIPTION_EXIT'))
def close_subscriptions(self): def close_subscriptions(self):
"Close subscriptions and return to the subreddit page" "Close subscriptions and return to the subreddit page"

View File

@@ -115,8 +115,8 @@ def test_config_from_file():
config.update(**fargs) config.update(**fargs)
config.keymap.set_bindings(fbindings) config.keymap.set_bindings(fbindings)
assert config.config == args assert config.config == args
assert config.keymap.get('REFRESH') == [ord('r'), 269] assert config.keymap.get('REFRESH') == ['r', '<KEY_F5>']
assert config.keymap.get('UPVOTE') == [] assert config.keymap.get('UPVOTE') == ['']
def test_config_refresh_token(): def test_config_refresh_token():

View File

@@ -276,7 +276,7 @@ def test_objects_controller_command():
assert 'ControllerA' in six.text_type(e) assert 'ControllerA' in six.text_type(e)
# Reset the character map # 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 # All commands must be defined in the keymap
keymap = KeyMap({'REFRESH': [0x10]}) keymap = KeyMap({'REFRESH': [0x10]})
@@ -310,12 +310,12 @@ def test_objects_keymap():
} }
keymap = KeyMap(bindings) 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(Command('exit')) == []
assert keymap.get('upvote') == [98, 269] assert keymap.get('upvote') == ['b', '<KEY_F5>']
with pytest.raises(exceptions.ConfigError) as e: with pytest.raises(exceptions.ConfigError) as e:
keymap.get('downvote') 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 # Updating the bindings wipes out the old ones
bindings = {'refresh': ['a', 0x12, '<LF>', '<KEY_UP>']} bindings = {'refresh': ['a', 0x12, '<LF>', '<KEY_UP>']}
@@ -323,11 +323,17 @@ def test_objects_keymap():
assert keymap.get('refresh') assert keymap.get('refresh')
with pytest.raises(exceptions.ConfigError) as e: with pytest.raises(exceptions.ConfigError) as e:
keymap.get('upvote') 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>', ''): for key in ('', None, '<lf>', '<DNS>', '<KEY_UD>', ''):
with pytest.raises(exceptions.ConfigError) as e: 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) assert six.text_type(key) in six.text_type(e)

View File

@@ -38,7 +38,7 @@ def test_page_logged_in(terminal):
def test_page_unauthenticated(reddit, terminal, config, oauth): def test_page_unauthenticated(reddit, terminal, config, oauth):
page = Page(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'), \ with mock.patch.object(page, 'refresh_content'), \
mock.patch.object(page, 'content'), \ mock.patch.object(page, 'content'), \
mock.patch.object(page, 'nav'), \ 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): def test_page_authenticated(reddit, terminal, config, oauth, refresh_token):
page = Page(reddit, terminal, config, oauth) page = Page(reddit, terminal, config, oauth)
page.controller = PageController(page) page.controller = PageController(page, keymap=config.keymap)
config.refresh_token = refresh_token config.refresh_token = refresh_token
# Login # Login