Skip to content

Commit

Permalink
Automate definition provider tests, misc cleanup (#589)
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-wiemer authored Jan 7, 2025
1 parent de43092 commit c285e79
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 5 deletions.
2 changes: 1 addition & 1 deletion ahk2
Submodule ahk2 updated from 5be918 to 83dabc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ SendMode, Input
SetBatchLines, -1
SetWorkingDir, %A_ScriptDir%

; Hi from included.ahk1
MyDefProviderFunc() {
MsgBox % "Hi from MyDefProviderFunc"
}
File renamed without changes.
2 changes: 1 addition & 1 deletion src/providers/completionProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ export const provideCompletionItemsInner = (
): vscode.CompletionItem[] =>
methods
.map((m) => completionItemsForMethod(m, uriString, lineNumber))
.reduce((a, b) => a.concat(b), [])
.reduce((a, b) => a.concat(b), []) // reduce from 2D array to 1D array
.concat(variables.map(completionItemForVariable));

export class CompletionProvider implements vscode.CompletionItemProvider {
Expand Down
30 changes: 30 additions & 0 deletions src/providers/defProvider.e2e.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,36 @@
import assert from 'assert';
import { tryGetFileLink } from './defProvider';
import path from 'path';
import {
getActiveEditor,
getDocument,
goToDefinition,
goToLine,
showDocument,
sleep,
} from '../test/utils';

/** Currently in `/out/src/providers`, want to get to `/` */
const rootPath = path.join(__dirname, '..', '..', '..');

suite('provideDefinition', () => {
test('go to definition', async () => {
const filePath = path.resolve(
rootPath,
`./e2e/definitionProvider/main.ahk1`,
);
const doc = await getDocument(filePath);
const editor = await showDocument(doc);
await sleep(1000); // wait for workspace to be indexed

goToLine(editor, 10);

await goToDefinition();
assert(getActiveEditor().document.fileName.includes('included.ahk1'));
});
});

// Unit test masked as e2e test due to dependency on vscode types
suite(tryGetFileLink.name, () => {
const tests: [
name: string,
Expand Down
6 changes: 3 additions & 3 deletions src/test/config.e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import {
sleep,
updateConfig,
} from './utils';
import { resolve } from 'path';
import { ConfigKey, LibIncludeType, ShowOutput } from '../common/global';
import { suite, before, test } from 'mocha';

/** Currently in `/out/src/test`, want to get to `/` */
const rootPath = path.join(__dirname, '..', '..', '..');

// Currently in `out` folder, need to get back to main `src` folder
Expand Down Expand Up @@ -88,7 +88,7 @@ suite('exclude', () => {
if (version === 1) {
await updateConfig<string[]>(ConfigKey.exclude, exclude);
}
const filePath = resolve(rootPath, `./e2e/main.ahk${version}`);
const filePath = path.resolve(rootPath, `./e2e/main.ahk${version}`);
const doc = await getDocument(filePath);
const editor = await showDocument(doc);
await sleep(1_000); // todo only these tests are extra flaky
Expand All @@ -105,7 +105,7 @@ suite('v2.general.librarySuggestions', () => {
let editor: vscode.TextEditor;
before(async () => {
await updateConfig<string[]>(ConfigKey.exclude, []);
const filePath = resolve(rootPath, './e2e/main.ahk2');
const filePath = path.resolve(rootPath, './e2e/main.ahk2');
const doc = await getDocument(filePath);
editor = await showDocument(doc);
await sleep(1_000);
Expand Down
26 changes: 26 additions & 0 deletions src/test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ export const updateConfig = async <T>(
/**
* 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 (
Expand Down Expand Up @@ -80,3 +81,28 @@ export const getCompletionSuggestionLabels = async (
const labels = completionItems?.items.map((i) => i.label);
return labels;
};

export const goToDefinition = async (): Promise<void> => {
await vscode.commands.executeCommand('editor.action.revealDefinition');
};

/**
* Move the seleciton to the 1-based line number.
*
* Optionally provide the 1-based column number: The selection will be placed before this column.
* Provide 0 for the start of the line.
*
* `columnNumber` defaults to 0
*/
export const goToLine = (
editor: vscode.TextEditor,
lineNumber: number,
columnNumber: number = 0,
): void => {
const pos = new vscode.Position(lineNumber - 1, columnNumber);
editor.selection = new vscode.Selection(pos, pos);
};

export const getActiveEditor = (): vscode.TextEditor => {
return vscode.window.activeTextEditor;
};

0 comments on commit c285e79

Please sign in to comment.