forked from SteamWar/SteamWar
Initial copy from other repository (nothing is fixed here)
This commit is contained in:
@@ -0,0 +1,124 @@
|
||||
/*
|
||||
* 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;
|
||||
|
||||
import de.steamwar.command.CommandFrameworkException;
|
||||
import de.steamwar.command.impl.ExecutionIdentifier;
|
||||
import de.steamwar.command.impl.TestCommand;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.experimental.UtilityClass;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
|
||||
@UtilityClass
|
||||
public class AssertionUtils {
|
||||
|
||||
@AllArgsConstructor
|
||||
public static class SimpleAssertion<T extends TestCommand> {
|
||||
|
||||
private T value;
|
||||
|
||||
public SimpleAssertion<T> assertNoInitialize(String message) {
|
||||
try {
|
||||
value.initialize();
|
||||
} catch (UnsupportedOperationException e) {
|
||||
if (e.getCause() == null) {
|
||||
assertThat(e.getMessage(), is(message));
|
||||
} else {
|
||||
assertThat(e.getCause(), instanceOf(UnsupportedOperationException.class));
|
||||
assertThat(e.getCause().getMessage(), is(message));
|
||||
}
|
||||
} catch (Exception e) {
|
||||
error(value.getClass().getSimpleName() + " had an unexpected error while initializing");
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleAssertion<T> assertExecute(String message, String... arguments) {
|
||||
try {
|
||||
value.execute("", "", arguments);
|
||||
error(value.getClass().getSimpleName() + " was not parsed.");
|
||||
} catch (Exception e) {
|
||||
assertCMDFramework(e, ExecutionIdentifier.class, message);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleAssertion<T> assertNoExecute(String... arguments) {
|
||||
try {
|
||||
value.execute("", "", arguments);
|
||||
} catch (Exception e) {
|
||||
error(value.getClass().getSimpleName() + " was parsed. It should not have been.", e);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleAssertion<T> assertTabCompletion(String[] expectedTabCompletions, String... arguments) {
|
||||
return assertTabCompletion("", expectedTabCompletions, arguments);
|
||||
}
|
||||
|
||||
public SimpleAssertion<T> assertTabCompletion(String sender, String[] expectedTabCompletions, String... arguments) {
|
||||
try {
|
||||
List<String> tabCompletions = value.tabComplete(sender, "", arguments);
|
||||
assertTabCompletes(tabCompletions, expectedTabCompletions);
|
||||
} catch (Exception e) {
|
||||
error(value.getClass().getSimpleName() + " tab-completion failed.", e);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public static SimpleAssertion<TestCommand> with(Object... commandObjects) {
|
||||
return new SimpleAssertion<>(new TestCommand(stringCommandLoader -> {
|
||||
for (Object commandObject : commandObjects) {
|
||||
stringCommandLoader.add(commandObject);
|
||||
}
|
||||
}));
|
||||
}
|
||||
|
||||
public static void error(String message) {
|
||||
throw new AssertionError(message);
|
||||
}
|
||||
|
||||
public static void error(String message, Throwable cause) {
|
||||
throw new AssertionError(message, cause);
|
||||
}
|
||||
|
||||
public static void assertCMDFramework(Exception e, Class<?> clazz, String message) {
|
||||
if (clazz.isInstance(e)) {
|
||||
assertThat(e.getMessage(), is(message));
|
||||
} else {
|
||||
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(Arrays.asList(elements) + " -> " + list, list.size(), is(elements.length));
|
||||
if (elements.length > 0) {
|
||||
assertThat(list, containsInAnyOrder(elements));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user