Add AssertionUtils

Dieser Commit ist enthalten in:
yoyosource 2022-04-22 12:19:43 +02:00
Ursprung af7cd51937
Commit 9022c62281
3 geänderte Dateien mit 63 neuen und 21 gelöschten Zeilen

Datei anzeigen

@ -0,0 +1,26 @@
package de.steamwar;
import de.steamwar.command.CommandFrameworkException;
import lombok.experimental.UtilityClass;
import java.util.List;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
@UtilityClass
public class AssertionUtils {
public static void assertCMDFramework(Exception e, Class<?> clazz, String message) {
assertThat(e, is(instanceOf(CommandFrameworkException.class)));
assertThat(e.getCause().getCause(), is(instanceOf(clazz)));
assertThat(e.getCause().getCause().getMessage(), is(message));
}
public static <T> void assertTabCompletes(List<T> list, T... elements) {
assertThat(list.size(), is(elements.length));
if (elements.length > 0) {
assertThat(list, containsInAnyOrder(elements));
}
}
}

Datei anzeigen

@ -3,9 +3,10 @@ package de.steamwar.command;
import de.steamwar.command.dto.ExecutionIdentifier; import de.steamwar.command.dto.ExecutionIdentifier;
import org.junit.Test; import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat; import java.util.List;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is; import static de.steamwar.AssertionUtils.assertCMDFramework;
import static de.steamwar.AssertionUtils.assertTabCompletes;
public class ArgumentCommandTest { public class ArgumentCommandTest {
@ -25,8 +26,7 @@ public class ArgumentCommandTest {
try { try {
cmd.execute("test", "", new String[]{"true", "false"}); cmd.execute("test", "", new String[]{"true", "false"});
} catch (Exception e) { } catch (Exception e) {
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class))); assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with Boolean");
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with Boolean"));
} }
} }
@ -36,8 +36,7 @@ public class ArgumentCommandTest {
try { try {
cmd.execute("test", "", new String[]{"0.0", "0.0", "0.0"}); cmd.execute("test", "", new String[]{"0.0", "0.0", "0.0"});
} catch (Exception e) { } catch (Exception e) {
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class))); assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with Float");
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with Float"));
} }
} }
@ -47,8 +46,7 @@ public class ArgumentCommandTest {
try { try {
cmd.execute("test", "", new String[]{"0.0", "0.0"}); cmd.execute("test", "", new String[]{"0.0", "0.0"});
} catch (Exception e) { } catch (Exception e) {
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class))); assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with Double");
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with Double"));
} }
} }
@ -58,8 +56,7 @@ public class ArgumentCommandTest {
try { try {
cmd.execute("test", "", new String[]{"0"}); cmd.execute("test", "", new String[]{"0"});
} catch (Exception e) { } catch (Exception e) {
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class))); assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with Integer");
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with Integer"));
} }
} }
@ -69,8 +66,7 @@ public class ArgumentCommandTest {
try { try {
cmd.execute("test", "", new String[]{"0", "0"}); cmd.execute("test", "", new String[]{"0", "0"});
} catch (Exception e) { } catch (Exception e) {
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class))); assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with Long");
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with Long"));
} }
} }
@ -80,8 +76,21 @@ public class ArgumentCommandTest {
try { try {
cmd.execute("test", "", new String[]{"Hello World"}); cmd.execute("test", "", new String[]{"Hello World"});
} catch (Exception e) { } catch (Exception e) {
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class))); assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with String");
assertThat(e.getCause().getCause().getMessage(), is("RunArgument with String"));
} }
} }
@Test
public void testTabComplete() {
ArgumentCommand cmd = new ArgumentCommand();
List<String> strings = cmd.tabComplete("test", "", new String[]{""});
assertTabCompletes(strings, "true", "false");
}
@Test
public void testPartialTabComplete() {
ArgumentCommand cmd = new ArgumentCommand();
List<String> strings = cmd.tabComplete("test", "", new String[]{"t"});
assertTabCompletes(strings, "true", "t");
}
} }

Datei anzeigen

@ -3,9 +3,10 @@ package de.steamwar.command;
import de.steamwar.command.dto.ExecutionIdentifier; import de.steamwar.command.dto.ExecutionIdentifier;
import org.junit.Test; import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat; import java.util.List;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is; import static de.steamwar.AssertionUtils.assertCMDFramework;
import static de.steamwar.AssertionUtils.assertTabCompletes;
public class SimpleCommandTest { public class SimpleCommandTest {
@ -14,9 +15,8 @@ public class SimpleCommandTest {
SimpleCommand cmd = new SimpleCommand(); SimpleCommand cmd = new SimpleCommand();
try { try {
cmd.execute("test", "", new String[0]); cmd.execute("test", "", new String[0]);
} catch (CommandFrameworkException e) { } catch (Exception e) {
assertThat(e.getCause().getCause(), is(instanceOf(ExecutionIdentifier.class))); assertCMDFramework(e, ExecutionIdentifier.class, "RunSimple with noArgs");
assertThat(e.getCause().getCause().getMessage(), is("RunSimple with noArgs"));
} }
} }
@ -29,4 +29,11 @@ public class SimpleCommandTest {
throw new AssertionError("No exception expected"); throw new AssertionError("No exception expected");
} }
} }
@Test
public void testSimpleTabComplete() {
SimpleCommand cmd = new SimpleCommand();
List<String> strings = cmd.tabComplete("test", "", new String[]{""});
assertTabCompletes(strings);
}
} }