summary refs log tree commit diff stats
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
parent3f388ce51c55b242e46824808c858aa28ece5ce2 (diff)
downloadranger-9588a0fb1fd45951eb8e640ad6c8bccaf7707586.tar.gz
keyparser: implemented aliases
-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")
>337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 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 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706