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

Commit

Permalink
Use regex to avoid matching just Test, Example or Benchmark
Browse files Browse the repository at this point in the history
  • Loading branch information
ramya-rao-a committed Sep 10, 2018
1 parent a11c5aa commit ad95b52
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions src/testUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ statusBarItem.text = '$(x) Cancel Running Tests';
*/
const runningTestProcesses: cp.ChildProcess[] = [];

const testSuiteMethodRegex = /^\(([^)]+)\)\.(Test.*)$/;
const testFuncRegex = /^Test.+|Example.+/;
const testMethodRegex = /^\(([^)]+)\)\.(Test.*)$/;
const benchmarkRegex = /^Benchmark$/;

/**
* Input to goTest.
Expand Down Expand Up @@ -100,7 +102,7 @@ export function getTestFunctions(doc: vscode.TextDocument, token: vscode.Cancell
const testify = symbols.some(sym => sym.kind === vscode.SymbolKind.Module && sym.name === "github.com/stretchr/testify/suite");
return symbols.filter(sym =>
sym.kind === vscode.SymbolKind.Function
&& (sym.name.startsWith('Test') || sym.name.startsWith('Example') || (testify && testSuiteMethodRegex.test(sym.name)))
&& (testFuncRegex.test(sym.name) || (testify && testMethodRegex.test(sym.name)))
)
});
}
Expand All @@ -112,7 +114,7 @@ export function getTestFunctions(doc: vscode.TextDocument, token: vscode.Cancell
* @param symbolName Symbol Name to extract method name from.
*/
export function extractInstanceTestName(symbolName: string): string {
const match = symbolName.match(testSuiteMethodRegex);
const match = symbolName.match(testMethodRegex);
if (!match || match.length !== 3) {
return null;
}
Expand All @@ -127,7 +129,7 @@ export function extractInstanceTestName(symbolName: string): string {
*/
export function findAllTestSuiteRuns(doc: vscode.TextDocument, allTests: vscode.SymbolInformation[]): vscode.SymbolInformation[] {
// get non-instance test functions
const testFunctions = allTests.filter(t => !testSuiteMethodRegex.test(t.name));
const testFunctions = allTests.filter(t => !testMethodRegex.test(t.name));
// filter further to ones containing suite.Run()
return testFunctions.filter(t => doc.getText(t.location.range).includes('suite.Run('));
}
Expand All @@ -145,7 +147,7 @@ export function getBenchmarkFunctions(doc: vscode.TextDocument, token: vscode.Ca
.then(symbols =>
symbols.filter(sym =>
sym.kind === vscode.SymbolKind.Function
&& sym.name.startsWith('Benchmark'))
&& benchmarkRegex.test(sym.name))
);
}

Expand Down Expand Up @@ -322,10 +324,10 @@ function targetArgs(testconfig: TestConfig): Thenable<Array<string>> {
params = ['-bench', util.format('^%s$', testconfig.functions.join('|'))];
} else {
let testFunctions = testconfig.functions;
let testifyMethods = testFunctions.filter(fn => testSuiteMethodRegex.test(fn));
let testifyMethods = testFunctions.filter(fn => testMethodRegex.test(fn));
if (testifyMethods.length > 0) {
// filter out testify methods
testFunctions = testFunctions.filter(fn => !testSuiteMethodRegex.test(fn));
testFunctions = testFunctions.filter(fn => !testMethodRegex.test(fn));
testifyMethods = testifyMethods.map(extractInstanceTestName);
}

Expand Down

0 comments on commit ad95b52

Please sign in to comment.