diff --git a/ahk2 b/ahk2 index d5a3f83e..297742ef 160000 --- a/ahk2 +++ b/ahk2 @@ -1 +1 @@ -Subproject commit d5a3f83e88d624eb2cd0ec06cc99b36e8855cd8a +Subproject commit 297742ef9d2db65985007b19603c2a0d28b3c173 diff --git a/src/common/global.ts b/src/common/global.ts index 0214a777..b678cf26 100644 --- a/src/common/global.ts +++ b/src/common/global.ts @@ -68,7 +68,7 @@ export enum LanguageId { export type ShowOutput = 'always' | 'never'; export enum LibIncludeType { - Disabled = 'Off', + Disabled = 'Disabled', Local = 'Local', UserAndStandard = 'User and Standard', All = 'All', diff --git a/src/test/config.e2e.ts b/src/test/config.e2e.ts index 01c31162..ba63a083 100644 --- a/src/test/config.e2e.ts +++ b/src/test/config.e2e.ts @@ -2,7 +2,9 @@ import * as assert from 'assert'; import * as vscode from 'vscode'; import * as path from 'path'; import { + addAndSelectSnippet, closePanel, + getCompletionSuggestionLabels, getDocument, isOutputVisible, showDocument, @@ -18,6 +20,13 @@ const rootPath = path.join(__dirname, '..', '..', '..'); // Currently in `out` folder, need to get back to main `src` folder const samplesParentPath = path.join(rootPath, 'src/test/samples'); +/** Snippet text that should result in `funcName` being suggested (based on config) */ +const snippetText = 'MyExclu'; +/** Func name included in a local library in the `excludedFileName` */ +const funcName = 'MyExcludedFunc'; +/** Name of file containing library functions for `exclude` testing */ +const excludedFileName = 'excluded.ahk'; + // CI does not have AHK installed suite('general.showOutput @ignoreCI', () => { const before = async (show: ShowOutput) => { @@ -61,8 +70,8 @@ suite('exclude', () => { ][] = [ ['v1 no exclusions', 1, [], true], ['v2 no exclusions', 2, [], true], - ['v1 exclusions', 1, ['excluded.ahk'], false], - ['v2 exclusions', 2, ['excluded.ahk'], false], + ['v1 exclusions', 1, [excludedFileName], false], + ['v2 exclusions', 2, [excludedFileName], false], ['back to v1 no exclusions', 1, [], true], ['back to v2 no exclusions', 2, [], true], ]; @@ -76,35 +85,48 @@ suite('exclude', () => { tests.forEach(([name, version, exclude, expected]) => { test(name, async () => { - const snippetText = 'MyExclu'; - const funcName = 'MyExcludedFunc'; if (version === 1) await updateConfig(ConfigKey.exclude, exclude); const filePath = resolve(rootPath, `./e2e/main.ahk${version}`); const doc = await getDocument(filePath); const editor = await showDocument(doc); - editor.insertSnippet( - new vscode.SnippetString(snippetText) - .appendTabstop(0) - .appendText('\n'), - ); - await sleep(100); - editor.selection = new vscode.Selection( - 0, - 0, - 0, - snippetText.length, + await addAndSelectSnippet(editor, snippetText); + + const labels = await getCompletionSuggestionLabels(editor); + + assert.strictEqual(labels.includes(funcName), expected); + }); + }); +}); + +suite.only('v2.general.librarySuggestions', () => { + let editor: vscode.TextEditor; + before(async () => { + await updateConfig(ConfigKey.exclude, []); + const filePath = resolve(rootPath, './e2e/main.ahk2'); + const doc = await getDocument(filePath); + editor = await showDocument(doc); + await sleep(1000); + }); + + const tests: [name: string, libType: LibIncludeType, expected: boolean][] = + [ + ['Disabled', LibIncludeType.Disabled, false], + ['Local', LibIncludeType.Local, true], + ['User and Standard', LibIncludeType.UserAndStandard, false], + ['All', LibIncludeType.All, true], + ]; + + tests.forEach(([name, libType, expected]) => { + test(name, async () => { + await updateConfig<{ librarySuggestions: LibIncludeType }>( + ConfigKey.generalV2, + { librarySuggestions: libType }, ); - await sleep(100); - - // Get completion items - const completionItems = - await vscode.commands.executeCommand( - 'vscode.executeCompletionItemProvider', - doc.uri, - editor.selection.active, - ); - const labels = completionItems?.items.map((i) => i.label); + await addAndSelectSnippet(editor, snippetText); + + const labels = await getCompletionSuggestionLabels(editor); + assert.strictEqual(labels.includes(funcName), expected); }); }); diff --git a/src/test/utils.ts b/src/test/utils.ts index 6c21effe..bcce5686 100644 --- a/src/test/utils.ts +++ b/src/test/utils.ts @@ -40,9 +40,43 @@ export const closePanel = async (): Promise => { }; /** Update the global AHK++ setting */ -export const updateConfig = async (section: string, value: T) => { +export const updateConfig = async ( + section: string, + value: T, +): Promise => { await vscode.workspace .getConfiguration(configPrefix) .update(section, value, false); await sleep(1500); // todo tests are flaky even at 1_000ms }; + +/** + * Adds the provided snippetText at the current selection of the editor + * and adds a newline to minimize syntax errors. + * Waits after selecting so callers don't have to. + */ +export const addAndSelectSnippet = async ( + editor: vscode.TextEditor, + snippetText: string, +): Promise => { + editor.insertSnippet( + new vscode.SnippetString(snippetText).appendTabstop(0).appendText('\n'), + ); + await sleep(100); + editor.selection = new vscode.Selection(0, 0, 0, snippetText.length); + await sleep(100); +}; + +/** Returns the labels of the completion suggestions for the current editor at its current position */ +export const getCompletionSuggestionLabels = async ( + editor: vscode.TextEditor, +): Promise<(string | vscode.CompletionItemLabel)[]> => { + const completionItems = + await vscode.commands.executeCommand( + 'vscode.executeCompletionItemProvider', + editor.document.uri, + editor.selection.active, + ); + const labels = completionItems?.items.map((i) => i.label); + return labels; +};