summary refs log tree commit diff stats
path: root/test
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-02-18 15:20:43 +0100
committerhut <hut@lavabit.com>2010-03-09 14:40:21 +0100
commit9588a0fb1fd45951eb8e640ad6c8bccaf7707586 (patch)
tree99d5e2c65aee869090e71ec43e2f1a1e82dd7cf8 /test
parent3f388ce51c55b242e46824808c858aa28ece5ce2 (diff)
downloadranger-9588a0fb1fd45951eb8e640ad6c8bccaf7707586.tar.gz
keyparser: implemented aliases
Diffstat (limited to 'test')
-rw-r--r--test/tc_newkeys.py98
1 files changed, 87 insertions, 11 deletions
diff --git a/test/tc_newkeys.py b/test/tc_newkeys.py
index aca6ba98..2b040954 100644
--- a/test/tc_newkeys.py
+++ b/test/tc_newkeys.py
@@ -14,8 +14,10 @@ except:
 FUNC = 'func'
 DIRECTION = 'direction'
 DIRARG = 'dir'
+ALIASARG = 'alias'
 DIRKEY = 9001
 ANYKEY = 9002
+MAX_ALIAS_RECURSION = 20
 
 def to_string(i):
 	"""convert a ord'd integer to a string"""
@@ -101,9 +103,20 @@ class KeyBuffer(object):
 		except KeyError:
 			self.failure = True
 		else:
-			if not isinstance(self.dir_tree_pointer, dict):
-				match = self.dir_tree_pointer
-				assert isinstance(match, Binding)
+			self._direction_try_to_finish()
+
+	def _direction_try_to_finish(self, rec=MAX_ALIAS_RECURSION):
+		if rec <= 0:
+			self.failure = True
+			return None
+		if not isinstance(self.dir_tree_pointer, dict):
+			match = self.dir_tree_pointer
+			assert isinstance(match, Binding)
+			if 'alias' in match.actions:
+				self.dir_tree_pointer = self.direction_keys.traverse(
+					match.alias)
+				self._direction_try_to_finish(rec - 1)
+			else:
 				direction = match.actions['dir'] * self.direction_quant
 				self.directions.append(direction)
 				self.direction_quant = None
@@ -151,11 +164,21 @@ class KeyBuffer(object):
 		else:
 			self._try_to_finish()
 
-	def _try_to_finish(self):
-		assert isinstance(self.tree_pointer, (Binding, dict))
-		if not isinstance(self.tree_pointer, dict):
-			self.command = self.tree_pointer
-			self.done = True
+	def _try_to_finish(self, rec=MAX_ALIAS_RECURSION):
+		if rec <= 0:
+			self.failure = True
+			return None
+		assert isinstance(self.tree_pointer, (Binding, dict, KeyMap))
+		if isinstance(self.tree_pointer, KeyMap):
+			self.tree_pointer = self.tree_pointer._tree
+		if isinstance(self.tree_pointer, Binding):
+			if 'alias' in self.tree_pointer.actions:
+				self.tree_pointer = self.keymap.traverse(
+					translate_keys(self.tree_pointer.actions['alias']))
+				self._try_to_finish(rec - 1)
+			else:
+				self.command = self.tree_pointer
+				self.done = True
 
 	def clear(self):
 		self.failure = False
@@ -177,8 +200,8 @@ class KeyBuffer(object):
 		return "".join(to_string(c) for c in self.all_keys)
 
 	def simulate_press(self, string):
-		for char in string:
-			self.add(ord(char))
+		for char in translate_keys(string):
+			self.add(char)
 			if self.done:
 				return self.command
 			if self.failure:
@@ -387,6 +410,12 @@ class Binding(object):
 			self.direction = self.actions[DIRARG]
 		except KeyError:
 			self.direction = None
+		try:
+			alias = self.actions[ALIASARG]
+		except KeyError:
+			self.alias = None
+		else:
+			self.alias = translate_keys(alias)
 
 class PressTestCase(TestCase):
 	"""Some useful methods for the actual test"""
@@ -399,7 +428,8 @@ class PressTestCase(TestCase):
 			self.assertTrue(keybuffer.done,
 					"parsing keys '"+keys+"' did not complete!")
 			arg = CommandArgs(None, None, keybuffer)
-			self.assert_(match.function, match.__dict__)
+			self.assert_(match.function, "No function found! " + \
+					str(match.__dict__))
 			return match.function(arg)
 		return press
 
@@ -453,6 +483,52 @@ class Test(PressTestCase):
 		test('k<a<>nz>')
 		test('>nz>')
 
