Add some unfinished new sorting
Einige Prüfungen sind fehlgeschlagen
SteamWarCI Build failed

Dieser Commit ist enthalten in:
yoyosource 2023-01-25 20:41:16 +01:00
Ursprung 69024c3bb9
Commit ef79a2e7db
5 geänderte Dateien mit 157 neuen und 10 gelöschten Zeilen

Datei anzeigen

@ -139,11 +139,8 @@ public abstract class AbstractSWCommand<T> {
}); });
} }
this.commandList.sort((o1, o2) -> { Collections.sort(commandList);
int compare = Integer.compare(-o1.subCommand.length, -o2.subCommand.length); System.out.println(commandList.stream().map(o -> o.method).collect(Collectors.toList()));
if (compare == 0) return Integer.compare(o1.comparableValue, o2.comparableValue);
return compare;
});
initialized = true; initialized = true;
} }

Datei anzeigen

@ -32,7 +32,7 @@ import java.util.function.Consumer;
import java.util.function.Function; import java.util.function.Function;
import java.util.function.Predicate; import java.util.function.Predicate;
public class SubCommand<T> { public class SubCommand<T> implements Comparable<SubCommand<T>> {
private AbstractSWCommand<T> abstractSWCommand; private AbstractSWCommand<T> abstractSWCommand;
Method method; Method method;
@ -42,7 +42,8 @@ public class SubCommand<T> {
private Function<T, ?> senderFunction; private Function<T, ?> senderFunction;
AbstractValidator<T, T> validator; AbstractValidator<T, T> validator;
boolean noTabComplete; boolean noTabComplete;
int comparableValue;
private Parameter[] parameters;
private CommandPart<T> commandPart; private CommandPart<T> commandPart;
@ -58,9 +59,7 @@ public class SubCommand<T> {
this.description = description; this.description = description;
this.noTabComplete = noTabComplete; this.noTabComplete = noTabComplete;
Parameter[] parameters = method.getParameters(); parameters = method.getParameters();
comparableValue = parameters[parameters.length - 1].isVarArgs() ? Integer.MAX_VALUE : -parameters.length;
AbstractSWCommand.Validator validator = parameters[0].getAnnotation(AbstractSWCommand.Validator.class); AbstractSWCommand.Validator validator = parameters[0].getAnnotation(AbstractSWCommand.Validator.class);
if (validator != null) { if (validator != null) {
this.validator = (AbstractValidator<T, T>) SWCommandUtils.getValidator(validator, parameters[0].getType(), localValidator); this.validator = (AbstractValidator<T, T>) SWCommandUtils.getValidator(validator, parameters[0].getType(), localValidator);
@ -72,6 +71,17 @@ public class SubCommand<T> {
senderFunction = t -> parameters[0].getType().cast(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;
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<Runnable> errors, T sender, String alias, String[] args) { boolean invoke(Consumer<Runnable> errors, T sender, String alias, String[] args) {
try { try {
if (!senderPredicate.test(sender)) { if (!senderPredicate.test(sender)) {

Datei anzeigen

@ -41,11 +41,13 @@ public class StaticValueCommandTest {
StaticValueCommand cmd = new StaticValueCommand(); StaticValueCommand cmd = new StaticValueCommand();
try { try {
cmd.execute("", "", new String[] {"hello"}); cmd.execute("", "", new String[] {"hello"});
assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with hello"); assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with hello");
} }
try { try {
cmd.execute("", "", new String[] {"world"}); cmd.execute("", "", new String[] {"world"});
assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with world"); assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with world");
} }
@ -56,16 +58,19 @@ public class StaticValueCommandTest {
StaticValueCommand cmd = new StaticValueCommand(); StaticValueCommand cmd = new StaticValueCommand();
try { try {
cmd.execute("", "", new String[] {"-a"}); cmd.execute("", "", new String[] {"-a"});
assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with false"); assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with false");
} }
try { try {
cmd.execute("", "", new String[] {"-b"}); cmd.execute("", "", new String[] {"-b"});
assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true"); assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true");
} }
try { try {
cmd.execute("", "", new String[] {"-c"}); cmd.execute("", "", new String[] {"-c"});
assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true"); assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true");
} }
@ -76,16 +81,19 @@ public class StaticValueCommandTest {
StaticValueCommand cmd = new StaticValueCommand(); StaticValueCommand cmd = new StaticValueCommand();
try { try {
cmd.execute("", "", new String[] {"-d"}); cmd.execute("", "", new String[] {"-d"});
assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true"); assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true");
} }
try { try {
cmd.execute("", "", new String[] {"-e"}); cmd.execute("", "", new String[] {"-e"});
assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with false"); assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with false");
} }
try { try {
cmd.execute("", "", new String[] {"-f"}); cmd.execute("", "", new String[] {"-f"});
assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true"); assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with true");
} }
@ -96,16 +104,19 @@ public class StaticValueCommandTest {
StaticValueCommand cmd = new StaticValueCommand(); StaticValueCommand cmd = new StaticValueCommand();
try { try {
cmd.execute("", "", new String[] {"-g"}); cmd.execute("", "", new String[] {"-g"});
assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with int 0"); assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with int 0");
} }
try { try {
cmd.execute("", "", new String[] {"-h"}); cmd.execute("", "", new String[] {"-h"});
assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with int 1"); assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with int 1");
} }
try { try {
cmd.execute("", "", new String[] {"-i"}); cmd.execute("", "", new String[] {"-i"});
assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with int 2"); assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with int 2");
} }
@ -116,16 +127,19 @@ public class StaticValueCommandTest {
StaticValueCommand cmd = new StaticValueCommand(); StaticValueCommand cmd = new StaticValueCommand();
try { try {
cmd.execute("", "", new String[] {"-j"}); cmd.execute("", "", new String[] {"-j"});
assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with long 0"); assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with long 0");
} }
try { try {
cmd.execute("", "", new String[] {"-k"}); cmd.execute("", "", new String[] {"-k"});
assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with long 1"); assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with long 1");
} }
try { try {
cmd.execute("", "", new String[] {"-l"}); cmd.execute("", "", new String[] {"-l"});
assert false;
} catch (Exception e) { } catch (Exception e) {
assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with long 2"); assertCMDFramework(e, ExecutionIdentifier.class, "RunStaticValue with long 2");
} }

Datei anzeigen

@ -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 <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) {
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");
}
}

Datei anzeigen

@ -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 <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 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");
}
}
}