Fixes some problems with TabCompletionHelper.getStringList
All checks were successful
KnarCraft/KnarLib/pipeline/head This commit looks good

This commit is contained in:
Kristian Knarvik 2024-05-04 13:37:49 +02:00
parent 5bdbaa351a
commit 1d6a3f976e
2 changed files with 57 additions and 2 deletions

View File

@ -38,7 +38,14 @@ public final class TabCompletionHelper {
} else {
// The argument base is everything the player has typed, until the last comma
List<String> arguments = List.of(typedText.split(","));
String base = getBase(arguments);
String base;
List<String> baseArguments;
if (typedText.endsWith(",")) {
baseArguments = arguments;
} else {
baseArguments = arguments.subList(0, arguments.size() - 1);
}
base = String.join(",", baseArguments) + ",";
String lastArgument = arguments.get(arguments.size() - 1);
// Filter available values by the text after the last comma only
@ -52,7 +59,7 @@ public final class TabCompletionHelper {
// Remove any already used values
List<String> unused = new ArrayList<>();
for (String string : filtered) {
if (!arguments.contains(string)) {
if (!baseArguments.contains(string)) {
unused.add(base + string);
}
}

View File

@ -0,0 +1,48 @@
package net.knarcraft.knarlib.util;
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
/**
* Tests for the tab-completion helper
*/
public class TabCompletionHelperTest {
@Test
public void getStringListUnfilteredTest() {
List<String> input = List.of("a", "b", "c", "d");
List<String> result = TabCompletionHelper.getStringList(input, "a,b,", null);
List<String> expected = List.of("a,b,c", "a,b,d");
assertEquals(expected, result);
}
@Test
public void getStringListUnfilteredTest2() {
List<String> input = List.of("a", "b", "c", "d");
List<String> result = TabCompletionHelper.getStringList(input, "a,b", null);
List<String> expected = List.of("a,b", "a,c", "a,d");
assertEquals(expected, result);
}
@Test
public void getStringListContainsTest() {
List<String> input = List.of("ape", "ball", "car", "donut");
List<String> result = TabCompletionHelper.getStringList(input, "ball,donut,r",
TabCompletionHelper::filterMatchingContains);
List<String> expected = List.of("ball,donut,car");
assertEquals(expected, result);
}
@Test
public void getStringListStartsWithTest() {
List<String> input = List.of("ape", "ball", "car", "donut");
List<String> result = TabCompletionHelper.getStringList(input, "ball,donut,a",
TabCompletionHelper::filterMatchingStartsWith);
List<String> expected = List.of("ball,donut,ape");
assertEquals(expected, result);
}
}