+	def test_alias(self):
+		def add_dirs(arg):
+			n = 0
+			for dir in arg.directions:
+				n += dir.down
+			return n
+		def return5(_):
+			return 5
+
+		directions = KeyMap()
+		directions.add('j', dir=Direction(down=1))
+		directions.add('k', dir=Direction(down=-1))
+		directions.add('<CR>', alias='j')
+
+		base = KeyMap()
+		base.add(add_dirs, 'a<dir>')
+		base.add(add_dirs, 'b<dir>')
+		base.add(add_dirs, 'x<dir>x<dir>')
+		base.add(return5, 'f')
+		base.add('yy', alias='y')
+		base.add('!', alias='!')
+
+		other = KeyMap()
+		other.add('b<dir>b<dir>', alias='x<dir>x<dir>')
+		other.add(add_dirs, 'c<dir>')
+		other.add('g', alias='f')
+
+		km = base.merge(other)
+		kb = KeyBuffer(km, directions)
+
+		press = self._mkpress(kb, km)
+
+		self.assertEqual(1, press('aj'))
+		self.assertEqual(2, press('bjbj'))
+		self.assertEqual(1, press('cj'))
+		self.assertEqual(1, press('c<CR>'))
+
+		self.assertEqual(5, press('f'))
+		self.assertEqual(5, press('g'))
+
+		for n in range(1, 50):
+			self.assertPressIncomplete(kb, 'y' * n)
+
+		for n in range(1, 5):
+			self.assertPressFails(kb, '!' * n)
+
 	def test_tree(self):
 		t = Tree()
 		t.set('abcd', "Yes")
ass="nf">test_flatten(self): def f(x): return list(flatten(x)) self.assertEqual( [1,2,3,4,5], f([1,2,3,4,5])) self.assertEqual( [1,2,3,4,5], f([1,[2,3],4,5])) self.assertEqual( [1,2,3,4,5], f([[1,[2,3]],4,5])) self.assertEqual( [], f([[[[]]]])) self.assertEqual( ['a', 'b', 'fskldfjl'], f(['a', ('b', 'fskldfjl')])) self.assertEqual( ['a', 'b', 'fskldfjl'], f(['a', deque(['b', 'fskldfjl'])])) self.assertEqual( set([3.5, 4.3, 5.2, 6.0]), set(f([6.0, set((3.5, 4.3)), (5.2, )]))) def test_unique(self): def u(x): return list(unique(x)) self.assertEqual( [1,2,3], u([1,2,3])) self.assertEqual( [1,2,3], u([1,2,3,2,1])) self.assertEqual( [1,2,3], u([1,2,3,1,2,3,2,2,3,1,2,3,1,2,3,2,3,2,1])) self.assertEqual( [1,[2,3]], u([1,[2,3],1,[2,3],[2,3],1,[2,3],1,[2,3],[2,3],1])) def test_unique_keeps_type(self): def u(x): return unique(x) self.assertEqual( [1,2,3], u([1,2,3,1])) self.assertEqual( (1,2,3), u((1,2,3,1))) self.assertEqual( set((1,2,3)), u(set((1,2,3,1)))) self.assertEqual( deque((1,2,3)), u(deque((1,2,3,1)))) def test_mount_path(self): # assuming ismount() is used def my_ismount(path): depth = path.count('/') if path.startswith('/media'): return depth == 0 or depth == 2 return depth <= 1 from ranger.ext import mount_path original_ismount = mount_path.ismount mount_path.ismount = my_ismount try: mp = mount_path.mount_path self.assertEqual('/home', mp('/home/hut/porn/bondage')) self.assertEqual('/', mp('/')) self.assertEqual('/media/sdb1', mp('/media/sdb1/foo/bar')) self.assertEqual('/media/sdc2', mp('/media/sdc2/a/b/c/d/e')) finally: mount_path.ismount = original_ismount # TODO: links are not tested but I don't see how its possible # without messing around with mounts. # self.assertEqual('/media/foo', # mount_path('/media/bar/some_link_to_a_foo_subdirectory')) def test_openstruct(self): from ranger.ext.openstruct import OpenStruct from random import randint, choice from string import ascii_letters os = OpenStruct(a='a') self.assertEqual(os.a, 'a') self.assertRaises(AttributeError, getattr, os, 'b') dictionary = {'foo': 'bar', 'zoo': 'zar'} os = OpenStruct(dictionary) self.assertEqual(os.foo, 'bar') self.assertEqual(os.zoo, 'zar') self.assertRaises(AttributeError, getattr, os, 'sdklfj') for i in range(100): attr_name = ''.join(choice(ascii_letters) \ for x in range(randint(3,9))) value = randint(100,999) if not attr_name in os: self.assertRaises(AttributeError, getattr, os, attr_name) setattr(os, attr_name, value) value2 = randint(100,999) setattr(os, attr_name, value2) self.assertEqual(value2, getattr(os, attr_name)) def test_shell_escape(self): from ranger.ext.shell_escape import shell_escape, shell_quote self.assertEqual(r"'luigi'\''s pizza'", shell_quote("luigi's pizza")) self.assertEqual(r"luigi\'s\ pizza", shell_escape("luigi's pizza")) self.assertEqual(r"\$lol/foo\\xyz\|\>\<\]\[", shell_escape(r"$lol/foo\xyz|><][")) if __name__ == '__main__': unittest.main()