Skip to content

Commit 09caa1c

Browse files
dreamworldjdneo
andauthored
issue fix: test name is truncated when name contains rounded bracket(s) (microsoft#1071)
Co-authored-by: Sheng Chen <sheche@microsoft.com>
1 parent 7cc1ead commit 09caa1c

File tree

4 files changed

+66
-2
lines changed

4 files changed

+66
-2
lines changed

src/runners/junitRunner/JUnitRunnerResultAnalyzer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,10 @@ export class JUnitRunnerResultAnalyzer extends BaseRunnerResultAnalyzer {
109109
* '(?:@AssumptionFailure: |@Ignore: )?' - indicate if the case is ignored due to assumption failure or disabled
110110
* '(.*?)' - test method name
111111
* '(?:\[\d+\])?' - execution index, it will appear for the JUnit4's parameterized test
112-
* '\((.*?)\)' - class fully qualified name
112+
* '\(([^)]*)\)[^(]*$' - class fully qualified name which wrapped by the last paired brackets, see:
113+
* https://github.com/microsoft/vscode-java-test/issues/1075
113114
*/
114-
const regexp: RegExp = /\d+,(?:@AssumptionFailure: |@Ignore: )?(.*?)(?:\[\d+\])?\((.*?)\)/;
115+
const regexp: RegExp = /\d+,(?:@AssumptionFailure: |@Ignore: )?(.*?)(?:\[\d+\])?\(([^)]*)\)[^(]*$/;
115116
const matchResults: RegExpExecArray | null = regexp.exec(message);
116117
if (matchResults && matchResults.length === 3) {
117118
return `${this.projectName}@${matchResults[2]}#${matchResults[1]}`;

test/maven-junit4-suite/codelens.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,36 @@ suite('Code Lens Tests', function() {
154154
assert.ok(failedDetail!.duration !== undefined, 'Should have execution time');
155155
});
156156

157+
test("Can run parameterized with name tests", async function() {
158+
const document: TextDocument = await workspace.openTextDocument(Uris.JUNIT4_PARAMETERIZED_WITH_NAME_TEST);
159+
await window.showTextDocument(document);
160+
161+
const codeLensProvider: TestCodeLensProvider = new TestCodeLensProvider();
162+
const codeLens: CodeLens[] = await codeLensProvider.provideCodeLenses(document, Token.cancellationToken);
163+
assert.equal(codeLens.length, 4, 'Code Lens should appear for @Test annotation');
164+
165+
const command: Command | undefined = codeLens[0].command;
166+
assert.notEqual(command, undefined, 'Command inside Code Lens should not be undefined');
167+
assert.notEqual(command, null, 'Command inside Code Lens should not be null');
168+
169+
const testItem: ITestItem[] = command!.arguments as ITestItem[];
170+
assert.notEqual(testItem, undefined, 'Test Item inside Code Lens Command should not be undefined');
171+
assert.notEqual(testItem, null, 'Test Item inside Code Lens Command should not be null');
172+
assert.equal(testItem.length, 1, 'Test Item inside Code Lens Command should has one element');
173+
174+
await commands.executeCommand(command!.command, testItem[0]);
175+
176+
const projectName: string = testItem[0].project;
177+
const passedDetail: ITestResult| undefined = testResultManager.getResultById(`${projectName}@junit4.ParameterizedWithNameTest#test[0: expect=1]`);
178+
assert.equal(passedDetail!.status, TestStatus.Pass, 'Should have passed case');
179+
assert.ok(passedDetail!.duration !== undefined, 'Should have execution time');
180+
181+
const failedDetail: ITestResult| undefined = testResultManager.getResultById(`${projectName}@junit4.ParameterizedWithNameTest#test[3: expect=()]`);
182+
assert.equal(failedDetail!.status, TestStatus.Fail, 'Should have failed case');
183+
assert.ok(failedDetail!.duration !== undefined, 'Should have execution time');
184+
185+
});
186+
157187
test("Assume failure should mark as skipped", async function() {
158188
const document: TextDocument = await workspace.openTextDocument(Uris.JUNIT4_ASSUME_TEST);
159189
await window.showTextDocument(document);

test/shared.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ export namespace Uris {
2929
export const JUNIT4_RUNWITH: Uri = Uri.file(path.join(TEST_PROJECT_BASE_PATH, JUNIT4_TEST_PACKAGE, 'RunWithAnnotation.java'));
3030
export const JUNIT4_EXCEPTION_BEFORE: Uri = Uri.file(path.join(TEST_PROJECT_BASE_PATH, JUNIT4_TEST_PACKAGE, 'ExceptionInBefore.java'));
3131
export const JUNIT4_PARAMETERIZED_TEST: Uri = Uri.file(path.join(TEST_PROJECT_BASE_PATH, JUNIT4_TEST_PACKAGE, 'ParameterizedTest.java'));
32+
export const JUNIT4_PARAMETERIZED_WITH_NAME_TEST: Uri = Uri.file(path.join(TEST_PROJECT_BASE_PATH, JUNIT4_TEST_PACKAGE, 'ParameterizedWithNameTest.java'));
3233
export const JUNIT4_ASSUME_TEST: Uri = Uri.file(path.join(TEST_PROJECT_BASE_PATH, JUNIT4_TEST_PACKAGE, 'AssumeTest.java'));
3334

3435
// Gradle modular
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package junit4;
2+
3+
import static org.junit.Assert.assertEquals;
4+
5+
import java.util.Arrays;
6+
import java.util.Collection;
7+
8+
import org.junit.Test;
9+
import org.junit.runner.RunWith;
10+
import org.junit.runners.Parameterized;
11+
import org.junit.runners.Parameterized.Parameter;
12+
import org.junit.runners.Parameterized.Parameters;
13+
14+
@RunWith(Parameterized.class)
15+
public class ParameterizedWithNameTest {
16+
17+
@Parameter
18+
public int expected;
19+
20+
@Parameters(name = "{index}: expect={0}")
21+
public static Collection<Object> data() {
22+
// If using the name annotation param and one of the inputs has a rounded
23+
// bracket, e.g. @Parameters(name = "test({index})"), then the test name needs
24+
// to be properly handled.
25+
return Arrays.asList(1, 2, "normalString", "()", "(()");
26+
}
27+
28+
@Test
29+
public void test() {
30+
assertEquals(expected, 1);
31+
}
32+
}

0 commit comments

Comments
 (0)