summary refs log tree commit diff stats
diff options
context:
space:
mode:
-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")
ref='/akkartik/mu/commit/apps/mulisp.subx?h=main&id=d7dc6cf12f19db15c0f32f626ce135775ed9599c'>d7dc6cf1 ^
1fafe7af ^
2c3dda21 ^









1fafe7af ^


7a583220 ^
1fafe7af ^



27decf77 ^




fd2b373a ^
57fa808b ^
7a583220 ^
1fafe7af ^


fd2b373a ^
0fba3393 ^
d7dc6cf1 ^




2c3dda21 ^











31bbb6ac ^
2c3dda21 ^
1fafe7af ^


d7dc6cf1 ^


fd2b373a ^
7a583220 ^
fd2b373a ^




2c3dda21 ^
fd2b373a ^




2c3dda21 ^
fd2b373a ^































7a583220 ^
fd2b373a ^


2c3dda21 ^
fd2b373a ^

7a583220 ^
d7dc6cf1 ^


1fafe7af ^
fd2b373a ^



57fa808b ^
7a583220 ^
1fafe7af ^





7a583220 ^
1fafe7af ^



57fa808b ^
7a583220 ^
1fafe7af ^








7a583220 ^
1fafe7af ^








1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234