-
Notifications
You must be signed in to change notification settings - Fork 646
Fix #656 "gotests" when generates test for current function #657
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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") | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,4 @@ func print(txt string) { | |
} | ||
func main() { | ||
print("Hello") | ||
} | ||
} | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not just re-use There was a problem hiding this comment. Choose a reason for hiding this commentThe 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" There was a problem hiding this comment. Choose a reason for hiding this commentThe 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'); | ||
|
@@ -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(() => { | ||
|
@@ -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}`); | ||
} | ||
}) | ||
); | ||
|
@@ -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) => { | ||
|
@@ -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); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
}); | ||
|
@@ -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) => { | ||
|
@@ -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); | ||
}); | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test updated