/* * 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; import de.steamwar.command.CommandFrameworkException; import de.steamwar.command.Handler; 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 { private T value; public SimpleAssertion assertNoInitialize(String message) { try { value.initialize(); } catch (UnsupportedOperationException e) { if (e.getCause() == null) { assertThat(e.getMessage(), is(message)); } else { assertThat(e.getCause(), instanceOf(Handler.HandlerException.class)); assertThat(e.getCause().getMessage(), is(message)); } } catch (Exception e) { error(value.getClass().getSimpleName() + " had an unexpected error while initializing"); } return this; } public SimpleAssertion 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 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 assertTabCompletion(String[] expectedTabCompletions, String... arguments) { return assertTabCompletion("", expectedTabCompletions, arguments); } public SimpleAssertion assertTabCompletion(String sender, String[] expectedTabCompletions, String... arguments) { try { List tabCompletions = value.tabComplete(sender, "", arguments); assertTabCompletes(tabCompletions, expectedTabCompletions); } catch (Exception e) { error(value.getClass().getSimpleName() + " tab-completion failed.", e); } return this; } } public static SimpleAssertion 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 void assertTabCompletes(List list, T... elements) { assertThat(Arrays.asList(elements) + " -> " + list, list.size(), is(elements.length)); if (elements.length > 0) { assertThat(list, containsInAnyOrder(elements)); } } }