From 1d6a3f976e96a497494f4ada46654a4d38a81541 Mon Sep 17 00:00:00 2001 From: EpicKnarvik97 Date: Sat, 4 May 2024 13:37:49 +0200 Subject: [PATCH] Fixes some problems with TabCompletionHelper.getStringList --- .../knarlib/util/TabCompletionHelper.java | 11 ++++- .../knarlib/util/TabCompletionHelperTest.java | 48 +++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 src/test/java/net/knarcraft/knarlib/util/TabCompletionHelperTest.java diff --git a/src/main/java/net/knarcraft/knarlib/util/TabCompletionHelper.java b/src/main/java/net/knarcraft/knarlib/util/TabCompletionHelper.java index 72f1c10..0cce89c 100644 --- a/src/main/java/net/knarcraft/knarlib/util/TabCompletionHelper.java +++ b/src/main/java/net/knarcraft/knarlib/util/TabCompletionHelper.java @@ -38,7 +38,14 @@ public final class TabCompletionHelper { } else { // The argument base is everything the player has typed, until the last comma List arguments = List.of(typedText.split(",")); - String base = getBase(arguments); + String base; + List 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 unused = new ArrayList<>(); for (String string : filtered) { - if (!arguments.contains(string)) { + if (!baseArguments.contains(string)) { unused.add(base + string); } } diff --git a/src/test/java/net/knarcraft/knarlib/util/TabCompletionHelperTest.java b/src/test/java/net/knarcraft/knarlib/util/TabCompletionHelperTest.java new file mode 100644 index 0000000..db2a157 --- /dev/null +++ b/src/test/java/net/knarcraft/knarlib/util/TabCompletionHelperTest.java @@ -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 input = List.of("a", "b", "c", "d"); + List result = TabCompletionHelper.getStringList(input, "a,b,", null); + List expected = List.of("a,b,c", "a,b,d"); + assertEquals(expected, result); + } + + @Test + public void getStringListUnfilteredTest2() { + List input = List.of("a", "b", "c", "d"); + List result = TabCompletionHelper.getStringList(input, "a,b", null); + List expected = List.of("a,b", "a,c", "a,d"); + assertEquals(expected, result); + } + + @Test + public void getStringListContainsTest() { + List input = List.of("ape", "ball", "car", "donut"); + List result = TabCompletionHelper.getStringList(input, "ball,donut,r", + TabCompletionHelper::filterMatchingContains); + List expected = List.of("ball,donut,car"); + assertEquals(expected, result); + } + + @Test + public void getStringListStartsWithTest() { + List input = List.of("ape", "ball", "car", "donut"); + List result = TabCompletionHelper.getStringList(input, "ball,donut,a", + TabCompletionHelper::filterMatchingStartsWith); + List expected = List.of("ball,donut,ape"); + assertEquals(expected, result); + } + +}