mirror of
https://github.com/microsoft/vscode-extension-samples.git
synced 2026-04-27 16:55:44 +08:00
Merge pull request #79 from Microsoft/octref/test
Add test for lsp-sample
This commit is contained in:
3
lsp-sample/.gitignore
vendored
3
lsp-sample/.gitignore
vendored
@ -1,3 +1,4 @@
|
||||
out
|
||||
node_modules
|
||||
client/server
|
||||
client/server
|
||||
.vscode-test
|
||||
18
lsp-sample/.vscode/launch.json
vendored
18
lsp-sample/.vscode/launch.json
vendored
@ -7,7 +7,7 @@
|
||||
"request": "launch",
|
||||
"name": "Launch Client",
|
||||
"runtimeExecutable": "${execPath}",
|
||||
"args": ["--extensionDevelopmentPath=${workspaceRoot}" ],
|
||||
"args": ["--extensionDevelopmentPath=${workspaceRoot}"],
|
||||
"stopOnEntry": false,
|
||||
"sourceMaps": true,
|
||||
"outFiles": ["${workspaceRoot}/client/out/**/*.js"],
|
||||
@ -22,6 +22,20 @@
|
||||
"port": 6009,
|
||||
"sourceMaps": true,
|
||||
"outFiles": ["${workspaceRoot}/server/out/**/*.js"]
|
||||
},
|
||||
{
|
||||
"name": "Language Server E2E Test",
|
||||
"type": "extensionHost",
|
||||
"request": "launch",
|
||||
"runtimeExecutable": "${execPath}",
|
||||
"args": [
|
||||
"--extensionDevelopmentPath=${workspaceRoot}",
|
||||
"--extensionTestsPath=${workspaceRoot}/client/out/test",
|
||||
"${workspaceRoot}/client/testFixture"
|
||||
],
|
||||
"stopOnEntry": false,
|
||||
"sourceMaps": true,
|
||||
"outFiles": ["${workspaceRoot}/client/out/test/**/*.js"]
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
|
||||
598
lsp-sample/client/package-lock.json
generated
598
lsp-sample/client/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
40
lsp-sample/client/src/test/completion.test.ts
Normal file
40
lsp-sample/client/src/test/completion.test.ts
Normal file
@ -0,0 +1,40 @@
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* 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)
|
||||
})
|
||||
}
|
||||
42
lsp-sample/client/src/test/diagnostics.test.ts
Normal file
42
lsp-sample/client/src/test/diagnostics.test.ts
Normal file
@ -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)
|
||||
})
|
||||
}
|
||||
45
lsp-sample/client/src/test/helper.ts
Normal file
45
lsp-sample/client/src/test/helper.ts
Normal file
@ -0,0 +1,45 @@
|
||||
/* --------------------------------------------------------------------------------------------
|
||||
* 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))
|
||||
}
|
||||
15
lsp-sample/client/src/test/index.ts
Normal file
15
lsp-sample/client/src/test/index.ts
Normal file
@ -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;
|
||||
0
lsp-sample/client/testFixture/completion.txt
Normal file
0
lsp-sample/client/testFixture/completion.txt
Normal file
1
lsp-sample/client/testFixture/diagnostics.txt
Normal file
1
lsp-sample/client/testFixture/diagnostics.txt
Normal file
@ -0,0 +1 @@
|
||||
ANY browsers, ANY OS.
|
||||
@ -52,7 +52,8 @@
|
||||
"watch:client": "tsc -w -p ./client/tsconfig.json",
|
||||
"watch:server": "tsc -w -p ./server/tsconfig.json",
|
||||
"compile": "npm run compile:client && npm run compile:server",
|
||||
"postinstall": "cd client && npm install && cd ../server && npm install && cd .."
|
||||
"postinstall": "cd client && npm install && cd ../server && npm install && cd ..",
|
||||
"test": "sh ./scripts/e2e.sh"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@types/mocha": "^5.2.0",
|
||||
|
||||
6
lsp-sample/scripts/e2e.sh
Executable file
6
lsp-sample/scripts/e2e.sh
Executable file
@ -0,0 +1,6 @@
|
||||
#!/usr/bin/env bash
|
||||
|
||||
export CODE_TESTS_PATH="$(pwd)/client/out/test"
|
||||
export CODE_TESTS_WORKSPACE="$(pwd)/client/testFixture"
|
||||
|
||||
node "$(pwd)/client/node_modules/vscode/bin/test"
|
||||
8
lsp-sample/server/package-lock.json
generated
8
lsp-sample/server/package-lock.json
generated
@ -14,8 +14,8 @@
|
||||
"resolved": "https://registry.npmjs.org/vscode-languageserver/-/vscode-languageserver-4.1.3.tgz",
|
||||
"integrity": "sha512-D6p3q9x8QPtPLRUO5d2UKizjFYfg8zLVJqKoMpAaom8Wuhl1oKRCjeLg+Cp4mgPeCwR71wbgX2BM/jL51ni/0g==",
|
||||
"requires": {
|
||||
"vscode-languageserver-protocol": "3.7.2",
|
||||
"vscode-uri": "1.0.3"
|
||||
"vscode-languageserver-protocol": "^3.7.2",
|
||||
"vscode-uri": "^1.0.1"
|
||||
}
|
||||
},
|
||||
"vscode-languageserver-protocol": {
|
||||
@ -23,8 +23,8 @@
|
||||
"resolved": "https://registry.npmjs.org/vscode-languageserver-protocol/-/vscode-languageserver-protocol-3.7.2.tgz",
|
||||
"integrity": "sha512-VVJwIA/FPl/FnVtrns0FPK6TLi/ET7n1Yo6tCrm6aG7+yAVwIGWdpTmKE+nbP8wEMMbHCkIabk63IJvfz2HNRg==",
|
||||
"requires": {
|
||||
"vscode-jsonrpc": "3.6.2",
|
||||
"vscode-languageserver-types": "3.7.2"
|
||||
"vscode-jsonrpc": "^3.6.2",
|
||||
"vscode-languageserver-types": "^3.7.2"
|
||||
}
|
||||
},
|
||||
"vscode-languageserver-types": {
|
||||
|
||||
@ -14,6 +14,5 @@
|
||||
"dependencies": {
|
||||
"vscode-languageserver": "^4.1.3"
|
||||
},
|
||||
"scripts": {
|
||||
}
|
||||
"scripts": {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user