2021-12-08 23:32:33 +01:00
|
|
|
/*
|
|
|
|
* 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 <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
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;
|
2021-12-09 15:09:07 +01:00
|
|
|
import static org.hamcrest.Matchers.*;
|
2021-12-08 23:32:33 +01:00
|
|
|
|
|
|
|
public class SimpleCommandPartTest {
|
|
|
|
|
|
|
|
private CommandPart stringCommandPart;
|
|
|
|
private CommandPart intCommandPart;
|
|
|
|
private CommandPart chainedCommandPart;
|
2021-12-09 15:09:07 +01:00
|
|
|
private CommandPart varArgCommandPart;
|
2021-12-08 23:32:33 +01:00
|
|
|
|
2021-12-09 13:24:28 +01:00
|
|
|
private CommandPart simpleGuardPart;
|
|
|
|
|
2021-12-09 15:23:46 +01:00
|
|
|
private CommandPart optionalCommandPart;
|
|
|
|
|
2021-12-24 13:16:46 +01:00
|
|
|
private CommandPart optionalCommandPartMultipleNext;
|
|
|
|
|
2021-12-08 23:32:33 +01:00
|
|
|
@Before
|
|
|
|
public void setUp() throws Exception {
|
2021-12-09 13:24:28 +01:00
|
|
|
stringCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, GuardCheckType.COMMAND);
|
|
|
|
intCommandPart = new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, GuardCheckType.COMMAND);
|
|
|
|
|
|
|
|
chainedCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, null, GuardCheckType.COMMAND);
|
|
|
|
chainedCommandPart.setNext(new CommandPart(SWCommandUtils.MAPPER_FUNCTIONS.get("int"), null, null, null, GuardCheckType.COMMAND));
|
2021-12-08 23:32:33 +01:00
|
|
|
|
2021-12-09 15:09:07 +01:00
|
|
|
varArgCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, String.class, null, GuardCheckType.COMMAND);
|
|
|
|
|
2021-12-09 13:24:28 +01:00
|
|
|
simpleGuardPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), (commandSender, guardCheckType, previousArguments, s) -> s.equals("hello") ? GuardResult.DENIED : GuardResult.ALLOWED, null, null, GuardCheckType.COMMAND);
|
2021-12-09 15:23:46 +01:00
|
|
|
|
|
|
|
optionalCommandPart = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, "hello", GuardCheckType.COMMAND);
|
|
|
|
optionalCommandPart.setNext(new CommandPart(SWCommandUtils.createMapper("hello2", "world2"), null, null, null, GuardCheckType.COMMAND));
|
2021-12-24 13:16:46 +01:00
|
|
|
|
|
|
|
optionalCommandPartMultipleNext = new CommandPart(SWCommandUtils.createMapper("hello", "world"), null, null, "hello", GuardCheckType.COMMAND);
|
|
|
|
CommandPart next = new CommandPart(SWCommandUtils.createMapper("hello2", "world2"), null, null, null, GuardCheckType.COMMAND);
|
|
|
|
next.setNext(new CommandPart(SWCommandUtils.createMapper("hello3", "world3"), null, null, null, GuardCheckType.COMMAND));
|
|
|
|
optionalCommandPartMultipleNext.setNext(next);
|
2021-12-08 23:32:33 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testCommandPartTabCompleteNoArguments() {
|
|
|
|
List<String> 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<Object> 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<Object> 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<Object> 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<Object> 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<String> 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<String> tabCompletes = new ArrayList<>();
|
|
|
|
chainedCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", ""}, 0);
|
|
|
|
assertThat(tabCompletes.size(), is(0));
|
|
|
|
}
|
2021-12-09 13:24:28 +01:00
|
|
|
|
2021-12-09 15:09:07 +01:00
|
|
|
@Test
|
|
|
|
public void testVarArgsCommandTabComplete() {
|
|
|
|
List<String> tabCompletes = new ArrayList<>();
|
|
|
|
varArgCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello"}, 0);
|
|
|
|
assertThat(tabCompletes.size(), is(2));
|
|
|
|
assertThat(tabCompletes.get(0), is("hello"));
|
|
|
|
assertThat(tabCompletes.get(1), is("world"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testVarArgsCommandTabCompleteDeeper() {
|
|
|
|
List<String> tabCompletes = new ArrayList<>();
|
|
|
|
varArgCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", "world", "hello", "world"}, 0);
|
|
|
|
System.out.println(tabCompletes);
|
|
|
|
assertThat(tabCompletes.size(), is(2));
|
|
|
|
assertThat(tabCompletes.get(0), is("hello"));
|
|
|
|
assertThat(tabCompletes.get(1), is("world"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testVarArgsCommandArgumentParsing() {
|
|
|
|
List<Object> argumentArray = new ArrayList<>();
|
|
|
|
varArgCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello"}, 0);
|
|
|
|
assertThat(argumentArray.size(), is(1));
|
|
|
|
assertThat(argumentArray.get(0), instanceOf(String[].class));
|
|
|
|
assertThat((String[]) argumentArray.get(0), arrayWithSize(1));
|
|
|
|
assertThat((String[]) argumentArray.get(0), is(new String[]{"hello"}));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testVarArgsCommandArgumentParsingDeeper() {
|
|
|
|
List<Object> argumentArray = new ArrayList<>();
|
|
|
|
varArgCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello", "world", "hello", "world"}, 0);
|
|
|
|
assertThat(argumentArray.size(), is(1));
|
|
|
|
assertThat(argumentArray.get(0), instanceOf(String[].class));
|
|
|
|
assertThat((String[]) argumentArray.get(0), arrayWithSize(4));
|
|
|
|
assertThat((String[]) argumentArray.get(0), is(new String[]{"hello", "world", "hello", "world"}));
|
|
|
|
}
|
|
|
|
|
2021-12-09 13:24:28 +01:00
|
|
|
@Test
|
|
|
|
public void testGuardCommandExecute() {
|
|
|
|
List<Object> argumentArray = new ArrayList<>();
|
|
|
|
simpleGuardPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello"}, 0);
|
|
|
|
assertThat(argumentArray.size(), is(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = CommandNoHelpException.class)
|
|
|
|
public void testGuardGuardCheck() {
|
|
|
|
simpleGuardPart.guardCheck(new TestCommandSender(), new String[]{"hello"}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testGuardCommandExecuteValid() {
|
|
|
|
List<Object> argumentArray = new ArrayList<>();
|
|
|
|
simpleGuardPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"world"}, 0);
|
|
|
|
assertThat(argumentArray.size(), is(1));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testGuardGuardCheckValid() {
|
|
|
|
boolean guardResult = simpleGuardPart.guardCheck(new TestCommandSender(), new String[]{"world"}, 0);
|
|
|
|
assertThat(guardResult, is(true));
|
|
|
|
}
|
2021-12-09 15:23:46 +01:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testOptionalCommandPartTabComplete() {
|
|
|
|
List<String> tabCompletes = new ArrayList<>();
|
|
|
|
optionalCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{""}, 0);
|
|
|
|
assertThat(tabCompletes.size(), is(4));
|
|
|
|
assertThat(tabCompletes.get(0), is("hello"));
|
|
|
|
assertThat(tabCompletes.get(1), is("world"));
|
|
|
|
assertThat(tabCompletes.get(2), is("hello2"));
|
|
|
|
assertThat(tabCompletes.get(3), is("world2"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testOptionalCommandPartTabCompleteSecond() {
|
|
|
|
List<String> tabCompletes = new ArrayList<>();
|
|
|
|
optionalCommandPart.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", ""}, 0);
|
|
|
|
assertThat(tabCompletes.size(), is(2));
|
|
|
|
assertThat(tabCompletes.get(0), is("hello2"));
|
|
|
|
assertThat(tabCompletes.get(1), is("world2"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = CommandParseException.class)
|
|
|
|
public void testOptionalCommandPartExecution() {
|
|
|
|
optionalCommandPart.generateArgumentArray(new ArrayList<>(), new TestCommandSender(), new String[]{""}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testOptionalCommandPartExecutionValid() {
|
|
|
|
List<Object> argumentArray = new ArrayList<>();
|
|
|
|
optionalCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"hello2"}, 0);
|
|
|
|
assertThat(argumentArray.size(), is(2));
|
|
|
|
assertThat(argumentArray.get(0), is("hello"));
|
|
|
|
assertThat(argumentArray.get(1), is("hello2"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test(expected = CommandParseException.class)
|
|
|
|
public void testOptionalCommandPartExecutionInvalid() {
|
|
|
|
optionalCommandPart.generateArgumentArray(new ArrayList<>(), new TestCommandSender(), new String[]{"hello"}, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testOptionalCommandPartExecutionFullyValid() {
|
|
|
|
List<Object> argumentArray = new ArrayList<>();
|
|
|
|
optionalCommandPart.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"world", "hello2"}, 0);
|
|
|
|
assertThat(argumentArray.size(), is(2));
|
|
|
|
assertThat(argumentArray.get(0), is("world"));
|
|
|
|
assertThat(argumentArray.get(1), is("hello2"));
|
|
|
|
}
|
2021-12-24 13:16:46 +01:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testOptionalCommandPartExecutionMultipleNext() {
|
|
|
|
List<Object> argumentArray = new ArrayList<>();
|
|
|
|
optionalCommandPartMultipleNext.generateArgumentArray(argumentArray, new TestCommandSender(), new String[]{"world", "hello2", "hello3"}, 0);
|
|
|
|
assertThat(argumentArray.size(), is(3));
|
|
|
|
assertThat(argumentArray.get(0), is("world"));
|
|
|
|
assertThat(argumentArray.get(1), is("hello2"));
|
|
|
|
assertThat(argumentArray.get(2), is("hello3"));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testOptionalCommandPartExecutionMultipleTabComplete() {
|
|
|
|
List<String> tabCompletes = new ArrayList<>();
|
|
|
|
optionalCommandPartMultipleNext.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{""}, 0);
|
|
|
|
assertThat(tabCompletes.size(), is(4));
|
|
|
|
assertThat(tabCompletes.get(0), is("hello"));
|
|
|
|
assertThat(tabCompletes.get(1), is("world"));
|
|
|
|
assertThat(tabCompletes.get(2), is("hello2"));
|
|
|
|
assertThat(tabCompletes.get(3), is("world2"));
|
|
|
|
|
|
|
|
tabCompletes = new ArrayList<>();
|
|
|
|
optionalCommandPartMultipleNext.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", ""}, 0);
|
|
|
|
assertThat(tabCompletes.size(), is(2));
|
|
|
|
assertThat(tabCompletes.get(0), is("hello2"));
|
|
|
|
assertThat(tabCompletes.get(1), is("world2"));
|
|
|
|
|
|
|
|
tabCompletes = new ArrayList<>();
|
|
|
|
optionalCommandPartMultipleNext.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"hello", "world2", ""}, 0);
|
|
|
|
assertThat(tabCompletes.size(), is(2));
|
|
|
|
assertThat(tabCompletes.get(0), is("hello3"));
|
|
|
|
assertThat(tabCompletes.get(1), is("world3"));
|
|
|
|
|
|
|
|
tabCompletes = new ArrayList<>();
|
|
|
|
optionalCommandPartMultipleNext.generateTabComplete(tabCompletes, new TestCommandSender(), new String[]{"world2", ""}, 0);
|
|
|
|
assertThat(tabCompletes.size(), is(2));
|
|
|
|
assertThat(tabCompletes.get(0), is("hello3"));
|
|
|
|
assertThat(tabCompletes.get(1), is("world3"));
|
|
|
|
}
|
2021-12-08 23:32:33 +01:00
|
|
|
}
|