RUBY-21693 Added test for find-usage view tree

This commit is contained in:
Alexey Merkulov
2018-08-01 13:47:28 +03:00
parent f7e4477653
commit bd82114426
3 changed files with 149 additions and 2 deletions
@@ -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) {
@@ -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<PsiReference> referenceList = getReferencesInFile(file);
Collection<PsiElement> 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<PsiReference> getReferencesInFile(@NotNull PsiFile file) {
final List<PsiReference> 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();
}
}
@@ -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