diff --git a/plugin/src/test/java/me/filoghost/chestcommands/api/ConfigurableIconTest.java b/plugin/src/test/java/me/filoghost/chestcommands/api/ConfigurableIconTest.java new file mode 100644 index 0000000..895ed3b --- /dev/null +++ b/plugin/src/test/java/me/filoghost/chestcommands/api/ConfigurableIconTest.java @@ -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"); + } + +} diff --git a/plugin/src/test/java/me/filoghost/chestcommands/api/MenuTest.java b/plugin/src/test/java/me/filoghost/chestcommands/api/MenuTest.java new file mode 100644 index 0000000..7a70aa6 --- /dev/null +++ b/plugin/src/test/java/me/filoghost/chestcommands/api/MenuTest.java @@ -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); + } + + +} diff --git a/plugin/src/test/java/me/filoghost/chestcommands/test/BukkitMocks.java b/plugin/src/test/java/me/filoghost/chestcommands/test/BukkitMocks.java new file mode 100644 index 0000000..04ec7b3 --- /dev/null +++ b/plugin/src/test/java/me/filoghost/chestcommands/test/BukkitMocks.java @@ -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()); + } + +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3e91498..7ce5dd7 100644 --- a/pom.xml +++ b/pom.xml @@ -38,7 +38,6 @@ UTF-8 1.8 1.8 - true @@ -136,6 +135,27 @@ 20.1.0 provided + + + org.junit.jupiter + junit-jupiter + 5.6.2 + test + + + + org.assertj + assertj-core + 3.17.0 + test + + + + org.mockito + mockito-core + 3.5.13 + test + @@ -211,6 +231,12 @@ directory-maven-plugin 0.3.1 + + + org.apache.maven.plugins + maven-surefire-plugin + 2.22.2 +