diff --git a/build.gradle b/build.gradle index 7397719..b625ffd 100644 --- a/build.gradle +++ b/build.gradle @@ -62,6 +62,16 @@ sourceSets { exclude '**/*.java', '**/*.kt' } } + + test { + java { + srcDirs = ['testsrc'] + } + resources { + srcDirs = ['testsrc'] + exclude '**/*.java', '**/*.kt' + } + } } repositories { @@ -73,6 +83,9 @@ dependencies { testCompileOnly 'org.projectlombok:lombok:1.18.22' annotationProcessor '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 { diff --git a/src/de/steamwar/command/SWCommandUtils.java b/src/de/steamwar/command/SWCommandUtils.java index 52e1d75..4a6088a 100644 --- a/src/de/steamwar/command/SWCommandUtils.java +++ b/src/de/steamwar/command/SWCommandUtils.java @@ -21,7 +21,11 @@ public class SWCommandUtils { private final Map> GUARD_FUNCTIONS = new HashMap<>(); 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(double.class, Double.class, createMapper(numberMapper(Double::parseDouble), numberCompleter(Double::parseDouble))); addMapper(int.class, Integer.class, createMapper(numberMapper(Integer::parseInt), numberCompleter(Integer::parseInt))); diff --git a/src/de/steamwar/command/SubCommand.java b/src/de/steamwar/command/SubCommand.java index 4b9a4a6..2f41791 100644 --- a/src/de/steamwar/command/SubCommand.java +++ b/src/de/steamwar/command/SubCommand.java @@ -4,6 +4,7 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.lang.reflect.Parameter; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.function.Function; diff --git a/testsrc/de/steamwar/command/ArgumentCommand.java b/testsrc/de/steamwar/command/ArgumentCommand.java new file mode 100644 index 0000000..c630a93 --- /dev/null +++ b/testsrc/de/steamwar/command/ArgumentCommand.java @@ -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"); + } +} diff --git a/testsrc/de/steamwar/command/ArgumentCommandTest.java b/testsrc/de/steamwar/command/ArgumentCommandTest.java new file mode 100644 index 0000000..d7a24f1 --- /dev/null +++ b/testsrc/de/steamwar/command/ArgumentCommandTest.java @@ -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")); + } + } +} diff --git a/testsrc/de/steamwar/command/SimpleCommand.java b/testsrc/de/steamwar/command/SimpleCommand.java new file mode 100644 index 0000000..88d0bd6 --- /dev/null +++ b/testsrc/de/steamwar/command/SimpleCommand.java @@ -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"); + } +} diff --git a/testsrc/de/steamwar/command/SimpleCommandTest.java b/testsrc/de/steamwar/command/SimpleCommandTest.java new file mode 100644 index 0000000..a039e75 --- /dev/null +++ b/testsrc/de/steamwar/command/SimpleCommandTest.java @@ -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"); + } + } +} diff --git a/testsrc/de/steamwar/command/dto/ExecutionIdentifier.java b/testsrc/de/steamwar/command/dto/ExecutionIdentifier.java new file mode 100644 index 0000000..bde7122 --- /dev/null +++ b/testsrc/de/steamwar/command/dto/ExecutionIdentifier.java @@ -0,0 +1,7 @@ +package de.steamwar.command.dto; + +public class ExecutionIdentifier extends RuntimeException { + public ExecutionIdentifier(String message) { + super(message); + } +} diff --git a/testsrc/de/steamwar/command/dto/TestGuardChecker.java b/testsrc/de/steamwar/command/dto/TestGuardChecker.java new file mode 100644 index 0000000..ff38ebc --- /dev/null +++ b/testsrc/de/steamwar/command/dto/TestGuardChecker.java @@ -0,0 +1,6 @@ +package de.steamwar.command.dto; + +import de.steamwar.command.AbstractGuardChecker; + +public interface TestGuardChecker extends AbstractGuardChecker { +} diff --git a/testsrc/de/steamwar/command/dto/TestSWCommand.java b/testsrc/de/steamwar/command/dto/TestSWCommand.java new file mode 100644 index 0000000..9081000 --- /dev/null +++ b/testsrc/de/steamwar/command/dto/TestSWCommand.java @@ -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 { + + 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 message) { + } +} diff --git a/testsrc/de/steamwar/command/dto/TestTypeChecker.java b/testsrc/de/steamwar/command/dto/TestTypeChecker.java new file mode 100644 index 0000000..42a7933 --- /dev/null +++ b/testsrc/de/steamwar/command/dto/TestTypeChecker.java @@ -0,0 +1,6 @@ +package de.steamwar.command.dto; + +import de.steamwar.command.AbstractTypeMapper; + +public interface TestTypeChecker extends AbstractTypeMapper { +}