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

Commit

Permalink
Simplify the logic even further
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffbean committed Aug 14, 2018
1 parent 77ae6ef commit 56e824b
Showing 1 changed file with 8 additions and 20 deletions.
28 changes: 8 additions & 20 deletions src/goGenerateTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ export function GenerateTests(genType: GenerationType): Thenable<boolean> {
}

let goConfig = getGoConfigObject(editor);
let documentPath = editor.document.uri.fsPath;

switch (genType) {
case GenerationType.Package:
return generateTestCurrentPackage(editor, goConfig);
return generateTests({ dir: path.dirname(documentPath), goConfig: goConfig });
case GenerationType.File:
return generateTestCurrentFile(editor, goConfig);
return generateTests({ dir: documentPath, goConfig: goConfig });
case GenerationType.Function:
return generateTestCurrentFunction(editor, goConfig);
default:
Expand All @@ -105,23 +106,9 @@ export function GenerateTests(genType: GenerationType): Thenable<boolean> {
}
}

function generateTestCurrentPackage(editor: vscode.TextEditor, goConfig: vscode.WorkspaceConfiguration): Thenable<boolean> {
let dir = path.dirname(editor.document.uri.fsPath);
const goGenerateTestsFlags: string[] = goConfig['genTestsFlags'] || [];
return generateTests({ dir: dir, genFlags: goGenerateTestsFlags });
}

function generateTestCurrentFile(editor: vscode.TextEditor, goConfig: vscode.WorkspaceConfiguration): Thenable<boolean> {
let file = editor.document.uri.fsPath;
const goGenerateTestsFlags: string[] = goConfig['genTestsFlags'] || [];
return generateTests({ dir: file, genFlags: goGenerateTestsFlags });
}

function generateTestCurrentFunction(editor: vscode.TextEditor, goConfig: vscode.WorkspaceConfiguration): Thenable<boolean> {
let file = editor.document.uri.fsPath;

const goGenerateTestsFlags: string[] = goConfig['genTestsFlags'] || [];

return getFunctions(editor.document).then(functions => {
let currentFunction: vscode.SymbolInformation;
for (let func of functions) {
Expand All @@ -139,7 +126,7 @@ function generateTestCurrentFunction(editor: vscode.TextEditor, goConfig: vscode
if (funcName.includes('.')) {
funcName = funcName.split('.')[1];
}
return generateTests({ dir: file, func: funcName , genFlags: goGenerateTestsFlags });
return generateTests({ dir: file, func: funcName , goConfig: goConfig });
});
}

Expand All @@ -152,9 +139,9 @@ interface Config {
*/
dir: string;
/**
* Optional Template dir for any custom templates for `gotests`.
* The config object for the go section
*/
genFlags: string[];
goConfig: vscode.WorkspaceConfiguration;
/**
* Specific function names to generate tests squeleton.
*/
Expand All @@ -165,8 +152,9 @@ function generateTests(conf: Config): Thenable<boolean> {
return new Promise<boolean>((resolve, reject) => {
let cmd = getBinPath('gotests');
let args = ['-w'];
let goGenerateTestsFlags: string[] = conf.goConfig['genTestsFlags'] || [];

conf.genFlags.forEach(flag => {
goGenerateTestsFlags.forEach(flag => {
args.push(flag);
});

Expand Down

0 comments on commit 56e824b

Please sign in to comment.