diff options
author | hut <hut@lavabit.com> | 2010-03-09 12:40:51 +0100 |
---|---|---|
committer | hut <hut@lavabit.com> | 2010-03-09 14:40:24 +0100 |
commit | efbde17048b14d43895e4cf91e798fb97702b68f (patch) | |
tree | e3abf4aea43ac6d615003534c3e152278cb17b65 | |
parent | 4a435286452a8a0177994a6f27f3806cdb74fa82 (diff) | |
download | ranger-efbde17048b14d43895e4cf91e798fb97702b68f.tar.gz |
keyparser: test for collisions with directories of a lenght > 1
-rw-r--r-- | ranger/container/keymap.py | 28 | ||||
-rw-r--r-- | test/tc_newkeys.py | 35 |
2 files changed, 42 insertions, 21 deletions
diff --git a/ranger/container/keymap.py b/ranger/container/keymap.py index 7f72a282..e8cf6119 100644 --- a/ranger/container/keymap.py +++ b/ranger/container/keymap.py @@ -115,7 +115,7 @@ class KeyBuffer(object): """The evaluator and storage for pressed keys""" def __init__(self, keymap, direction_keys): self.assign(keymap, direction_keys) - + def assign(self, keymap, direction_keys): self.keymap = keymap self.direction_keys = direction_keys @@ -210,20 +210,23 @@ class KeyBuffer(object): self.failure = True return None except KeyError: - if DIRKEY in self.tree_pointer: - self.eval_command = False - self.eval_quantifier = True + try: self.tree_pointer = self.tree_pointer[DIRKEY] + except KeyError: + try: + self.tree_pointer = self.tree_pointer[ANYKEY] + except KeyError: + self.failure = True + return None + else: + self.matches.append(key) + assert isinstance(self.tree_pointer, (Binding, dict)) + self._try_to_finish() + else: assert isinstance(self.tree_pointer, (Binding, dict)) + self.eval_command = False + self.eval_quantifier = True self.dir_tree_pointer = self.direction_keys._tree - elif ANYKEY in self.tree_pointer: - self.matches.append(key) - self.tree_pointer = self.tree_pointer[ANYKEY] - assert isinstance(self.tree_pointer, (Binding, dict)) - self._try_to_finish() - else: - self.failure = True - return None else: if isinstance(self.tree_pointer, dict): try: @@ -286,7 +289,6 @@ special_keys = { 'cr': ord("\n"), 'enter': ord("\n"), 'space': ord(" "), - 'space': ord(" "), 'down': curses.KEY_DOWN, 'up': curses.KEY_UP, 'left': curses.KEY_LEFT, diff --git a/test/tc_newkeys.py b/test/tc_newkeys.py index 92bfb513..0c810af5 100644 --- a/test/tc_newkeys.py +++ b/test/tc_newkeys.py @@ -83,6 +83,31 @@ class Test(PressTestCase): self.assert_(match.function) self.assertEqual(8, match.function(args)) + def test_map_collision(self): + def add_dirs(arg): + return sum(dir.down for dir in arg.directions) + def return5(_): + return 5 + + + directions = KeyMap() + directions.map('gg', dir=Direction(down=1)) + + + km = KeyMap() + km.map('gh', return5) + km.map('agh', return5) + km.map('a<dir>', add_dirs) + + kb = KeyBuffer(km, directions) + press = self._mkpress(kb, km) + + self.assertEqual(5, press('gh')) + self.assertEqual(5, press('agh')) +# self.assertPressFails(kb, 'agh') + self.assertEqual(1, press('agg')) + + def test_translate_keys(self): def test(string, *args): if not args: @@ -120,10 +145,7 @@ class Test(PressTestCase): def test_alias(self): def add_dirs(arg): - n = 0 - for dir in arg.directions: - n += dir.down - return n + return sum(dir.down for dir in arg.directions) def return5(_): return 5 @@ -379,10 +401,7 @@ class Test(PressTestCase): directions.map('k', dir=Direction(down=-1)) def add_dirs(arg): - n = 0 - for dir in arg.directions: - n += dir.down - return n + return sum(dir.down for dir in arg.directions) km.map('x<dir>y<dir>', add_dirs) km.map('four<dir><dir><dir><dir>', add_dirs) |