CommonDB #46
@ -139,11 +139,7 @@ public abstract class AbstractSWCommand<T> {
|
||||
});
|
||||
}
|
||||
|
||||
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);
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
|
@ -124,6 +124,8 @@ class CommandPart<T> {
|
||||
}
|
||||
if (next != null) {
|
||||
next.generateArgumentArray(errors, current, sender, args, startIndex + 1);
|
||||
} else if (startIndex + 1 < args.length) {
|
||||
throw new CommandParseException();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -32,7 +32,7 @@ import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
public class SubCommand<T> {
|
||||
public class SubCommand<T> implements Comparable<SubCommand<T>> {
|
||||
|
||||
private AbstractSWCommand<T> abstractSWCommand;
|
||||
Method method;
|
||||
@ -42,7 +42,8 @@ public class SubCommand<T> {
|
||||
private Function<T, ?> senderFunction;
|
||||
AbstractValidator<T, T> validator;
|
||||
boolean noTabComplete;
|
||||
int comparableValue;
|
||||
|
||||
private Parameter[] parameters;
|
||||
|
||||
private CommandPart<T> commandPart;
|
||||
|
||||
@ -58,9 +59,7 @@ public class SubCommand<T> {
|
||||
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<T, T>) SWCommandUtils.getValidator(validator, parameters[0].getType(), localValidator);
|
||||
@ -72,6 +71,22 @@ public class SubCommand<T> {
|
||||
senderFunction = t -> parameters[0].getType().cast(t);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int compareTo(SubCommand<T> o) {
|
||||
int tLength = parameters.length + subCommand.length;
|
||||
int oLength = o.parameters.length + o.subCommand.length;
|
||||
|
||||
boolean tVarArgs = parameters[parameters.length - 1].isVarArgs();
|
||||
boolean oVarArgs = o.parameters[o.parameters.length - 1].isVarArgs();
|
||||
|
||||
if (tVarArgs) tLength *= -1;
|
||||
if (oVarArgs) oLength *= -1;
|
||||
|
||||
if (tVarArgs && oVarArgs) return Integer.compare(tLength, oLength);
|
||||
|
||||
return -Integer.compare(tLength, oLength);
|
||||
}
|
||||
|
||||
boolean invoke(Consumer<Runnable> errors, T sender, String alias, String[] args) {
|
||||
try {
|
||||
if (!senderPredicate.test(sender)) {
|
||||
|
@ -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");
|
||||
}
|
||||
|
50
testsrc/de/steamwar/command/SubCMDSortingCommand.java
Normale Datei
50
testsrc/de/steamwar/command/SubCMDSortingCommand.java
Normale Datei
@ -0,0 +1,50 @@
|
||||
/*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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) {
|
||||
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 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");
|
||||
}
|
||||
}
|
83
testsrc/de/steamwar/command/SubCMDSortingCommandTest.java
Normale Datei
83
testsrc/de/steamwar/command/SubCMDSortingCommandTest.java
Normale Datei
@ -0,0 +1,83 @@
|
||||
/*
|
||||
* 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 <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
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 parameter");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
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 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");
|
||||
}
|
||||
}
|
||||
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren