Skip to content

Commit

Permalink
add support for setting language on the testcase
Browse files Browse the repository at this point in the history
- setting language will add sleeps to allow for language server latency
- also add some basic tests for `gd` go to definition
  • Loading branch information
alisonatwork committed May 20, 2020
1 parent 4ca5b6d commit 3702ea0
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
7 changes: 6 additions & 1 deletion test/Framework/BlackBox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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);
Expand Down
22 changes: 14 additions & 8 deletions test/Framework/Util.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import {
workspace,
window,
Uri,
TextDocument,
TextEditor,
Position,
Expand All @@ -13,22 +12,29 @@ import {
export function createTempDocument(
content?: string,
reusableDocument?: TextDocument,
language: string = 'plaintext',
): Thenable<TextEditor> {
let getTextEditor: Thenable<TextEditor>;

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<TextDocument>((resolve) => {
setTimeout(() => resolve(document), 1500);
}),
);
getTextEditor = getDocument.then((document) => window.showTextDocument(document));
}

return getTextEditor.then((textEditor) => {
Expand Down
43 changes: 43 additions & 0 deletions test/ModeNormal/g d.test.ts
Original file line number Diff line number Diff line change
@@ -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]);
}
});

0 comments on commit 3702ea0

Please sign in to comment.