about summary refs log tree commit diff stats
path: root/112read-byte.subx
blob: 387cbb668ea704fcc5b1debd54177725a0e3d647 (plain) (blame)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
# read-byte-buffered: one higher-level abstraction atop 'read'.
#
# There are many situations where 'read' is a lot to manage, and we need
# to abstract some details away. One of them is when we want to read a file
# character by character. In this situation we follow C's FILE data structure,
# which manages the underlying file descriptor together with the buffer it
# reads into. We call our version 'buffered-file'. Should be useful with other
# primitives as well, in later layers.

== data

# The buffered file for standard input. Also illustrates the layout for
# buffered-file: a pointer to the backing store, followed by a 'buffer' stream
Stdin:  # buffered-file
    # file descriptor or (addr stream byte)
    0/imm32  # standard input
$Stdin->buffer:
    # inlined fields for a stream
    #   current write index
    0/imm32
    #   current read index
    0/imm32
    #   size
    8/imm32
    #   data
    00 00 00 00 00 00 00 00  # 8 bytes

# TODO: 8 bytes is too small. We'll need to grow the buffer for efficiency. But
# I don't want to type in 1024 bytes here.

== code
#   instruction                     effective address                                                   register    displacement    immediate
# . op          subop               mod             rm32          base        index         scale       r32
# . 1-3 bytes   3 bits              2 bits          3 bits        3 bits      3 bits        2 bits      2 bits      0/1/2/4 bytes   0/1/2/4 bytes

