-
Notifications
You must be signed in to change notification settings - Fork 646
Added support for tests using stretchr/testify #1707
Changes from 1 commit
1d4f9eb
b109cab
29f7268
273966b
8d0721e
714dd3a
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 |
---|---|---|
|
@@ -14,6 +14,7 @@ import { getNonVendorPackages } from './goPackages'; | |
|
||
let outputChannel = vscode.window.createOutputChannel('Go Tests'); | ||
|
||
const testSuiteMethodRe = /^\([^)]+\)\.Test/; | ||
|
||
/** | ||
* Input to goTest. | ||
|
@@ -89,10 +90,36 @@ export function getTestFunctions(doc: vscode.TextDocument, token: vscode.Cancell | |
.then(symbols => | ||
symbols.filter(sym => | ||
sym.kind === vscode.SymbolKind.Function | ||
&& (sym.name.startsWith('Test') || sym.name.startsWith('Example'))) | ||
&& (sym.name.startsWith('Test') || sym.name.startsWith('Example') || isInstanceTestMethod(sym)) | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* Checks whether given symbol is a test method of a test suite instance. | ||
* | ||
* @param symbol Symbol to check. | ||
*/ | ||
export function isInstanceTestMethod(symbol: vscode.SymbolInformation | string): boolean { | ||
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. Since this function isn't used anywhere, it need not be exported. |
||
let symbolName = typeof symbol === 'string' ? symbol : symbol.name; | ||
return testSuiteMethodRe.test(symbolName); | ||
} | ||
|
||
/** | ||
* Extracts test method name of a suite test function. | ||
* For example a symbol with name "(*testSuite).TestMethod" will return "TestMethod". | ||
* | ||
* @param symbol Symbol to extract method name from. | ||
*/ | ||
export function extractInstanceTestName(symbol: vscode.SymbolInformation | string): string { | ||
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. All uses of this function passes in a string. So we don't need to support both string and |
||
let symbolName = typeof symbol === 'string' ? symbol : symbol.name; | ||
const match = symbolName.match(/\.(Test.*)$/); | ||
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 re-use the |
||
if (!match) { | ||
return null; | ||
} | ||
return match[1]; | ||
} | ||
|
||
/** | ||
* Returns all Benchmark functions in the given source file. | ||
* | ||
|
@@ -242,7 +269,26 @@ function expandFilePathInOutput(output: string, cwd: string): string { | |
*/ | ||
function targetArgs(testconfig: TestConfig): Thenable<Array<string>> { | ||
if (testconfig.functions) { | ||
return Promise.resolve([testconfig.isBenchmark ? '-bench' : '-run', util.format('^%s$', testconfig.functions.join('|'))]); | ||
let params: string[] = []; | ||
if (testconfig.isBenchmark) { | ||
params = ['-bench', util.format('^%s$', testconfig.functions.join('|'))]; | ||
} else { | ||
let testFunctions = testconfig.functions; | ||
let testifyMethods = testFunctions.filter(isInstanceTestMethod); | ||
if (testifyMethods.length > 0) { | ||
// filter out testify methods | ||
testFunctions = testFunctions.filter(fn => !isInstanceTestMethod(fn)); | ||
testifyMethods = testifyMethods.map(extractInstanceTestName); | ||
} | ||
|
||
if (testFunctions.length > 0) { | ||
params = params.concat(['-run', util.format('^%s$', testFunctions.join('|'))]); | ||
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. We have to pass |
||
} | ||
if (testifyMethods.length > 0) { | ||
params = params.concat(['-testify.m', util.format('^%s$', testifyMethods.join('|'))]); | ||
} | ||
} | ||
return Promise.resolve(params); | ||
} else if (testconfig.includeSubDirectories && !testconfig.isBenchmark) { | ||
return getGoVersion().then((ver: SemVersion) => { | ||
if (ver && (ver.major > 1 || (ver.major === 1 && ver.minor >= 9))) { | ||
|
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.
isInstanceTestMethod
is an unused import here