summary refs log tree commit diff stats
path: root/test
diff options
context:
space:
mode:
authorhut <hut@lavabit.com>2010-02-10 00:20:41 +0100
committerhut <hut@lavabit.com>2010-03-09 14:40:18 +0100
commit8d56a585f6c0cbda42b8b3c2278309b67e46ad7d (patch)
tree1c9865bea3020538476d28b9549158c7f4bd5354 /test
parentd8cd2b7573a43d94ac28a83d3ff4ffca14cde11a (diff)
downloadranger-8d56a585f6c0cbda42b8b3c2278309b67e46ad7d.tar.gz
keyparser: some improvements
Diffstat (limited to 'test')
-rw-r--r--test/tc_newkeys.py103
1 files changed, 52 insertions, 51 deletions
diff --git a/test/tc_newkeys.py b/test/tc_newkeys.py
index 4b46b0f8..e32ddbd5 100644
--- a/test/tc_newkeys.py
+++ b/test/tc_newkeys.py
@@ -1,5 +1,6 @@
 if __name__ == '__main__': from __init__ import init; init()
 from unittest import TestCase, main
+from pprint import pprint as print
 
 from inspect import isfunction, getargspec
 import inspect
@@ -7,6 +8,8 @@ from sys import intern
 
 FUNC = 'func'
 DIRECTION = 'direction'
+DIRKEY = 9999
+ANYKEY = 'any'
 QUANTIFIER = 'n'
 MATCH = intern('!!')
 
@@ -75,26 +78,24 @@ class KeyBuffer(object):
 		if self.level == 1:
 			try:
 				self.tree_pointer = self.tree_pointer[key]
+			except TypeError:
+				self.failure = True
+				return None
 			except KeyError:
-				try:
-					match = self.tree_pointer[MATCH]
-				except KeyError:
-					self.failure = True
-					return None
-				# self.command = match
 				if is_ascii_digit(key):
 					if self.quant2 is None:
 						self.quant2 = 0
 					self.quant2 = self.quant2 * 10 + key - 48
-				else:
+				elif DIRKEY in self.tree_pointer:
 					self.level = 2
+					self.command = self.tree_pointer[DIRKEY]
 					self.tree_pointer = self.direction_keys._tree
-			else:
-				try:
-					match = self.tree_pointer[MATCH]
-				except KeyError:
-					pass
 				else:
+					self.failure = True
+					return None
+			else:
+				if not isinstance(self.tree_pointer, dict):
+					match = self.tree_pointer
 					self.command = match
 					if not match.has_direction:
 						if self.quant2 is not None:
@@ -108,11 +109,8 @@ class KeyBuffer(object):
 			except KeyError:
 				self.failure = True
 			else:
-				try:
-					match = self.tree_pointer[MATCH]
-				except KeyError:
-					pass
-				else:
+				if not isinstance(self.tree_pointer, dict):
+					match = self.tree_pointer
 					self.direction = match.actions['dir'] * self.quant2
 					self.done = True
 
@@ -165,12 +163,20 @@ class Keymap(object):
 	def _split(self, key):
 		assert isinstance(key, (tuple, int, str))
 		if isinstance(key, tuple):
-			return key
-		if isinstance(key, str):
-			return (ord(k) for k in key)
-		if isinstance(key, int):
-			return (key, )
-		raise TypeError(key)
+			for char in key:
+					yield char
+		elif isinstance(key, str):
+			for char in key:
+				if char == '.':
+					yield ANYKEY
+				elif char == '}':
+					yield DIRKEY
+				else:
+					yield ord(char)
+		elif isinstance(key, int):
+			yield key
+		else:
+			raise TypeError(key)
 
 	def add_binding(self, *keys, **actions):
 		assert keys
@@ -179,8 +185,9 @@ class Keymap(object):
 		for key in keys:
 			assert key
 			chars = tuple(self._split(key))
-			tree = self.traverse_tree(chars)
-			tree[MATCH] = bind
+			tree = self.traverse_tree(chars[:-1])
+			if isinstance(tree, dict):
+				tree[chars[-1]] = bind
 
 	def traverse_tree(self, generator):
 		tree = self._tree
@@ -204,7 +211,7 @@ class Keymap(object):
 			except KeyError:
 				raise KeyError(str(char) + " not in tree " + str(tree))
 		try:
-			return tree[MATCH]
+			return tree
 		except KeyError:
 			raise KeyError(str(char) + " not in tree " + str(tree))
 
@@ -282,40 +289,34 @@ class Test(TestCase):
 		kb = KeyBuffer(km, directions)
 		directions.add('j', dir=Direction(down=1))
 		directions.add('k', dir=Direction(down=-1))
-		km.add(nd, 'd')
+		km.add(nd, 'd}')
 		km.add('dd', func=nd, with_direction=False)
 
-		match = kb.simulate_press('3d5j')
-		self.assertEqual(15, match.function(n=kb.quant1, direction=kb.direction))
-		kb.clear()
-
-		match = kb.simulate_press('3d5k')
-		self.assertEqual(-15, match.function(n=kb.quant1, direction=kb.direction))
-		kb.clear()
-
-		match = kb.simulate_press('3d5d')
-		self.assertEqual(15, match.function(n=kb.quant1, direction=kb.direction))
-		kb.clear()
 
-		match = kb.simulate_press('3dd')
-		self.assertEqual(3, match.function(n=kb.quant1, direction=kb.direction))
-		kb.clear()
+		def press(keys):
+			kb.clear()
+			match = kb.simulate_press(keys)
+			self.assertFalse(kb.failure, "parsing keys '"+keys+"' did fail!")
+			self.assertTrue(kb.done, "parsing keys '"+keys+ \
+					"' did not complete!")
+			dic = {QUANTIFIER:kb.quant1, DIRECTION:kb.direction}
+			return match.function(**dic)
 
-		match = kb.simulate_press('dd')
-		self.assertEqual(1, match.function(n=kb.quant1, direction=kb.direction))
-		kb.clear()
+		self.assertEqual(  3, press('3ddj'))
+		self.assertEqual( 15, press('3d5j'))
+		self.assertEqual(-15, press('3d5k'))
+		self.assertEqual( 15, press('3d5d'))
+		self.assertEqual(  3, press('3dd'))
+		self.assertEqual(  1, press('dd'))
 
-		km.add(nd, 'x')
+		km.add(nd, 'x}')
 		km.add('xxxx', func=nd, with_direction=False)
 
-		match = kb.simulate_press('xxxxj')
-		self.assertEqual(1, match.function(n=kb.quant1, direction=kb.direction))
-		kb.clear()
+		self.assertEqual(1, press('xxxxj'))
+		self.assertEqual(1, press('xxxxjsomeinvalitchars'))
 
-		match = kb.simulate_press('xxxxjsomeinvalidchars')
-		self.assertEqual(1, match.function(n=kb.quant1, direction=kb.direction))
+		# these combinations should break:
 		kb.clear()
-
 		self.assertEqual(None, kb.simulate_press('xxxj'))
 		kb.clear()
 		self.assertEqual(None, kb.simulate_press('xxj'))
'#n386'>386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514