mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-05-06 05:10:22 +07:00
refactor HierarchyTester to allow node descriptor comparator to test the children order
GitOrigin-RevId: 0e307b174a68169e37d66e8ef4432fcdaab0f02d
This commit is contained in:
committed by
intellij-monorepo-bot
parent
92958fbc4e
commit
dcd374eb82
@@ -3,6 +3,7 @@ package com.intellij.java.ide.hierarchy;
|
||||
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.ide.hierarchy.HierarchyBrowserBaseEx;
|
||||
import com.intellij.ide.hierarchy.JavaHierarchyUtil;
|
||||
import com.intellij.ide.hierarchy.actions.BrowseTypeHierarchyAction;
|
||||
import com.intellij.ide.hierarchy.call.CalleeMethodsTreeStructure;
|
||||
import com.intellij.ide.hierarchy.call.CallerMethodsTreeStructure;
|
||||
@@ -47,14 +48,14 @@ public class JavaCallHierarchyTest extends HierarchyViewTestBase {
|
||||
assertNotNull("Method '" + methodName + "' not found in " + classFqn + ". Available methods are " +
|
||||
Arrays.toString(psiClass.getMethods()), method);
|
||||
return new CallerMethodsTreeStructure(getProject(), method, HierarchyBrowserBaseEx.SCOPE_PROJECT);
|
||||
}, fileNames);
|
||||
}, JavaHierarchyUtil.getComparator(myProject), fileNames);
|
||||
}
|
||||
private void doJavaCalleeTypeHierarchyTest(@NotNull String classFqn, @NotNull String methodName, String @NotNull ... fileNames) throws Exception {
|
||||
doHierarchyTest(() -> {
|
||||
PsiClass psiClass = JavaPsiFacade.getInstance(getProject()).findClass(classFqn, ProjectScope.getProjectScope(getProject()));
|
||||
PsiMember method = psiClass.findMethodsByName(methodName, false) [0];
|
||||
return new CalleeMethodsTreeStructure(getProject(), method, HierarchyBrowserBaseEx.SCOPE_PROJECT);
|
||||
}, fileNames);
|
||||
}, JavaHierarchyUtil.getComparator(myProject),fileNames);
|
||||
}
|
||||
|
||||
public void testDirectRecursion() throws Exception {
|
||||
@@ -98,7 +99,7 @@ public class JavaCallHierarchyTest extends HierarchyViewTestBase {
|
||||
PsiClass psiClass = JavaPsiFacade.getInstance(getProject()).findClass("A", ProjectScope.getProjectScope(getProject()));
|
||||
PsiMember method = psiClass.findMethodsByName("testMethod", false) [0];
|
||||
return new CalleeMethodsTreeStructure(getProject(), method, HierarchyBrowserBaseEx.SCOPE_PROJECT);
|
||||
}, "A.java");
|
||||
}, JavaHierarchyUtil.getComparator(myProject),"A.java");
|
||||
}
|
||||
|
||||
public void testField() throws Exception {
|
||||
@@ -106,7 +107,7 @@ public class JavaCallHierarchyTest extends HierarchyViewTestBase {
|
||||
PsiClass psiClass = JavaPsiFacade.getInstance(getProject()).findClass("A", ProjectScope.getProjectScope(getProject()));
|
||||
PsiField field = psiClass.findFieldByName("testField", false);
|
||||
return new CallerMethodsTreeStructure(getProject(), field, HierarchyBrowserBaseEx.SCOPE_PROJECT);
|
||||
}, "A.java");
|
||||
}, JavaHierarchyUtil.getComparator(myProject),"A.java");
|
||||
}
|
||||
|
||||
public void testAnonymous2() throws Exception {
|
||||
|
||||
@@ -18,6 +18,7 @@ package com.intellij.java.ide.hierarchy;
|
||||
import com.intellij.JavaTestUtil;
|
||||
import com.intellij.ide.hierarchy.HierarchyBrowserBaseEx;
|
||||
import com.intellij.ide.hierarchy.HierarchyBrowserManager;
|
||||
import com.intellij.ide.hierarchy.JavaHierarchyUtil;
|
||||
import com.intellij.ide.hierarchy.method.MethodHierarchyTreeStructure;
|
||||
import com.intellij.openapi.projectRoots.Sdk;
|
||||
import com.intellij.pom.java.LanguageLevel;
|
||||
@@ -91,7 +92,7 @@ public class JavaMethodHierarchyTest extends HierarchyViewTestBase {
|
||||
final PsiClass psiClass = JavaPsiFacade.getInstance(getProject()).findClass(classFqn, ProjectScope.getProjectScope(getProject()));
|
||||
final PsiMethod method = psiClass.findMethodsByName(methodName, false) [0];
|
||||
return new MethodHierarchyTreeStructure(getProject(), method, HierarchyBrowserBaseEx.SCOPE_PROJECT);
|
||||
}, fileNames);
|
||||
}, JavaHierarchyUtil.getComparator(myProject), fileNames);
|
||||
}
|
||||
|
||||
private void doTestHideIrrelevantClasses(String classFqn, String methodName, String... fileNames) throws Exception {
|
||||
|
||||
@@ -17,15 +17,18 @@ package com.intellij.testFramework.codeInsight.hierarchy;
|
||||
|
||||
import com.intellij.codeInsight.daemon.DaemonAnalyzerTestCase;
|
||||
import com.intellij.ide.hierarchy.HierarchyTreeStructure;
|
||||
import com.intellij.ide.util.treeView.NodeDescriptor;
|
||||
import com.intellij.openapi.application.PathManager;
|
||||
import com.intellij.openapi.util.Computable;
|
||||
import com.intellij.openapi.vfs.newvfs.impl.VfsRootAccess;
|
||||
import com.intellij.testFramework.ExpectedHighlightingData;
|
||||
import groovy.lang.GroovyObject;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.util.Comparator;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
/**
|
||||
* Checks tree structure for Type Hierarchy (Ctrl+H), Call Hierarchy (Ctrl+Alt+H), Method Hierarchy (Ctrl+Shift+H).
|
||||
@@ -43,11 +46,12 @@ public abstract class HierarchyViewTestBase extends DaemonAnalyzerTestCase {
|
||||
|
||||
protected abstract String getBasePath();
|
||||
|
||||
protected void doHierarchyTest(@NotNull Computable<? extends HierarchyTreeStructure> treeStructureComputable,
|
||||
protected void doHierarchyTest(@NotNull Supplier<? extends HierarchyTreeStructure> treeStructure,
|
||||
@Nullable Comparator<? super NodeDescriptor<?>> comparator,
|
||||
String @NotNull ... fileNames) throws IOException {
|
||||
configure(fileNames);
|
||||
String verificationFilePath = getTestDataPath() + "/" + getBasePath() + "/verification.xml";
|
||||
HierarchyViewTestFixture.doHierarchyTest(treeStructureComputable.compute(), new File(verificationFilePath));
|
||||
HierarchyViewTestFixture.doHierarchyTest(treeStructure.get(), comparator, new File(verificationFilePath));
|
||||
}
|
||||
|
||||
private void configure(String @NotNull [] fileNames) {
|
||||
|
||||
@@ -3,6 +3,7 @@ package com.intellij.testFramework.codeInsight.hierarchy;
|
||||
|
||||
import com.intellij.ide.hierarchy.HierarchyNodeDescriptor;
|
||||
import com.intellij.ide.hierarchy.HierarchyTreeStructure;
|
||||
import com.intellij.ide.util.treeView.NodeDescriptor;
|
||||
import com.intellij.openapi.util.JDOMUtil;
|
||||
import com.intellij.openapi.util.io.FileUtil;
|
||||
import com.intellij.rt.execution.junit.FileComparisonFailure;
|
||||
@@ -31,7 +32,7 @@ public final class HierarchyViewTestFixture {
|
||||
*/
|
||||
public static void doHierarchyTest(@NotNull HierarchyTreeStructure treeStructure,
|
||||
@NotNull String expectedStructure) {
|
||||
doHierarchyTest(treeStructure, expectedStructure, null);
|
||||
doHierarchyTest(treeStructure, expectedStructure, null, null);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -43,18 +44,24 @@ public final class HierarchyViewTestFixture {
|
||||
*/
|
||||
public static void doHierarchyTest(@NotNull HierarchyTreeStructure treeStructure,
|
||||
@NotNull File expectedFile) throws IOException {
|
||||
doHierarchyTest(treeStructure, FileUtil.loadFile(expectedFile), expectedFile);
|
||||
doHierarchyTest(treeStructure, null, expectedFile);
|
||||
}
|
||||
public static void doHierarchyTest(@NotNull HierarchyTreeStructure treeStructure,
|
||||
@Nullable Comparator<? super NodeDescriptor<?>> comparator,
|
||||
@NotNull File expectedFile) throws IOException {
|
||||
doHierarchyTest(treeStructure, FileUtil.loadFile(expectedFile), comparator, expectedFile);
|
||||
}
|
||||
|
||||
private static void doHierarchyTest(@NotNull HierarchyTreeStructure treeStructure,
|
||||
@NotNull String expectedStructure,
|
||||
@Nullable Comparator<? super NodeDescriptor<?>> comparator,
|
||||
@Nullable File expectedFile) {
|
||||
Element element;
|
||||
try {
|
||||
element = JDOMUtil.load(expectedStructure);
|
||||
}
|
||||
catch (Throwable e) {
|
||||
String actual = dump(treeStructure, null, 0);
|
||||
String actual = dump(treeStructure, null, comparator, 0);
|
||||
if (!expectedStructure.equals(actual)) {
|
||||
throw new FileComparisonFailure("XML structure comparison for your convenience, actual failure details BELOW",
|
||||
expectedStructure, actual,
|
||||
@@ -62,20 +69,22 @@ public final class HierarchyViewTestFixture {
|
||||
}
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
checkHierarchyTreeStructure(treeStructure, element);
|
||||
checkHierarchyTreeStructure(treeStructure, element, comparator);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static String dump(@NotNull HierarchyTreeStructure treeStructure,
|
||||
@Nullable HierarchyNodeDescriptor descriptor,
|
||||
@Nullable Comparator<? super NodeDescriptor<?>> comparator,
|
||||
int level) {
|
||||
StringBuilder s = new StringBuilder();
|
||||
dump(treeStructure, descriptor, level, s);
|
||||
dump(treeStructure, descriptor, comparator,level, s);
|
||||
return s.toString();
|
||||
}
|
||||
|
||||
private static void dump(@NotNull HierarchyTreeStructure treeStructure,
|
||||
@Nullable HierarchyNodeDescriptor descriptor,
|
||||
@Nullable Comparator<? super NodeDescriptor<?>> comparator,
|
||||
int level,
|
||||
@NotNull StringBuilder b) {
|
||||
if (level > 10) {
|
||||
@@ -89,12 +98,12 @@ public final class HierarchyViewTestFixture {
|
||||
b.append("<node text=\"").append(descriptor.getHighlightedText().getText()).append("\"")
|
||||
.append(treeStructure.getBaseDescriptor() == descriptor ? " base=\"true\"" : "");
|
||||
|
||||
Object[] children = treeStructure.getChildElements(descriptor);
|
||||
Object[] children = getSortedChildren(treeStructure, descriptor, comparator);
|
||||
if (children.length > 0) {
|
||||
b.append(">\n");
|
||||
for (Object o : children) {
|
||||
HierarchyNodeDescriptor d = (HierarchyNodeDescriptor)o;
|
||||
dump(treeStructure, d, level + 1, b);
|
||||
dump(treeStructure, d, comparator, level + 1, b);
|
||||
}
|
||||
b.append(" ".repeat(level));
|
||||
b.append("</node>\n");
|
||||
@@ -104,21 +113,34 @@ public final class HierarchyViewTestFixture {
|
||||
}
|
||||
}
|
||||
|
||||
private static void checkHierarchyTreeStructure(@NotNull HierarchyTreeStructure treeStructure, @Nullable Element rootElement) {
|
||||
@NotNull
|
||||
private static Object @NotNull [] getSortedChildren(@NotNull HierarchyTreeStructure treeStructure,
|
||||
@NotNull HierarchyNodeDescriptor descriptor,
|
||||
@Nullable Comparator<? super NodeDescriptor<?>> comparator) {
|
||||
Object[] children = treeStructure.getChildElements(descriptor);
|
||||
if (comparator == null) comparator = Comparator.comparingInt(NodeDescriptor::getIndex);
|
||||
Arrays.sort(children, (Comparator)comparator);
|
||||
return children;
|
||||
}
|
||||
|
||||
private static void checkHierarchyTreeStructure(@NotNull HierarchyTreeStructure treeStructure,
|
||||
@Nullable Element rootElement,
|
||||
@Nullable Comparator<? super NodeDescriptor<?>> comparator) {
|
||||
HierarchyNodeDescriptor rootNodeDescriptor = (HierarchyNodeDescriptor)treeStructure.getRootElement();
|
||||
rootNodeDescriptor.update();
|
||||
if (rootElement == null || !NODE_ELEMENT_NAME.equals(rootElement.getName())) {
|
||||
throw new IllegalArgumentException("Incorrect root element in verification resource");
|
||||
}
|
||||
checkNodeDescriptorRecursively(treeStructure, rootNodeDescriptor, rootElement);
|
||||
checkNodeDescriptorRecursively(treeStructure, rootNodeDescriptor, rootElement, comparator);
|
||||
}
|
||||
|
||||
private static void checkNodeDescriptorRecursively(@NotNull HierarchyTreeStructure treeStructure,
|
||||
@NotNull HierarchyNodeDescriptor descriptor,
|
||||
@NotNull Element expectedElement) {
|
||||
@NotNull Element expectedElement,
|
||||
@Nullable Comparator<? super NodeDescriptor<?>> comparator) {
|
||||
checkBaseNode(treeStructure, descriptor, expectedElement);
|
||||
checkContent(descriptor, expectedElement);
|
||||
checkChildren(treeStructure, descriptor, expectedElement);
|
||||
checkChildren(treeStructure, descriptor, expectedElement, comparator);
|
||||
}
|
||||
|
||||
private static void checkBaseNode(@NotNull HierarchyTreeStructure treeStructure,
|
||||
@@ -131,17 +153,19 @@ public final class HierarchyViewTestFixture {
|
||||
}
|
||||
|
||||
private static void checkContent(@NotNull HierarchyNodeDescriptor descriptor, @NotNull Element expectedElement) {
|
||||
assertEquals("parent: "+descriptor.getParentDescriptor(), expectedElement.getAttributeValue(TEXT_ATTR_NAME), descriptor.getHighlightedText().getText());
|
||||
assertEquals("parent: " + descriptor.getParentDescriptor(), expectedElement.getAttributeValue(TEXT_ATTR_NAME),
|
||||
descriptor.getHighlightedText().getText());
|
||||
}
|
||||
|
||||
private static void checkChildren(@NotNull HierarchyTreeStructure treeStructure,
|
||||
@NotNull HierarchyNodeDescriptor descriptor,
|
||||
@NotNull Element element) {
|
||||
@NotNull Element element,
|
||||
@Nullable Comparator<? super NodeDescriptor<?>> comparator) {
|
||||
if (element.getChild(ANY_NODES_ELEMENT_NAME) != null) {
|
||||
return;
|
||||
}
|
||||
|
||||
Object[] children = treeStructure.getChildElements(descriptor);
|
||||
Object[] children = getSortedChildren(treeStructure, descriptor, comparator);
|
||||
List<Element> expectedChildren = new ArrayList<>(element.getChildren(NODE_ELEMENT_NAME));
|
||||
|
||||
StringBuilder messageBuilder = new StringBuilder("Actual children of [" + descriptor.getHighlightedText().getText() + "]:\n");
|
||||
@@ -158,7 +182,7 @@ public final class HierarchyViewTestFixture {
|
||||
|
||||
Iterator<Element> iterator = expectedChildren.iterator();
|
||||
for (Object child : children) {
|
||||
checkNodeDescriptorRecursively(treeStructure, (HierarchyNodeDescriptor)child, iterator.next());
|
||||
checkNodeDescriptorRecursively(treeStructure, (HierarchyNodeDescriptor)child, iterator.next(), comparator);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -60,10 +60,10 @@ public class PyCallHierarchyTest extends PyTestCase {
|
||||
private void checkHierarchyTreeStructure(PyFunction function) {
|
||||
final PyCallerFunctionTreeStructure callerStructure = new PyCallerFunctionTreeStructure(myFixture.getProject(), function,
|
||||
HierarchyBrowserBaseEx.SCOPE_PROJECT);
|
||||
assertSameLinesWithFile(getVerificationCallerFilePath(), HierarchyViewTestFixture.dump(callerStructure, null, 0));
|
||||
assertSameLinesWithFile(getVerificationCallerFilePath(), HierarchyViewTestFixture.dump(callerStructure, null, null,0));
|
||||
final PyCalleeFunctionTreeStructure calleeStructure = new PyCalleeFunctionTreeStructure(myFixture.getProject(), function,
|
||||
HierarchyBrowserBaseEx.SCOPE_PROJECT);
|
||||
assertSameLinesWithFile(getVerificationCalleeFilePath(), HierarchyViewTestFixture.dump(calleeStructure, null, 0));
|
||||
assertSameLinesWithFile(getVerificationCalleeFilePath(), HierarchyViewTestFixture.dump(calleeStructure, null, null,0));
|
||||
}
|
||||
|
||||
private void doTestCallHierarchy(String ... fileNames) {
|
||||
|
||||
Reference in New Issue
Block a user