From d08b9f47be157d8e2a7f1da815fb31a687b8cc89 Mon Sep 17 00:00:00 2001 From: Michael Lazar Date: Tue, 9 Feb 2016 23:42:07 -0800 Subject: [PATCH] Fixed the tests. --- rtv/objects.py | 11 +++++++---- rtv/subscription.py | 2 +- tests/test_config.py | 4 ++-- tests/test_objects.py | 18 ++++++++++++------ tests/test_page.py | 4 ++-- 5 files changed, 24 insertions(+), 15 deletions(-) diff --git a/rtv/objects.py b/rtv/objects.py index 93ec0e8..214bdde 100644 --- a/rtv/objects.py +++ b/rtv/objects.py @@ -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. """ diff --git a/rtv/subscription.py b/rtv/subscription.py index eb0f29d..89f7877 100644 --- a/rtv/subscription.py +++ b/rtv/subscription.py @@ -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" diff --git a/tests/test_config.py b/tests/test_config.py index 87aa428..1f409c4 100644 --- a/tests/test_config.py +++ b/tests/test_config.py @@ -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', ''] + assert config.keymap.get('UPVOTE') == [''] def test_config_refresh_token(): diff --git a/tests/test_objects.py b/tests/test_objects.py index 6e0a751..d9592f6 100644 --- a/tests/test_objects.py +++ b/tests/test_objects.py @@ -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, '', ''] assert keymap.get(Command('exit')) == [] - assert keymap.get('upvote') == [98, 269] + assert keymap.get('upvote') == ['b', ''] 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, '', '']} @@ -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('') == 10 + assert KeyMap.parse('') == 259 + assert KeyMap.parse('') == 269 for key in ('', None, '', '', '', '♬'): 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) diff --git a/tests/test_page.py b/tests/test_page.py index 01978d9..2891e16 100644 --- a/tests/test_page.py +++ b/tests/test_page.py @@ -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