Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Fixes Issue #2822 #2883

Merged
merged 7 commits into from
Jan 28, 2020
Merged
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
37 changes: 32 additions & 5 deletions src/goGenerateTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,15 +70,28 @@ export function generateTestCurrentPackage(): Promise<boolean> {
if (!editor) {
return;
}
return generateTests({ dir: path.dirname(editor.document.uri.fsPath) }, getGoConfig(editor.document.uri));
return generateTests(
{
dir: path.dirname(editor.document.uri.fsPath),
isTestFile: editor.document.fileName.endsWith('_test.go')
},
getGoConfig(editor.document.uri)
);
}

export function generateTestCurrentFile(): Promise<boolean> {
const editor = checkActiveEditor();
if (!editor) {
return;
}
return generateTests({ dir: editor.document.uri.fsPath }, getGoConfig(editor.document.uri));

return generateTests(
{
dir: editor.document.uri.fsPath,
isTestFile: editor.document.fileName.endsWith('_test.go')
},
getGoConfig(editor.document.uri)
);
}

export async function generateTestCurrentFunction(): Promise<boolean> {
Expand All @@ -99,13 +112,21 @@ export async function generateTestCurrentFunction(): Promise<boolean> {
}
let funcName = currentFunction.name;
const funcNameParts = funcName.match(/^\(\*?(.*)\)\.(.*)$/);
if (funcNameParts != null && funcNameParts.length === 3) { // receiver type specified
if (funcNameParts != null && funcNameParts.length === 3) {
// receiver type specified
const rType = funcNameParts[1].replace(/^\w/, (c) => c.toUpperCase());
const fName = funcNameParts[2].replace(/^\w/, (c) => c.toUpperCase());
funcName = rType + fName;
}

return generateTests({ dir: editor.document.uri.fsPath, func: funcName }, getGoConfig(editor.document.uri));
return generateTests(
{
dir: editor.document.uri.fsPath,
func: funcName,
isTestFile: editor.document.fileName.endsWith('_test.go')
},
getGoConfig(editor.document.uri)
);
}

/**
Expand All @@ -120,6 +141,11 @@ interface Config {
* Specific function names to generate tests skeleton.
*/
func?: string;

/**
* Whether or not the file to generate test functions for is a test file.
*/
isTestFile?: boolean;
}

function generateTests(conf: Config, goConfig: vscode.WorkspaceConfiguration): Promise<boolean> {
Expand Down Expand Up @@ -179,7 +205,8 @@ function generateTests(conf: Config, goConfig: vscode.WorkspaceConfiguration): P

vscode.window.showInformationMessage(message);
outputChannel.append(message);
if (testsGenerated) {

if (testsGenerated && !conf.isTestFile) {
toggleTestFile();
}

Expand Down