fix: Handle methods which have more than 2 arguments (#1532)

- Use global match when parsing the method parameters list.
- Eliminate all spaces in the parameters list of the test IDs.

Signed-off-by: Sheng Chen <sheche@microsoft.com>
This commit is contained in:
Sheng Chen 2023-02-17 14:03:45 +08:00 коммит произвёл GitHub
Родитель d46a5923f9
Коммит e67c44db43
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 70 добавлений и 29 удалений

Просмотреть файл

@ -52,7 +52,8 @@ public class TestItemUtils {
// Generics don't come through in the test results, so we need to strip
// them out now.
final String methodName = JavaElementLabels.getElementLabel(element, JavaElementLabels.ALL_DEFAULT)
.replaceAll("<.*?>", "");
.replaceAll("<.*?>", "")
.replaceAll(" ", "");
return className + "#" + methodName;
} else {
return method.getDeclaringType().getFullyQualifiedName() + "#" + method.getElementName();

Просмотреть файл

@ -72,12 +72,12 @@ public class TestNGListener
for (final Class<?> paramClazz : result.getMethod().getParameterTypes()) {
params.append(paramClazz.getSimpleName().replaceAll("<.*?>", ""));
params.append(", ");
params.append(",");
}
// Remove the last ", "
if (params.length() > 0) {
params.delete(params.length() - 2, params.length());
params.delete(params.length() - 1, params.length());
}
return className + "#" + methodName + "(" + params.toString() + ")";

Просмотреть файл

@ -53,9 +53,9 @@
</build>
<repositories>
<repository>
<id>202209</id>
<id>202212</id>
<layout>p2</layout>
<url>https://download.eclipse.org/releases/2022-09/202209141001</url>
<url>https://download.eclipse.org/eclipse/updates/4.27-I-builds/I20221206-1800</url>
</repository>
<repository>
<id>oss.sonatype.org</id>

Просмотреть файл

@ -55,18 +55,18 @@
"main": "./main.js",
"contributes": {
"javaExtensions": [
"./server/junit-jupiter-api_5.9.0.jar",
"./server/junit-jupiter-engine_5.9.0.jar",
"./server/junit-jupiter-migrationsupport_5.9.0.jar",
"./server/junit-jupiter-params_5.9.0.jar",
"./server/junit-platform-commons_1.9.0.jar",
"./server/junit-platform-engine_1.9.0.jar",
"./server/junit-platform-launcher_1.9.0.jar",
"./server/junit-platform-runner_1.9.0.jar",
"./server/junit-platform-suite-api_1.9.0.jar",
"./server/junit-platform-suite-commons_1.9.0.jar",
"./server/junit-platform-suite-engine_1.9.0.jar",
"./server/junit-vintage-engine_5.9.0.jar",
"./server/junit-jupiter-api_5.9.1.jar",
"./server/junit-jupiter-engine_5.9.1.jar",
"./server/junit-jupiter-migrationsupport_5.9.1.jar",
"./server/junit-jupiter-params_5.9.1.jar",
"./server/junit-platform-commons_1.9.1.jar",
"./server/junit-platform-engine_1.9.1.jar",
"./server/junit-platform-launcher_1.9.1.jar",
"./server/junit-platform-runner_1.9.1.jar",
"./server/junit-platform-suite-api_1.9.1.jar",
"./server/junit-platform-suite-commons_1.9.1.jar",
"./server/junit-platform-suite-engine_1.9.1.jar",
"./server/junit-vintage-engine_5.9.1.jar",
"./server/org.apiguardian.api_1.1.2.jar",
"./server/org.eclipse.jdt.junit4.runtime_1.3.0.v20220609-1843.jar",
"./server/org.eclipse.jdt.junit5.runtime_1.1.100.v20220907-0450.jar",

Просмотреть файл

@ -226,14 +226,14 @@ export class JUnitRunnerResultAnalyzer extends RunnerResultAnalyzer {
className = `${className}$${nestedClassName}`;
} else if (part.startsWith(testTemplateId)) {
const rawMethodName: string = part.substring(testTemplateId.length)
.replace('\\,', ',')
.replace(' ', '');
.replace(/\\,/g, ',')
.replace(/ /g, '');
// If the method name exists then we want to include the '#' qualifier.
methodName = `#${this.getJUnit5MethodName(rawMethodName)}`;
} else if (part.startsWith(propertyId)) {
const rawMethodName: string = part.substring(propertyId.length)
.replace('\\,', ',')
.replace(' ', '');
.replace(/\\,/g, ',')
.replace(/ /g, '');
// If the method name exists then we want to include the '#' qualifier.
methodName = `#${this.getJUnit5MethodName(rawMethodName)}`;
}
@ -279,11 +279,11 @@ export class JUnitRunnerResultAnalyzer extends RunnerResultAnalyzer {
const params: string[] = rawParamsString.split(',');
let paramString: string = '';
params.forEach((param: string) => {
paramString += `${param.substring(param.lastIndexOf('.') + 1)}, `;
paramString += `${param.substring(param.lastIndexOf('.') + 1)},`;
});
// We want to remove the final comma.
if (paramString.length > 0) {
paramString = paramString.substring(0, paramString.length - 2);
paramString = paramString.substring(0, paramString.length - 1);
}
const methodName: string = rawName.substring(0, rawName.indexOf('('));

Просмотреть файл

@ -311,8 +311,8 @@ org.junit.ComparisonFailure: expected:<hello
workspaceFolder: workspace.workspaceFolders?.[0]!,
};
let analyzer = new JUnitRunnerResultAnalyzer(runnerContext)
// We need to stub this method to avoid isssues with the TestController
// not being set up in the non-test version of the utils file.
// We need to stub this method to avoid issues with the TestController
// not being set up in the non-test version of the utils file.
const stub = sinon.stub(analyzer, "enlistDynamicMethodToTestMapping");
stub.returnsArg(0);
analyzer.analyzeData(testRunnerOutput);
@ -345,8 +345,8 @@ org.junit.ComparisonFailure: expected:<hello
};
analyzer = new JUnitRunnerResultAnalyzer(runnerContext);
// We need to stub this method to avoid isssues with the TestController
// not being set up in the non-test version of the utils file.
// We need to stub this method to avoid issues with the TestController
// not being set up in the non-test version of the utils file.
sinon.stub(analyzer, "enlistDynamicMethodToTestMapping");
analyzer.analyzeData(testRunnerOutput);
@ -403,4 +403,39 @@ org.opentest4j.AssertionFailedError: expected: <1> but was: <2>
assert.strictEqual(testMessage.location?.range.start.line, 10); // =11 - 1, (most precise info we get from the stack trace)
});
test("can handle test cases with more than 3 arguments", () => {
const testItem = generateTestItem(testController, 'junit@junit5.ParameterizedAnnotationTest#testMultiArguments(String,String,String)', TestKind.JUnit5, new Range(10, 0, 16, 0));
const testRunRequest = new TestRunRequest([testItem], []);
const testRun = testController.createTestRun(testRunRequest);
const startedSpy = sinon.spy(testRun, 'started');
const passedSpy = sinon.spy(testRun, 'passed');
const testRunnerOutput = `%TESTC 0 v2
%TSTTREE2,junit5.ParameterizedAnnotationTest,true,1,false,1,ParameterizedAnnotationTest,,[engine:junit-jupiter]/[class:junit5.ParameterizedAnnotationTest]
%TSTTREE3,testMultiArguments(junit5.ParameterizedAnnotationTest),true,0,false,2,testMultiArguments(String\\, String\\, String),java.lang.String\\, java.lang.String\\, java.lang.String,[engine:junit-jupiter]/[class:junit5.ParameterizedAnnotationTest]/[test-template:testMultiArguments(java.lang.String\\, java.lang.String\\, java.lang.String)]
%TSTTREE4,testMultiArguments(junit5.ParameterizedAnnotationTest),false,1,true,3,[1] a\\, b\\, c,java.lang.String\\, java.lang.String\\, java.lang.String,[engine:junit-jupiter]/[class:junit5.ParameterizedAnnotationTest]/[test-template:testMultiArguments(java.lang.String\\, java.lang.String\\, java.lang.String)]/[test-template-invocation:#1]
%TESTS 4,testMultiArguments(junit5.ParameterizedAnnotationTest)
%TESTE 4,testMultiArguments(junit5.ParameterizedAnnotationTest)
%RUNTIME162`;
const runnerContext: IRunTestContext = {
isDebug: false,
kind: TestKind.JUnit5,
projectName: 'junit',
testItems: [testItem],
testRun: testRun,
workspaceFolder: workspace.workspaceFolders?.[0]!,
};
const analyzer = new JUnitRunnerResultAnalyzer(runnerContext);
// We need to stub this method to avoid isssues with the TestController
// not being set up in the non-test version of the utils file.
const stub = sinon.stub(analyzer, "enlistDynamicMethodToTestMapping");
const dummy = generateTestItem(testController, '[__INVOCATION__]-dummy', TestKind.JUnit5, new Range(10, 0, 16, 0));
stub.returns(dummy);
analyzer.analyzeData(testRunnerOutput);
sinon.assert.calledWith(startedSpy, dummy);
sinon.assert.calledWith(passedSpy, dummy);
});
});

Просмотреть файл

@ -12,8 +12,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>

Просмотреть файл

@ -54,4 +54,9 @@ public class ParameterizedAnnotationTest {
@MethodSource("users")
public void testCheckUser(User user){
}
@ParameterizedTest
@CsvSource({"a,b,c"})
public void testMultiArguments(String a, String b, String c) throws Exception {
}
}