Add few unit tests and testing libraries

This commit is contained in:
filoghost 2020-10-18 10:35:03 +02:00
parent 5c91ffb62d
commit d48a0c2478
4 changed files with 218 additions and 1 deletions

View File

@ -0,0 +1,88 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package me.filoghost.chestcommands.api;
import me.filoghost.chestcommands.icon.BaseConfigurableIcon;
import me.filoghost.chestcommands.test.BukkitMocks;
import org.bukkit.Material;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
class ConfigurableIconTest {
@BeforeAll
static void beforeAll() {
ChestCommandsAPI.registerPlaceholder(BukkitMocks.PLUGIN, "test", (player, argument) -> {
if (argument != null) {
return argument;
} else {
return "EMPTY";
}
});
}
@AfterAll
static void afterAll() {
ChestCommandsAPI.unregisterPlaceholder(BukkitMocks.PLUGIN, "test");
}
@Test
void customPlaceholderReplacements() {
BaseConfigurableIcon icon = (BaseConfigurableIcon) ConfigurableIcon.create(Material.STONE);
icon.setPlaceholdersEnabled(true);
icon.setName("{test: start} abc {test} {MockPlugin/test} {test: 1} {mOckPLuGin/tEsT: 2} 123 {test: end}");
assertThat(icon.renderName(BukkitMocks.PLAYER)).isEqualTo("start abc EMPTY EMPTY 1 2 123 end");
}
@Test
void placeholdersEnabled() {
BaseConfigurableIcon icon = (BaseConfigurableIcon) ConfigurableIcon.create(Material.STONE);
icon.setPlaceholdersEnabled(true);
icon.setName("abc {player} {test} 123");
assertThat(icon.renderName(BukkitMocks.PLAYER)).isEqualTo("abc " + BukkitMocks.PLAYER.getName() + " EMPTY 123");
}
@Test
void placeholdersNotEnabled() {
BaseConfigurableIcon icon = (BaseConfigurableIcon) ConfigurableIcon.create(Material.STONE);
icon.setName("abc {player} {test} 123");
assertThat(icon.renderName(BukkitMocks.PLAYER)).isEqualTo("abc {player} {test} 123");
}
@Test
void dynamicPlaceholderRegistration() {
BaseConfigurableIcon icon = (BaseConfigurableIcon) ConfigurableIcon.create(Material.STONE);
icon.setPlaceholdersEnabled(true);
icon.setName("abc {temp} 123");
try {
ChestCommandsAPI.registerPlaceholder(BukkitMocks.PLUGIN, "temp", (player, argument) -> "value");
assertThat(icon.renderName(BukkitMocks.PLAYER)).isEqualTo("abc value 123");
} finally {
ChestCommandsAPI.unregisterPlaceholder(BukkitMocks.PLUGIN, "temp");
}
}
@Test
void placeholderUnregistration() {
BaseConfigurableIcon icon = (BaseConfigurableIcon) ConfigurableIcon.create(Material.STONE);
icon.setPlaceholdersEnabled(true);
icon.setName("abc {temp} 123");
try {
ChestCommandsAPI.registerPlaceholder(BukkitMocks.PLUGIN, "temp", (player, argument) -> "value");
} finally {
ChestCommandsAPI.unregisterPlaceholder(BukkitMocks.PLUGIN, "temp");
}
assertThat(icon.renderName(BukkitMocks.PLAYER)).isEqualTo("abc {temp} 123");
}
}

View File

@ -0,0 +1,73 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package me.filoghost.chestcommands.api;
import me.filoghost.chestcommands.test.BukkitMocks;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.assertj.core.api.Assertions.*;
class MenuTest {
@Test
void setIcon() {
Menu menu = createMenu(1);
menu.setIcon(0, 0, StaticIcon.create(new ItemStack(Material.APPLE)));
assertThat(menu.getIcon(0, 0)).isNotNull();
}
@Test
void unsetIcon() {
Menu menu = createMenu(1);
menu.setIcon(0, 0, StaticIcon.create(new ItemStack(Material.APPLE)));
menu.setIcon(0, 0, null);
assertThat(menu.getIcon(0, 0)).isNull();
}
@Test
void iterationRowsColumns() {
Menu menu = createMenu(3);
for (int row = 0; row < menu.getRows(); row++) {
for (int column = 0; column < menu.getColumns(); column++) {
menu.setIcon(row, column, StaticIcon.create(new ItemStack(Material.APPLE)));
}
}
}
@Test
void invalidEmptyMenu() {
assertThatIllegalArgumentException().isThrownBy(() -> {
createMenu(0);
});
}
@ParameterizedTest
@CsvSource({
"9, 0",
"0, 9",
"-1, 0",
"0, -1",
})
void iconOutOfBounds(int row, int column) {
Menu menu = createMenu(1);
assertThatExceptionOfType(IndexOutOfBoundsException.class).isThrownBy(() -> {
menu.getIcon(row, column);
});
}
private Menu createMenu(int rowCount) {
return Menu.create(BukkitMocks.PLUGIN, "Test menu", rowCount);
}
}

View File

@ -0,0 +1,30 @@
/*
* Copyright (C) filoghost and contributors
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
package me.filoghost.chestcommands.test;
import me.filoghost.chestcommands.DefaultBackendAPI;
import me.filoghost.chestcommands.api.internal.BackendAPI;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import static org.mockito.Mockito.*;
public final class BukkitMocks {
public static final Plugin PLUGIN;
public static final Player PLAYER;
static {
// Server server = mock(Server.class, RETURNS_DEEP_STUBS);
// Bukkit.setServer(server);
PLUGIN = mock(Plugin.class);
when(PLUGIN.getName()).thenReturn("MockPlugin");
PLAYER = mock(Player.class);
when(PLAYER.getName()).thenReturn("filoghost");
BackendAPI.setImplementation(new DefaultBackendAPI());
}
}

28
pom.xml
View File

@ -38,7 +38,6 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.test.skip>true</maven.test.skip>
</properties>
<modules>
@ -136,6 +135,27 @@
<version>20.1.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.6.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.17.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>3.5.13</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
@ -211,6 +231,12 @@
<artifactId>directory-maven-plugin</artifactId>
<version>0.3.1</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version>
</plugin>
</plugins>
</pluginManagement>