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

Commit

Permalink
Fix "gotests" when generates test for current function
Browse files Browse the repository at this point in the history
Issue:
"Generate unit tests for current function" fails with recent
vscode-go version. This due to the fact that the GoDocumentSymbolProvider
returns now also the receiverType in the name function.
example: before "DecodeJSON", after (*Volume).DecodeJSON
gotests bin is not able to understand this string with the option -only
since it is not a valid regular expression.

Fixes: #656
check is the function name contains a ".", if yes: returns only the
second part of the name that is corresponding to the function name
without the receiverType.
  • Loading branch information
clamoriniere committed Nov 29, 2016
1 parent 88dfe84 commit 6f2cfb4
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 15 deletions.
13 changes: 6 additions & 7 deletions src/goGenerateTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ export function generateTestCurrentPackage(): Thenable<boolean> {
return;
}
let dir = path.dirname(editor.document.uri.fsPath);
let message = 'Unit tests generated for package: ' + path.basename(dir);
return generateTests({ dir: dir });
}

Expand All @@ -86,7 +85,6 @@ export function generateTestCurrentFile(): Thenable<boolean> {
return;
}
let file = editor.document.uri.fsPath;
let message = 'Unit tests generated for file: ' + path.basename(file);
return generateTests({ dir: file });
}

