From c051ff20df6bd92a57e91dbd2082014c951369d5 Mon Sep 17 00:00:00 2001 From: Jason Date: Sun, 18 Oct 2020 12:34:27 -0700 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Fix=20wrong=20suggestions=20foll?= =?UTF-8?q?owing=20an=20invalid=20literal=20(#72)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * ✅ Add a test for suggestions after an invalid literal * :bug: Fix wrong suggestions following an invalid literal * :books: Document literal suggestion fix in CHANGELOG Co-authored-by: Alexander Söderberg --- CHANGELOG.md | 1 + .../java/cloud/commandframework/CommandTree.java | 13 +++++++++++-- .../commandframework/CommandSuggestionsTest.java | 13 +++++++++++++ 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9556d057..3be4ac56 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Fixed quoted parsing in StringArgument + - Fixed wrong suggestions following invalid literals ## [1.0.1] - 2020-10-14 diff --git a/cloud-core/src/main/java/cloud/commandframework/CommandTree.java b/cloud-core/src/main/java/cloud/commandframework/CommandTree.java index 7e8bb5fb..6985f116 100644 --- a/cloud-core/src/main/java/cloud/commandframework/CommandTree.java +++ b/cloud-core/src/main/java/cloud/commandframework/CommandTree.java @@ -541,14 +541,23 @@ public final class CommandTree { } } } + if (commandQueue.size() > 1) { + /* + * In this case we were unable to match any of the literals, and so we cannot + * possibly attempt to match any of its children (which is what we want, according + * to the input queue). Because of this, we terminate immediately + */ + return Collections.emptyList(); + } } final List suggestions = new LinkedList<>(); for (final Node> argument : root.getChildren()) { if (argument.getValue() == null || this.isPermitted(commandContext.getSender(), argument) != null) { continue; } - suggestions.addAll(argument.getValue().getSuggestionsProvider() - .apply(commandContext, stringOrEmpty(commandQueue.peek()))); + final List suggestionsToAdd = argument.getValue().getSuggestionsProvider() + .apply(commandContext, stringOrEmpty(commandQueue.peek())); + suggestions.addAll(suggestionsToAdd); } return suggestions; } diff --git a/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java b/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java index c667b554..4aaf0e50 100644 --- a/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java +++ b/cloud-core/src/test/java/cloud/commandframework/CommandSuggestionsTest.java @@ -191,6 +191,19 @@ public class CommandSuggestionsTest { Assertions.assertEquals(Arrays.asList("5", "6", "7", "8", "9"), suggestions5); } + @Test + void testInvalidLiteralThenSpace() { + final String input = "test o"; + final List suggestions = manager.suggest(new TestCommandSender(), input); + Assertions.assertEquals(Collections.singletonList("one"), suggestions); + final String input2 = "test o "; + final List suggestions2 = manager.suggest(new TestCommandSender(), input2); + Assertions.assertEquals(Collections.emptyList(), suggestions2); + final String input3 = "test o abc123xyz"; + final List suggestions3 = manager.suggest(new TestCommandSender(), input3); + Assertions.assertEquals(Collections.emptyList(), suggestions3); + } + public enum TestEnum { FOO,