|
| 1 | +import * as assert from 'assert'; |
| 2 | +import * as vscode from 'vscode'; |
| 3 | + |
| 4 | +export function sleep(ms: number): Promise<void> { |
| 5 | + return new Promise((resolve) => setTimeout(resolve, ms)); |
| 6 | +} |
| 7 | + |
| 8 | +export async function testCompletion( |
| 9 | + editor: vscode.TextEditor, |
| 10 | + position: vscode.Position, |
| 11 | + expectedCompletionList: vscode.CompletionList, |
| 12 | + type: "exact" | "contains", |
| 13 | + testKeys: (keyof vscode.CompletionItem)[] = ["label", "kind"] |
| 14 | +) { |
| 15 | + editor = await vscode.window.showTextDocument(editor.document, editor.viewColumn); |
| 16 | + await sleep(500); |
| 17 | + editor.selection = new vscode.Selection(position, position); |
| 18 | + await sleep(500); |
| 19 | + |
| 20 | + // Executing the command `vscode.executeCompletionItemProvider` to simulate triggering completion |
| 21 | + const actualCompletionList = (await vscode.commands.executeCommand( |
| 22 | + 'vscode.executeCompletionItemProvider', |
| 23 | + editor.document.uri, |
| 24 | + position |
| 25 | + )) as vscode.CompletionList; |
| 26 | + |
| 27 | + if (type === "exact") { |
| 28 | + assert.strictEqual(actualCompletionList.items.length, expectedCompletionList.items.length); |
| 29 | + expectedCompletionList.items.forEach((expectedItem, i) => { |
| 30 | + const actualItem = actualCompletionList.items[i]; |
| 31 | + testKeys.forEach(key => { |
| 32 | + assert.strictEqual(actualItem[key], expectedItem[key], |
| 33 | + "completion " |
| 34 | + + JSON.stringify(expectedItem.label) |
| 35 | + + " mismatch on key " + JSON.stringify(key) + ":\n" |
| 36 | + + "expected = " + JSON.stringify(expectedItem[key]) + "\n" |
| 37 | + + " actual = " + JSON.stringify(actualItem[key])); |
| 38 | + }); |
| 39 | + }); |
| 40 | + } else if (type === "contains") { |
| 41 | + assert.ok(actualCompletionList.items.length >= expectedCompletionList.items.length, |
| 42 | + "Expected at least " + expectedCompletionList.items.length |
| 43 | + + " completions, but only got " + actualCompletionList.items.length); |
| 44 | + expectedCompletionList.items.forEach((expectedItem, i) => { |
| 45 | + const actualItem = actualCompletionList.items.find(i => i.label == expectedItem.label); |
| 46 | + if (!actualItem) |
| 47 | + assert.fail("can't find completion item " |
| 48 | + + JSON.stringify(expectedItem.label) |
| 49 | + + " in " |
| 50 | + + JSON.stringify(actualCompletionList.items.map(c => c.label))); |
| 51 | + |
| 52 | + testKeys.forEach(key => { |
| 53 | + assert.strictEqual(actualItem[key], expectedItem[key], |
| 54 | + "completion " |
| 55 | + + JSON.stringify(expectedItem.label) |
| 56 | + + " mismatch on key " + JSON.stringify(key) + ":\n" |
| 57 | + + "expected = " + JSON.stringify(expectedItem[key]) + "\n" |
| 58 | + + " actual = " + JSON.stringify(actualItem[key])); |
| 59 | + }); |
| 60 | + }); |
| 61 | + } else { |
| 62 | + throw new Error("invalid type"); |
| 63 | + } |
| 64 | +} |
0 commit comments