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 16, 2018
1 parent 77ae6ef commit 598e084
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 60 deletions.
72 changes: 19 additions & 53 deletions src/goGenerateTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,6 @@ import { outputChannel } from './goStatus';

const generatedWord = 'Generated ';

/**
* This enum is the types of generation supported via the gotests tooling
*/
export enum GenerationType {
Function,
File,
Package,
}

/**
* If current active editor has a Go file, returns the editor.
*/
Expand Down Expand Up @@ -79,48 +70,19 @@ function getGoConfigObject(editor: vscode.TextEditor): vscode.WorkspaceConfigura
return vscode.workspace.getConfiguration('go', documentUri);
}

/**
*
* @param genType the type of generation we want to use gotests to make us. The GenerationType
* enum has the supported types.
*/
export function GenerateTests(genType: GenerationType): Thenable<boolean> {
let editor = checkActiveEditor();
if (!editor) {
return;
}

let goConfig = getGoConfigObject(editor);

switch (genType) {
case GenerationType.Package:
return generateTestCurrentPackage(editor, goConfig);
case GenerationType.File:
return generateTestCurrentFile(editor, goConfig);
case GenerationType.Function:
return generateTestCurrentFunction(editor, goConfig);
default:
vscode.window.showErrorMessage('unknown type passed to generate tests: ' + genType);
return Promise.resolve(false);
}
export function generateTestCurrentPackage(uri: vscode.Uri): Thenable<boolean> {
return generateTests({ dir: path.dirname(uri.fsPath) });
}

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 });
export function generateTestCurrentFile(uri: vscode.Uri): Thenable<boolean> {
return generateTests({ dir: uri.fsPath });
}

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'] || [];
export function generateTestCurrentFunction(uri: vscode.Uri): Thenable<boolean> {
let editor = checkActiveEditor();
if (!editor) {
return;
}

return getFunctions(editor.document).then(functions => {
let currentFunction: vscode.SymbolInformation;
Expand All @@ -139,7 +101,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: uri.fsPath, func: funcName });
});
}

Expand All @@ -151,22 +113,26 @@ interface Config {
* The working directory for `gotests`.
*/
dir: string;
/**
* Optional Template dir for any custom templates for `gotests`.
*/
genFlags: string[];
/**
* Specific function names to generate tests squeleton.
*/
func?: string;
}

function generateTests(conf: Config): Thenable<boolean> {
let editor = checkActiveEditor();
if (!editor) {
return;
}

let goConfig = getGoConfigObject(editor);

return new Promise<boolean>((resolve, reject) => {
let cmd = getBinPath('gotests');
let args = ['-w'];
let goGenerateTestsFlags: string[] = goConfig['genTestsFlags'] || [];

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

Expand Down
6 changes: 3 additions & 3 deletions src/goMain.ts
Original file line number Diff line number Diff line change
Expand Up @@ -329,15 +329,15 @@ export function activate(ctx: vscode.ExtensionContext): void {
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.test.generate.package', () => {
goGenerateTests.GenerateTests(goGenerateTests.GenerationType.Package);
goGenerateTests.generateTestCurrentPackage(vscode.window.activeTextEditor.document.uri);
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.test.generate.file', () => {
goGenerateTests.GenerateTests(goGenerateTests.GenerationType.File);
goGenerateTests.generateTestCurrentFile(vscode.window.activeTextEditor.document.uri);
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.test.generate.function', () => {
goGenerateTests.GenerateTests(goGenerateTests.GenerationType.Function);
goGenerateTests.generateTestCurrentFunction(vscode.window.activeTextEditor.document.uri);
}));

ctx.subscriptions.push(vscode.commands.registerCommand('go.toggle.test.file', () => {
Expand Down
8 changes: 4 additions & 4 deletions test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { testCurrentFile } from '../src/goTest';
import { getBinPath, getGoVersion, isVendorSupported } from '../src/util';
import { documentSymbols } from '../src/goOutline';
import { listPackages, getTextEditForAddImport } from '../src/goImport';
import { GenerateTests, GenerationType } from '../src/goGenerateTests';
import { generateTestCurrentFile, generateTestCurrentFunction, generateTestCurrentPackage } from '../src/goGenerateTests';
import { getAllPackages } from '../src/goPackages';
import { getImportPath } from '../src/util';
import { goPlay } from '../src/goPlayground';
Expand Down Expand Up @@ -302,7 +302,7 @@ It returns the number of bytes written and any write error encountered.
let uri = vscode.Uri.file(path.join(generateTestsSourcePath, 'generatetests.go'));
return vscode.workspace.openTextDocument(uri).then(document => {
return vscode.window.showTextDocument(document).then(editor => {
return GenerateTests(GenerationType.File).then((result: boolean) => {
return generateTestCurrentFile(uri).then((result: boolean) => {
assert.equal(result, true);
return Promise.resolve();
});
Expand Down Expand Up @@ -331,7 +331,7 @@ It returns the number of bytes written and any write error encountered.
assert(vscode.window.activeTextEditor, 'No active editor');
let selection = new vscode.Selection(5, 0, 6, 0);
editor.selection = selection;
return GenerateTests(GenerationType.Function).then((result: boolean) => {
return generateTestCurrentFunction(uri).then((result: boolean) => {
assert.equal(result, true);
return Promise.resolve();
});
Expand All @@ -357,7 +357,7 @@ It returns the number of bytes written and any write error encountered.
let uri = vscode.Uri.file(path.join(generatePackageTestSourcePath, 'generatetests.go'));
return vscode.workspace.openTextDocument(uri).then(document => {
return vscode.window.showTextDocument(document).then(editor => {
return GenerateTests(GenerationType.Package).then((result: boolean) => {
return generateTestCurrentPackage(uri).then((result: boolean) => {
assert.equal(result, true);
return Promise.resolve();
});
Expand Down

0 comments on commit 598e084

Please sign in to comment.