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

Fix #656 "gotests" when generates test for current function #657

Merged
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
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")
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removal of this line at the end is breaking one of the tests which tries to add some text there

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test updated

59 changes: 51 additions & 8 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');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why not just re-use fixturePath ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if the tests use the same file, 2 tests will failed, since the all tests will be already generated by the "generate test in current package"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Got it, thanks!

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 @@ -293,7 +299,40 @@ It returns the number of bytes written and any write error encountered.
});
}).then(() => {
vscode.commands.executeCommand('workbench.action.closeActiveEditor');
if (fs.existsSync(path.join(generateTestsSourcePath, 'generatetests_test.go'))) {
return Promise.resolve();
} else {
return Promise.reject('generatetests_test.go not found');
}
});
}).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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ideally, I'd like to check whether the test file was created with the test function. You don't have do it now, but if you have some time, I'd appreciate it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea and done.

return Promise.resolve();
});
});
}).then(() => {
vscode.commands.executeCommand('workbench.action.closeActiveEditor');
if (fs.existsSync(path.join(generateTestsSourcePath, 'generatetests_test.go'))) {
return Promise.resolve();
} else {
return Promise.reject('generatetests_test.go not found');
}
});
}).then(() => done(), done);
});
Expand All @@ -305,7 +344,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(generatePackageTestSourcePath, 'generatetests.go'));
return vscode.workspace.openTextDocument(uri).then(document => {
return vscode.window.showTextDocument(document).then(editor => {
return generateTestCurrentPackage().then((result: boolean) => {
Expand All @@ -315,7 +354,11 @@ It returns the number of bytes written and any write error encountered.
});
}).then(() => {
vscode.commands.executeCommand('workbench.action.closeActiveEditor');
return Promise.resolve();
if (fs.existsSync(path.join(generateTestsSourcePath, 'generatetests_test.go'))) {
return Promise.resolve();
} else {
return Promise.reject('generatetests_test.go not found');
}
});
}).then(() => done(), done);
});
Expand Down