fix: Find the location of inherited test methods (#1375)

- When append test messages, keep finding the test method from current
  type to all of its supertype.

Signed-off-by: sheche <sheche@microsoft.com>
This commit is contained in:
Sheng Chen 2022-02-22 16:45:48 +08:00 коммит произвёл GitHub
Родитель ff9e8ba4ab
Коммит ae11aac824
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 52 добавлений и 6 удалений

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

@ -33,6 +33,7 @@ import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragment;
import org.eclipse.jdt.core.IPackageFragmentRoot;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.ITypeHierarchy;
import org.eclipse.jdt.core.JavaCore;
import org.eclipse.jdt.core.JavaModelException;
import org.eclipse.jdt.core.dom.AST;
@ -499,17 +500,35 @@ public class TestSearchUtils {
return new Location(JDTUtils.getFileURI(type.getResource()), TestItemUtils.parseTestItemRange(type));
}
for (final IMethod method : type.getMethods()) {
if (methodName.equals(method.getElementName())) {
// TODO: handle the override method
IMethod method = findMethod(type, methodName);
if (method != null) {
return new Location(JDTUtils.getFileURI(method.getResource()),
TestItemUtils.parseTestItemRange(method));
}
final ITypeHierarchy typeHierarchy = type.newSupertypeHierarchy(null);
final IType[] supertypes = typeHierarchy.getAllSupertypes(type);
for (final IType supertype : supertypes) {
method = findMethod(supertype, methodName);
if (method != null) {
return new Location(JDTUtils.getFileURI(method.getResource()),
TestItemUtils.parseTestItemRange(method));
TestItemUtils.parseTestItemRange(method));
}
}
return null;
}
protected static final IMethod findMethod(IType type, String methodName) throws JavaModelException {
for (final IMethod method : type.getMethods()) {
if (methodName.equals(method.getElementName())) {
// TODO: handle the override method
return method;
}
}
return null;
}
protected static final IType findType(final IJavaProject project, String className, IProgressMonitor monitor) {
final IType[] result = { null };
final String dottedName = className.replace('$', '.'); // for nested classes...

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

@ -285,7 +285,10 @@ export class JUnitRunnerResultAnalyzer extends RunnerResultAnalyzer {
if (this.testContext.kind === TestKind.JUnit) {
// change test[0] -> test
// to fix: https://github.com/microsoft/vscode-java-test/issues/1296
id = id.substring(0, id.lastIndexOf('['));
const indexOfParameterizedTest: number = id.lastIndexOf('[');
if (indexOfParameterizedTest > -1) {
id = id.substring(0, id.lastIndexOf('['));
}
}
}
const location: Location | undefined = await findTestLocation(id);

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

@ -5,9 +5,10 @@
import * as assert from 'assert';
import * as path from 'path';
import { Uri, window } from 'vscode';
import { Location, Uri, window } from 'vscode';
import { ITestNavigationResult } from '../../src/commands/navigation/navigationCommands';
import { JavaTestRunnerDelegateCommands } from '../../src/constants';
import { findTestLocation } from '../../src/runners/utils';
import { executeJavaLanguageServerCommand } from '../../src/utils/commandUtils';
import { setupTestEnv } from "./utils";
@ -41,4 +42,9 @@ suite('Test Navigation Tests', () => {
assert.strictEqual(searchResult?.items[0].simpleName, 'App');
assert.strictEqual(searchResult?.items[0].fullyQualifiedName, 'junit.App');
});
test('test find inherited test method location', async () => {
const location: Location | undefined = await findTestLocation("junit@junit4.ExtendedTest#test");
assert.ok(location?.uri.fsPath.endsWith("BaseTest.java"));
});
});

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

@ -0,0 +1,9 @@
package junit4;
import org.junit.Test;
public class BaseTest {
@Test
public void test() {
}
}

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

@ -0,0 +1,9 @@
package junit4;
import org.junit.Test;
public class ExtendedTest extends BaseTest {
@Test
public void extendedTest() {
}
}