-
Notifications
You must be signed in to change notification settings - Fork 3
/
playback_snippet.test.js
182 lines (174 loc) · 7.72 KB
/
playback_snippet.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
'use strict';
const assert = require('assert');
const vscode = require('vscode');
const util = require('../../src/util.js');
const { TestUtil } = require('./test_util.js');
const { CommandsToTest } = require('./commands_to_test.js');
const { keyboardMacro, awaitController } = require('../../src/extension.js');
describe('Recording and Playback: Real Snippet Insertion', () => {
let textEditor;
const Cmd = CommandsToTest;
const Type = text => ({ command: '$type', args: { text } });
const Move = (ch, ln, sel) => {
const motion = { characterDelta: ch };
if (ln) motion.lineDelta = ln;
if (sel) motion.selectionLength = sel;
return {
command: '$moveCursor',
args: motion
};
};
const GroupMotion = (size, ch, ln, sel) => {
const m = Move(ch, ln, sel);
m.args.groupSize = size;
return m;
};
const setSelections = async function(array) {
await awaitController.waitFor('selection', 1).catch(() => {});
const newSelections = TestUtil.arrayToSelections(array);
if (!util.isEqualSelections(textEditor.selections, newSelections)) {
const timeout = 1000;
textEditor.selections = newSelections;
await awaitController.waitFor('selection', timeout).catch(
() => { console.log('Warning: timeout in setSelections!'); }
);
}
};
const getSelections = function() {
return TestUtil.selectionsToArray(textEditor.selections);
};
const record = async function(sequence) {
keyboardMacro.startRecording();
for (let i = 0; i < sequence.length; i++) {
const { command, args } = sequence[i];
if (command === '$wrap') {
await keyboardMacro.wrapSync(args);
} else {
await vscode.commands.executeCommand(command, args);
}
}
keyboardMacro.finishRecording();
};
before(async () => {
vscode.window.showInformationMessage('Started test for Recording and Playback of real Snippet insertion.');
textEditor = await TestUtil.setupTextEditor({ content: '', language: 'javascript' });
});
describe('JavaScript snippets', () => {
beforeEach(async () => {
await TestUtil.resetDocument(textEditor, (
'\n' +
'\n' +
'\n' +
'\n' +
'\n' +
''
));
});
it('forof', async () => {
const seq = [
{ command: "$wrap", args: {
command: "editor.action.insertSnippet",
args: {
snippet:
"for (const ${1:element} of ${2:array}) {\n" +
"\t$0\n" +
"}"
},
record: "side-effect"
} },
{ command: "default:type", args: { text: "elem" } },
{ command: "$wrap", args: Cmd.NextSnippetPlaceholder },
{ command: "default:type", args: { text: "obj" } },
{ command: "$wrap", args: Cmd.NextSnippetPlaceholder },
{ command: "$wrap", args: Cmd.CursorDown },
{ command: "$wrap", args: Cmd.Enter }
];
await setSelections([[0, 0]]);
await record(seq);
assert.strictEqual(textEditor.document.lineAt(0).text, 'for (const elem of obj) {');
assert.strictEqual(textEditor.document.lineAt(2).text, '}');
assert.deepStrictEqual(getSelections(), [[3, 0]]);
assert.deepStrictEqual(keyboardMacro.getCurrentSequence(), [
Type('for (const element of array) {\n' +
' \n' +
'}'),
Move(-19, -2, 7),
Type('elem'),
Move(4, 0, 5),
Type('obj'),
Move(4, 1),
Cmd.CursorDown,
Cmd.Enter
]);
await keyboardMacro.playback();
assert.strictEqual(textEditor.document.lineAt(3).text, 'for (const elem of obj) {');
assert.strictEqual(textEditor.document.lineAt(5).text, '}');
assert.deepStrictEqual(getSelections(), [[6, 0]]);
});
it('forin', async () => {
const seq = [
{ command: "$wrap", args: {
command: "editor.action.insertSnippet",
args: {
snippet:
"for (const ${1:key} in ${2:object}) {\n" +
"\tif (Object.hasOwnProperty.call(${2}, ${1})) {\n" +
"\t\tconst ${3:element} = ${2}[${1}];\n" +
"\t\t$0\n" +
"\t}\n" +
"}"
},
record: "side-effect"
} },
{ command: "$wrap", args: Cmd.NextSnippetPlaceholder },
{ command: "default:type", args: { text: "obj" } },
{ command: "$wrap", args: Cmd.PrevSnippetPlaceholder },
{ command: "default:type", args: { text: "prop" } },
{ command: "$wrap", args: Cmd.NextSnippetPlaceholder },
{ command: "$wrap", args: Cmd.NextSnippetPlaceholder },
{ command: "default:type", args: { text: "val" } },
{ command: "$wrap", args: Cmd.NextSnippetPlaceholder },
{ command: "$wrap", args: Cmd.CursorDown },
{ command: "$wrap", args: Cmd.CursorDown },
{ command: "$wrap", args: Cmd.Enter }
];
await setSelections([[0, 0]]);
await record(seq);
assert.strictEqual(textEditor.document.lineAt(0).text, 'for (const prop in obj) {');
assert.strictEqual(textEditor.document.lineAt(1).text, ' if (Object.hasOwnProperty.call(obj, prop)) {');
assert.strictEqual(textEditor.document.lineAt(2).text, ' const val = obj[prop];');
assert.strictEqual(textEditor.document.lineAt(4).text, ' }');
assert.strictEqual(textEditor.document.lineAt(5).text, '}');
assert.deepStrictEqual(getSelections(), [[6, 0]]);
assert.deepStrictEqual(keyboardMacro.getCurrentSequence(), [
Type(
'for (const key in object) {\n' +
' if (Object.hasOwnProperty.call(object, key)) {\n' +
' const element = object[key];\n' +
' \n' +
' }\n' +
'}'
),
Move([-16, -7, -5], [-5, -4, -3], 3),
GroupMotion(3, [7, 35, 24], [0, 1, 2], 6),
Type('obj'),
GroupMotion(3, [-10, 40, 28], [0, 1, 2], 3),
Type('prop'),
GroupMotion(3, [4, 35, 24], [0, 1, 2], 3),
GroupMotion(3, 14, 2, 7),
Type('val'),
Move(8, 1),
Cmd.CursorDown,
Cmd.CursorDown,
Cmd.Enter
]);
await keyboardMacro.playback();
assert.strictEqual(textEditor.document.lineAt(6).text, 'for (const prop in obj) {');
assert.strictEqual(textEditor.document.lineAt(7).text, ' if (Object.hasOwnProperty.call(obj, prop)) {');
assert.strictEqual(textEditor.document.lineAt(8).text, ' const val = obj[prop];');
assert.strictEqual(textEditor.document.lineAt(10).text, ' }');
assert.strictEqual(textEditor.document.lineAt(11).text, '}');
assert.deepStrictEqual(getSelections(), [[12, 0]]);
});
});
});