# return next byte value in eax, with top 3 bytes cleared.
# On reaching end of file, return 0xffffffff (Eof).
read-byte-buffered:  # f: (addr buffered-file) -> byte-or-Eof/eax: byte
    # . prologue
    55/push-ebp
    89/copy                         3/mod/direct    5/rm32/ebp    .           .             .           4/r32/esp   .               .                 # copy esp to ebp
    # . save registers
    51/push-ecx
    56/push-esi
    # esi = f
    8b/copy                         1/mod/*+disp8   5/rm32/ebp    .           .             .           6/r32/esi   8/disp8         .                 # copy *(ebp+8) to esi
    # ecx = f->read
    8b/copy                         1/mod/*+disp8   6/rm32/esi    .           .             .           1/r32/ecx   8/disp8         .                 # copy *(esi+8) to ecx
    # if (f->read >= f->write) populate stream from file
    3b/compare                      1/mod/*+disp8   6/rm32/esi    .           .             .           1/r32/ecx   4/disp8         .                 # compare ecx with *(esi+4)
    7c/jump-if-<  $read-byte-buffered:from-stream/disp8
    # . clear-stream(stream = f+4)
    # . . push args
    8d/copy-address                 1/mod/*+disp8   6/rm32/esi    .           .             .           0/r32/eax   4/disp8         .                 # copy esi+4 to eax
    50/push-eax
    # . . call
    e8/call  clear-stream/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # . f->read must now be 0; update its cache at ecx
    31/xor                          3/mod/direct    1/rm32/ecx    .           .             .           1/r32/ecx   .               .                 # clear ecx
    # . eax = read(f->fd, stream = f+4)
    # . . push args
    50/push-eax
    ff          6/subop/push        0/mod/indirect  6/rm32/esi    .           .             .           .           .               .                 # push *esi
    # . . call
    e8/call  read/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # if (eax == 0) return 0xffffffff
    3d/compare-eax-and  0/imm32
    75/jump-if-!=  $read-byte-buffered:from-stream/disp8
    b8/copy-to-eax  0xffffffff/imm32/Eof
    eb/jump  $read-byte-buffered:end/disp8
$read-byte-buffered:from-stream:
    # byte-or-Eof = f->data[f->read]
    31/xor                          3/mod/direct    0/rm32/eax    .           .             .           0/r32/eax   .               .                 # clear eax
    8a/copy-byte                    1/mod/*+disp8   4/rm32/sib    6/base/esi  1/index/ecx   .           0/r32/AL    0x10/disp8      .                 # copy byte at *(esi+ecx+16) to AL
    # ++f->read
    ff          0/subop/increment   1/mod/*+disp8   6/rm32/esi    .           .             .           .           8/disp8         .                 # increment *(esi+8)
$read-byte-buffered:end:
    # . restore registers
    5e/pop-to-esi
    59/pop-to-ecx
    # . epilogue
    89/copy                         3/mod/direct    4/rm32/esp    .           .             .           5/r32/ebp   .               .                 # copy ebp to esp
    5d/pop-to-ebp
    c3/return

# - tests

test-read-byte-buffered-single:
    # - check that read-byte-buffered returns first byte of 'file'
    # setup
    # . clear-stream(_test-stream)
    # . . push args
    68/push  _test-stream/imm32
    # . . call
    e8/call  clear-stream/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # . clear-stream(_test-buffered-file->buffer)
    # . . push args
    68/push  $_test-buffered-file->buffer/imm32
    # . . call
    e8/call  clear-stream/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # . write(_test-stream, "Ab")
    # . . push args
    68/push  "Ab"/imm32
    68/push  _test-stream/imm32
    # . . call
    e8/call  write/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # read-byte-buffered(_test-buffered-file)
    # . . push args
    68/push  _test-buffered-file/imm32
    # . . call
    e8/call  read-byte-buffered/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(eax, 'A', msg)
    # . . push args
    68/push  "F - test-read-byte-buffered-single"/imm32
    68/push  0x41/imm32
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # . end
    c3/return

test-read-byte-buffered-multiple:
    # - call read-byte-buffered twice, check that second call returns second byte
    # setup
    # . clear-stream(_test-stream)
    # . . push args
    68/push  _test-stream/imm32
    # . . call
    e8/call  clear-stream/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # . clear-stream($_test-buffered-file->buffer)
    # . . push args
    68/push  $_test-buffered-file->buffer/imm32
    # . . call
    e8/call  clear-stream/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # . write(_test-stream, "Ab")
    # . . push args
    68/push  "Ab"/imm32
    68/push  _test-stream/imm32
    # . . call
    e8/call  write/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # read-byte-buffered(_test-buffered-file)
    # . . push args
    68/push  _test-buffered-file/imm32
    # . . call
    e8/call  read-byte-buffered/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # read-byte-buffered(_test-buffered-file)
    # . . push args
    68/push  _test-buffered-file/imm32
    # . . call
    e8/call  read-byte-buffered/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(eax, 'b', msg)
    # . . push args
    68/push  "F - test-read-byte-buffered-multiple"/imm32
    68/push  0x62/imm32
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # . end
    c3/return

test-read-byte-buffered-end-of-file:
    # - call read-byte-buffered on an empty 'file', check that it returns Eof
    # setup
    # . clear-stream(_test-stream)
    # . . push args
    68/push  _test-stream/imm32
    # . . call
    e8/call  clear-stream/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # . clear-stream($_test-buffered-file->buffer)
    # . . push args
    68/push  $_test-buffered-file->buffer/imm32
    # . . call
    e8/call  clear-stream/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # read-byte-buffered(_test-buffered-file)
    # . . push args
    68/push  _test-buffered-file/imm32
    # . . call
    e8/call  read-byte-buffered/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(eax, 0xffffffff, msg)
    # . . push args
    68/push  "F - test-read-byte-buffered-end-of-file"/imm32
    68/push  0xffffffff/imm32/Eof
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # . end
    c3/return

test-read-byte-buffered-refills-buffer:
    # - consume buffered-file's buffer, check that next read-byte-buffered still works
    # setup
    # . clear-stream(_test-stream)
    # . . push args
    68/push  _test-stream/imm32
    # . . call
    e8/call  clear-stream/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # . clear-stream($_test-buffered-file->buffer)
    # . . push args
    68/push  $_test-buffered-file->buffer/imm32
    # . . call
    e8/call  clear-stream/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # . write(_test-stream, "Abcdefgh")
    # . . push args
    68/push  "Abcdefgh"/imm32
    68/push  _test-stream/imm32
    # . . call
    e8/call  write/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               8/imm32           # add to esp
    # pretend buffer is full
    # . _test-buffered-file->read = 6  # >= _test-buffered-file->size
    b8/copy-to-eax  _test-buffered-file/imm32
    c7          0/subop/copy        1/mod/*+disp8   0/rm32/eax    .           .             .           .           8/disp8         6/imm32           # copy to *(eax+8)
    # read-byte-buffered(_test-buffered-file)
    # . . push args
    68/push  _test-buffered-file/imm32
    # . . call
    e8/call  read-byte-buffered/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               4/imm32           # add to esp
    # check-ints-equal(eax, 'A', msg)
    # . . push args
    68/push  "F - test-read-byte-buffered-refills-buffer"/imm32
    68/push  0x41/imm32
    50/push-eax
    # . . call
    e8/call  check-ints-equal/disp32
    # . . discard args
    81          0/subop/add         3/mod/direct    4/rm32/esp    .           .             .           .           .               0xc/imm32         # add to esp
    # . end
    c3/return

== data

# a test buffered file for _test-stream
_test-buffered-file:  # buffered-file
    # file descriptor or (addr stream byte)
    _test-stream/imm32
$_test-buffered-file->buffer:
    # current write index
    0/imm32
    # current read index
    0/imm32
    # size
    6/imm32
    # data
    00 00 00 00 00 00  # 6 bytes

_test-input-stream:  # (stream byte)
    # current write index
    0/imm32
    # current read index
    0/imm32
    # size
    0x100/imm32  # 256 bytes
    # data (16 lines x 16 bytes/line)
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
    00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

# a test buffered file for _test-input-stream
_test-input-buffered-file:  # buffered-file
    # file descriptor or (addr stream byte)
    _test-input-stream/imm32
$_test-input-buffered-file->buffer:
    # current write index
    0/imm32
    # current read index
    0/imm32
    # size
    6/imm32
    # data
    00 00 00 00 00 00  # 6 bytes

# . . vim:nowrap:textwidth=0
="n">function, "No function found! " + \ str(match.__dict__)) return match.function(arg) return press def assertPressFails(self, kb, keys): kb.clear() simulate_press(kb, keys) self.assertTrue(kb.failure, "Keypress did not fail as expected") kb.clear() def assertPressIncomplete(self, kb, keys): kb.clear() simulate_press(kb, keys) self.assertFalse(kb.failure, "Keypress failed, expected incomplete") self.assertFalse(kb.done, "Keypress done which was unexpected") kb.clear() class Test(PressTestCase): """The test cases""" def test_passive_action(self): km = KeyMap() directions = KeyMap() kb = KeyBuffer(km, directions) def n(value): """return n or value""" def fnc(arg=None): if arg is None or arg.n is None: return value return arg.n return fnc km.map('ppp', n(5)) km.map('pp<bg>', n(8)) km.map('pp<dir>', n(2)) directions.map('j', dir=Direction(down=1)) press = self._mkpress(kb, km) self.assertEqual(5, press('ppp')) self.assertEqual(3, press('3ppp')) self.assertEqual(2, press('ppj')) kb.clear() match = simulate_press(kb, 'pp') args = CommandArgs(0, 0, kb) self.assert_(match) self.assert_(match.function) self.assertEqual(8, match.function(args)) def test_translate_keys(self): def test(string, *args): if not args: args = (string, ) self.assertEqual(ordtuple(*args), tuple(parse_keybinding(string))) def ordtuple(*args): lst = [] for arg in args: if isinstance(arg, str): lst.extend(ord(c) for c in arg) else: lst.append(arg) return tuple(lst) # 1 argument means: assume nothing is translated. test('k') test('kj') test('k<dir>', 'k', DIRKEY) test('k<ANY>z<any>', 'k', ANYKEY, 'z', ANYKEY) test('k<anY>z<dir>', 'k', ANYKEY, 'z', DIRKEY) test('<cr>', "\n") test('<tab><tab><cr>', "\t\t\n") test('<') test('>') test('<C-a>', 1) test('<C-b>', 2) for i in range(1, 26): test('<C-' + chr(i+ord('a')-1) + '>', i) test('<A-x>', 27, ord('x')) test('<a-o>', 27, ord('o')) test('k<a') test('k<anz>') test('k<a<nz>') test('k<a<nz>') test('k<a<>nz>') test('>nz>') def test_alias(self): def add_dirs(arg): return sum(dir.down() for dir in arg.directions) def return5(_): return 5 directions = KeyMap() directions.map('j', dir=Direction(down=1)) directions.map('k', dir=Direction(down=-1)) directions.map('<CR>', alias='j') directions.map('@', alias='<CR>') base = KeyMap() base.map('a<dir>', add_dirs) base.map('b<dir>', add_dirs) base.map('x<dir>x<dir>', add_dirs) base.map('f', return5) base.map('yy', alias='y') base.map('!', alias='!') other = KeyMap() other.map('b<dir>b<dir>', alias='x<dir>x<dir>') other.map('c<dir>', add_dirs) other.map('g', alias='f') km = base.merge(other, copy=True) 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')) self.assertEqual(press('c<CR>'), press('c@')) self.assertEqual(press('c<CR>'), press('c@')) self.assertEqual(press('c<CR>'), press('c@')) for n in range(1, 10): 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") self.assertEqual("Yes", t.traverse('abcd')) self.assertRaises(KeyError, t.traverse, 'abcde') self.assertRaises(KeyError, t.traverse, 'xyz') self.assert_(isinstance(t.traverse('abc'), Tree)) t2 = Tree() self.assertRaises(KeyError, t2.set, 'axy', "Lol", force=False) t2.set('axx', 'ololol') t2.set('axyy', "Lol") self.assertEqual("Yes", t.traverse('abcd')) self.assertRaises(KeyError, t2.traverse, 'abcd') self.assertEqual("Lol", t2.traverse('axyy')) self.assertEqual("ololol", t2.traverse('axx')) t2.unset('axyy') self.assertEqual("ololol", t2.traverse('axx')) self.assertRaises(KeyError, t2.traverse, 'axyy') self.assertRaises(KeyError, t2.traverse, 'axy') t2.unset('a') self.assertRaises(KeyError, t2.traverse, 'abcd') self.assertRaises(KeyError, t2.traverse, 'a') self.assert_(t2.empty()) def test_merge_trees(self): def makeTreeA(): t = Tree() t.set('aaaX', 1) t.set('aaaY', 2) t.set('aaaZ', 3) t.set('bbbA', 11) t.set('bbbB', 12) t.set('bbbC', 13) t.set('bbbD', 14) t.set('bP', 21) t.set('bQ', 22) return t def makeTreeB(): u = Tree() u.set('aaaX', 0) u.set('bbbC', 'Yes') u.set('bbbD', None) u.set('bbbE', 15) u.set('bbbF', 16) u.set('bQ', 22) u.set('bR', 23) u.set('ffff', 1337) return u # test 1 t = Tree('a') u = Tree('b') merged = t.merge(u, copy=True) self.assertEqual('b', merged._tree) # test 2 t = Tree('a') u = makeTreeA() merged = t.merge(u, copy=True) self.assertEqual(u._tree, merged._tree) # test 3 t = makeTreeA() u = makeTreeB() v = t.merge(u, copy=True) self.assertEqual(0, v['aaaX']) self.assertEqual(2, v['aaaY']) self.assertEqual(3, v['aaaZ']) self.assertEqual(11, v['bbbA']) self.assertEqual('Yes', v['bbbC']) self.assertEqual(None, v['bbbD']) self.assertEqual(15, v['bbbE']) self.assertEqual(16, v['bbbF']) self.assertRaises(KeyError, t.__getitem__, 'bbbG') self.assertEqual(21, v['bP']) self.assertEqual(22, v['bQ']) self.assertEqual(23, v['bR']) self.assertEqual(1337, v['ffff']) # merge shouldn't be destructive self.assertEqual(makeTreeA()._tree, t._tree) self.assertEqual(makeTreeB()._tree, u._tree) v['fff'].replace('Lolz') self.assertEqual('Lolz', v['fff']) v['aaa'].replace('Very bad') v.plow('qqqqqqq').replace('eww.') self.assertEqual(makeTreeA()._tree, t._tree) self.assertEqual(makeTreeB()._tree, u._tree) def test_add(self): c = KeyMap() c.map('aa', 'b', lambda *_: 'lolz') self.assert_(c['aa'].function(), 'lolz') @c.map('a', 'c') def test(): return 5 self.assert_(c['b'].function(), 'lolz') self.assert_(c['c'].function(), 5) self.assert_(c['a'].function(), 5) def test_quantifier(self): km = KeyMap() directions = KeyMap() kb = KeyBuffer(km, directions) def n(value): """return n or value""" def fnc(arg=None): if arg is None or arg.n is None: return value return arg.n return fnc km.map('p', n(5)) press = self._mkpress(kb, km) self.assertEqual(5, press('p')) self.assertEqual(3, press('3p')) self.assertEqual(6223, press('6223p')) def test_direction(self): km = KeyMap() directions = KeyMap() kb = KeyBuffer(km, directions) directions.map('j', dir=Direction(down=1)) directions.map('k', dir=Direction(down=-1)) def nd(arg): """ n * direction """ n = arg.n is None and 1 or arg.n dir = arg.direction is None and Direction(down=1) \ or arg.direction return n * dir.down() km.map('d<dir>', nd) km.map('dd', func=nd) press = self._mkpress(kb, km) self.assertPressIncomplete(kb, 'd') self.assertEqual( 1, press('dj')) self.assertEqual( 3, press('3ddj')) self.assertEqual( 15, press('3d5j')) self.assertEqual(-15, press('3d5k')) # supporting this kind of key combination would be too confusing: # self.assertEqual( 15, press('3d5d')) self.assertEqual( 3, press('3dd')) self.assertEqual( 33, press('33dd')) self.assertEqual( 1, press('dd')) km.map('x<dir>', nd) km.map('xxxx', func=nd) self.assertEqual(1, press('xxxxj')) self.assertEqual(1, press('xxxxjsomeinvalitchars')) # these combinations should break: self.assertPressFails(kb, 'xxxj') self.assertPressFails(kb, 'xxj') self.assertPressFails(kb, 'xxkldfjalksdjklsfsldkj') self.assertPressFails(kb, 'xyj') self.assertPressIncomplete(kb, 'x') # direction missing def test_any_key(self): km = KeyMap() directions = KeyMap() kb = KeyBuffer(km, directions) directions.map('j', dir=Direction(down=1)) directions.map('k', dir=Direction(down=-1)) directions.map('g<any>', dir=Direction(down=-1)) def cat(arg): n = arg.n is None and 1 or arg.n return ''.join(chr(c) for c in arg.matches) * n km.map('return<any>', cat) km.map('cat4<any><any><any><any>', cat) km.map('foo<dir><any>', cat) press = self._mkpress(kb, km) self.assertEqual('x', press('returnx')) self.assertEqual('abcd', press('cat4abcd')) self.assertEqual('abcdabcd', press('2cat4abcd')) self.assertEqual('55555', press('5return5')) self.assertEqual('x', press('foojx')) self.assertPressFails(kb, 'fooggx') # ANYKEY forbidden in DIRECTION km.map('<any>', lambda _: Ellipsis) self.assertEqual('x', press('returnx')) self.assertEqual('abcd', press('cat4abcd')) self.assertEqual(Ellipsis, press('2cat4abcd')) self.assertEqual(Ellipsis, press('5return5')) self.assertEqual(Ellipsis, press('g')) self.assertEqual(Ellipsis, press('ß')) self.assertEqual(Ellipsis, press('ア')) self.assertEqual(Ellipsis, press('9')) def test_multiple_directions(self): km = KeyMap() directions = KeyMap() kb = KeyBuffer(km, directions) directions.map('j', dir=Direction(down=1)) directions.map('k', dir=Direction(down=-1)) def add_dirs(arg): return sum(dir.down() for dir in arg.directions) km.map('x<dir>y<dir>', add_dirs) km.map('four<dir><dir><dir><dir>', add_dirs) 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')) def test_corruptions(self): km = KeyMap() directions = KeyMap() kb = KeyBuffer(km, directions) press = self._mkpress(kb, km) directions.map('j', dir=Direction(down=1)) directions.map('k', dir=Direction(down=-1)) km.map('xxx', lambda _: 1) self.assertEqual(1, press('xxx')) # corrupt the tree tup = tuple(parse_keybinding('xxx')) x = ord('x') km._tree[x][x][x] = "Boo" self.assertPressFails(kb, 'xxy') self.assertPressFails(kb, 'xzy') self.assertPressIncomplete(kb, 'xx') self.assertPressIncomplete(kb, 'x') if not sys.flags.optimize: self.assertRaises(AssertionError, simulate_press, kb, 'xxx') kb.clear() def test_directions_as_functions(self): km = KeyMap() directions = KeyMap() kb = KeyBuffer(km, directions) press = self._mkpress(kb, km) def move(arg): return arg.direction.down() directions.map('j', dir=Direction(down=1)) directions.map('s', alias='j') directions.map('k', dir=Direction(down=-1)) km.map('<dir>', func=move) self.assertEqual(1, press('j')) self.assertEqual(1, press('j')) self.assertEqual(1, press('j')) self.assertEqual(1, press('j')) self.assertEqual(1, press('j')) self.assertEqual(1, press('s')) self.assertEqual(1, press('s')) self.assertEqual(1, press('s')) self.assertEqual(1, press('s')) self.assertEqual(1, press('s')) self.assertEqual(-1, press('k')) self.assertEqual(-1, press('k')) self.assertEqual(-1, press('k')) km.map('k', func=lambda _: 'love') self.assertEqual(1, press('j')) self.assertEqual('love', press('k')) self.assertEqual(1, press('40j')) self.assertEqual(40, kb.quant) km.map('<dir><dir><any><any>', func=move) self.assertEqual(1, press('40jkhl')) self.assertEqual(40, kb.quant) def test_tree_deep_copy(self): t = Tree() s = t.plow('abcd') s.replace('X') u = t.copy() self.assertEqual(t._tree, u._tree) s = t.traverse('abc') s.replace('Y') self.assertNotEqual(t._tree, u._tree) def test_keymanager(self): def func(arg): return 5 def getdown(arg): return arg.direction.down() buffer = KeyBuffer(None, None) press = self._mkpress(buffer) keymanager = KeyManager(buffer, ['foo', 'bar']) map = keymanager.get_context('foo') map('a', func) map('b', func) map = keymanager.get_context('bar') map('c', func) map('<dir>', getdown) keymanager.dir('foo', 'j', down=1) keymanager.dir('bar', 'j', down=1) keymanager.use_context('foo') self.assertEqual(5, press('a')) self.assertEqual(5, press('b')) self.assertPressFails(buffer, 'c') keymanager.use_context('bar') self.assertPressFails(buffer, 'a') self.assertPressFails(buffer, 'b') self.assertEqual(5, press('c')) self.assertEqual(1, press('j')) keymanager.use_context('foo') keymanager.use_context('foo') keymanager.use_context('foo') keymanager.use_context('bar') keymanager.use_context('foo') keymanager.use_context('bar') keymanager.use_context('bar') self.assertEqual(1, press('j')) def test_alias_to_direction(self): def func(arg): return arg.direction.down() km = KeyMapWithDirections() kb = KeyBuffer(km, km.directions) press = self._mkpress(kb) km.map('<dir>', func) km.map('d<dir>', func) km.dir('j', down=42) km.dir('k', alias='j') self.assertEqual(42, press('j')) km.dir('o', alias='j') km.dir('ick', alias='j') self.assertEqual(42, press('o')) self.assertEqual(42, press('dj')) self.assertEqual(42, press('dk')) self.assertEqual(42, press('do')) self.assertEqual(42, press('dick')) self.assertPressFails(kb, 'dioo') def test_both_directory_and_any_key(self): def func(arg): return arg.direction.down() def func2(arg): return "yay" km = KeyMap() directions = KeyMap() kb = KeyBuffer(km, directions) press = self._mkpress(kb) km.map('abc<dir>', func) directions.map('j', dir=Direction(down=42)) self.assertEqual(42, press('abcj')) km.unmap('abc<dir>') km.map('abc<any>', func2) self.assertEqual("yay", press('abcd')) km.map('abc<dir>', func) km.map('abc<any>', func2) self.assertEqual("yay", press('abcd')) def test_map_collision(self): def add_dirs(arg): return sum(dir.down() for dir in arg.directions) def return5(_): return 5 directions = KeyMap() directions.map('gg', dir=Direction(down=1)) km = KeyMap() km.map('gh', return5) km.map('agh', return5) km.map('a<dir>', add_dirs) kb = KeyBuffer(km, directions) press = self._mkpress(kb, km) self.assertEqual(5, press('gh')) self.assertEqual(5, press('agh')) # self.assertPressFails(kb, 'agh') # TODO: Make the next line work! For now, skip it. # self.assertEqual(1, press('agg')) def test_keymap_with_dir(self): def func(arg): return arg.direction.down() km = KeyMapWithDirections() kb = KeyBuffer(km, km.directions) press = self._mkpress(kb) km.map('abc<dir>', func) km.dir('j', down=42) self.assertEqual(42, press('abcj')) if __name__ == '__main__': main()