Skip to content

Commit

Permalink
Pulled the snippet search utility methods up into the common test bas…
Browse files Browse the repository at this point in the history
…e class and updated the folding range tests to use them as well.
  • Loading branch information
SCWells72 committed Dec 10, 2024
1 parent 7e2e144 commit b8d4133
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)}
}
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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 <i>before</i> 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 <i>after</i> 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 <i>before</i> 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 <i>after</i> 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();
}
}

0 comments on commit b8d4133

Please sign in to comment.