IDEA-240930 refactored braces highlighting tests to be more friendly

GitOrigin-RevId: d7d758b0c574aef1918591931ace1fa2239eb1fa
This commit is contained in:
Alexandr Evstigneev
2020-05-15 07:34:19 +00:00
committed by intellij-monorepo-bot
parent e3c6c152b4
commit e986bf3265
65 changed files with 209 additions and 83 deletions
@@ -1,3 +1,3 @@
class Foo <pair>{<caret>
class Foo {<caret>
int field;
<pair>}
}
@@ -0,0 +1,3 @@
class Foo <brace>{<caret></brace>
int field;
<brace>}</brace>
@@ -1,2 +1,2 @@
class Foo <pair>{<caret>int field;
<pair>}
class Foo {<caret>int field;
}
@@ -0,0 +1,2 @@
class Foo <brace>{<caret></brace>int field;
<brace>}</brace>
@@ -1,3 +1,3 @@
class Foo <pair>{
class Foo {
int field;
<pair>}<caret>
}<caret>
@@ -0,0 +1,3 @@
class Foo <brace>{</brace>
int field;
<brace>}<caret></brace>
@@ -1,3 +1,3 @@
class Foo <caret>{
int field;
<pair>}
}
@@ -0,0 +1,3 @@
class Foo <caret><brace>{</brace>
int field;
<brace>}</brace>
@@ -1,3 +1,3 @@
class Foo <pair>{
class Foo {
int field;
<caret>}
@@ -0,0 +1,3 @@
class Foo <brace>{</brace>
int field;
<caret><brace>}</brace>
@@ -1,5 +1,5 @@
class Foo {
void foo() {
call(<pair>{<caret>});
call({<caret>});
}
}
@@ -0,0 +1,5 @@
class Foo {
void foo() {
call(<brace>{<caret></brace><brace>}</brace>);
}
}
@@ -1,5 +1,5 @@
class Foo {
void foo() {
call<caret>({}<pair>);
call<caret>({});
}
}
@@ -0,0 +1,5 @@
class Foo {
void foo() {
call<caret><brace>(</brace>{}<brace>)</brace>;
}
}
@@ -1,5 +1,5 @@
class Foo {
void foo() {
call(<caret>{<pair>});
call(<caret>{});
}
}
@@ -0,0 +1,5 @@
class Foo {
void foo() {
call(<caret><brace>{</brace><brace>}</brace>);
}
}
@@ -0,0 +1,3 @@
class Foo {
<caret>int field;
}
@@ -1,2 +1,2 @@
class Foo <pair>{ <caret>
<pair>}
class Foo { <caret>
}
@@ -0,0 +1,2 @@
class Foo <brace>{</brace> <caret>
<brace>}</brace>
@@ -1 +1 @@
<pair>{{{{{{{{{}}}}}}}}<pair>}<caret>
{{{{{{{{{}}}}}}}}}<caret>
@@ -0,0 +1 @@
<brace>{</brace>{{{{{{{{}}}}}}}}<brace>}<caret></brace>
@@ -1 +1 @@
<caret>{{{{{{{{{}}}}}}}}<pair>}
<caret>{{{{{{{{{}}}}}}}}}
@@ -0,0 +1 @@
<caret><brace>{</brace>{{{{{{{{}}}}}}}}<brace>}</brace>
@@ -1 +1 @@
{<pair>{{{{{{{{}}}}}}}<pair>}<caret>}
{{{{{{{{{}}}}}}}}<caret>}
@@ -0,0 +1 @@
{<brace>{</brace>{{{{{{{}}}}}}}<brace>}<caret></brace>}
@@ -1 +1 @@
{<caret>{{{{{{{{}}}}}}}<pair>}}
{<caret>{{{{{{{{}}}}}}}}}
@@ -0,0 +1 @@
{<caret><brace>{</brace>{{{{{{{}}}}}}}<brace>}</brace>}
@@ -1 +1 @@
{{{{{{{{<caret>{<pair>}}}}}}}}}
{{{{{{{{<caret>{}}}}}}}}}
@@ -0,0 +1 @@
{{{{{{{{<caret><brace>{</brace><brace>}</brace>}}}}}}}}
@@ -1 +1 @@
{{{{{{{{<pair>{<caret>}}}}}}}}}
{{{{{{{{{<caret>}}}}}}}}}
@@ -0,0 +1 @@
{{{{{{{{<brace>{<caret></brace><brace>}</brace>}}}}}}}}
@@ -1,3 +1,3 @@
class Foo <pair>{<caret>
class Foo {<caret>
int field;
<pair>}
}
@@ -0,0 +1,3 @@
class Foo <brace>{<caret></brace>
int field;
<brace>}</brace>
@@ -1,2 +1,2 @@
class Foo <pair>{<caret>int field;
<pair>}
class Foo {<caret>int field;
}
@@ -0,0 +1,2 @@
class Foo <brace>{<caret></brace>int field;
<brace>}</brace>
@@ -1,3 +1,3 @@
class Foo <pair>{
class Foo {
int field;
<pair>}<caret>
}<caret>
@@ -0,0 +1,3 @@
class Foo <brace>{</brace>
int field;
<brace>}<caret></brace>
@@ -1,3 +1,3 @@
class Foo <caret>{
int field;
<pair>}
}
@@ -0,0 +1,3 @@
class Foo <caret><brace>{</brace>
int field;
<brace>}</brace>
@@ -1,3 +1,3 @@
class Foo <pair>{
class Foo {
int field;
<caret>}
@@ -0,0 +1,3 @@
class Foo <brace>{</brace>
int field;
<caret><brace>}</brace>
@@ -1,5 +1,5 @@
class Foo {
void foo() {
call(<pair>{<caret>});
call({<caret>});
}
}
@@ -0,0 +1,5 @@
class Foo {
void foo() {
call(<brace>{<caret></brace><brace>}</brace>);
}
}
@@ -1,5 +1,5 @@
class Foo {
void foo() {
call<caret>({}<pair>);
call<caret>({});
}
}
@@ -0,0 +1,5 @@
class Foo {
void foo() {
call<caret><brace>(</brace>{}<brace>)</brace>;
}
}
@@ -1,5 +1,5 @@
class Foo {
void foo() {
call(<caret>{<pair>});
call(<caret>{});
}
}
@@ -0,0 +1,5 @@
class Foo {
void foo() {
call(<caret><brace>{</brace><brace>}</brace>);
}
}
@@ -0,0 +1,3 @@
class Foo {
<caret>int field;
}
@@ -1,2 +1,2 @@
class Foo <pair>{ <caret>
<pair>}
class Foo { <caret>
}
@@ -0,0 +1,2 @@
class Foo <brace>{</brace> <caret>
<brace>}</brace>
@@ -1 +1 @@
<pair>{{{{{{{{{}}}}}}}}<pair>}<caret>
{{{{{{{{{}}}}}}}}}<caret>
@@ -0,0 +1 @@
<brace>{</brace>{{{{{{{{}}}}}}}}<brace>}<caret></brace>
@@ -1 +1 @@
<caret>{{{{{{{{{}}}}}}}}<pair>}
<caret>{{{{{{{{{}}}}}}}}}
@@ -0,0 +1 @@
<caret><brace>{</brace>{{{{{{{{}}}}}}}}<brace>}</brace>
@@ -1 +1 @@
<pair>{{{{{{{{{}}}}}}}}<caret>}
{{{{{{{{{}}}}}}}}<caret>}
@@ -0,0 +1 @@
<brace>{</brace>{{{{{{{{}}}}}}}}<caret><brace>}</brace>
@@ -5,5 +5,5 @@
}
}
}
<pair>}
}
}
@@ -0,0 +1,9 @@
{
<caret><brace>{</brace>{{{{{{{}}
}
}
}
}
}
<brace>}</brace>
}
@@ -1,6 +1,6 @@
{{{{{{{{
<caret>{
<pair>}}
}}
}
}
}
@@ -0,0 +1,10 @@
{{{{{{{{
<caret><brace>{</brace>
<brace>}</brace>}
}
}
}
}
}
}
}
@@ -1,5 +1,5 @@
{{{{{{{{
<pair>{
{
<caret>}}
}
}
@@ -0,0 +1,10 @@
{{{{{{{{
<brace>{</brace>
<caret><brace>}</brace>}
}
}
}
}
}
}
}
@@ -1,10 +1,13 @@
// 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.
// Copyright 2000-2020 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 com.intellij.codeInsight.highlighting;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.testFramework.FileBasedTestCaseHelper;
import com.intellij.testFramework.LightPlatformCodeInsightTestCase;
import com.intellij.testFramework.TestDataPath;
import com.intellij.testFramework.UsefulTestCase;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -20,13 +23,19 @@ public class BraceHighlightingHandlerBlockCaretTest extends LightPlatformCodeIns
configureByFile(myFileSuffix);
Editor editor = getEditor();
editor.getSettings().setBlockCursor(true);
BraceHighlightingHandlerTest.doTest(getProject(), getFile(), editor);
String result = BraceHighlightingHandlerTest.getTextEditorWithHighlightedBraces(getEditor(), getFile());
UsefulTestCase.assertSameLinesWithFile(getAnswerFilePath(), result);
});
}
@Nullable
@Override
public String getFileSuffix(String fileName) {
return fileName;
return StringUtil.endsWith(fileName, ".txt") ? null : fileName;
}
@Override
public @Nullable String getBaseName(@NotNull String fileAfterSuffix) {
return StringUtil.endsWith(fileAfterSuffix, ".txt") ? fileAfterSuffix.substring(0, fileAfterSuffix.length() - 4) : null;
}
}
@@ -1,24 +1,28 @@
// 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.
// Copyright 2000-2020 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 com.intellij.codeInsight.highlighting;
import com.intellij.injected.editor.EditorWindow;
import com.intellij.openapi.command.WriteCommandAction;
import com.intellij.openapi.editor.Document;
import com.intellij.openapi.editor.Editor;
import com.intellij.openapi.editor.ex.EditorEx;
import com.intellij.openapi.editor.markup.RangeHighlighter;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.util.Disposer;
import com.intellij.openapi.util.Pair;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.PsiFile;
import com.intellij.testFramework.FileBasedTestCaseHelper;
import com.intellij.testFramework.LightPlatformCodeInsightTestCase;
import com.intellij.testFramework.TestDataPath;
import com.intellij.testFramework.UsefulTestCase;
import com.intellij.util.Alarm;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import static com.intellij.testFramework.EdtTestUtil.runInEdtAndWait;
@RunWith(com.intellij.testFramework.Parameterized.class)
@@ -30,47 +34,49 @@ public class BraceHighlightingHandlerTest extends LightPlatformCodeInsightTestCa
public void testAction() {
runInEdtAndWait(() -> {
configureByFile(myFileSuffix);
doTest(getProject(), getFile(), getEditor());
String result = getTextEditorWithHighlightedBraces(getEditor(), getFile());
UsefulTestCase.assertSameLinesWithFile(getAnswerFilePath(), result);
});
}
public static void doTest(@NotNull Project project, @NotNull PsiFile psiFile, @NotNull Editor editor) {
Editor hostEditor = editor instanceof EditorWindow ? ((EditorWindow)editor).getDelegate() : editor;
final Document document = hostEditor.getDocument();
int first = document.getText().indexOf(PAIR_MARKER);
if (first >= 0) {
WriteCommandAction.runWriteCommandAction(null, () -> document.replaceString(first, first + PAIR_MARKER.length(), ""));
}
int second;
int secondCandidate = document.getText().indexOf(PAIR_MARKER);
if (secondCandidate >= 0) {
WriteCommandAction.runWriteCommandAction(null, () -> document.replaceString(secondCandidate, secondCandidate + PAIR_MARKER.length(), ""));
second = secondCandidate;
} else {
second = hostEditor.getCaretModel().getOffset();
}
Alarm alarm = new Alarm();
try {
new BraceHighlightingHandler(project, (EditorEx)editor, alarm, psiFile).updateBraces();
RangeHighlighter[] highlighters = editor.getMarkupModel().getAllHighlighters();
int braceHighlighters = 0;
for (RangeHighlighter highlighter : highlighters) {
if (highlighter.getLayer() == BraceHighlightingHandler.LAYER) {
braceHighlighters++;
assertTrue("Expected offsets: " + first + " or " + second + ", got: " + highlighter.getStartOffset(),
first == highlighter.getStartOffset() || second == highlighter.getStartOffset());
}
}
assertEquals(first >= 0 ? 2 : 0, braceHighlighters);
} finally {
Disposer.dispose(alarm);
}
}
@Nullable
@Override
public String getFileSuffix(String fileName) {
return fileName;
return StringUtil.endsWith(fileName, ".txt") ? null : fileName;
}
}
@Override
public @Nullable String getBaseName(@NotNull String fileAfterSuffix) {
return StringUtil.endsWith(fileAfterSuffix, ".txt") ? fileAfterSuffix.substring(0, fileAfterSuffix.length() - 4) : null;
}
/**
* @return a text from passed editor with highlighted braces wrapped in {@code <brace></brace>} tags. And {@link <caret>} marker
*/
public static String getTextEditorWithHighlightedBraces(@NotNull Editor editor, @NotNull PsiFile psiFile) {
Editor hostEditor = editor instanceof EditorWindow ? ((EditorWindow)editor).getDelegate() : editor;
List<Pair<Integer, String>> markers = new ArrayList<>();
Alarm alarm = new Alarm();
try {
new BraceHighlightingHandler(psiFile.getProject(), (EditorEx)editor, alarm, psiFile).updateBraces();
RangeHighlighter[] highlighters = editor.getMarkupModel().getAllHighlighters();
for (RangeHighlighter highlighter : highlighters) {
if (highlighter.getLayer() == BraceHighlightingHandler.LAYER) {
markers.add(Pair.create(highlighter.getStartOffset(), "<brace>"));
markers.add(Pair.create(highlighter.getEndOffset(), "</brace>"));
}
}
}
finally {
Disposer.dispose(alarm);
}
hostEditor.getCaretModel().getAllCarets().forEach(it -> markers.add(Pair.create(it.getOffset(), "<caret>")));
StringBuilder result = new StringBuilder(hostEditor.getDocument().getCharsSequence());
markers.stream()
.sorted(Comparator.comparingInt(it -> -it.first))
.forEach(it -> result.insert(it.first, it.second));
return result.toString();
}
}
@@ -1,4 +1,4 @@
// Copyright 2000-2019 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.
// Copyright 2000-2020 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 com.intellij.testFramework;
import com.intellij.codeInsight.daemon.DaemonCodeAnalyzer;
@@ -108,6 +108,11 @@ public abstract class LightPlatformCodeInsightTestCase extends LightPlatformTest
}
}
@NotNull
protected String getAnswerFilePath() {
return getTestDataPath() + myFileSuffix + ".txt";
}
private static void checkCaseSensitiveFS(@NotNull String fullOrRelativePath, @NotNull File ioFile) throws IOException {
fullOrRelativePath = FileUtil.toSystemDependentName(FileUtil.toCanonicalPath(fullOrRelativePath));
if (!ioFile.getCanonicalPath().endsWith(fullOrRelativePath)) {