From 87b6d03182f3ad2a0f4688a077b26e467719b906 Mon Sep 17 00:00:00 2001 From: Matt Bierner Date: Tue, 4 Feb 2020 18:02:38 -0800 Subject: [PATCH] Add cursor position check to jsDocCompletionTest --- .../src/test/jsDocCompletions.test.ts | 35 +++++++++-------- .../src/test/testUtils.ts | 39 +++++++++++-------- 2 files changed, 41 insertions(+), 33 deletions(-) diff --git a/extensions/typescript-language-features/src/test/jsDocCompletions.test.ts b/extensions/typescript-language-features/src/test/jsDocCompletions.test.ts index f9c6c39da88..63d3f144f9a 100644 --- a/extensions/typescript-language-features/src/test/jsDocCompletions.test.ts +++ b/extensions/typescript-language-features/src/test/jsDocCompletions.test.ts @@ -3,12 +3,11 @@ * Licensed under the MIT License. See License.txt in the project root for license information. *--------------------------------------------------------------------------------------------*/ -import * as assert from 'assert'; import 'mocha'; import * as vscode from 'vscode'; import { disposeAll } from '../utils/dispose'; import { acceptFirstSuggestion } from './suggestTestHelpers'; -import { Config, createTestEditor, joinLines, updateConfig, VsCodeConfiguration, wait } from './testUtils'; +import { assertEditorContents, Config, createTestEditor, CURSOR, enumerateConfig, insertModesValues, joinLines, updateConfig, VsCodeConfiguration, wait } from './testUtils'; const testDocumentUri = vscode.Uri.parse('untitled:test.ts'); @@ -38,21 +37,25 @@ suite('JSDoc Completions', () => { }); test('Should complete jsdoc inside single line comment', async () => { - await createTestEditor(testDocumentUri, - `/**$0 */`, - `function abcdef(x, y) { }`, - ); + await enumerateConfig(testDocumentUri, Config.insertMode, insertModesValues, async config => { - const document = await acceptFirstSuggestion(testDocumentUri, _disposables); - assert.strictEqual( - document.getText(), - joinLines( - `/**`, - ` * `, - ` * @param x `, - ` * @param y `, - ` */`, + const editor = await createTestEditor(testDocumentUri, + `/**$0 */`, `function abcdef(x, y) { }`, - )); + ); + + await acceptFirstSuggestion(testDocumentUri, _disposables); + + assertEditorContents(editor, + joinLines( + `/**`, + ` * `, + ` * @param x ${CURSOR}`, + ` * @param y `, + ` */`, + `function abcdef(x, y) { }`, + ), + `Config: ${config}`); + }); }); }); diff --git a/extensions/typescript-language-features/src/test/testUtils.ts b/extensions/typescript-language-features/src/test/testUtils.ts index e0b4ac867e2..7b2c95dcf1c 100644 --- a/extensions/typescript-language-features/src/test/testUtils.ts +++ b/extensions/typescript-language-features/src/test/testUtils.ts @@ -74,14 +74,9 @@ export const joinLines = (...args: string[]) => args.join('\n'); export async function createTestEditor(uri: vscode.Uri, ...lines: string[]) { const document = await vscode.workspace.openTextDocument(uri); - await vscode.window.showTextDocument(document); - const activeEditor = vscode.window.activeTextEditor; - if (!activeEditor) { - throw new Error('no active editor'); - } - - await activeEditor.insertSnippet(new vscode.SnippetString(joinLines(...lines)), new vscode.Range(0, 0, 1000, 0)); - return activeEditor; + const editor = await vscode.window.showTextDocument(document); + await editor.insertSnippet(new vscode.SnippetString(joinLines(...lines)), new vscode.Range(0, 0, 1000, 0)); + return editor; } export function assertEditorContents(editor: vscode.TextEditor, expectedDocContent: string, message?: string): void { @@ -94,15 +89,11 @@ export function assertEditorContents(editor: vscode.TextEditor, expectedDocConte if (cursorIndex >= 0) { const expectedCursorPos = editor.document.positionAt(cursorIndex); - assert.strictEqual( - editor.selection.active.line, - expectedCursorPos.line + assert.deepEqual( + { line: editor.selection.active.line, character: editor.selection.active.line }, + { line: expectedCursorPos.line, character: expectedCursorPos.line }, + 'Cursor position' ); - assert.strictEqual( - editor.selection.active.character, - expectedCursorPos.character - ); - } } @@ -120,7 +111,6 @@ export async function updateConfig(documentUri: vscode.Uri, newConfig: VsCodeCon return oldConfig; } - export const Config = Object.freeze({ autoClosingBrackets: 'editor.autoClosingBrackets', completeFunctionCalls: 'typescript.suggest.completeFunctionCalls', @@ -128,3 +118,18 @@ export const Config = Object.freeze({ snippetSuggestions: 'editor.snippetSuggestions', suggestSelection: 'editor.suggestSelection', } as const); + +export const insertModesValues = Object.freeze(['insert', 'replace']); + +export async function enumerateConfig( + documentUri: vscode.Uri, + configKey: string, + values: readonly string[], + f: (message: string) => Promise +): Promise { + for (const value of values) { + const newConfig = { [configKey]: value }; + await updateConfig(documentUri, newConfig); + await f(JSON.stringify(newConfig)); + } +}