diff options
Diffstat (limited to 'build/extension/client/src/test')
-rw-r--r-- | build/extension/client/src/test/completion.test.ts | 44 | ||||
-rw-r--r-- | build/extension/client/src/test/diagnostics.test.ts | 42 | ||||
-rw-r--r-- | build/extension/client/src/test/helper.ts | 48 | ||||
-rw-r--r-- | build/extension/client/src/test/index.ts | 15 |
4 files changed, 149 insertions, 0 deletions
diff --git a/build/extension/client/src/test/completion.test.ts b/build/extension/client/src/test/completion.test.ts new file mode 100644 index 00000000..2cb65642 --- /dev/null +++ b/build/extension/client/src/test/completion.test.ts @@ -0,0 +1,44 @@ +/* -------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + * ------------------------------------------------------------------------------------------ */ +'use strict'; + +import * as vscode from 'vscode'; +import * as assert from 'assert'; +import { getDocUri, activate } from './helper'; + +describe('Should do completion', () => { + const docUri = getDocUri('completion.txt'); + + it('Completes JS/TS in txt file', async () => { + await testCompletion(docUri, new vscode.Position(0, 0), { + items: [ + { label: 'JavaScript', kind: vscode.CompletionItemKind.Text }, + { label: 'TypeScript', kind: vscode.CompletionItemKind.Text } + ] + }); + }); +}); + +async function testCompletion( + docUri: vscode.Uri, + position: vscode.Position, + expectedCompletionList: vscode.CompletionList +) { + await activate(docUri); + + // Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion + const actualCompletionList = (await vscode.commands.executeCommand( + 'vscode.executeCompletionItemProvider', + docUri, + position + )) as vscode.CompletionList; + + assert.equal(actualCompletionList.items.length, expectedCompletionList.items.length); + expectedCompletionList.items.forEach((expectedItem, i) => { + const actualItem = actualCompletionList.items[i]; + assert.equal(actualItem.label, expectedItem.label); + assert.equal(actualItem.kind, expectedItem.kind); + }); +} diff --git a/build/extension/client/src/test/diagnostics.test.ts b/build/extension/client/src/test/diagnostics.test.ts new file mode 100644 index 00000000..1eee4990 --- /dev/null +++ b/build/extension/client/src/test/diagnostics.test.ts @@ -0,0 +1,42 @@ +/* -------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + * ------------------------------------------------------------------------------------------ */ +'use strict'; + +import * as vscode from 'vscode' +import * as assert from 'assert' +import { getDocUri, activate } from './helper' + +describe('Should get diagnostics', () => { + const docUri = getDocUri('diagnostics.txt') + + it('Diagnoses uppercase texts', async () => { + await testDiagnostics(docUri, [ + { message: 'ANY is all uppercase.', range: toRange(0, 0, 0, 3), severity: vscode.DiagnosticSeverity.Warning, source: 'ex' }, + { message: 'ANY is all uppercase.', range: toRange(0, 14, 0, 17), severity: vscode.DiagnosticSeverity.Warning, source: 'ex' }, + { message: 'OS is all uppercase.', range: toRange(0, 18, 0, 20), severity: vscode.DiagnosticSeverity.Warning, source: 'ex' } + ]) + }) +}) + +function toRange(sLine: number, sChar: number, eLine: number, eChar: number) { + const start = new vscode.Position(sLine, sChar) + const end = new vscode.Position(eLine, eChar) + return new vscode.Range(start, end) +} + +async function testDiagnostics(docUri: vscode.Uri, expectedDiagnostics: vscode.Diagnostic[]) { + await activate(docUri) + + const actualDiagnostics = vscode.languages.getDiagnostics(docUri); + + assert.equal(actualDiagnostics.length, expectedDiagnostics.length); + + expectedDiagnostics.forEach((expectedDiagnostic, i) => { + const actualDiagnostic = actualDiagnostics[i] + assert.equal(actualDiagnostic.message, expectedDiagnostic.message) + assert.deepEqual(actualDiagnostic.range, expectedDiagnostic.range) + assert.equal(actualDiagnostic.severity, expectedDiagnostic.severity) + }) +}
\ No newline at end of file diff --git a/build/extension/client/src/test/helper.ts b/build/extension/client/src/test/helper.ts new file mode 100644 index 00000000..22121c1c --- /dev/null +++ b/build/extension/client/src/test/helper.ts @@ -0,0 +1,48 @@ +/* -------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + * ------------------------------------------------------------------------------------------ */ +'use strict'; + +import * as vscode from 'vscode'; +import * as path from 'path'; + +export let doc: vscode.TextDocument; +export let editor: vscode.TextEditor; +export let documentEol: string; +export let platformEol: string; + +/** + * Activates the vscode.lsp-sample extension + */ +export async function activate(docUri: vscode.Uri) { + // The extensionId is `publisher.name` from package.json + const ext = vscode.extensions.getExtension('vscode.lsp-sample'); + await ext.activate(); + try { + doc = await vscode.workspace.openTextDocument(docUri); + editor = await vscode.window.showTextDocument(doc); + await sleep(2000); // Wait for server activation + } catch (e) { + console.error(e); + } +} + +async function sleep(ms: number) { + return new Promise(resolve => setTimeout(resolve, ms)); +} + +export const getDocPath = (p: string) => { + return path.resolve(__dirname, '../../testFixture', p); +}; +export const getDocUri = (p: string) => { + return vscode.Uri.file(getDocPath(p)); +}; + +export async function setTestContent(content: string): Promise<boolean> { + const all = new vscode.Range( + doc.positionAt(0), + doc.positionAt(doc.getText().length) + ); + return editor.edit(eb => eb.replace(all, content)); +} diff --git a/build/extension/client/src/test/index.ts b/build/extension/client/src/test/index.ts new file mode 100644 index 00000000..7fedf55f --- /dev/null +++ b/build/extension/client/src/test/index.ts @@ -0,0 +1,15 @@ +/* -------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + * ------------------------------------------------------------------------------------------ */ +'use strict'; + +import * as testRunner from 'vscode/lib/testrunner'; + +testRunner.configure({ + ui: 'bdd', + useColors: true, + timeout: 100000 +}); + +module.exports = testRunner;
\ No newline at end of file |