Expand All @@ -96,7 +94,7 @@ export function generateTestCurrentFunction(): Thenable<boolean> {
return;
}
let file = editor.document.uri.fsPath;
getFunctions(editor.document).then(functions => {
return getFunctions(editor.document).then(functions => {
let currentFunction: vscode.SymbolInformation;
for (let func of functions) {
let selection = editor.selection;
Expand All @@ -109,10 +107,11 @@ export function generateTestCurrentFunction(): Thenable<boolean> {
vscode.window.setStatusBarMessage('No function found at cursor.', 5000);
return;
}
let message = 'Unit test generated for function: ' + currentFunction.name + ' in file: ' + path.basename(file);
return generateTests({ dir: file, func: currentFunction.name });
}).then(null, err => {
console.error(err);
let funcName = currentFunction.name;
if (funcName.includes('.')) {
funcName = funcName.split('.')[1];
}
return generateTests({ dir: file, func: funcName });
});
}

Expand Down
11 changes: 11 additions & 0 deletions test/fixtures/generatetests/generatetests.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package generatetests

type obj struct {}

func (o obj) String() string {
return ""
}

func main() {
fmt.Println("do nothing")
}
2 changes: 1 addition & 1 deletion test/fixtures/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ func print(txt string) {
}
func main() {
print("Hello")
}
}
45 changes: 38 additions & 7 deletions test/go.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,17 @@ import { testCurrentFile } from '../src/goTest';
import { getGoVersion, isVendorSupported } from '../src/util';
import { documentSymbols } from '../src/goOutline';
import { listPackages } from '../src/goImport';
import { generateTestCurrentFile, generateTestCurrentPackage } from '../src/goGenerateTests';
import { generateTestCurrentFile, generateTestCurrentPackage, generateTestCurrentFunction } from '../src/goGenerateTests';
import { getBinPath } from '../src/goPath';

suite('Go Extension Tests', () => {
let gopath = process.env['GOPATH'];
let repoPath = path.join(gopath, 'src', 'test');
let fixturePath = path.join(repoPath, 'testfixture');
let fixtureSourcePath = path.join(__dirname, '..', '..', 'test', 'fixtures');
let generateTestsSourcePath = path.join(repoPath, 'generatetests');
let generateFunctionTestSourcePath = path.join(repoPath, 'generatefunctiontest');
let generatePackageTestSourcePath = path.join(repoPath, 'generatePackagetest');

suiteSetup(() => {
assert.ok(gopath !== null, 'GOPATH is not defined');
Expand All @@ -35,6 +38,9 @@ suite('Go Extension Tests', () => {
fs.copySync(path.join(fixtureSourcePath, 'errorsTest', 'errors.go'), path.join(fixturePath, 'errorsTest', 'errors.go'));
fs.copySync(path.join(fixtureSourcePath, 'sample_test.go'), path.join(fixturePath, 'sample_test.go'));
fs.copySync(path.join(fixtureSourcePath, 'gogetdocTestData', 'test.go'), path.join(fixturePath, 'gogetdocTestData', 'test.go'));
fs.copySync(path.join(fixtureSourcePath, 'generatetests', 'generatetests.go'), path.join(generateTestsSourcePath, 'generatetests.go'));
fs.copySync(path.join(fixtureSourcePath, 'generatetests', 'generatetests.go'), path.join(generateFunctionTestSourcePath, 'generatetests.go'));
fs.copySync(path.join(fixtureSourcePath, 'generatetests', 'generatetests.go'), path.join(generatePackageTestSourcePath, 'generatetests.go'));
});

suiteTeardown(() => {
Expand Down Expand Up @@ -221,22 +227,22 @@ It returns the number of bytes written and any write error encountered.
});
let provider = new GoCompletionItemProvider();
let testCases: [vscode.Position, string[]][] = [
[new vscode.Position(12, 2), ['bytes']],
[new vscode.Position(13, 5), ['Abs', 'Acos', 'Asin']]
[new vscode.Position(11, 3), ['bytes']],
[new vscode.Position(12, 5), ['Abs', 'Acos', 'Asin']]
];
let uri = vscode.Uri.file(path.join(fixturePath, 'test.go'));

vscode.workspace.openTextDocument(uri).then((textDocument) => {
return vscode.window.showTextDocument(textDocument).then(editor => {
return editor.edit(editbuilder => {
editbuilder.insert(new vscode.Position(12, 0), 'by\n');
editbuilder.insert(new vscode.Position(12, 1), 'by\n');
editbuilder.insert(new vscode.Position(13, 0), 'math.\n');
}).then(() => {
let promises = testCases.map(([position, expected]) =>
provider.provideCompletionItemsInternal(editor.document, position, null, config).then(items => {
let labels = items.map(x => x.label);
for (let entry of expected) {
assert.equal(labels.indexOf(entry) > -1, true, `missing expected item in competion list: ${entry} Actual: ${labels}`);
assert.equal(labels.indexOf(entry) > -1, true, `missing expected item in completion list: ${entry} Actual: ${labels}`);
}
})
);
Expand Down Expand Up @@ -283,7 +289,7 @@ It returns the number of bytes written and any write error encountered.
return Promise.resolve();
}

let uri = vscode.Uri.file(path.join(fixturePath, 'test.go'));
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 generateTestCurrentFile().then((result: boolean) => {
Expand All @@ -298,14 +304,39 @@ It returns the number of bytes written and any write error encountered.
}).then(() => done(), done);
});

test('Test Generate unit tests squeleton for a function', (done) => {
getGoVersion().then(version => {
if (version.major === 1 && version.minor < 6) {
// gotests is not supported in Go 1.5, so skip the test
return Promise.resolve();
}

let uri = vscode.Uri.file(path.join(generateFunctionTestSourcePath, 'generatetests.go'));
return vscode.workspace.openTextDocument(uri).then(document => {
return vscode.window.showTextDocument(document).then((editor: vscode.TextEditor) => {
assert(vscode.window.activeTextEditor, 'No active editor');
let selection = new vscode.Selection(5, 0, 6, 0);
editor.selection = selection;
return generateTestCurrentFunction().then((result: boolean) => {
assert.equal(result, true);
return Promise.resolve();
});
});
}).then(() => {
vscode.commands.executeCommand('workbench.action.closeActiveEditor');
return Promise.resolve();
});
}).then(() => done(), done);
});

test('Test Generate unit tests squeleton for package', (done) => {
getGoVersion().then(version => {
if (version.major === 1 && version.minor < 6) {
// gotests is not supported in Go 1.5, so skip the test
return Promise.resolve();
}

let uri = vscode.Uri.file(path.join(fixturePath, 'test.go'));
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 generateTestCurrentPackage().then((result: boolean) => {
Expand Down

0 comments on commit 6f2cfb4

Please sign in to comment.