summary refs log tree commit diff stats
path: root/test
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-02-10 02:30:11 +0100
committerhut <hut@lavabit.com>2010-03-09 14:40:19 +0100
commit4ba2112b6db4cd556708324473f34f12f16063ec (patch)
treeb85f92fca49e575f46bcec91896fc1136f741f97 /test
parent95e3dfb1d6cdbb4b3501236d44be6ba1afe47462 (diff)
downloadranger-4ba2112b6db4cd556708324473f34f12f16063ec.tar.gz
keyparser: allow arbitrary number/order of directions
Diffstat (limited to 'test')
-rw-r--r--test/tc_newkeys.py100
1 files changed, 66 insertions, 34 deletions
diff --git a/test/tc_newkeys.py b/test/tc_newkeys.py
index 2db9b160..8f6be3f5 100644
--- a/test/tc_newkeys.py
+++ b/test/tc_newkeys.py
@@ -71,45 +71,30 @@ class KeyBuffer(object):
 			return
 
 		# evaluate the command
-		if self.eval_command:
-			try:
-				self.tree_pointer = self.tree_pointer[key]
-			except TypeError:
-				print(self.tree_pointer)
-				self.failure = True
-				return None
-			except KeyError:
-				if DIRKEY in self.tree_pointer:
-					self.eval_command = False
-					self.eval_quantifier = True
-					self.tree_pointer = self.tree_pointer[DIRKEY]
-					self.dir_tree_pointer = self.direction_keys._tree
-				elif ANYKEY in self.tree_pointer:
-					self.moo.append(key)
-					self.tree_pointer = self.tree_pointer[ANYKEY]
-					self._try_to_finish()
-				else:
-					self.failure = True
-					return None
-			else:
-				self._try_to_finish()
+		if self.eval_command and self._do_eval_command(key):
+			return
 
+		# evaluate (the first number of) the direction-quantifier
 		if self.eval_quantifier and self._do_eval_quantifier(key):
 			return
 
 		# evaluate direction keys {j,k,gg,pagedown,...}
 		if not self.eval_command:
-			try:
-				self.dir_tree_pointer = self.dir_tree_pointer[key]
-			except KeyError:
-				self.failure = True
-			else:
-				if not isinstance(self.dir_tree_pointer, dict):
-					match = self.dir_tree_pointer
-					direction = match.actions['dir'] * self.direction_quant
-					self.directions.append(direction)
-					self.direction_quant = None
-					self._try_to_finish()
+			self._do_eval_direction(key)
+
+	def _do_eval_direction(self, key):
+		try:
+			self.dir_tree_pointer = self.dir_tree_pointer[key]
+		except KeyError:
+			self.failure = True
+		else:
+			if not isinstance(self.dir_tree_pointer, dict):
+				match = self.dir_tree_pointer
+				direction = match.actions['dir'] * self.direction_quant
+				self.directions.append(direction)
+				self.direction_quant = None
+				self.eval_command = True
+				self._try_to_finish()
 
 	def _do_eval_quantifier(self, key):
 		if self.eval_command:
@@ -123,9 +108,32 @@ class KeyBuffer(object):
 			setattr(self, attr, getattr(self, attr) * 10 + key - 48)
 		else:
 			self.eval_quantifier = False
-			return False
+			return None
 		return True
 
+	def _do_eval_command(self, key):
+		try:
+			self.tree_pointer = self.tree_pointer[key]
+		except TypeError:
+			print(self.tree_pointer)
+			self.failure = True
+			return None
+		except KeyError:
+			if DIRKEY in self.tree_pointer:
+				self.eval_command = False
+				self.eval_quantifier = True
+				self.tree_pointer = self.tree_pointer[DIRKEY]
+				self.dir_tree_pointer = self.direction_keys._tree
+			elif ANYKEY in self.tree_pointer:
+				self.moo.append(key)
+				self.tree_pointer = self.tree_pointer[ANYKEY]
+				self._try_to_finish()
+			else:
+				self.failure = True
+				return None
+		else:
+			self._try_to_finish()
+
 	def _try_to_finish(self):
 		if not isinstance(self.tree_pointer, dict):
 			self.command = self.tree_pointer
@@ -387,4 +395,28 @@ class Test(TestCase):
 		self.assertEqual(Ellipsis, press('ア'))
 		self.assertEqual(Ellipsis, press('9'))
 
+	def test_multiple_directions(self):
+		km = Keymap()
+		directions = Keymap()
+		kb = KeyBuffer(km, directions)
+		directions.add('j', dir=Direction(down=1))
+		directions.add('k', dir=Direction(down=-1))
+
+		def add_dirs(arg):
+			n = 0
+			for dir in arg.directions:
+				n += dir.down
+			return n
+
+		km.add(add_dirs, 'x}y}')
+		km.add(add_dirs, 'four}}}}')
+
+		press = self._mkpress(kb, km)
+
+		self.assertEqual(2, press('xjyj'))
+		self.assertEqual(0, press('fourjkkj'))
+		self.assertEqual(2, press('four2j4k2j2j'))
+		self.assertEqual(10, press('four1j2j3j4j'))
+		self.assertEqual(10, press('four1j2j3j4jafslkdfjkldj'))
+
 if __name__ == '__main__': main()