SteamWar/SpigotCore
Archiviert
13
0

Fix CommandPart.generateTabComplete for varArg Parameter
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2021-12-09 15:09:07 +01:00
Ursprung 2bc15adfda
Commit 7d94d8dd6c
2 geänderte Dateien mit 57 neuen und 3 gelöschten Zeilen

Datei anzeigen

@ -133,6 +133,20 @@ public class CommandPart {
} }
public void generateTabComplete(List<String> current, CommandSender commandSender, String[] args, int startIndex) { public void generateTabComplete(List<String> current, CommandSender commandSender, String[] args, int startIndex) {
if (varArgType != null) {
for (int i = startIndex; i < args.length - 1; i++) {
CheckArgumentResult validArgument = checkArgument(null, commandSender, args, i);
if (!validArgument.success) {
return;
}
}
List<String> strings = typeMapper.tabCompletes(commandSender, Arrays.copyOf(args, args.length - 1), args[args.length - 1]);
if (strings != null) {
current.addAll(strings);
}
return;
}
if (args.length - 1 > startIndex) { if (args.length - 1 > startIndex) {
CheckArgumentResult checkArgumentResult = checkArgument(GuardCheckType.TAB_COMPLETE, commandSender, args, startIndex); CheckArgumentResult checkArgumentResult = checkArgument(GuardCheckType.TAB_COMPLETE, commandSender, args, startIndex);
if (checkArgumentResult.success && next != null) { if (checkArgumentResult.success && next != null) {

Datei anzeigen

@ -27,14 +27,14 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf; import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.is;
public class SimpleCommandPartTest { public class SimpleCommandPartTest {
private CommandPart stringCommandPart; private CommandPart stringCommandPart;
private CommandPart intCommandPart; private CommandPart intCommandPart;
private CommandPart chainedCommandPart; private CommandPart chainedCommandPart;
private CommandPart varArgCommandPart;
private CommandPart simpleGuardPart; private CommandPart simpleGuardPart;
@ -46,6 +46,8 @@ public class SimpleCommandPartTest {
chainedCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, GuardCheckType.COMMAND); chainedCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, GuardCheckType.COMMAND);
chainedCommandPart.setNext(new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, GuardCheckType.COMMAND)); chainedCommandPart.setNext(new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, GuardCheckType.COMMAND));
varArgCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, String.class, null, GuardCheckType.COMMAND);
simpleGuardPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), (commandSender, guardCheckType, previousArguments, s) -> s.equals("hello") ? GuardResult.DENIED : GuardResult.ALLOWED, null, null, GuardCheckType.COMMAND); simpleGuardPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), (commandSender, guardCheckType, previousArguments, s) -> s.equals("hello") ? GuardResult.DENIED : GuardResult.ALLOWED, null, null, GuardCheckType.COMMAND);
} }
@ -119,10 +121,48 @@ public class SimpleCommandPartTest {
public void testChainedCommandTabCompleteOther() { public void testChainedCommandTabCompleteOther() {
List<String> tabCompletes = new ArrayList<>(); List<String> tabCompletes = new ArrayList<>();
chainedCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", ""}, 0); chainedCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", ""}, 0);
System.out.println(tabCompletes);
assertThat(tabCompletes.size(), is(0)); assertThat(tabCompletes.size(), is(0));
} }
@Test
public void testVarArgsCommandTabComplete() {
List<String> tabCompletes = new ArrayList<>();
varArgCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello"}, 0);
assertThat(tabCompletes.size(), is(2));
assertThat(tabCompletes.get(0), is("hello"));
assertThat(tabCompletes.get(1), is("world"));
}
@Test
public void testVarArgsCommandTabCompleteDeeper() {
List<String> tabCompletes = new ArrayList<>();
varArgCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", "world", "hello", "world"}, 0);
System.out.println(tabCompletes);
assertThat(tabCompletes.size(), is(2));
assertThat(tabCompletes.get(0), is("hello"));
assertThat(tabCompletes.get(1), is("world"));
}
@Test
public void testVarArgsCommandArgumentParsing() {
List<Object> argumentArray = new ArrayList<>();
varArgCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello"}, 0);
assertThat(argumentArray.size(), is(1));
assertThat(argumentArray.get(0), instanceOf(String[].class));
assertThat((String[]) argumentArray.get(0), arrayWithSize(1));
assertThat((String[]) argumentArray.get(0), is(new String[]{"hello"}));
}
@Test
public void testVarArgsCommandArgumentParsingDeeper() {
List<Object> argumentArray = new ArrayList<>();
varArgCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello", "world", "hello", "world"}, 0);
assertThat(argumentArray.size(), is(1));
assertThat(argumentArray.get(0), instanceOf(String[].class));
assertThat((String[]) argumentArray.get(0), arrayWithSize(4));
assertThat((String[]) argumentArray.get(0), is(new String[]{"hello", "world", "hello", "world"}));
}
@Test @Test
public void testGuardCommandExecute() { public void testGuardCommandExecute() {
List<Object> argumentArray = new ArrayList<>(); List<Object> argumentArray = new ArrayList<>();