Fix command sorting with varargs
Alle Prüfungen waren erfolgreich
SteamWarCI Build successful

Dieser Commit ist enthalten in:
yoyosource 2023-02-02 17:53:56 +01:00
Ursprung ef79a2e7db
Commit 65df8ddab0
4 geänderte Dateien mit 16 neuen und 31 gelöschten Zeilen

Datei anzeigen

@ -124,6 +124,8 @@ class CommandPart<T> {
} }
if (next != null) { if (next != null) {
next.generateArgumentArray(errors, current, sender, args, startIndex + 1); next.generateArgumentArray(errors, current, sender, args, startIndex + 1);
} else if (startIndex + 1 < args.length) {
throw new CommandParseException();
} }
} }

Datei anzeigen

@ -76,8 +76,11 @@ public class SubCommand<T> implements Comparable<SubCommand<T>> {
int tLength = parameters.length + subCommand.length; int tLength = parameters.length + subCommand.length;
int oLength = o.parameters.length + o.subCommand.length; int oLength = o.parameters.length + o.subCommand.length;
if (parameters[parameters.length - 1].isVarArgs()) tLength *= -1; boolean tVarArgs = parameters[parameters.length - 1].isVarArgs();
if (o.parameters[o.parameters.length - 1].isVarArgs()) oLength *= -1; boolean oVarArgs = o.parameters[o.parameters.length - 1].isVarArgs();
if (tVarArgs) tLength *= -1;
if (oVarArgs) oLength *= -1;
return -Integer.compare(tLength, oLength); return -Integer.compare(tLength, oLength);
} }

Datei anzeigen

@ -30,25 +30,16 @@ public class SubCMDSortingCommand extends TestSWCommand {
@Register @Register
public void test(String s) { public void test(String s) {
System.out.println("HERE 1");
throw new ExecutionIdentifier("Command with 0 parameters"); throw new ExecutionIdentifier("Command with 0 parameters");
} }
@Register
public void test(String s, String args) {
throw new ExecutionIdentifier("Command with 1 parameter");
}
@Register @Register
public void test(String s, String... args) { public void test(String s, String... args) {
System.out.println("HERE 2"); throw new ExecutionIdentifier("Command with n parameters");
throw new ExecutionIdentifier("Command with 1 parameters");
}
@Register
public void test(String s, String p, String... args) {
System.out.println("HERE 3");
throw new ExecutionIdentifier("Command with 2 parameters");
}
@Register
public void test(String s, String p, String p2, String... args) {
System.out.println("HERE 4");
throw new ExecutionIdentifier("Command with 3 parameters");
} }
} }

Datei anzeigen

@ -44,29 +44,18 @@ public class SubCMDSortingCommandTest {
cmd.execute("", "", new String[]{"Hello"}); cmd.execute("", "", new String[]{"Hello"});
assert false; assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "Command with 1 parameters"); assertCMDFramework(e, ExecutionIdentifier.class, "Command with 1 parameter");
} }
} }
@Test @Test
public void testTwoArgs() { public void testOneArgsVarArg() {
SubCMDSortingCommand cmd = new SubCMDSortingCommand(); SubCMDSortingCommand cmd = new SubCMDSortingCommand();
try { try {
cmd.execute("", "", new String[]{"Hello", "World"}); cmd.execute("", "", new String[]{"Hello", "World"});
assert false; assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "Command with 2 parameters"); assertCMDFramework(e, ExecutionIdentifier.class, "Command with n parameters");
}
}
@Test
public void testThreeArgs() {
SubCMDSortingCommand cmd = new SubCMDSortingCommand();
try {
cmd.execute("", "", new String[]{"Hello", "World", "!"});
assert false;
} catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "Command with 3 parameters");
} }
} }
} }