Skip to content

Commit

Permalink
refactor: Remove the Test Level: NESTED_CLASS (#717)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdneo authored May 31, 2019
1 parent daad335 commit 2feae9c
Show file tree
Hide file tree
Showing 8 changed files with 13 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,5 @@ public enum TestLevel {
CLASS,

@SerializedName("4")
NESTED_CLASS,

@SerializedName("5")
METHOD;
}
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,6 @@ public TestItem parseTestItem(IMethod method) throws JavaModelException {

@Override
public TestItem parseTestItem(IType type) throws JavaModelException {
return TestItemUtils.constructTestItem(type, TestItemUtils.getTestLevelForIType(type), this.getTestKind());
return TestItemUtils.constructTestItem(type, TestLevel.CLASS, this.getTestKind());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
import com.microsoft.java.test.plugin.model.TestKind;
import com.microsoft.java.test.plugin.model.TestLevel;

import org.eclipse.jdt.core.ICompilationUnit;
import org.eclipse.jdt.core.IJavaElement;
import org.eclipse.jdt.core.IMethod;
import org.eclipse.jdt.core.IPackageFragment;
Expand Down Expand Up @@ -55,14 +54,6 @@ public static TestItem constructTestItem(IJavaElement element, TestLevel level,
return new TestItem(displayName, fullName, uri, projectName, Collections.emptyList(), range, level, kind);
}

public static TestLevel getTestLevelForIType(IType type) {
if (type.getParent() instanceof ICompilationUnit) {
return TestLevel.CLASS;
} else {
return TestLevel.NESTED_CLASS;
}
}

public static Range parseTestItemRange(IJavaElement element) throws JavaModelException {
if (element instanceof ISourceReference) {
final ISourceRange range = ((ISourceReference) element).getNameRange();
Expand All @@ -74,7 +65,6 @@ public static Range parseTestItemRange(IJavaElement element) throws JavaModelExc
private static String parseTestItemFullName(IJavaElement element, TestLevel level) {
switch (level) {
case CLASS:
case NESTED_CLASS:
final IType type = (IType) element;
return type.getFullyQualifiedName();
case METHOD:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static List<TestItem> searchCodeLens(List<Object> arguments, IProgressMon
}
}).filter(Objects::nonNull).collect(Collectors.toList());
if (testMethodList.size() > 0) {
final TestItem parent = TestItemUtils.constructTestItem(type, TestItemUtils.getTestLevelForIType(type));
final TestItem parent = TestItemUtils.constructTestItem(type, TestLevel.CLASS);
parent.setChildren(testMethodList);
// Assume the kinds of all methods are the same.
parent.setKind(testMethodList.get(0).getKind());
Expand All @@ -112,8 +112,7 @@ public static List<TestItem> searchCodeLens(List<Object> arguments, IProgressMon
}
// Class annotated by @RunWith should be considered as a Suite even it has no test method children
if (TestFrameworkUtils.hasAnnotation(type, JUnit4TestSearcher.RUN_WITH)) {
resultList.add(TestItemUtils.constructTestItem(type, TestItemUtils.getTestLevelForIType(type),
TestKind.JUnit));
resultList.add(TestItemUtils.constructTestItem(type, TestLevel.CLASS, TestKind.JUnit));
}
}

Expand Down Expand Up @@ -150,9 +149,6 @@ public static List<TestItem> searchTestItems(List<Object> arguments, IProgressMo
case CLASS:
searchInClass(resultList, params);
break;
case NESTED_CLASS:
searchInNestedClass(resultList, params);
break;
default:
break;
}
Expand Down Expand Up @@ -210,8 +206,7 @@ public void acceptSearchMatch(SearchMatch match) throws CoreException {
if (classItem != null) {
classItem.addChild(methodItem);
} else {
final TestItem newClassItem = TestItemUtils.constructTestItem(type,
TestItemUtils.getTestLevelForIType(type));
final TestItem newClassItem = TestItemUtils.constructTestItem(type, TestLevel.CLASS);
newClassItem.addChild(methodItem);
classMap.put(type.getFullyQualifiedName(), newClassItem);
}
Expand Down Expand Up @@ -315,7 +310,6 @@ private static IJavaSearchScope createSearchScope(SearchTestItemParams params)
return SearchEngine.createJavaSearchScope(new IJavaElement[] { packageElement },
IJavaSearchScope.SOURCES);
case CLASS:
case NESTED_CLASS:
final ICompilationUnit compilationUnit = JDTUtils.resolveCompilationUnit(params.getUri());
final IType[] types = compilationUnit.getAllTypes();
for (final IType type : types) {
Expand Down Expand Up @@ -395,10 +389,14 @@ private static IPackageFragment resolvePackage(String uriString, String fullName
private static void searchInClass(List<TestItem> resultList, SearchTestItemParams params)
throws JavaModelException {
final ICompilationUnit unit = JDTUtils.resolveCompilationUnit(params.getUri());
for (final IType type : unit.getTypes()) {
for (final IType type : unit.getAllTypes()) {
if (type.getFullyQualifiedName().equals(params.getFullName())) {
for (final IType innerType : type.getTypes()) {
resultList.add(TestItemUtils.constructTestItem(innerType, TestLevel.NESTED_CLASS));
resultList.add(TestItemUtils.constructTestItem(innerType, TestLevel.CLASS));
}

if (!isTestableClass(type)) {
continue;
}
for (final IMethod method : type.getMethods()) {
final TestItem item = TestFrameworkUtils.resoveTestItemForMethod(method);
Expand All @@ -410,27 +408,6 @@ private static void searchInClass(List<TestItem> resultList, SearchTestItemParam
}
}

private static void searchInNestedClass(List<TestItem> resultList, SearchTestItemParams params)
throws JavaModelException {
final ICompilationUnit compilationUnit = JDTUtils.resolveCompilationUnit(params.getUri());
for (final IType type : compilationUnit.getAllTypes()) {
if (type.getFullyQualifiedName().equals(params.getFullName())) {
resultList.addAll(searchTestMethodsOfType(type));
}
}
}

private static List<TestItem> searchTestMethodsOfType(IType type) throws JavaModelException {
final List<TestItem> results = new LinkedList<>();
for (final IMethod method : type.getMethods()) {
final TestItem item = TestFrameworkUtils.resoveTestItemForMethod(method);
if (item != null) {
results.add(item);
}
}
return results;
}

private static boolean isTestableClass(IType type) throws JavaModelException {
final int flags = type.getFlags();
if (Flags.isInterface(flags) || Flags.isAbstract(flags)) {
Expand Down
3 changes: 1 addition & 2 deletions src/codeLensProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ class TestCodeLensProvider implements CodeLensProvider {
}
break;
case TestLevel.Class:
case TestLevel.NestedClass:
if (!test.children) {
break;
}
Expand Down Expand Up @@ -147,7 +146,7 @@ class TestCodeLensProvider implements CodeLensProvider {
if (item.level === TestLevel.Method) {
return 1;
}
if (item.level === TestLevel.Class || item.level === TestLevel.NestedClass) {
if (item.level === TestLevel.Class) {
if (item.children) {
return item.children.length;
}
Expand Down
1 change: 0 additions & 1 deletion src/explorer/testExplorer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ export class TestExplorer implements TreeDataProvider<TestTreeNode> {
light: this._context.asAbsolutePath(path.join('resources', 'media', 'light', 'method.svg')),
};
case TestLevel.Class:
case TestLevel.NestedClass:
return {
dark: this._context.asAbsolutePath(path.join('resources', 'media', 'dark', 'class.svg')),
light: this._context.asAbsolutePath(path.join('resources', 'media', 'light', 'class.svg')),
Expand Down
3 changes: 1 addition & 2 deletions src/protocols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ export enum TestLevel {
Folder = 1,
Package = 2,
Class = 3,
NestedClass = 4,
Method = 5,
Method = 4,
}

export enum TestKind {
Expand Down
2 changes: 1 addition & 1 deletion src/runners/junit5Runner/JUnit5Runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class JUnit5Runner extends BaseRunner {
private constructParamsForTests(): string[] {
const params: string[] = [];
for (const test of this.tests) {
if (test.level === TestLevel.Class || test.level === TestLevel.NestedClass) {
if (test.level === TestLevel.Class) {
params.push('-c', test.fullName);
} else if (test.level === TestLevel.Method) {
params.push('-m', `${test.fullName}(${test.paramTypes.join(',')})`);
Expand Down

0 comments on commit 2feae9c

Please sign in to comment.