-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
test.js
253 lines (219 loc) · 8.28 KB
/
test.js
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
var test = require('tape')
var neatInput = require('.')
var util = require('./util')
var EventEmitter = require('events').EventEmitter
var ee = new EventEmitter()
ee.setRawMode = function () {}
ee.resume = function () {}
test('moving cursor left/right, ctrl-b/ctrl-f', function (t) {
var input = neatInput({ stdin: ee })
input.set('ONE')
var cursor = input.cursor
ee.emit('keypress', undefined, { name: 'left' })
t.is(input.cursor, cursor - 1, 'cursor moved left')
ee.emit('keypress', undefined, { name: 'right' })
t.is(input.cursor, cursor, 'cursor moved right')
ee.emit('keypress', undefined, { ctrl: true, name: 'b' })
t.is(input.cursor, cursor - 1, 'cursor moved left')
ee.emit('keypress', undefined, { ctrl: true, name: 'f' })
t.is(input.cursor, cursor, 'cursor moved right')
t.end()
})
test('delete char, delete key', deleteCharTest({
name: 'delete'
}))
test('delete char, ctrl-d', deleteCharTest({
ctrl: true,
name: 'd'
}))
function deleteCharTest (key) {
return function (t) {
var input = neatInput({ stdin: ee })
input.set('')
var cursor = input.cursor
ee.emit('keypress', undefined, key)
t.is(input.rawLine(), '', 'no change')
t.is(input.cursor, cursor, 'cursor still at end of line')
input.set('ONE')
cursor = input.cursor
ee.emit('keypress', undefined, key)
t.is(input.rawLine(), 'ONE', 'no change')
t.is(input.cursor, cursor, 'cursor still at end of line')
input.set('ONE')
cursor = input.cursor
ee.emit('keypress', undefined, { name: 'left' })
ee.emit('keypress', undefined, key)
t.is(input.rawLine(), 'ON', 'E is gone')
t.is(input.cursor, cursor - 1, 'cursor moved left')
input.set('ONE')
cursor = input.cursor
ee.emit('keypress', undefined, { name: 'left' })
ee.emit('keypress', undefined, { name: 'left' })
ee.emit('keypress', undefined, { name: 'left' })
ee.emit('keypress', undefined, key)
t.is(input.rawLine(), 'NE', 'O is gone')
t.is(input.cursor, cursor - 3, 'cursor moved left thrice')
input.set('ONE')
cursor = input.cursor
ee.emit('keypress', undefined, { name: 'left' })
ee.emit('keypress', undefined, { name: 'left' })
ee.emit('keypress', undefined, { name: 'left' })
ee.emit('keypress', undefined, key)
ee.emit('keypress', undefined, key)
ee.emit('keypress', undefined, key)
t.is(input.rawLine(), '', 'ONE is gone')
t.is(input.cursor, cursor - 3, 'cursor moved left thrice')
t.end()
}
}
test('backspace', function (t) {
var input = neatInput({ stdin: ee })
input.set('ONE')
var cursor = input.cursor
ee.emit('keypress', undefined, { name: 'backspace' })
t.is(input.rawLine(), 'ON', 'E is gone')
t.is(input.cursor, cursor - 1, 'cursor moved left')
input.set('ONE')
cursor = input.cursor
ee.emit('keypress', undefined, { name: 'left' })
ee.emit('keypress', undefined, { name: 'backspace' })
t.is(input.rawLine(), 'OE', 'N is gone')
t.is(input.cursor, cursor - 2, 'cursor moved left twice')
input.set('ONE')
ee.emit('keypress', undefined, { name: 'left' })
ee.emit('keypress', undefined, { name: 'left' })
ee.emit('keypress', undefined, { name: 'left' })
ee.emit('keypress', undefined, { name: 'backspace' })
t.is(input.rawLine(), 'ONE', 'no change')
t.is(input.cursor, 0, 'cursor at the start')
t.end()
})
test('moving cursor to beginning/end of line, ctrl-a/ctrl-e', function (t) {
var input = neatInput({ stdin: ee })
input.set('ONE')
var cursor = input.cursor
ee.emit('keypress', undefined, { ctrl: true, name: 'a' })
t.is(input.cursor, 0, 'cursor at the start of line')
ee.emit('keypress', undefined, { ctrl: true, name: 'e' })
t.is(input.cursor, cursor, 'cursor back at end of line')
t.end()
})
test('erasing whole line, ctrl-u', function (t) {
var input = neatInput({ stdin: ee })
input.set('ONE TWO THREE')
ee.emit('keypress', undefined, { ctrl: true, name: 'u' })
t.is(input.cursor, 0, 'cursor at the start of line')
t.is(input.rawLine(), '', 'empty line')
t.end()
})
test('util: finding start of word backwards', function (t) {
var str = 'foo bar baz'
t.is(util.findWordBeginBackward(str, 0), 0)
t.is(util.findWordBeginBackward(str, 1), 0)
t.is(util.findWordBeginBackward(str, 2), 0)
t.is(util.findWordBeginBackward(str, 3), 0)
t.is(util.findWordBeginBackward(str, 4), 0)
t.is(util.findWordBeginBackward(str, 5), 4)
t.is(util.findWordBeginBackward(str, 6), 4)
t.is(util.findWordBeginBackward(str, 7), 4)
t.is(util.findWordBeginBackward(str, 8), 4)
t.is(util.findWordBeginBackward(str, 9), 4)
t.is(util.findWordBeginBackward(str, 10), 4)
t.is(util.findWordBeginBackward(str, 11), 10)
t.is(util.findWordBeginBackward(str, 12), 10)
t.is(util.findWordBeginBackward(str, 13), 10)
t.end()
})
test('util: finding end of word forwards', function (t) {
var str = 'foo bar baz'
t.is(util.findWordEndForward(str, 0), 3)
t.is(util.findWordEndForward(str, 1), 3)
t.is(util.findWordEndForward(str, 2), 3)
t.is(util.findWordEndForward(str, 3), 7)
t.is(util.findWordEndForward(str, 4), 7)
t.is(util.findWordEndForward(str, 5), 7)
t.is(util.findWordEndForward(str, 6), 7)
t.is(util.findWordEndForward(str, 7), 13)
t.is(util.findWordEndForward(str, 8), 13)
t.is(util.findWordEndForward(str, 9), 13)
t.is(util.findWordEndForward(str, 10), 13)
t.is(util.findWordEndForward(str, 11), 13)
t.is(util.findWordEndForward(str, 12), 13)
t.is(util.findWordEndForward(str, 13), 13)
t.end()
})
test('moving cursor on word boundaries', function (t) {
var input = neatInput({ stdin: ee })
input.set('ONE TWO THREE')
ee.emit('keypress', undefined, { meta: true, name: 'b' })
t.is(input.cursor, 8, 'cursor at start of THREE')
ee.emit('keypress', undefined, { meta: true, name: 'b' })
t.is(input.cursor, 4, 'cursor at start of TWO')
ee.emit('keypress', undefined, { meta: true, name: 'b' })
t.is(input.cursor, 0, 'cursor at start of ONE')
ee.emit('keypress', undefined, { meta: true, name: 'f' })
t.is(input.cursor, 3, 'cursor at end of ONE')
ee.emit('keypress', undefined, { meta: true, name: 'f' })
t.is(input.cursor, 7, 'cursor at end of TWO')
ee.emit('keypress', undefined, { meta: true, name: 'f' })
t.is(input.cursor, 13, 'cursor at end of THREE')
t.end()
})
test('erasing words backwards, alt-backspace', deleteBackwardsTest({
meta: true,
name: 'backspace'
}))
test('erasing words backwards, ctrl-w', deleteBackwardsTest({
ctrl: true,
name: 'w'
}))
function deleteBackwardsTest (key) {
return function (t) {
var input = neatInput({ stdin: ee })
input.set('ONE TWO THREE')
ee.emit('keypress', undefined, key)
t.is(input.rawLine(), 'ONE TWO ', 'THREE gone')
t.is(input.cursor, 8, 'cursor at end of TWO ')
ee.emit('keypress', undefined, key)
t.is(input.rawLine(), 'ONE ', 'TWO gone')
t.is(input.cursor, 4, 'cursor at start of ONE ')
ee.emit('keypress', undefined, key)
t.is(input.rawLine(), '', 'ONE gone')
t.is(input.cursor, 0, 'cursor at zero')
input.set('ONE TWO THREE')
input.cursor = 9
ee.emit('keypress', undefined, key)
t.is(input.rawLine(), 'ONE THREE', 'TWO gone')
t.is(input.cursor, 4, 'cursor at beginning of THREE')
t.end()
}
}
test('erasing words forwards, alt-d', function (t) {
var input = neatInput({ stdin: ee })
input.set('ONE TWO THREE')
ee.emit('keypress', undefined, { ctrl: true, name: 'a' })
ee.emit('keypress', undefined, { meta: true, name: 'd' })
t.is(input.rawLine(), ' TWO THREE', 'ONE gone')
t.is(input.cursor, 0, 'cursor at zero')
ee.emit('keypress', undefined, { meta: true, name: 'd' })
t.is(input.rawLine(), ' THREE', 'TWO gone')
t.is(input.cursor, 0, 'cursor at zero')
ee.emit('keypress', undefined, { meta: true, name: 'd' })
t.is(input.rawLine(), '', 'THREE gone')
t.is(input.cursor, 0, 'cursor at zero')
input.set('ONE TWO THREE')
input.cursor = 3
ee.emit('keypress', undefined, { meta: true, name: 'd' })
t.is(input.rawLine(), 'ONE THREE', 'TWO gone')
t.is(input.cursor, 3, 'cursor unchanged')
t.end()
})
test('erasing all to the right of cursor, ctrl-k', function (t) {
var input = neatInput({ stdin: ee })
input.set('ONE TWO THREE FOUR')
input.cursor = 7
ee.emit('keypress', undefined, { ctrl: true, name: 'k' })
t.is(input.rawLine(), 'ONE TWO', 'THREE and FOUR gone')
t.is(input.cursor, 7, 'cursor unchanged')
t.end()
})