From 58221d50c26507685741ba3de24b9ccd04fa778d Mon Sep 17 00:00:00 2001 From: yoyosource Date: Wed, 8 Dec 2021 23:20:58 +0100 Subject: [PATCH] Add CommandPartTest --- .../src/de/steamwar/command/CommandPart.java | 29 +++++++- .../de/steamwar/command/CommandPartTest.java | 73 +++++++++++++++++++ 2 files changed, 98 insertions(+), 4 deletions(-) create mode 100644 SpigotCore_Main/testsrc/de/steamwar/command/CommandPartTest.java diff --git a/SpigotCore_Main/src/de/steamwar/command/CommandPart.java b/SpigotCore_Main/src/de/steamwar/command/CommandPart.java index bcb6c3f..720ebde 100644 --- a/SpigotCore_Main/src/de/steamwar/command/CommandPart.java +++ b/SpigotCore_Main/src/de/steamwar/command/CommandPart.java @@ -85,22 +85,43 @@ public class CommandPart { } if (optional != null) { current.add(typeMapper.map(commandSender, EMPTY_ARRAY, optional)); - next.generateArgumentArray(current, commandSender, args, startIndex); + if (next != null) { + next.generateArgumentArray(current, commandSender, args, startIndex); + } return; } current.add(validArgument.value); - next.generateArgumentArray(current, commandSender, args, startIndex + 1); + if (next != null) { + next.generateArgumentArray(current, commandSender, args, startIndex + 1); + } } void generateTabComplete(List current, CommandSender commandSender, String[] args, int startIndex) { + if (args.length < startIndex) { + CheckArgumentResult checkArgumentResult = checkArgument(GuardCheckType.TAB_COMPLETE, commandSender, args, startIndex); + if (checkArgumentResult.success && next != null) { + next.generateTabComplete(current, commandSender, args, startIndex + 1); + } + return; + } + List strings = typeMapper.tabCompletes(commandSender, Arrays.copyOf(args, startIndex), args[startIndex]); + if (strings != null) { + current.addAll(strings); + } + if (optional != null && next != null) { + next.generateTabComplete(current, commandSender, args, startIndex); + } } private CheckArgumentResult checkArgument(GuardCheckType guardCheckType, CommandSender commandSender, String[] args, int index) { try { - Object value = typeMapper.map(commandSender, Arrays.copyOf(args, index - 1), args[index]); + Object value = typeMapper.map(commandSender, Arrays.copyOf(args, index), args[index]); + if (value == null) { + return new CheckArgumentResult(false, null); + } if (guard != null) { - GuardResult guardResult = guard.guard(commandSender, GuardCheckType.TAB_COMPLETE, Arrays.copyOf(args, index - 1), args[index]); + GuardResult guardResult = guard.guard(commandSender, guardCheckType, Arrays.copyOf(args, index), args[index]); switch (guardResult) { case ALLOWED: return new CheckArgumentResult(true, value); diff --git a/SpigotCore_Main/testsrc/de/steamwar/command/CommandPartTest.java b/SpigotCore_Main/testsrc/de/steamwar/command/CommandPartTest.java new file mode 100644 index 0000000..6d86763 --- /dev/null +++ b/SpigotCore_Main/testsrc/de/steamwar/command/CommandPartTest.java @@ -0,0 +1,73 @@ +/* + * 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")); + } +}