From 3702ea04bb32b33fa125e3d9bee950186d82acdc Mon Sep 17 00:00:00 2001 From: Alison Winters Date: Tue, 28 Jan 2020 22:15:26 +0800 Subject: [PATCH] add support for setting `language` on the testcase - setting language will add sleeps to allow for language server latency - also add some basic tests for `gd` go to definition --- test/Framework/BlackBox.ts | 7 +++++- test/Framework/Util.ts | 22 ++++++++++++------- test/ModeNormal/g d.test.ts | 43 +++++++++++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 test/ModeNormal/g d.test.ts diff --git a/test/Framework/BlackBox.ts b/test/Framework/BlackBox.ts index 1476ee29..ee61abe2 100644 --- a/test/Framework/BlackBox.ts +++ b/test/Framework/BlackBox.ts @@ -4,6 +4,7 @@ import { TextEditor, TextDocument, Selection } from 'vscode'; import { getCurrentMode } from '../../src/extension'; export interface TestCase { + language?: string; from: string; inputs: string; to: string; @@ -110,7 +111,7 @@ export const run = (testCase: TestCase, before?: (textEditor: TextEditor) => voi const toInfo = extractInfo(testCase.to); const inputs = testCase.inputs.split(' '); - TestUtil.createTempDocument(fromInfo.cleanText, reusableDocument).then( + TestUtil.createTempDocument(fromInfo.cleanText, reusableDocument, testCase.language).then( async (textEditor) => { reusableDocument = textEditor.document; @@ -127,6 +128,10 @@ export const run = (testCase: TestCase, before?: (textEditor: TextEditor) => voi await waitForMillisecond(20 * tries); } + if (testCase.language) { + await waitForMillisecond(50 * tries); + } + try { assert.equal(TestUtil.getDocument()!.getText(), toInfo.cleanText); assert.deepEqual(TestUtil.getSelections(), toInfo.selections); diff --git a/test/Framework/Util.ts b/test/Framework/Util.ts index 3f6f4a5b..0c0d2673 100644 --- a/test/Framework/Util.ts +++ b/test/Framework/Util.ts @@ -1,7 +1,6 @@ import { workspace, window, - Uri, TextDocument, TextEditor, Position, @@ -13,22 +12,29 @@ import { export function createTempDocument( content?: string, reusableDocument?: TextDocument, + language: string = 'plaintext', ): Thenable { let getTextEditor: Thenable; if ( - reusableDocument && + reusableDocument?.languageId === language && window.activeTextEditor && window.activeTextEditor.document === reusableDocument ) { getTextEditor = Promise.resolve(window.activeTextEditor); } else { - const uri = reusableDocument - ? reusableDocument.uri - : Uri.parse(`untitled:${__dirname}.${Math.random()}.tmp`); - getTextEditor = workspace - .openTextDocument(uri) - .then((document) => window.showTextDocument(document)); + // for non-plaintext files, sleep for a while to let the language server load + const getDocument = + reusableDocument?.languageId === language + ? workspace.openTextDocument(reusableDocument.uri) + : workspace.openTextDocument({ language }).then((document) => + language === 'plaintext' + ? document + : new Promise((resolve) => { + setTimeout(() => resolve(document), 1500); + }), + ); + getTextEditor = getDocument.then((document) => window.showTextDocument(document)); } return getTextEditor.then((textEditor) => { diff --git a/test/ModeNormal/g d.test.ts b/test/ModeNormal/g d.test.ts new file mode 100644 index 00000000..23f5937f --- /dev/null +++ b/test/ModeNormal/g d.test.ts @@ -0,0 +1,43 @@ +import * as BlackBox from '../Framework/BlackBox'; + +suite('Normal: g d', () => { + const testCases: BlackBox.TestCase[] = [ + { + language: 'javascript', + from: 'class C {\n x = 0;\n}\nconst c = new C();\n[]c.x = 1;', + inputs: 'g d', + to: 'class C {\n x = 0;\n}\nconst []c = new C();\nc.x = 1;', + }, + { + language: 'javascript', + from: 'class C {\n x = 0;\n}\nconst c = new C();\nc.[]x = 1;', + inputs: 'g d', + to: 'class C {\n []x = 0;\n}\nconst c = new C();\nc.x = 1;', + }, + ]; + + for (let i = 0; i < testCases.length; i++) { + BlackBox.run(testCases[i]); + } +}); + +suite('Normal: g D', () => { + const testCases: BlackBox.TestCase[] = [ + { + language: 'javascript', + from: 'class C {\n x = 0;\n}\nconst c = new C();\n[]c.x = 1;', + inputs: 'g D', + to: 'class []C {\n x = 0;\n}\nconst c = new C();\nc.x = 1;', + }, + { + language: 'javascript', + from: 'class C {\n x = 0;\n}\nconst c = new C();\nc.[]x = 1;', + inputs: 'g D', + to: 'class C {\n x = 0;\n}\nconst c = new C();\nc.[]x = 1;', + }, + ]; + + for (let i = 0; i < testCases.length; i++) { + BlackBox.run(testCases[i]); + } +});