Skip to content

Commit

Permalink
Test library suggestions setting (#547)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-wiemer authored Oct 17, 2024
1 parent 30f9ca1 commit 46a3dc7
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 28 deletions.
2 changes: 1 addition & 1 deletion ahk2
Submodule ahk2 updated from d5a3f8 to 297742
2 changes: 1 addition & 1 deletion src/common/global.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
72 changes: 47 additions & 25 deletions src/test/config.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) => {
Expand Down Expand Up @@ -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],
];
Expand All @@ -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<string[]>(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<string[]>(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.CompletionList>(
'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);
});
});
Expand Down
36 changes: 35 additions & 1 deletion src/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,43 @@ export const closePanel = async (): Promise<void> => {
};

/** Update the global AHK++ setting */
export const updateConfig = async <T>(section: string, value: T) => {
export const updateConfig = async <T>(
section: string,
value: T,
): Promise<void> => {
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<void> => {
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.CompletionList>(
'vscode.executeCompletionItemProvider',
editor.document.uri,
editor.selection.active,
);
const labels = completionItems?.items.map((i) => i.label);
return labels;
};

0 comments on commit 46a3dc7

Please sign in to comment.