From 93156a097694feab1c1d40a9855eea9f5c1c3050 Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 8 Dec 2021 23:32:33 +0100 Subject: [PATCH] Add some more Tests --- .../src/de/steamwar/command/CommandPart.java | 6 +- .../de/steamwar/command/CommandPartTest.java | 73 ----------- .../command/SimpleCommandPartTest.java | 121 ++++++++++++++++++ 3 files changed, 124 insertions(+), 76 deletions(-) delete mode 100644 SpigotCore_Main/testsrc/de/steamwar/command/CommandPartTest.java create mode 100644 SpigotCore_Main/testsrc/de/steamwar/command/SimpleCommandPartTest.java diff --git a/SpigotCore_Main/src/de/steamwar/command/CommandPart.java b/SpigotCore_Main/src/de/steamwar/command/CommandPart.java index 720ebde..27762e4 100644 --- a/SpigotCore_Main/src/de/steamwar/command/CommandPart.java +++ b/SpigotCore_Main/src/de/steamwar/command/CommandPart.java @@ -37,8 +37,8 @@ public class CommandPart { private TypeMapper typeMapper; private GuardChecker guard; - private Class varArgType = null; - private String optional = null; + private Class varArgType; + private String optional; private boolean help; private CommandPart next = null; @@ -97,7 +97,7 @@ public class CommandPart { } void generateTabComplete(List current, CommandSender commandSender, String[] args, int startIndex) { - if (args.length < startIndex) { + if (args.length - 1 > startIndex) { CheckArgumentResult checkArgumentResult = checkArgument(GuardCheckType.TAB_COMPLETE, commandSender, args, startIndex); if (checkArgumentResult.success && next != null) { next.generateTabComplete(current, commandSender, args, startIndex + 1); diff --git a/SpigotCore_Main/testsrc/de/steamwar/command/CommandPartTest.java b/SpigotCore_Main/testsrc/de/steamwar/command/CommandPartTest.java deleted file mode 100644 index 6d86763..0000000 --- a/SpigotCore_Main/testsrc/de/steamwar/command/CommandPartTest.java +++ /dev/null @@ -1,73 +0,0 @@ -/* - * This file is a part of the SteamWar software. - * - * Copyright (C) 2020 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 . - */ - -package de.steamwar.command; - -import de.steamwar.TestCommandSender; -import org.junit.Before; -import org.junit.Test; - -import java.util.ArrayList; -import java.util.List; - -import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.instanceOf; -import static org.hamcrest.Matchers.is; - -public class CommandPartTest { - - private CommandPart commandPart; - - @Before - public void setUp() throws Exception { - commandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, false); - } - - @Test - public void testCommandPartTabCompleteNoArguments() { - List tabComplete = new ArrayList<>(); - commandPart.generateTabComplete(tabComplete, new TestCommandSender(), new String[]{""}, 0); - assertThat(tabComplete.size(), is(2)); - assertThat(tabComplete.get(0), is("hello")); - assertThat(tabComplete.get(1), is("world")); - } - - @Test(expected = CommandParseException.class) - public void testCommandExecuteInvalidArgument() { - commandPart.generateArgumentArray(new ArrayList<>(), new TestCommandSender(), new String[]{""}, 0); - } - - @Test - public void testCommandExecuteValidArgument() { - List argumentArray = new ArrayList<>(); - commandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello"}, 0); - assertThat(argumentArray.size(), is(1)); - assertThat(argumentArray.get(0), instanceOf(String.class)); - assertThat(argumentArray.get(0), is("hello")); - } - - @Test - public void testCommandExecuteValidOtherArgument() { - List argumentArray = new ArrayList<>(); - commandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"world"}, 0); - assertThat(argumentArray.size(), is(1)); - assertThat(argumentArray.get(0), instanceOf(String.class)); - assertThat(argumentArray.get(0), is("world")); - } -} diff --git a/SpigotCore_Main/testsrc/de/steamwar/command/SimpleCommandPartTest.java b/SpigotCore_Main/testsrc/de/steamwar/command/SimpleCommandPartTest.java new file mode 100644 index 0000000..1d7b99f --- /dev/null +++ b/SpigotCore_Main/testsrc/de/steamwar/command/SimpleCommandPartTest.java @@ -0,0 +1,121 @@ +/* + * This file is a part of the SteamWar software. + * + * Copyright (C) 2020 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 . + */ + +package de.steamwar.command; + +import de.steamwar.TestCommandSender; +import org.junit.Before; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.is; + +public class SimpleCommandPartTest { + + private CommandPart stringCommandPart; + private CommandPart intCommandPart; + private CommandPart chainedCommandPart; + + @Before + public void setUp() throws Exception { + stringCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, false); + intCommandPart = new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, true); + + chainedCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, false); + chainedCommandPart.setNext(new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, true)); + } + + @Test + public void testCommandPartTabCompleteNoArguments() { + List tabComplete = new ArrayList<>(); + stringCommandPart.generateTabComplete(tabComplete, new TestCommandSender(), new String[]{""}, 0); + assertThat(tabComplete.size(), is(2)); + assertThat(tabComplete.get(0), is("hello")); + assertThat(tabComplete.get(1), is("world")); + } + + @Test(expected = CommandParseException.class) + public void testCommandExecuteInvalidArgument() { + stringCommandPart.generateArgumentArray(new ArrayList<>(), new TestCommandSender(), new String[]{""}, 0); + } + + @Test + public void testCommandExecuteValidArgument() { + List argumentArray = new ArrayList<>(); + stringCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello"}, 0); + assertThat(argumentArray.size(), is(1)); + assertThat(argumentArray.get(0), instanceOf(String.class)); + assertThat(argumentArray.get(0), is("hello")); + } + + @Test + public void testCommandExecuteValidOtherArgument() { + List argumentArray = new ArrayList<>(); + stringCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"world"}, 0); + assertThat(argumentArray.size(), is(1)); + assertThat(argumentArray.get(0), instanceOf(String.class)); + assertThat(argumentArray.get(0), is("world")); + } + + @Test(expected = CommandParseException.class) + public void testCommandExecuteNonNumberArgument() { + intCommandPart.generateArgumentArray(new ArrayList<>(), new TestCommandSender(), new String[]{"world"}, 0); + } + + @Test + public void testCommandExecuteValidNumberArgument() { + List argumentArray = new ArrayList<>(); + intCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"0"}, 0); + assertThat(argumentArray.size(), is(1)); + assertThat(argumentArray.get(0), instanceOf(int.class)); + assertThat(argumentArray.get(0), is(0)); + } + + @Test + public void testChainedCommandExecuteValidArgument() { + List argumentArray = new ArrayList<>(); + chainedCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello", "0"}, 0); + assertThat(argumentArray.size(), is(2)); + assertThat(argumentArray.get(0), instanceOf(String.class)); + assertThat(argumentArray.get(0), is("hello")); + assertThat(argumentArray.get(1), instanceOf(int.class)); + assertThat(argumentArray.get(1), is(0)); + } + + @Test + public void testChainedCommandTabComplete() { + List tabCompletes = new ArrayList<>(); + chainedCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{""}, 0); + assertThat(tabCompletes.size(), is(2)); + assertThat(tabCompletes.get(0), is("hello")); + assertThat(tabCompletes.get(1), is("world")); + } + + @Test + public void testChainedCommandTabCompleteOther() { + List tabCompletes = new ArrayList<>(); + chainedCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", ""}, 0); + System.out.println(tabCompletes); + assertThat(tabCompletes.size(), is(0)); + } +}