From ef79a2e7db64e3694d1f913b7e1b5a256dd25c38 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 25 Jan 2023 20:41:16 +0100 Subject: [PATCH 1/3] Add some unfinished new sorting --- .../steamwar/command/AbstractSWCommand.java | 7 +- src/de/steamwar/command/SubCommand.java | 20 ++++-- .../command/StaticValueCommandTest.java | 14 ++++ .../command/SubCMDSortingCommand.java | 54 ++++++++++++++ .../command/SubCMDSortingCommandTest.java | 72 +++++++++++++++++++ 5 files changed, 157 insertions(+), 10 deletions(-) create mode 100644 testsrc/de/steamwar/command/SubCMDSortingCommand.java create mode 100644 testsrc/de/steamwar/command/SubCMDSortingCommandTest.java diff --git a/src/de/steamwar/command/AbstractSWCommand.java b/src/de/steamwar/command/AbstractSWCommand.java index 6bc3177..1ad1969 100644 --- a/src/de/steamwar/command/AbstractSWCommand.java +++ b/src/de/steamwar/command/AbstractSWCommand.java @@ -139,11 +139,8 @@ public abstract class AbstractSWCommand { }); } - this.commandList.sort((o1, o2) -> { - int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length); - if (compare == 0) return Integer.compare(o1.comparableValue, o2.comparableValue); - return compare; - }); + 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 d74423b..86543d5 100644 --- a/src/de/steamwar/command/SubCommand.java +++ b/src/de/steamwar/command/SubCommand.java @@ -32,7 +32,7 @@ import java.util.function.Consumer; import java.util.function.Function; import java.util.function.Predicate; -public class SubCommand { +public class SubCommand implements Comparable> { private AbstractSWCommand abstractSWCommand; Method method; @@ -42,7 +42,8 @@ public class SubCommand { private Function senderFunction; AbstractValidator validator; boolean noTabComplete; - int comparableValue; + + private Parameter[] parameters; private CommandPart commandPart; @@ -58,9 +59,7 @@ public class SubCommand { this.description = description; this.noTabComplete = noTabComplete; - Parameter[] parameters = method.getParameters(); - comparableValue = parameters[parameters.length - 1].isVarArgs() ? Integer.MAX_VALUE : -parameters.length; - + parameters = method.getParameters(); AbstractSWCommand.Validator validator = parameters[0].getAnnotation(AbstractSWCommand.Validator.class); if (validator != null) { this.validator = (AbstractValidator) SWCommandUtils.getValidator(validator, parameters[0].getType(), localValidator); @@ -72,6 +71,17 @@ public class SubCommand { senderFunction = t -> parameters[0].getType().cast(t); } + @Override + public int compareTo(SubCommand o) { + int tLength = parameters.length + subCommand.length; + int oLength = o.parameters.length + o.subCommand.length; + + if (parameters[parameters.length - 1].isVarArgs()) tLength *= -1; + if (o.parameters[o.parameters.length - 1].isVarArgs()) oLength *= -1; + + return -Integer.compare(tLength, oLength); + } + boolean invoke(Consumer errors, T sender, String alias, String[] args) { try { if (!senderPredicate.test(sender)) { diff --git a/testsrc/de/steamwar/command/StaticValueCommandTest.java b/testsrc/de/steamwar/command/StaticValueCommandTest.java index 665250a..75a7dfd 100644 --- a/testsrc/de/steamwar/command/StaticValueCommandTest.java +++ b/testsrc/de/steamwar/command/StaticValueCommandTest.java @@ -41,11 +41,13 @@ public class StaticValueCommandTest { StaticValueCommand cmd = new StaticValueCommand(); try { cmd.execute("", "", new String[] {"hello"}); + assert false; } catch (Exception e) { assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with hello"); } try { cmd.execute("", "", new String[] {"world"}); + assert false; } catch (Exception e) { assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with world"); } @@ -56,16 +58,19 @@ public class StaticValueCommandTest { StaticValueCommand cmd = new StaticValueCommand(); try { cmd.execute("", "", new String[] {"-a"}); + assert false; } catch (Exception e) { assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with false"); } try { cmd.execute("", "", new String[] {"-b"}); + assert false; } catch (Exception e) { assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true"); } try { cmd.execute("", "", new String[] {"-c"}); + assert false; } catch (Exception e) { assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true"); } @@ -76,16 +81,19 @@ public class StaticValueCommandTest { StaticValueCommand cmd = new StaticValueCommand(); try { cmd.execute("", "", new String[] {"-d"}); + assert false; } catch (Exception e) { assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true"); } try { cmd.execute("", "", new String[] {"-e"}); + assert false; } catch (Exception e) { assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with false"); } try { cmd.execute("", "", new String[] {"-f"}); + assert false; } catch (Exception e) { assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true"); } @@ -96,16 +104,19 @@ public class StaticValueCommandTest { StaticValueCommand cmd = new StaticValueCommand(); try { cmd.execute("", "", new String[] {"-g"}); + assert false; } catch (Exception e) { assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with int 0"); } try { cmd.execute("", "", new String[] {"-h"}); + assert false; } catch (Exception e) { assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with int 1"); } try { cmd.execute("", "", new String[] {"-i"}); + assert false; } catch (Exception e) { assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with int 2"); } @@ -116,16 +127,19 @@ public class StaticValueCommandTest { StaticValueCommand cmd = new StaticValueCommand(); try { cmd.execute("", "", new String[] {"-j"}); + assert false; } catch (Exception e) { assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with long 0"); } try { cmd.execute("", "", new String[] {"-k"}); + assert false; } catch (Exception e) { assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with long 1"); } try { cmd.execute("", "", new String[] {"-l"}); + assert false; } catch (Exception e) { assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with long 2"); } diff --git a/testsrc/de/steamwar/command/SubCMDSortingCommand.java b/testsrc/de/steamwar/command/SubCMDSortingCommand.java new file mode 100644 index 0000000..35781e5 --- /dev/null +++ b/testsrc/de/steamwar/command/SubCMDSortingCommand.java @@ -0,0 +1,54 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.command; + +import de.steamwar.command.dto.ExecutionIdentifier; +import de.steamwar.command.dto.TestSWCommand; + +public class SubCMDSortingCommand extends TestSWCommand { + + public SubCMDSortingCommand() { + super("subcmdsorting"); + } + + @Register + public void test(String s) { + System.out.println("HERE 1"); + throw new ExecutionIdentifier("Command with 0 parameters"); + } + + @Register + public void test(String s, String... args) { + System.out.println("HERE 2"); + 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"); + } +} diff --git a/testsrc/de/steamwar/command/SubCMDSortingCommandTest.java b/testsrc/de/steamwar/command/SubCMDSortingCommandTest.java new file mode 100644 index 0000000..8f22085 --- /dev/null +++ b/testsrc/de/steamwar/command/SubCMDSortingCommandTest.java @@ -0,0 +1,72 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2022 SteamWar.de-Serverteam + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package de.steamwar.command; + +import de.steamwar.command.dto.ExecutionIdentifier; +import org.junit.Test; + +import static de.steamwar.AssertionUtils.assertCMDFramework; + +public class SubCMDSortingCommandTest { + + @Test + public void testNoArgs() { + SubCMDSortingCommand cmd = new SubCMDSortingCommand(); + try { + cmd.execute("", "", new String[]{}); + assert false; + } catch (Exception e) { + assertCMDFramework(e, ExecutionIdentifier.class, "Command with 0 parameters"); + } + } + + @Test + public void testOneArgs() { + SubCMDSortingCommand cmd = new SubCMDSortingCommand(); + try { + cmd.execute("", "", new String[]{"Hello"}); + assert false; + } catch (Exception e) { + assertCMDFramework(e, ExecutionIdentifier.class, "Command with 1 parameters"); + } + } + + @Test + public void testTwoArgs() { + SubCMDSortingCommand cmd = new SubCMDSortingCommand(); + try { + cmd.execute("", "", new String[]{"Hello", "World"}); + assert false; + } catch (Exception e) { + assertCMDFramework(e, ExecutionIdentifier.class, "Command with 2 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"); + } + } +} -- 2.39.2 From 65df8ddab08419d7daa2af625c1f87435601e483 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 2 Feb 2023 17:53:56 +0100 Subject: [PATCH 2/3] Fix command sorting with varargs --- src/de/steamwar/command/CommandPart.java | 2 ++ src/de/steamwar/command/SubCommand.java | 7 +++++-- .../command/SubCMDSortingCommand.java | 21 ++++++------------- .../command/SubCMDSortingCommandTest.java | 17 +++------------ 4 files changed, 16 insertions(+), 31 deletions(-) diff --git a/src/de/steamwar/command/CommandPart.java b/src/de/steamwar/command/CommandPart.java index 971775a..5eb264c 100644 --- a/src/de/steamwar/command/CommandPart.java +++ b/src/de/steamwar/command/CommandPart.java @@ -124,6 +124,8 @@ class CommandPart { } if (next != null) { next.generateArgumentArray(errors, current, sender, args, startIndex + 1); + } else if (startIndex + 1 < args.length) { + throw new CommandParseException(); } } diff --git a/src/de/steamwar/command/SubCommand.java b/src/de/steamwar/command/SubCommand.java index 86543d5..6c076b1 100644 --- a/src/de/steamwar/command/SubCommand.java +++ b/src/de/steamwar/command/SubCommand.java @@ -76,8 +76,11 @@ public class SubCommand implements Comparable> { int tLength = parameters.length + subCommand.length; int oLength = o.parameters.length + o.subCommand.length; - if (parameters[parameters.length - 1].isVarArgs()) tLength *= -1; - if (o.parameters[o.parameters.length - 1].isVarArgs()) oLength *= -1; + boolean tVarArgs = parameters[parameters.length - 1].isVarArgs(); + boolean oVarArgs = o.parameters[o.parameters.length - 1].isVarArgs(); + + if (tVarArgs) tLength *= -1; + if (oVarArgs) oLength *= -1; return -Integer.compare(tLength, oLength); } diff --git a/testsrc/de/steamwar/command/SubCMDSortingCommand.java b/testsrc/de/steamwar/command/SubCMDSortingCommand.java index 35781e5..d2a67dd 100644 --- a/testsrc/de/steamwar/command/SubCMDSortingCommand.java +++ b/testsrc/de/steamwar/command/SubCMDSortingCommand.java @@ -30,25 +30,16 @@ public class SubCMDSortingCommand extends TestSWCommand { @Register public void test(String s) { - System.out.println("HERE 1"); throw new ExecutionIdentifier("Command with 0 parameters"); } + @Register + public void test(String s, String args) { + throw new ExecutionIdentifier("Command with 1 parameter"); + } + @Register public void test(String s, String... args) { - System.out.println("HERE 2"); - 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"); + throw new ExecutionIdentifier("Command with n parameters"); } } diff --git a/testsrc/de/steamwar/command/SubCMDSortingCommandTest.java b/testsrc/de/steamwar/command/SubCMDSortingCommandTest.java index 8f22085..6981e29 100644 --- a/testsrc/de/steamwar/command/SubCMDSortingCommandTest.java +++ b/testsrc/de/steamwar/command/SubCMDSortingCommandTest.java @@ -44,29 +44,18 @@ public class SubCMDSortingCommandTest { cmd.execute("", "", new String[]{"Hello"}); assert false; } catch (Exception e) { - assertCMDFramework(e, ExecutionIdentifier.class, "Command with 1 parameters"); + assertCMDFramework(e, ExecutionIdentifier.class, "Command with 1 parameter"); } } @Test - public void testTwoArgs() { + public void testOneArgsVarArg() { SubCMDSortingCommand cmd = new SubCMDSortingCommand(); try { cmd.execute("", "", new String[]{"Hello", "World"}); assert false; } catch (Exception e) { - assertCMDFramework(e, ExecutionIdentifier.class, "Command with 2 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"); + assertCMDFramework(e, ExecutionIdentifier.class, "Command with n parameters"); } } } -- 2.39.2 From 45e9698634f9c6c4e8ff4de315d6133056931da5 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Thu, 2 Feb 2023 18:07:29 +0100 Subject: [PATCH 3/3] 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"); + } + } } -- 2.39.2