diff --git a/platform/testFramework/src/com/intellij/testFramework/TreeTester.java b/platform/testFramework/src/com/intellij/testFramework/TreeTester.java index 53184eed5c3f..1812ee770ed3 100644 --- a/platform/testFramework/src/com/intellij/testFramework/TreeTester.java +++ b/platform/testFramework/src/com/intellij/testFramework/TreeTester.java @@ -16,6 +16,7 @@ package com.intellij.testFramework; import com.intellij.openapi.util.text.StringUtil; +import org.jetbrains.annotations.NotNull; import org.junit.Assert; import javax.swing.*; @@ -49,10 +50,15 @@ public class TreeTester { return this; } - public void assertStructureEquals(String expected) { + @NotNull + public String constructTextRepresentation() { StringBuilder buffer = new StringBuilder(); printSubTree(myNode, 0, buffer); - Assert.assertEquals(expected, buffer.toString()); + return buffer.toString(); + } + + public void assertStructureEquals(String expected) { + Assert.assertEquals(expected, constructTextRepresentation()); } private void printSubTree(TreeNode node, int level, StringBuilder result) { diff --git a/plugins/yaml/testSrc/org/jetbrains/yaml/resolve/YAMLUsageViewTreeTest.java b/plugins/yaml/testSrc/org/jetbrains/yaml/resolve/YAMLUsageViewTreeTest.java new file mode 100644 index 000000000000..501b183cfb03 --- /dev/null +++ b/plugins/yaml/testSrc/org/jetbrains/yaml/resolve/YAMLUsageViewTreeTest.java @@ -0,0 +1,102 @@ +// Copyright 2000-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file. +package org.jetbrains.yaml.resolve; + +import com.intellij.find.FindManager; +import com.intellij.find.findUsages.FindUsagesHandler; +import com.intellij.find.findUsages.FindUsagesManager; +import com.intellij.find.impl.FindManagerImpl; +import com.intellij.openapi.application.ex.PathManagerEx; +import com.intellij.openapi.editor.Editor; +import com.intellij.psi.PsiElement; +import com.intellij.psi.PsiElementVisitor; +import com.intellij.psi.PsiFile; +import com.intellij.psi.PsiReference; +import com.intellij.testFramework.TreeTester; +import com.intellij.testFramework.fixtures.LightPlatformCodeInsightFixtureTestCase; +import com.intellij.usages.impl.UsageViewImpl; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.yaml.YAMLTextUtil; + +import java.util.*; +import java.util.stream.Collectors; + +public class YAMLUsageViewTreeTest extends LightPlatformCodeInsightFixtureTestCase { + @Override + protected String getTestDataPath() { + return PathManagerEx.getCommunityHomePath() + "/plugins/yaml/testSrc/org/jetbrains/yaml/resolve/data/"; + } + + public void testAnchorsAndAliases() { + doTest(); + } + + private void doTest() { + String testName = getTestName(true); + myFixture.configureByFile(testName + ".yml"); + String answerFilePath = getTestDataPath() + testName + ".usage.txt"; + String result = getSerializedResolveResults(); + assertSameLinesWithFile(answerFilePath, result); + } + + @NotNull + private String getSerializedResolveResults() { + PsiFile file = myFixture.getFile(); + List referenceList = getReferencesInFile(file); + Collection elements = referenceList.stream() + .map(ref -> ref.resolve()) + .filter(e -> e != null) + .collect(Collectors.toCollection(() -> new LinkedHashSet<>())); + Editor editor = myFixture.getEditor(); + StringBuilder sb = new StringBuilder(); + + for (PsiElement element : elements) { + String usage = constructUsageViewStructureText(element); + + sb + .append("- element: '") + .append(element.getText()) + .append("'\n") + .append(" position: '") + .append(editor.offsetToLogicalPosition(element.getTextOffset())) + .append("'\n") + .append(" view: |") + .append('\n') + .append(YAMLTextUtil.indentText(usage, 4)) + .append('\n') + ; + } + return sb.toString(); + } + + @NotNull + private static List getReferencesInFile(@NotNull PsiFile file) { + final List referencesList = new ArrayList<>(); + + file.accept(new PsiElementVisitor() { + @Override + public void visitElement(PsiElement element) { + referencesList.addAll(Arrays.asList(element.getReferences())); + element.acceptChildren(this); + } + }); + + Collections.sort(referencesList, + Comparator.comparingInt(o -> o.getElement().getTextRange().getStartOffset())); + + return referencesList; + } + + private String constructUsageViewStructureText(PsiElement element) { + assertNotNull(element); + FindUsagesManager usagesManager = ((FindManagerImpl)FindManager.getInstance(getProject())).getFindUsagesManager(); + FindUsagesHandler handler = usagesManager.getFindUsagesHandler(element, false); + assertNotNull(handler); + UsageViewImpl usageView = (UsageViewImpl)usagesManager.doFindUsages(new PsiElement[]{element}, + PsiElement.EMPTY_ARRAY, + handler, + handler.getFindUsagesOptions(), + false); + usageView.expandAll(); + return TreeTester.forNode(usageView.getRoot()).withPresenter(usageView::getNodeText).constructTextRepresentation(); + } +} diff --git a/plugins/yaml/testSrc/org/jetbrains/yaml/resolve/data/anchorsAndAliases.usage.txt b/plugins/yaml/testSrc/org/jetbrains/yaml/resolve/data/anchorsAndAliases.usage.txt new file mode 100644 index 000000000000..eb3c94c760c0 --- /dev/null +++ b/plugins/yaml/testSrc/org/jetbrains/yaml/resolve/data/anchorsAndAliases.usage.txt @@ -0,0 +1,39 @@ +- element: '&xxx' + position: 'LogicalPosition: (1, 21)' + view: | + Usage (2 usages) + Anchor + xxx + Found usages (2 usages) + Value read (2 usages) + light_idea_test_case (2 usages) + (2 usages) + anchorsAndAliases.yml (2 usages) + 4Second occurrence: *xxx + 5Thrid: *xxx + +- element: '&xxx' + position: 'LogicalPosition: (5, 20)' + view: | + Usage (1 usage) + Anchor + xxx + Found usages (1 usage) + Value read (1 usage) + light_idea_test_case (1 usage) + (1 usage) + anchorsAndAliases.yml (1 usage) + 7Reuse anchor: *xxx + +- element: '&yyy' + position: 'LogicalPosition: (2, 9)' + view: | + Usage (1 usage) + Anchor + yyy + Found usages (1 usage) + Value read (1 usage) + light_idea_test_case (1 usage) + (1 usage) + anchorsAndAliases.yml (1 usage) + 8Simple: *yyy \ No newline at end of file