Skip to content

Commit 4e9b801

Browse files
committed
Slight refactor in tests
1 parent 3f1ad54 commit 4e9b801

File tree

2 files changed

+31
-29
lines changed

2 files changed

+31
-29
lines changed

test/mode/modeNormal.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2216,15 +2216,15 @@ suite('Mode Normal', () => {
22162216

22172217
newTest({
22182218
title: 'Can do cit on a multiline tag',
2219-
start: [' <blink>\nhe|llo\ntext</blink>'],
2219+
start: [' <blink>', 'he|llo', 'text</blink>'],
22202220
keysPressed: 'cit',
22212221
end: [' <blink>|</blink>'],
22222222
endMode: Mode.Insert,
22232223
});
22242224

22252225
newTest({
22262226
title: 'Can do cit on a multiline tag with nested tags',
2227-
start: [' <blink>\n<h1>hello</h1>\nh<br>e|llo\nte</h1>xt</blink>'],
2227+
start: [' <blink>', '<h1>hello</h1>', 'h<br>e|llo', 'te</h1>xt</blink>'],
22282228
keysPressed: 'cit',
22292229
end: [' <blink>|</blink>'],
22302230
endMode: Mode.Insert,

test/testSimplifier.ts

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import { globalState } from '../src/state/globalState';
1616
import { StatusBar } from '../src/statusBar';
1717
import { TextEditor } from '../src/textEditor';
1818
import { assertEqualLines, reloadConfiguration, setupWorkspace } from './testUtils';
19+
import { VimState } from '../src/state/vimState';
1920

2021
function newTestGeneric<T extends ITestObject | ITestWithRemapsObject>(
2122
testObj: T,
@@ -193,6 +194,28 @@ function tokenizeKeySequence(sequence: string): string[] {
193194
return result;
194195
}
195196

197+
async function applyDocState(editor: vscode.TextEditor, docState: DocState): Promise<void> {
198+
assert.ok(
199+
await editor.edit((builder) => {
200+
builder.replace(TextEditor.getDocumentRange(editor.document), docState.lines.join('\n'));
201+
}),
202+
'Edit failed',
203+
);
204+
editor.selections = [new vscode.Selection(docState.cursor, docState.cursor)];
205+
}
206+
207+
function assertDocState(vimState: VimState, docState: DocState): void {
208+
assertEqualLines(docState.lines);
209+
210+
const actualPos = vimState.editor.selection.start;
211+
const expectedPos = docState.cursor;
212+
assert.deepEqual(
213+
{ line: actualPos.line, character: actualPos.character },
214+
{ line: expectedPos.line, character: expectedPos.character },
215+
'Cursor position is wrong.',
216+
);
217+
}
218+
196219
async function testIt(testObj: ITestObject): Promise<ModeHandler> {
197220
if (vscode.window.activeTextEditor === undefined) {
198221
await setupWorkspace({
@@ -210,22 +233,18 @@ async function testIt(testObj: ITestObject): Promise<ModeHandler> {
210233
editor.options = testObj.editorOptions;
211234
}
212235

213-
// Initialize the editor with the starting text and cursor selection
214-
assert.ok(
215-
await editor.edit((builder) => {
216-
builder.replace(TextEditor.getDocumentRange(editor.document), start.lines.join('\n'));
217-
}),
218-
'Edit failed',
219-
);
236+
await applyDocState(editor, start);
237+
220238
if (testObj.saveDocBeforeTest) {
221239
assert.ok(await editor.document.save(), 'Save failed');
222240
}
223-
editor.selections = [new vscode.Selection(start.cursor, start.cursor)];
224241

225242
// Generate a brand new ModeHandler for this editor
226243
ModeHandlerMap.clear();
227244
const [modeHandler, _] = await ModeHandlerMap.getOrCreate(editor);
228245

246+
assertDocState(modeHandler.vimState, start);
247+
229248
globalState.lastInvokedMacro = undefined;
230249
globalState.jumpTracker.clearJumps();
231250

@@ -243,17 +262,7 @@ async function testIt(testObj: ITestObject): Promise<ModeHandler> {
243262
await modeHandler.handleMultipleKeyEvents(tokenizeKeySequence(testObj.keysPressed), false);
244263
}
245264

246-
// Check given end output is correct
247-
assertEqualLines(end.lines);
248-
249-
// Check final cursor position
250-
const actualPosition = modeHandler.vimState.editor.selection.start;
251-
const expectedPosition = end.cursor;
252-
assert.deepEqual(
253-
{ line: actualPosition.line, character: actualPosition.character },
254-
{ line: expectedPosition.line, character: expectedPosition.character },
255-
'Cursor position is wrong.',
256-
);
265+
assertDocState(modeHandler.vimState, end);
257266

258267
if (testObj.endMode !== undefined) {
259268
assert.equal(
@@ -315,14 +324,7 @@ async function testItWithRemaps(testObj: ITestWithRemapsObject): Promise<ModeHan
315324
const editor = vscode.window.activeTextEditor;
316325
assert(editor, 'Expected an active editor');
317326

318-
// Initialize the editor with the starting text and cursor selection
319-
await editor.edit((builder) => {
320-
builder.insert(new Position(0, 0), testObj.start.join('\n').replace('|', ''));
321-
});
322-
{
323-
const start = DocState.parse(testObj.start);
324-
editor.selections = [new vscode.Selection(start.cursor, start.cursor)];
325-
}
327+
await applyDocState(editor, DocState.parse(testObj.start));
326328

327329
// Generate a brand new ModeHandler for this editor
328330
ModeHandlerMap.clear();

0 commit comments

Comments
 (0)