From 9022c62281dec823f48786b46fd15ba8007ddd1c Mon Sep 17 00:00:00 2001 From: yoyosource Date: Fri, 22 Apr 2022 12:19:43 +0200 Subject: [PATCH] Add AssertionUtils --- testsrc/de/steamwar/AssertionUtils.java | 26 +++++++++++++ .../steamwar/command/ArgumentCommandTest.java | 39 ++++++++++++------- .../steamwar/command/SimpleCommandTest.java | 19 ++++++--- 3 files changed, 63 insertions(+), 21 deletions(-) create mode 100644 testsrc/de/steamwar/AssertionUtils.java diff --git a/testsrc/de/steamwar/AssertionUtils.java b/testsrc/de/steamwar/AssertionUtils.java new file mode 100644 index 0000000..58fe01c --- /dev/null +++ b/testsrc/de/steamwar/AssertionUtils.java @@ -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 void assertTabCompletes(List list, T... elements) { + assertThat(list.size(), is(elements.length)); + if (elements.length > 0) { + assertThat(list, containsInAnyOrder(elements)); + } + } +} diff --git a/testsrc/de/steamwar/command/ArgumentCommandTest.java b/testsrc/de/steamwar/command/ArgumentCommandTest.java index d7a24f1..7b74126 100644 --- a/testsrc/de/steamwar/command/ArgumentCommandTest.java +++ b/testsrc/de/steamwar/command/ArgumentCommandTest.java @@ -3,9 +3,10 @@ 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; +import java.util.List; + +import static de.steamwar.AssertionUtils.assertCMDFramework; +import static de.steamwar.AssertionUtils.assertTabCompletes; public class ArgumentCommandTest { @@ -25,8 +26,7 @@ public class ArgumentCommandTest { 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")); + assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with Boolean"); } } @@ -36,8 +36,7 @@ public class ArgumentCommandTest { 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")); + assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with Float"); } } @@ -47,8 +46,7 @@ public class ArgumentCommandTest { 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")); + assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with Double"); } } @@ -58,8 +56,7 @@ public class ArgumentCommandTest { 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")); + assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with Integer"); } } @@ -69,8 +66,7 @@ public class ArgumentCommandTest { 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")); + assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with Long"); } } @@ -80,8 +76,21 @@ public class ArgumentCommandTest { 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")); + assertCMDFramework(e, ExecutionIdentifier.class, "RunArgument with String"); } } + + @Test + public void testTabComplete() { + ArgumentCommand cmd = new ArgumentCommand(); + List strings = cmd.tabComplete("test", "", new String[]{""}); + assertTabCompletes(strings, "true", "false"); + } + + @Test + public void testPartialTabComplete() { + ArgumentCommand cmd = new ArgumentCommand(); + List strings = cmd.tabComplete("test", "", new String[]{"t"}); + assertTabCompletes(strings, "true", "t"); + } } diff --git a/testsrc/de/steamwar/command/SimpleCommandTest.java b/testsrc/de/steamwar/command/SimpleCommandTest.java index a039e75..4ba3ef1 100644 --- a/testsrc/de/steamwar/command/SimpleCommandTest.java +++ b/testsrc/de/steamwar/command/SimpleCommandTest.java @@ -3,9 +3,10 @@ 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; +import java.util.List; + +import static de.steamwar.AssertionUtils.assertCMDFramework; +import static de.steamwar.AssertionUtils.assertTabCompletes; public class SimpleCommandTest { @@ -14,9 +15,8 @@ public class SimpleCommandTest { 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")); + } catch (Exception e) { + assertCMDFramework(e, ExecutionIdentifier.class, "RunSimple with noArgs"); } } @@ -29,4 +29,11 @@ public class SimpleCommandTest { throw new AssertionError("No exception expected"); } } + + @Test + public void testSimpleTabComplete() { + SimpleCommand cmd = new SimpleCommand(); + List strings = cmd.tabComplete("test", "", new String[]{""}); + assertTabCompletes(strings); + } }