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")
'/akkartik/mu/blame/065write-buffered.subx?h=hlt&id=22fb844ba4edf14eb85245a35f42e721658c4f8e'>^
a94b60b5 ^

33352536 ^
6070c23e ^
bfcc0f85 ^
33352536 ^
6070c23e ^
a94b60b5 ^
33352536 ^
a94b60b5 ^

33352536 ^
a94b60b5 ^


33352536 ^
a94b60b5 ^

33352536 ^

a94b60b5 ^


33352536 ^


a94b60b5 ^



33352536 ^

a94b60b5 ^
33352536 ^
a94b60b5 ^
33352536 ^
a94b60b5 ^
33352536 ^
a94b60b5 ^


33352536 ^
a94b60b5 ^

33352536 ^





7a583220 ^
33352536 ^

a94b60b5 ^










33352536 ^
686a52bd ^
a94b60b5 ^
686a52bd ^
a94b60b5 ^


33352536 ^
a94b60b5 ^






33352536 ^
a94b60b5 ^





33352536 ^
ddd2e989 ^
a94b60b5 ^

ddd2e989 ^

a94b60b5 ^
ddd2e989 ^
a94b60b5 ^
33352536 ^
a94b60b5 ^











33352536 ^
686a52bd ^
a94b60b5 ^
686a52bd ^
a94b60b5 ^


33352536 ^
a94b60b5 ^








33352536 ^
a94b60b5 ^






33352536 ^

a94b60b5 ^


33352536 ^
a94b60b5 ^
ddd2e989 ^
a94b60b5 ^



33352536 ^

a94b60b5 ^


33352536 ^
a94b60b5 ^


e968d773 ^


f1eade72 ^
7e7a8a6e ^
e968d773 ^
686a52bd ^
f0b7e327 ^

e968d773 ^
f0b7e327 ^
e968d773 ^
bfcc0f85 ^
e968d773 ^
f0b7e327 ^
e968d773 ^




a94b60b5 ^
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