-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplayback_async_commands.test.js
90 lines (81 loc) · 3.64 KB
/
playback_async_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
'use strict';
const assert = require('assert');
const vscode = require('vscode');
const { TestUtil } = require('./test_util.js');
const { CommandsToTest } = require('./commands_to_test.js');
const { keyboardMacro } = require('../../src/extension.js');
const internalCommands = require('../../src/internal_commands.js');
describe('Recording and Playback: Asynchronous Commands', () => {
let textEditor;
const Cmd = CommandsToTest;
const PerformType = text => ({
command: 'kb-macro.performType',
args: { text },
await: 'selection document'
});
const setSelections = function(array) {
textEditor.selections = TestUtil.arrayToSelections(array);
};
const getSelections = function() {
return TestUtil.selectionsToArray(textEditor.selections);
};
const record = async function(sequence) {
keyboardMacro.startRecording();
for (let i = 0; i < sequence.length; i++) {
await keyboardMacro.wrapSync(sequence[i]);
}
keyboardMacro.finishRecording();
};
before(async () => {
vscode.window.showInformationMessage('Started test for Recording and Playback of Asynchronous Commands.');
textEditor = await TestUtil.setupTextEditor({ content: '' });
});
// Here we register the internal command performType to vscode as an external command.
// We use it in order to test synchronous execution of asynchronous command using 'await' option.
vscode.commands.registerCommand('kb-macro.performType', internalCommands.performType);
describe('performType', () => {
beforeEach(async () => {
await TestUtil.resetDocument(textEditor, (
'abcde\n' +
' fghij\n' +
' klmno pqrstu vwxyz'
));
});
it('should insert a text', async () => {
const seq = [ PerformType('12345') ];
setSelections([[0, 5]]);
await record(seq);
assert.deepStrictEqual(keyboardMacro.getCurrentSequence(), seq);
assert.strictEqual(textEditor.document.lineAt(0).text, 'abcde12345');
assert.deepStrictEqual(getSelections(), [[0, 10]]);
setSelections([[1, 4]]);
await keyboardMacro.playback();
assert.strictEqual(textEditor.document.lineAt(1).text, ' 12345fghij');
assert.deepStrictEqual(getSelections(), [[1, 9]]);
});
it('should insert texts with cursor movement', async () => {
const seq = [
PerformType('111'),
Cmd.CursorRight,
PerformType('222'),
Cmd.CursorDown,
PerformType('333'),
Cmd.CursorEnd,
PerformType('444')
];
setSelections([[0, 5]]);
await record(seq);
assert.deepStrictEqual(keyboardMacro.getCurrentSequence(), seq);
assert.strictEqual(textEditor.document.lineAt(0).text, 'abcde111');
assert.strictEqual(textEditor.document.lineAt(1).text, '222 fghij');
assert.strictEqual(textEditor.document.lineAt(2).text, ' 333 klmno pqrstu vwxyz444');
assert.deepStrictEqual(getSelections(), [[2, 28]]);
setSelections([[0, 0]]);
await keyboardMacro.playback();
assert.strictEqual(textEditor.document.lineAt(0).text, '111a222bcde111');
assert.strictEqual(textEditor.document.lineAt(1).text, '222 333fghij444');
assert.strictEqual(textEditor.document.lineAt(2).text, ' 333 klmno pqrstu vwxyz444');
assert.deepStrictEqual(getSelections(), [[1, 18]]);
});
});
});