-
Notifications
You must be signed in to change notification settings - Fork 3
/
internal_commands.test.js
427 lines (395 loc) · 21.2 KB
/
internal_commands.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
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
327
328
329
330
331
332
333
334
335
336
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
'use strict';
const assert = require('assert');
const vscode = require('vscode');
const { TestUtil } = require('./test_util.js');
const internalCommands = require('../../src/internal_commands.js');
describe('internalCommands', () => {
let textEditor;
const setSelections = function(array) {
textEditor.selections = TestUtil.arrayToSelections(array);
};
const getSelections = function() {
return TestUtil.selectionsToArray(textEditor.selections);
};
before(async () => {
vscode.window.showInformationMessage('Started test for Internal Commands.');
textEditor = await TestUtil.setupTextEditor({ content: '' });
});
describe('performType (basic)', () => {
beforeEach(async () => {
await TestUtil.resetDocument(textEditor, (
'\n'.repeat(10) +
'abcd\n'.repeat(10) +
' efgh\n'.repeat(10)
));
});
it('should insert a text (1)', async () => {
setSelections([[3, 0]]);
await internalCommands.performType({ text: 'C' });
assert.strictEqual(textEditor.document.lineAt(3).text, 'C');
assert.deepStrictEqual(getSelections(), [[3, 1]]);
});
it('should insert a text (2)', async () => {
setSelections([[12, 3]]);
await internalCommands.performType({ text: 'XYZ' });
assert.strictEqual(textEditor.document.lineAt(12).text, 'abcXYZd');
assert.deepStrictEqual(getSelections(), [[12, 6]]);
});
it('should insert a text (3)', async () => {
setSelections([[12, 3]]);
await internalCommands.performType({ text: '123\n456' });
assert.strictEqual(textEditor.document.lineAt(12).text, 'abc123');
assert.strictEqual(textEditor.document.lineAt(13).text, '456d');
assert.strictEqual(textEditor.document.lineAt(20).text, 'abcd');
assert.deepStrictEqual(getSelections(), [[13, 3]]);
});
it('should not trigger bracket completion', async () => {
setSelections([[3, 0]]);
await internalCommands.performType({ text: '(' });
assert.strictEqual(textEditor.document.lineAt(3).text, '(');
assert.deepStrictEqual(getSelections(), [[3, 1]]);
});
it('should replace selected range with a text (1)', async () => {
setSelections([[12, 1, 12, 3]]);
await internalCommands.performType({ text: 'X' });
assert.strictEqual(textEditor.document.lineAt(12).text, 'aXd');
assert.deepStrictEqual(getSelections(), [[12, 2]]);
});
it('should replace selected range with a text (2)', async () => {
setSelections([[2, 0, 3, 0]]);
await internalCommands.performType({ text: 'X' });
assert.strictEqual(textEditor.document.lineAt(2).text, 'X');
assert.strictEqual(textEditor.document.lineAt(9).text, 'abcd');
assert.deepStrictEqual(getSelections(), [[2, 1]]);
});
it('should replace selected range with a text (3)', async () => {
setSelections([[10, 2, 12, 3]]);
await internalCommands.performType({ text: 'X' });
assert.strictEqual(textEditor.document.lineAt(10).text, 'abXd');
assert.strictEqual(textEditor.document.lineAt(18).text, ' efgh');
assert.deepStrictEqual(getSelections(), [[10, 3]]);
});
it('should insert a text into each location of multi-cursor (1)', async () => {
setSelections([[5, 0], [15, 1], [25, 2]]);
await internalCommands.performType({ text: 'X' });
assert.strictEqual(textEditor.document.lineAt(5).text, 'X');
assert.strictEqual(textEditor.document.lineAt(15).text, 'aXbcd');
assert.strictEqual(textEditor.document.lineAt(25).text, ' X efgh');
assert.deepStrictEqual(getSelections(), [[5, 1], [15, 2], [25, 3]]);
});
it('should insert a text into each location of multi-cursor (2)', async () => {
setSelections([[15, 1], [25, 2], [5, 0]]);
await internalCommands.performType({ text: 'X' });
assert.strictEqual(textEditor.document.lineAt(5).text, 'X');
assert.strictEqual(textEditor.document.lineAt(15).text, 'aXbcd');
assert.strictEqual(textEditor.document.lineAt(25).text, ' X efgh');
assert.deepStrictEqual(getSelections(), [[15, 2], [25, 3], [5, 1]]);
});
it('should insert a text into each location of multi-cursor (3)', async () => {
setSelections([[0, 0], [10, 4], [20, 8]]);
await internalCommands.performType({ text: 'hello\nbye' });
assert.strictEqual(textEditor.document.lineAt(0).text, 'hello');
assert.strictEqual(textEditor.document.lineAt(1).text, 'bye');
assert.strictEqual(textEditor.document.lineAt(11).text, 'abcdhello');
assert.strictEqual(textEditor.document.lineAt(12).text, 'bye');
assert.strictEqual(textEditor.document.lineAt(22).text, ' efghhello');
assert.strictEqual(textEditor.document.lineAt(23).text, 'bye');
assert.deepStrictEqual(getSelections(), [[1, 3], [12, 3], [23, 3]]);
});
it('should insert a text into each location of multi-cursor (4)', async () => {
setSelections([[25, 0], [25, 4], [25, 8]]);
await internalCommands.performType({ text: 'X' });
assert.strictEqual(textEditor.document.lineAt(25).text, 'X XefghX');
assert.deepStrictEqual(getSelections(), [[25, 1], [25, 6], [25, 11]]);
});
it('should replace every selected range with a text (1)', async () => {
setSelections([[0, 0, 2, 0], [10, 4, 12, 0], [20, 8, 22, 4]]);
await internalCommands.performType({ text: '99\n00' });
assert.strictEqual(textEditor.document.lineAt(0).text, '99');
assert.strictEqual(textEditor.document.lineAt(1).text, '00');
assert.strictEqual(textEditor.document.lineAt(9).text, 'abcd99');
assert.strictEqual(textEditor.document.lineAt(10).text, '00abcd');
assert.strictEqual(textEditor.document.lineAt(18).text, ' efgh99');
assert.strictEqual(textEditor.document.lineAt(19).text, '00efgh');
assert.deepStrictEqual(getSelections(), [[1, 2], [10, 2], [19, 2]]);
});
it('should replace every selected range with a text (2)', async () => {
setSelections([[25, 0, 25, 2], [25, 3, 25, 5], [25, 6, 25, 8]]);
await internalCommands.performType({ text: 'X' });
assert.strictEqual(textEditor.document.lineAt(25).text, 'X XfX');
assert.deepStrictEqual(getSelections(), [[25, 1], [25, 3], [25, 5]]);
});
it('should replace every selected range with a text (3)', async () => {
setSelections([[25, 2, 26, 0], [26, 4, 27, 2], [27, 6, 28, 4]]);
await internalCommands.performType({ text: 'X' });
assert.strictEqual(textEditor.document.lineAt(25).text, ' X X efXefgh');
assert.deepStrictEqual(getSelections(), [[25, 3], [25, 8], [25, 13]]);
});
it('should replace every selected range with a text (4)', async () => {
setSelections([[25, 0, 25, 1], [25, 3, 25, 4], [25, 6, 25, 7]]);
await internalCommands.performType({ text: 'X\nYZ' });
assert.strictEqual(textEditor.document.lineAt(25).text, 'X');
assert.strictEqual(textEditor.document.lineAt(26).text, 'YZ X');
assert.strictEqual(textEditor.document.lineAt(27).text, 'YZefX');
assert.strictEqual(textEditor.document.lineAt(28).text, 'YZh');
assert.deepStrictEqual(getSelections(), [[26, 2], [27, 2], [28, 2]]);
});
});
describe('performType (with deleteLeft)', () => {
beforeEach(async () => {
await TestUtil.resetDocument(textEditor, (
'\n'.repeat(10) +
'abcd\n'.repeat(10) +
' efgh\n'.repeat(10)
));
});
it('should delete left-hand side characters and insert a text (1)', async () => {
setSelections([[10, 4]]);
await internalCommands.performType({ deleteLeft: 2, text: 'CDEFG' });
assert.strictEqual(textEditor.document.lineAt(10).text, 'abCDEFG');
assert.deepStrictEqual(getSelections(), [[10, 7]]);
});
it('should delete left-hand side characters and insert a text (2)', async () => {
setSelections([[10, 4]]);
await internalCommands.performType({ deleteLeft: 2, text: 'CD' });
assert.strictEqual(textEditor.document.lineAt(10).text, 'abCD');
assert.deepStrictEqual(getSelections(), [[10, 4]]);
});
it('should delete left-hand side characters and insert a text with multi-cursor (1)', async () => {
setSelections([[10, 4], [11, 4]]);
await internalCommands.performType({ deleteLeft: 2, text: 'CDEFG' });
assert.strictEqual(textEditor.document.lineAt(10).text, 'abCDEFG');
assert.strictEqual(textEditor.document.lineAt(11).text, 'abCDEFG');
assert.deepStrictEqual(getSelections(), [[10, 7], [11, 7]]);
});
it('should delete left-hand side characters and insert a text with multi-cursor (2)', async () => {
setSelections([[20, 2], [20, 4], [20, 6]]);
await internalCommands.performType({ deleteLeft: 1, text: 'XY' });
assert.strictEqual(textEditor.document.lineAt(20).text, ' XY XYeXYgh');
assert.deepStrictEqual(getSelections(), [[20, 3], [20, 6], [20, 9]]);
});
it('should stop deleting at the beginning of the line', async () => {
setSelections([[10, 2]]);
await internalCommands.performType({ deleteLeft: 4, text: 'AB' });
assert.strictEqual(textEditor.document.lineAt(10).text, 'ABcd');
assert.deepStrictEqual(getSelections(), [[10, 2]]);
});
});
describe('performType (with deleteLeft and deleteRight)', () => {
beforeEach(async () => {
await TestUtil.resetDocument(textEditor, (
'\n'.repeat(10) +
'abcd\n'.repeat(10) +
' efgh\n'.repeat(10)
));
});
it('should replace characters around the cursor with a text', async () => {
setSelections([[10, 1]]);
await internalCommands.performType({ deleteLeft: 1, deleteRight: 3, text: 'aXXXbcd' });
assert.strictEqual(textEditor.document.lineAt(10).text, 'aXXXbcd');
assert.deepStrictEqual(getSelections(), [[10, 7]]);
});
it('should stop deleting at the end of the line', async () => {
setSelections([[10, 1]]);
await internalCommands.performType({ deleteLeft: 99, deleteRight: 99, text: 'aXXXbcd' });
assert.strictEqual(textEditor.document.lineAt(10).text, 'aXXXbcd');
assert.deepStrictEqual(getSelections(), [[10, 7]]);
});
});
describe('performCursorMotion (horizontal motion)', () => {
before(async () => {
await TestUtil.resetDocument(textEditor, (
'abcde\n'.repeat(10) +
'fghij klmno\n'.repeat(10)
));
});
it('should move the cursor to left horizontally', async () => {
setSelections([[3, 3]]);
await internalCommands.performCursorMotion({ characterDelta: -2 });
assert.deepStrictEqual(getSelections(), [[3, 1]]);
});
it('should move cursors to left horizontally', async () => {
setSelections([[3, 3], [4, 4]]);
await internalCommands.performCursorMotion({ characterDelta: -2 });
assert.deepStrictEqual(getSelections(), [[3, 1], [4, 2]]);
});
it('should move the cursor to right horizontally', async () => {
setSelections([[3, 2]]);
await internalCommands.performCursorMotion({ characterDelta: 2 });
assert.deepStrictEqual(getSelections(), [[3, 4]]);
});
it('should move cursors to right horizontally', async () => {
setSelections([[3, 2], [4, 3]]);
await internalCommands.performCursorMotion({ characterDelta: 2 });
assert.deepStrictEqual(getSelections(), [[3, 4], [4, 5]]);
});
it('should stop at beginning of a line', async () => {
setSelections([[3, 2]]);
await internalCommands.performCursorMotion({ characterDelta: -4 });
assert.deepStrictEqual(getSelections(), [[3, 0]]);
});
it('should stop at end of a line', async () => {
setSelections([[3, 2]]);
await internalCommands.performCursorMotion({ characterDelta: 4 });
assert.deepStrictEqual(getSelections(), [[3, 5]]);
});
it('should move the cursor to left based on start position of current selection (1)', async () => {
setSelections([[12, 6, 12, 11]]);
await internalCommands.performCursorMotion({ characterDelta: -4 });
assert.deepStrictEqual(getSelections(), [[12, 2]]);
});
it('should move the cursor to left based on start position of current selection (2)', async () => {
setSelections([[12, 11, 12, 6]]);
await internalCommands.performCursorMotion({ characterDelta: -4 });
assert.deepStrictEqual(getSelections(), [[12, 2]]);
});
it('should move the cursor to right based on start position of current selection', async () => {
setSelections([[12, 2, 12, 5]]);
await internalCommands.performCursorMotion({ characterDelta: 4 });
assert.deepStrictEqual(getSelections(), [[12, 6]]);
});
});
describe('performCursorMotion (with lineDelta)', () => {
before(async () => {
await TestUtil.resetDocument(textEditor, (
'abcde\n'.repeat(10) +
'fghij klmno\n'.repeat(10)
));
});
it('should move the cursor up and locate relative to the end of the line', async () => {
setSelections([[11, 3]]);
await internalCommands.performCursorMotion({ lineDelta: -3, characterDelta: -3 });
assert.deepStrictEqual(getSelections(), [[8, 2]]);
});
it('should move each cursor up and locate relative to the end of the line', async () => {
setSelections([[11, 3], [12, 4]]);
await internalCommands.performCursorMotion({ lineDelta: -3, characterDelta: -3 });
assert.deepStrictEqual(getSelections(), [[8, 2], [9, 2]]);
});
it('should move the cursor down and locate relative to the beginning of the line', async () => {
setSelections([[8, 3]]);
await internalCommands.performCursorMotion({ lineDelta: 4, characterDelta: 4 });
assert.deepStrictEqual(getSelections(), [[12, 4]]);
});
it('should move each cursor down and locate relative to the beginning of the line', async () => {
setSelections([[8, 3], [9, 4]]);
await internalCommands.performCursorMotion({ lineDelta: 4, characterDelta: 4 });
assert.deepStrictEqual(getSelections(), [[12, 4], [13, 4]]);
});
it('should stop at beginning of a line (move up)', async () => {
setSelections([[11, 3]]);
await internalCommands.performCursorMotion({ lineDelta: -3, characterDelta: -15 });
assert.deepStrictEqual(getSelections(), [[8, 0]]);
});
it('should stop at beginning of a line (move down)', async () => {
setSelections([[8, 3]]);
await internalCommands.performCursorMotion({ lineDelta: 4, characterDelta: -15 });
assert.deepStrictEqual(getSelections(), [[12, 0]]);
});
it('should stop at end of a line (move up)', async () => {
setSelections([[11, 3]]);
await internalCommands.performCursorMotion({ lineDelta: -3, characterDelta: 15 });
assert.deepStrictEqual(getSelections(), [[8, 5]]);
});
it('should stop at end of a line (move down)', async () => {
setSelections([[8, 3]]);
await internalCommands.performCursorMotion({ lineDelta: 4, characterDelta: 15 });
assert.deepStrictEqual(getSelections(), [[12, 11]]);
});
it('should stop at the first line of the document (move up)', async () => {
setSelections([[5, 3]]);
await internalCommands.performCursorMotion({ lineDelta: -7, characterDelta: -3 });
assert.deepStrictEqual(getSelections(), [[0, 2]]);
});
it('should stop at the last line of the document (move down)', async () => {
setSelections([[15, 3]]);
await internalCommands.performCursorMotion({ lineDelta: 7, characterDelta: 3 });
assert.deepStrictEqual(getSelections(), [[20, 0]]);
});
it('should move the cursor up and locate relative to the end of the line and make a selection', async () => {
setSelections([[11, 3]]);
await internalCommands.performCursorMotion({ lineDelta: -3, characterDelta: -3, selectionLength: 2 });
assert.deepStrictEqual(getSelections(), [[8, 2, 8, 4]]);
});
it('should move the cursor down and locate relative to the beginning of the line and make a selection', async () => {
setSelections([[8, 3]]);
await internalCommands.performCursorMotion({ lineDelta: 4, characterDelta: 4, selectionLength: 2 });
assert.deepStrictEqual(getSelections(), [[12, 4, 12, 6]]);
});
});
describe('performCursorMotion (with selectionLength)', () => {
before(async () => {
await TestUtil.resetDocument(textEditor, (
'abcde\n'.repeat(10) +
'fghij klmno\n'.repeat(10)
));
});
it('should move the cursor horizontally and make a selection', async () => {
setSelections([[3, 5]]);
await internalCommands.performCursorMotion({ characterDelta: -4, selectionLength: 3 });
assert.deepStrictEqual(getSelections(), [[3, 1, 3, 4]]);
});
it('should move cursors horizontally and make selections', async () => {
setSelections([[3, 5], [4, 5]]);
await internalCommands.performCursorMotion({ characterDelta: -4, selectionLength: 3 });
assert.deepStrictEqual(getSelections(), [[3, 1, 3, 4], [4, 1, 4, 4]]);
});
});
describe('performCursorMotion (split into multiple cursors)', () => {
before(async () => {
await TestUtil.resetDocument(textEditor, (
'abcde\n'.repeat(10) +
'fghij klmno\n'.repeat(10)
));
});
it('should split cursor into multiple cursors', async () => {
setSelections([[3, 4]]);
await internalCommands.performCursorMotion({ characterDelta: [ -3, -1 ] });
assert.deepStrictEqual(getSelections(), [[3, 1], [3, 3]]);
});
it('should split cursor into multiple cursors (with lineDelta)', async () => {
setSelections([[3, 4]]);
await internalCommands.performCursorMotion({
characterDelta: [ -3, -1 ],
lineDelta: [ -1, -2 ]
});
assert.deepStrictEqual(getSelections(), [[2, 2], [1, 4]]);
});
it('should split cursor into multiple cursors (with selectionLength)', async () => {
setSelections([[3, 4]]);
await internalCommands.performCursorMotion({
characterDelta: [ -3, -1 ],
selectionLength: 1
});
assert.deepStrictEqual(getSelections(), [[3, 1, 3, 2], [3, 3, 3, 4]]);
});
});
describe('performCursorMotion (motion with grouped cursors)', () => {
before(async () => {
await TestUtil.resetDocument(textEditor, (
'abcde\n'.repeat(10) +
'fghij klmno\n'.repeat(10)
));
});
it('should move cursors each with group of cursors', async () => {
setSelections([[10, 2], [11, 7], [13, 2], [14, 7]]);
await internalCommands.performCursorMotion({ characterDelta: 5, groupSize: 2 });
assert.deepStrictEqual(getSelections(), [[10, 7], [13, 7]]);
});
it('should move cursors each with group of cursors', async () => {
setSelections([[10, 2], [11, 7], [13, 2], [14, 7]]);
await internalCommands.performCursorMotion({ characterDelta: [5, 3], lineDelta: [0, 1], groupSize: 2 });
assert.deepStrictEqual(getSelections(), [[10, 7], [11, 3], [13, 7], [14, 3]]);
});
it('should ignore if groupSize is invalid (1)', async () => {
setSelections([[10, 2]]);
await internalCommands.performCursorMotion({ characterDelta: 5, groupSize: 2 });
assert.deepStrictEqual(getSelections(), [[10, 2]]);
});
it('should ignore if groupSize is invalid (2)', async () => {
setSelections([[10, 2], [11, 2], [12, 2], [13, 2]]);
await internalCommands.performCursorMotion({ characterDelta: 5, groupSize: 3 });
assert.deepStrictEqual(getSelections(), [[10, 2], [11, 2], [12, 2], [13, 2]]);
});
});
});