Add some simple tests. More to come
Dieser Commit ist enthalten in:
Ursprung
598e8b4061
Commit
af7cd51937
13
build.gradle
13
build.gradle
@ -62,6 +62,16 @@ sourceSets {
|
|||||||
exclude '**/*.java', '**/*.kt'
|
exclude '**/*.java', '**/*.kt'
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
test {
|
||||||
|
java {
|
||||||
|
srcDirs = ['testsrc']
|
||||||
|
}
|
||||||
|
resources {
|
||||||
|
srcDirs = ['testsrc']
|
||||||
|
exclude '**/*.java', '**/*.kt'
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
repositories {
|
repositories {
|
||||||
@ -73,6 +83,9 @@ dependencies {
|
|||||||
testCompileOnly 'org.projectlombok:lombok:1.18.22'
|
testCompileOnly 'org.projectlombok:lombok:1.18.22'
|
||||||
annotationProcessor 'org.projectlombok:lombok:1.18.22'
|
annotationProcessor 'org.projectlombok:lombok:1.18.22'
|
||||||
testAnnotationProcessor 'org.projectlombok:lombok:1.18.22'
|
testAnnotationProcessor 'org.projectlombok:lombok:1.18.22'
|
||||||
|
|
||||||
|
testImplementation 'junit:junit:4.13.2'
|
||||||
|
testImplementation 'org.hamcrest:hamcrest:2.2'
|
||||||
}
|
}
|
||||||
|
|
||||||
task buildProject {
|
task buildProject {
|
||||||
|
@ -21,7 +21,11 @@ public class SWCommandUtils {
|
|||||||
private final Map<String, AbstractGuardChecker<?>> GUARD_FUNCTIONS = new HashMap<>();
|
private final Map<String, AbstractGuardChecker<?>> GUARD_FUNCTIONS = new HashMap<>();
|
||||||
|
|
||||||
static {
|
static {
|
||||||
addMapper(boolean.class, Boolean.class, createMapper(Boolean::parseBoolean, s -> Arrays.asList("true", "false")));
|
addMapper(boolean.class, Boolean.class, createMapper(s -> {
|
||||||
|
if (s.equalsIgnoreCase("true")) return true;
|
||||||
|
if (s.equalsIgnoreCase("false")) return false;
|
||||||
|
return null;
|
||||||
|
}, s -> Arrays.asList("true", "false")));
|
||||||
addMapper(float.class, Float.class, createMapper(numberMapper(Float::parseFloat), numberCompleter(Float::parseFloat)));
|
addMapper(float.class, Float.class, createMapper(numberMapper(Float::parseFloat), numberCompleter(Float::parseFloat)));
|
||||||
addMapper(double.class, Double.class, createMapper(numberMapper(Double::parseDouble), numberCompleter(Double::parseDouble)));
|
addMapper(double.class, Double.class, createMapper(numberMapper(Double::parseDouble), numberCompleter(Double::parseDouble)));
|
||||||
addMapper(int.class, Integer.class, createMapper(numberMapper(Integer::parseInt), numberCompleter(Integer::parseInt)));
|
addMapper(int.class, Integer.class, createMapper(numberMapper(Integer::parseInt), numberCompleter(Integer::parseInt)));
|
||||||
|
@ -4,6 +4,7 @@ import java.lang.reflect.InvocationTargetException;
|
|||||||
import java.lang.reflect.Method;
|
import java.lang.reflect.Method;
|
||||||
import java.lang.reflect.Parameter;
|
import java.lang.reflect.Parameter;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.function.Function;
|
import java.util.function.Function;
|
||||||
|
41
testsrc/de/steamwar/command/ArgumentCommand.java
Normale Datei
41
testsrc/de/steamwar/command/ArgumentCommand.java
Normale Datei
@ -0,0 +1,41 @@
|
|||||||
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
import de.steamwar.command.dto.ExecutionIdentifier;
|
||||||
|
import de.steamwar.command.dto.TestSWCommand;
|
||||||
|
|
||||||
|
public class ArgumentCommand extends TestSWCommand {
|
||||||
|
|
||||||
|
public ArgumentCommand() {
|
||||||
|
super("argument");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Register
|
||||||
|
public void argument(String sender, boolean b, boolean b2) {
|
||||||
|
throw new ExecutionIdentifier("RunArgument with Boolean");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Register
|
||||||
|
public void argument(String sender, float f, float f2, float f3) {
|
||||||
|
throw new ExecutionIdentifier("RunArgument with Float");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Register
|
||||||
|
public void argument(String sender, double d, double d2) {
|
||||||
|
throw new ExecutionIdentifier("RunArgument with Double");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Register
|
||||||
|
public void argument(String sender, int i) {
|
||||||
|
throw new ExecutionIdentifier("RunArgument with Integer");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Register
|
||||||
|
public void argument(String sender, long l, long l2) {
|
||||||
|
throw new ExecutionIdentifier("RunArgument with Long");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Register
|
||||||
|
public void argument(String sender, String arg) {
|
||||||
|
throw new ExecutionIdentifier("RunArgument with String");
|
||||||
|
}
|
||||||
|
}
|
87
testsrc/de/steamwar/command/ArgumentCommandTest.java
Normale Datei
87
testsrc/de/steamwar/command/ArgumentCommandTest.java
Normale Datei
@ -0,0 +1,87 @@
|
|||||||
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
import de.steamwar.command.dto.ExecutionIdentifier;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.instanceOf;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
|
||||||
|
public class ArgumentCommandTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testNoArgs() {
|
||||||
|
ArgumentCommand cmd = new ArgumentCommand();
|
||||||
|
try {
|
||||||
|
cmd.execute("test", "", new String[0]);
|
||||||
|
} catch (Exception e) {
|
||||||
|
throw new AssertionError("No exception expected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testBoolean() {
|
||||||
|
ArgumentCommand cmd = new ArgumentCommand();
|
||||||
|
try {
|
||||||
|
cmd.execute("test", "", new String[]{"true", "false"});
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class)));
|
||||||
|
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with Boolean"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testFloat() {
|
||||||
|
ArgumentCommand cmd = new ArgumentCommand();
|
||||||
|
try {
|
||||||
|
cmd.execute("test", "", new String[]{"0.0", "0.0", "0.0"});
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class)));
|
||||||
|
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with Float"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testDouble() {
|
||||||
|
ArgumentCommand cmd = new ArgumentCommand();
|
||||||
|
try {
|
||||||
|
cmd.execute("test", "", new String[]{"0.0", "0.0"});
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class)));
|
||||||
|
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with Double"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testInt() {
|
||||||
|
ArgumentCommand cmd = new ArgumentCommand();
|
||||||
|
try {
|
||||||
|
cmd.execute("test", "", new String[]{"0"});
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class)));
|
||||||
|
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with Integer"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testLong() {
|
||||||
|
ArgumentCommand cmd = new ArgumentCommand();
|
||||||
|
try {
|
||||||
|
cmd.execute("test", "", new String[]{"0", "0"});
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class)));
|
||||||
|
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with Long"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testString() {
|
||||||
|
ArgumentCommand cmd = new ArgumentCommand();
|
||||||
|
try {
|
||||||
|
cmd.execute("test", "", new String[]{"Hello World"});
|
||||||
|
} catch (Exception e) {
|
||||||
|
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class)));
|
||||||
|
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with String"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
16
testsrc/de/steamwar/command/SimpleCommand.java
Normale Datei
16
testsrc/de/steamwar/command/SimpleCommand.java
Normale Datei
@ -0,0 +1,16 @@
|
|||||||
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
import de.steamwar.command.dto.ExecutionIdentifier;
|
||||||
|
import de.steamwar.command.dto.TestSWCommand;
|
||||||
|
|
||||||
|
public class SimpleCommand extends TestSWCommand {
|
||||||
|
|
||||||
|
public SimpleCommand() {
|
||||||
|
super("simple");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Register
|
||||||
|
public void simple(String s) {
|
||||||
|
throw new ExecutionIdentifier("RunSimple with noArgs");
|
||||||
|
}
|
||||||
|
}
|
32
testsrc/de/steamwar/command/SimpleCommandTest.java
Normale Datei
32
testsrc/de/steamwar/command/SimpleCommandTest.java
Normale Datei
@ -0,0 +1,32 @@
|
|||||||
|
package de.steamwar.command;
|
||||||
|
|
||||||
|
import de.steamwar.command.dto.ExecutionIdentifier;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
|
import static org.hamcrest.Matchers.instanceOf;
|
||||||
|
import static org.hamcrest.Matchers.is;
|
||||||
|
|
||||||
|
public class SimpleCommandTest {
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleParsing() {
|
||||||
|
SimpleCommand cmd = new SimpleCommand();
|
||||||
|
try {
|
||||||
|
cmd.execute("test", "", new String[0]);
|
||||||
|
} catch (CommandFrameworkException e) {
|
||||||
|
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class)));
|
||||||
|
assertThat(e.getCause().getCause().getMessage(), is("RunSimple with noArgs"));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testSimpleParsingNoResult() {
|
||||||
|
SimpleCommand cmd = new SimpleCommand();
|
||||||
|
try {
|
||||||
|
cmd.execute("test", "", new String[]{"Hello"});
|
||||||
|
} catch (CommandFrameworkException e) {
|
||||||
|
throw new AssertionError("No exception expected");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
7
testsrc/de/steamwar/command/dto/ExecutionIdentifier.java
Normale Datei
7
testsrc/de/steamwar/command/dto/ExecutionIdentifier.java
Normale Datei
@ -0,0 +1,7 @@
|
|||||||
|
package de.steamwar.command.dto;
|
||||||
|
|
||||||
|
public class ExecutionIdentifier extends RuntimeException {
|
||||||
|
public ExecutionIdentifier(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
|
}
|
6
testsrc/de/steamwar/command/dto/TestGuardChecker.java
Normale Datei
6
testsrc/de/steamwar/command/dto/TestGuardChecker.java
Normale Datei
@ -0,0 +1,6 @@
|
|||||||
|
package de.steamwar.command.dto;
|
||||||
|
|
||||||
|
import de.steamwar.command.AbstractGuardChecker;
|
||||||
|
|
||||||
|
public interface TestGuardChecker extends AbstractGuardChecker<String> {
|
||||||
|
}
|
40
testsrc/de/steamwar/command/dto/TestSWCommand.java
Normale Datei
40
testsrc/de/steamwar/command/dto/TestSWCommand.java
Normale Datei
@ -0,0 +1,40 @@
|
|||||||
|
package de.steamwar.command.dto;
|
||||||
|
|
||||||
|
import de.steamwar.command.AbstractSWCommand;
|
||||||
|
import de.steamwar.command.CommandFrameworkException;
|
||||||
|
|
||||||
|
import java.util.function.Supplier;
|
||||||
|
|
||||||
|
public class TestSWCommand extends AbstractSWCommand<String> {
|
||||||
|
|
||||||
|
protected TestSWCommand(String command) {
|
||||||
|
super(String.class, command);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected TestSWCommand(String command, String[] aliases) {
|
||||||
|
super(String.class, command, aliases);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void createAndSafeCommand(String command, String[] aliases) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void unregister() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void register() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void commandSystemError(String sender, CommandFrameworkException e) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void commandSystemWarning(Supplier<String> message) {
|
||||||
|
}
|
||||||
|
}
|
6
testsrc/de/steamwar/command/dto/TestTypeChecker.java
Normale Datei
6
testsrc/de/steamwar/command/dto/TestTypeChecker.java
Normale Datei
@ -0,0 +1,6 @@
|
|||||||
|
package de.steamwar.command.dto;
|
||||||
|
|
||||||
|
import de.steamwar.command.AbstractTypeMapper;
|
||||||
|
|
||||||
|
public interface TestTypeChecker<T> extends AbstractTypeMapper<String, T> {
|
||||||
|
}
|
Laden…
In neuem Issue referenzieren
Einen Benutzer sperren