From 45e9698634f9c6c4e8ff4de315d6133056931da5 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 2 Feb 2023 18:07:29 +0100 Subject: [PATCH] Fix command sorting with varargs --- .../steamwar/command/AbstractSWCommand.java | 1 - src/de/steamwar/command/SubCommand.java | 2 ++ .../command/SubCMDSortingCommand.java | 5 +++++ .../command/SubCMDSortingCommandTest.java | 22 +++++++++++++++++++ 4 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/de/steamwar/command/AbstractSWCommand.java b/src/de/steamwar/command/AbstractSWCommand.java index 1ad1969..84ae620 100644 --- a/src/de/steamwar/command/AbstractSWCommand.java +++ b/src/de/steamwar/command/AbstractSWCommand.java @@ -140,7 +140,6 @@ public abstract class AbstractSWCommand { } Collections.sort(commandList); - System.out.println(commandList.stream().map(o -> o.method).collect(Collectors.toList())); initialized = true; } diff --git a/src/de/steamwar/command/SubCommand.java b/src/de/steamwar/command/SubCommand.java index 6c076b1..58fa919 100644 --- a/src/de/steamwar/command/SubCommand.java +++ b/src/de/steamwar/command/SubCommand.java @@ -82,6 +82,8 @@ public class SubCommand implements Comparable> { if (tVarArgs) tLength *= -1; if (oVarArgs) oLength *= -1; + if (tVarArgs && oVarArgs) return Integer.compare(tLength, oLength); + return -Integer.compare(tLength, oLength); } diff --git a/testsrc/de/steamwar/command/SubCMDSortingCommand.java b/testsrc/de/steamwar/command/SubCMDSortingCommand.java index d2a67dd..0f4e151 100644 --- a/testsrc/de/steamwar/command/SubCMDSortingCommand.java +++ b/testsrc/de/steamwar/command/SubCMDSortingCommand.java @@ -38,6 +38,11 @@ public class SubCMDSortingCommand extends TestSWCommand { throw new ExecutionIdentifier("Command with 1 parameter"); } + @Register + public void test(String s, String i1, String i2, String i3, String... args) { + throw new ExecutionIdentifier("Command with 3+n parameters"); + } + @Register public void test(String s, String... args) { throw new ExecutionIdentifier("Command with n parameters"); diff --git a/testsrc/de/steamwar/command/SubCMDSortingCommandTest.java b/testsrc/de/steamwar/command/SubCMDSortingCommandTest.java index 6981e29..1f3203c 100644 --- a/testsrc/de/steamwar/command/SubCMDSortingCommandTest.java +++ b/testsrc/de/steamwar/command/SubCMDSortingCommandTest.java @@ -58,4 +58,26 @@ public class SubCMDSortingCommandTest { assertCMDFramework(e, ExecutionIdentifier.class, "Command with n parameters"); } } + + @Test + public void testThreeArgsVarArg() { + SubCMDSortingCommand cmd = new SubCMDSortingCommand(); + try { + cmd.execute("", "", new String[]{"Hello", "World", "YoyoNow", "Hugo"}); + assert false; + } catch (Exception e) { + assertCMDFramework(e, ExecutionIdentifier.class, "Command with 3+n parameters"); + } + } + + @Test + public void testThreeArgsVarArg2() { + SubCMDSortingCommand cmd = new SubCMDSortingCommand(); + try { + cmd.execute("", "", new String[]{"Hello", "World", "YoyoNow"}); + assert false; + } catch (Exception e) { + assertCMDFramework(e, ExecutionIdentifier.class, "Command with 3+n parameters"); + } + } }