IDEA-384355 [java]: add some verification to test

GitOrigin-RevId: 00357b1bc4a701461a78b40777385bc462b8bbf8
This commit is contained in:
Bas Leijdekkers
2026-02-11 14:32:12 +00:00
committed by intellij-monorepo-bot
parent aff2a99cab
commit c37f8b8b97
@@ -1,7 +1,8 @@
// 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.
// Copyright 2000-2026 JetBrains s.r.o. and contributors. Use of this source code is governed by the Apache 2.0 license.
package com.intellij.psi.util;
import com.intellij.openapi.util.TextRange;
import com.intellij.openapi.util.text.StringUtil;
import com.intellij.psi.JavaPsiFacade;
import com.intellij.psi.PsiElementFactory;
import com.intellij.psi.PsiLiteralExpression;
@@ -10,23 +11,23 @@ import com.intellij.testFramework.LightPlatformCodeInsightTestCase;
public class MapBackTextBlockTextRangeTest extends LightPlatformCodeInsightTestCase {
public void testEmpty() {
doTest("\"\"\"\n\"\"\"", 0, 0, new TextRange(4, 4));
doTest("\"\"\"\n\"\"\"", 0, 0, new TextRange(4, 4), "");
}
public void testExtraCharsBeforeContent() {
doTest("\"\"\" \n\"\"\"", 0, 0, new TextRange(7, 7));
doTest("\"\"\" \n\"\"\"", 0, 0, new TextRange(7, 7), "");
}
public void testOneLineContentNoTrailingLine() {
doTest("\"\"\"\n foo \"\"\"", 0, 5, new TextRange(7, 12));
doTest("\"\"\"\n foo \"\"\"", 0, 5, new TextRange(7, 12), "foo ");
}
public void testOneLineContentTrailingLine() {
doTest("\"\"\"\nfoo \n \"\"\"", 0, 3, new TextRange(4, 7));
doTest("\"\"\"\nfoo \n \"\"\"", 0, 3, new TextRange(4, 7), "foo");
}
public void testTrailingLineWithContent() {
doTest("\"\"\"\nfoo \n b\"\"\"", 0, 6, new TextRange(4, 12));
doTest("\"\"\"\nfoo \n b\"\"\"", 0, 6, new TextRange(4, 12), "foo \n b");
}
public void testContentWithBlankLines() {
@@ -35,28 +36,31 @@ public class MapBackTextBlockTextRangeTest extends LightPlatformCodeInsightTestC
\s
foo \t\f
\s
""\"""", 0, 6, new TextRange(9, 24));
""\"""", 0, 6, new TextRange(9, 24), "\n foo \t\f\n \n");
}
public void testIndentNonZero() {
doTest("\"\"\"\n foo\n bar\n \"\"\"", 0, 8, new TextRange(5, 14));
doTest("\"\"\"\n foo\n bar\n \"\"\"", 0, 8, new TextRange(5, 14), "foo\n bar\n");
}
public void testContentWithEscapeSequences() {
doTest("\"\"\"\n\\u005c\\u005c\\u005c\\u005c\"\"\"", 1, 2, new TextRange(10, 16));
doTest("\"\"\"\n\\u005c\\u005c\\u005c\\u005c\"\"\"", 1, 2, new TextRange(10, 16), "\\u005c");
}
public void testInvalidRange() {
doTest("\"\"\"\n\"\"\"", 0, 1, null);
doTest("\"\"\"\n\"\"\"", 0, 1, null, null);
}
private void doTest(String blockText, int from, int to, TextRange expectedRange) {
private void doTest(String blockText, int from, int to, TextRange expectedRange, String expectedRangeText) {
PsiElementFactory factory = JavaPsiFacade.getElementFactory(getProject());
PsiLiteralExpression textBlock = (PsiLiteralExpression)factory.createExpressionFromText(blockText, null);
assertTrue(textBlock.isTextBlock());
int indent = PsiLiteralUtil.getTextBlockIndent(textBlock);
assertTrue(indent >= 0);
TextRange actualRange = PsiLiteralUtil.mapBackTextBlockRange(textBlock.getText(), from, to, indent);
assertEquals(expectedRange, actualRange);
assertEquals(expectedRange, PsiLiteralUtil.mapBackTextBlockRange(textBlock.getText(), from, to, indent));
if (expectedRange != null) {
assertEquals(StringUtil.escapeStringCharacters(expectedRangeText),
StringUtil.escapeStringCharacters(expectedRange.substring(blockText)));
}
}
}