From b8d41338a94faf2bec7c631fcc419ef533f31416 Mon Sep 17 00:00:00 2001 From: Scott Wells Date: Tue, 10 Dec 2024 11:56:36 -0600 Subject: [PATCH] Pulled the snippet search utility methods up into the common test base class and updated the folding range tests to use them as well. --- .../TypeScriptCodeBlockProviderTest.java | 14 ++-- .../TypeScriptFoldingRangeTest.java | 4 +- .../LSPCodeBlockProviderFixtureTestCase.java | 42 ---------- .../LSPCodeInsightFixtureTestCase.java | 82 +++++++++++++++++++ 4 files changed, 91 insertions(+), 51 deletions(-) diff --git a/src/test/java/com/redhat/devtools/lsp4ij/features/foldingRange/TypeScriptCodeBlockProviderTest.java b/src/test/java/com/redhat/devtools/lsp4ij/features/foldingRange/TypeScriptCodeBlockProviderTest.java index 4637cc24b..79b9c34bf 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/features/foldingRange/TypeScriptCodeBlockProviderTest.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/features/foldingRange/TypeScriptCodeBlockProviderTest.java @@ -52,19 +52,19 @@ public void testCodeBlocks() { """, new int[][]{ // From "console" to the begin and end of the "demo()" function's braced block - {before(DEMO_TS_FILE_BODY, "console", 1), after(DEMO_TS_FILE_BODY, "{", 2), beforeLast(DEMO_TS_FILE_BODY, "}", 2)}, + {beforeFirst(DEMO_TS_FILE_BODY, "console", 1), afterFirst(DEMO_TS_FILE_BODY, "{", 2), beforeLast(DEMO_TS_FILE_BODY, "}", 2)}, // From "demo" to the begin and end of the "Demo" class's braced block - {before(DEMO_TS_FILE_BODY, "demo", 1), after(DEMO_TS_FILE_BODY, "{", 1), beforeLast(DEMO_TS_FILE_BODY, "}", 1)}, + {beforeFirst(DEMO_TS_FILE_BODY, "demo", 1), afterFirst(DEMO_TS_FILE_BODY, "{", 1), beforeLast(DEMO_TS_FILE_BODY, "}", 1)}, // From before the "Demo" class' open brace to the begin and end of the "Demo" class' braced block - {before(DEMO_TS_FILE_BODY, "{", 1), after(DEMO_TS_FILE_BODY, "{", 1), beforeLast(DEMO_TS_FILE_BODY, "}", 1)}, + {beforeFirst(DEMO_TS_FILE_BODY, "{", 1), afterFirst(DEMO_TS_FILE_BODY, "{", 1), beforeLast(DEMO_TS_FILE_BODY, "}", 1)}, // From after the "Demo" class' open brace to the begin and end of the "Demo" class' braced block - {after(DEMO_TS_FILE_BODY, "{", 1), after(DEMO_TS_FILE_BODY, "{", 1), beforeLast(DEMO_TS_FILE_BODY, "}", 1)}, + {afterFirst(DEMO_TS_FILE_BODY, "{", 1), afterFirst(DEMO_TS_FILE_BODY, "{", 1), beforeLast(DEMO_TS_FILE_BODY, "}", 1)}, // From before the "Demo" class' close brace to the begin and end of the "Demo" class' braced block - {beforeLast(DEMO_TS_FILE_BODY, "}", 1), after(DEMO_TS_FILE_BODY, "{", 1), beforeLast(DEMO_TS_FILE_BODY, "}", 1)}, + {beforeLast(DEMO_TS_FILE_BODY, "}", 1), afterFirst(DEMO_TS_FILE_BODY, "{", 1), beforeLast(DEMO_TS_FILE_BODY, "}", 1)}, // From after the "Demo" class' close brace to the begin and end of the "Demo" class' braced block - {afterLast(DEMO_TS_FILE_BODY, "}", 1), after(DEMO_TS_FILE_BODY, "{", 1), beforeLast(DEMO_TS_FILE_BODY, "}", 1)}, + {afterLast(DEMO_TS_FILE_BODY, "}", 1), afterFirst(DEMO_TS_FILE_BODY, "{", 1), beforeLast(DEMO_TS_FILE_BODY, "}", 1)}, // From "Demo" which shouldn't move at all - {before(DEMO_TS_FILE_BODY, "Demo", 1), before(DEMO_TS_FILE_BODY, "Demo", 1), before(DEMO_TS_FILE_BODY, "Demo", 1)} + {beforeFirst(DEMO_TS_FILE_BODY, "Demo", 1), beforeFirst(DEMO_TS_FILE_BODY, "Demo", 1), beforeFirst(DEMO_TS_FILE_BODY, "Demo", 1)} } ); } diff --git a/src/test/java/com/redhat/devtools/lsp4ij/features/foldingRange/TypeScriptFoldingRangeTest.java b/src/test/java/com/redhat/devtools/lsp4ij/features/foldingRange/TypeScriptFoldingRangeTest.java index 6e0463faa..729670989 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/features/foldingRange/TypeScriptFoldingRangeTest.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/features/foldingRange/TypeScriptFoldingRangeTest.java @@ -30,9 +30,9 @@ export class Demo { """; // Demo class braced block exclusive of braces - private static final TextRange DEMO_CLASS_BODY_TEXT_RANGE = TextRange.create(19, 68); + private static final TextRange DEMO_CLASS_BODY_TEXT_RANGE = TextRange.create(afterFirst(DEMO_TS_FILE_BODY, "{", 1), beforeLast(DEMO_TS_FILE_BODY, "}", 1)); // demo() function braced block exclusive of braces - private static final TextRange DEMO_METHOD_BODY_TEXT_RANGE = TextRange.create(32, 66); + private static final TextRange DEMO_METHOD_BODY_TEXT_RANGE = TextRange.create(afterFirst(DEMO_TS_FILE_BODY, "{", 2), beforeLast(DEMO_TS_FILE_BODY, "}", 2)); public TypeScriptFoldingRangeTest() { super("*.ts"); diff --git a/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeBlockProviderFixtureTestCase.java b/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeBlockProviderFixtureTestCase.java index e854a37ea..9fa7bfe29 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeBlockProviderFixtureTestCase.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeBlockProviderFixtureTestCase.java @@ -35,48 +35,6 @@ protected LSPCodeBlockProviderFixtureTestCase(String... fileNamePatterns) { super(fileNamePatterns); } - protected static int before(@NotNull String fileBody, @NotNull String snippet, int count) { - int fromIndex = 0; - for (int i = 0; i < count; i++) { - int index = fileBody.indexOf(snippet, fromIndex); - assertFalse("Failed to find occurrence " + (i + 1) + " of '" + snippet + "'.", index == -1); - if (count == (i + 1)) { - return index; - } else { - fromIndex = index + 1; - if (fromIndex == fileBody.length()) { - fail("Failed to find occurrence " + (i + 1) + " of '" + snippet + "'."); - } - } - } - return fromIndex; - } - - protected static int after(@NotNull String fileBody, @NotNull String snippet, int count) { - return before(fileBody, snippet, count) + snippet.length(); - } - - protected static int beforeLast(@NotNull String fileBody, @NotNull String snippet, int count) { - int fromIndex = fileBody.length() - 1; - for (int i = 0; i < count; i++) { - int index = fileBody.lastIndexOf(snippet, fromIndex); - assertFalse("Failed to find last occurrence " + (i + 1) + " of '" + snippet + "'.", index == -1); - if (count == (i + 1)) { - return index; - } else { - fromIndex = index - 1; - if (fromIndex == 0) { - fail("Failed to find occurrence " + (i + 1) + " of '" + snippet + "'."); - } - } - } - return fromIndex; - } - - protected static int afterLast(@NotNull String fileBody, @NotNull String snippet, int count) { - return beforeLast(fileBody, snippet, count) + snippet.length(); - } - protected void assertCodeBlocks(@NotNull String fileName, @NotNull String fileBody, @NotNull String mockFoldingRangesJson, diff --git a/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeInsightFixtureTestCase.java b/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeInsightFixtureTestCase.java index 5d6cf6a30..a9b364a62 100644 --- a/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeInsightFixtureTestCase.java +++ b/src/test/java/com/redhat/devtools/lsp4ij/fixtures/LSPCodeInsightFixtureTestCase.java @@ -18,6 +18,7 @@ import com.redhat.devtools.lsp4ij.launching.ServerMappingSettings; import com.redhat.devtools.lsp4ij.mock.MockLanguageServer; import com.redhat.devtools.lsp4ij.mock.MockLanguageServerDefinition; +import org.jetbrains.annotations.NotNull; import java.util.List; @@ -72,4 +73,85 @@ private void registerServer() { private void unregisterServer() { LanguageServersRegistry.getInstance().removeServerDefinition(myFixture.getProject(), serverDefinition); } + + // Utility methods for getting the before/after index of the n'th occurrence of a substring of the test file body + // from the beginning or end as appropriate. These methods help to avoid hard-coding offsets into the file. + + /** + * Returns the index immediately before the {@code count} occurrence of {@code snippet} in {@code fileBody} + * searching from the beginning. + * + * @param fileBody the file body + * @param snippet the search snippet + * @param count the occurrence count + * @return the index immediately before the occurrence; fails fast if not found + */ + protected static int beforeFirst(@NotNull String fileBody, @NotNull String snippet, int count) { + int fromIndex = 0; + for (int i = 0; i < count; i++) { + int index = fileBody.indexOf(snippet, fromIndex); + assertFalse("Failed to find occurrence " + (i + 1) + " of '" + snippet + "'.", index == -1); + if (count == (i + 1)) { + return index; + } else { + fromIndex = index + 1; + if (fromIndex == fileBody.length()) { + fail("Failed to find occurrence " + (i + 1) + " of '" + snippet + "'."); + } + } + } + return fromIndex; + } + + /** + * Returns the index immediately after the {@code count} occurrence of {@code snippet} in {@code fileBody} + * searching from the beginning. + * + * @param fileBody the file body + * @param snippet the search snippet + * @param count the occurrence count + * @return the index immediately after the occurrence; fails fast if not found + */ + protected static int afterFirst(@NotNull String fileBody, @NotNull String snippet, int count) { + return beforeFirst(fileBody, snippet, count) + snippet.length(); + } + + /** + * Returns the index immediately before the {@code count} occurrence of {@code snippet} in {@code fileBody} + * searching from the end. + * + * @param fileBody the file body + * @param snippet the search snippet + * @param count the occurrence count + * @return the index immediately before the last occurrence; fails fast if not found + */ + protected static int beforeLast(@NotNull String fileBody, @NotNull String snippet, int count) { + int fromIndex = fileBody.length() - 1; + for (int i = 0; i < count; i++) { + int index = fileBody.lastIndexOf(snippet, fromIndex); + assertFalse("Failed to find last occurrence " + (i + 1) + " of '" + snippet + "'.", index == -1); + if (count == (i + 1)) { + return index; + } else { + fromIndex = index - 1; + if (fromIndex == 0) { + fail("Failed to find occurrence " + (i + 1) + " of '" + snippet + "'."); + } + } + } + return fromIndex; + } + + /** + * Returns the index immediately after the {@code count} occurrence of {@code snippet} in {@code fileBody} + * searching from the end. + * + * @param fileBody the file body + * @param snippet the search snippet + * @param count the occurrence count + * @return the index immediately after the last occurrence; fails fast if not found + */ + protected static int afterLast(@NotNull String fileBody, @NotNull String snippet, int count) { + return beforeLast(fileBody, snippet, count) + snippet.length(); + } }