Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automate definition provider tests, misc cleanup #589

Merged
merged 4 commits into from
Jan 7, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
}
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;
};
Loading