Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
Not yet updating to new API because I think PlanarEnchanting needs some retooling.
  • Loading branch information
Jikoo committed Aug 1, 2024
1 parent a95e2ed commit 9da2f11
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.notNull;
Expand Down Expand Up @@ -35,9 +34,9 @@
import org.bukkit.enchantments.Enchantment;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.PrepareAnvilEvent;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.EnchantmentStorageMeta;
import org.bukkit.inventory.view.AnvilView;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitScheduler;
import org.jetbrains.annotations.NotNull;
Expand Down Expand Up @@ -177,7 +176,6 @@ void afterEach() {

private @NotNull Player prepareEventPlayer() {
var player = mock(Player.class);
when(player.setWindowProperty(any(), anyInt())).thenReturn(true);
var world = WorldMocks.newWorld("world");
when(player.getWorld()).thenReturn(world);

Expand All @@ -191,7 +189,7 @@ void afterEach() {
additionItem.setItemMeta(additionMeta);
inventory.setItem(1, additionItem);

InventoryView view = mock(InventoryView.class);
AnvilView view = mock(AnvilView.class);
when(view.getTopInventory()).thenReturn(inventory);
when(view.getPlayer()).thenReturn(player);

Expand All @@ -202,7 +200,7 @@ void afterEach() {

@Test
void testInvalidItem() {
var view = prepareEventPlayer().getOpenInventory();
var view = (AnvilView) prepareEventPlayer().getOpenInventory();
view.getTopInventory().setItem(0, null);
var event = spy(new PrepareAnvilEvent(view, null));
assertDoesNotThrow(() -> enchanter.onPrepareAnvil(event));
Expand All @@ -212,7 +210,7 @@ void testInvalidItem() {

@Test
void testUnregisteredMaterial() {
var view = prepareEventPlayer().getOpenInventory();
var view = (AnvilView) prepareEventPlayer().getOpenInventory();
view.getTopInventory().setItem(0, new ItemStack(badMat));
var event = spy(new PrepareAnvilEvent(view, null));
assertDoesNotThrow(() -> enchanter.onPrepareAnvil(event));
Expand All @@ -223,15 +221,15 @@ void testUnregisteredMaterial() {
@Test
void testNoPermission() {
doReturn(false).when(registration).hasEnchantPermission(notNull(), anyString());
var event = spy(new PrepareAnvilEvent(prepareEventPlayer().getOpenInventory(), null));
var event = spy(new PrepareAnvilEvent((AnvilView) prepareEventPlayer().getOpenInventory(), null));
assertDoesNotThrow(() -> enchanter.onPrepareAnvil(event));
verify(registration).hasEnchantPermission(notNull(), anyString());
verify(event, times(0)).setResult(any());
}

@Test
void testNoChange() {
var view = prepareEventPlayer().getOpenInventory();
var view = (AnvilView) prepareEventPlayer().getOpenInventory();
view.getTopInventory().setItem(1, itemStack.clone());
var event = spy(new PrepareAnvilEvent(view, null));

Expand All @@ -242,7 +240,7 @@ void testNoChange() {
@ParameterizedTest
@MethodSource("getSlots")
void testChangePostCalculate(int... slots) {
var view = prepareEventPlayer().getOpenInventory();
var view = (AnvilView) prepareEventPlayer().getOpenInventory();
var event = spy(new PrepareAnvilEvent(view, null));

assertDoesNotThrow(() -> enchanter.onPrepareAnvil(event));
Expand Down Expand Up @@ -270,7 +268,7 @@ static Collection<Arguments> getSlots() {

@Test
void testSuccess() {
var view = prepareEventPlayer().getOpenInventory();
var view = (AnvilView) prepareEventPlayer().getOpenInventory();
var event = spy(new PrepareAnvilEvent(view, null));

assertDoesNotThrow(() -> enchanter.onPrepareAnvil(event));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import org.bukkit.entity.Player;
import org.bukkit.event.enchantment.EnchantItemEvent;
import org.bukkit.event.enchantment.PrepareItemEnchantEvent;
import org.bukkit.inventory.InventoryView;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.view.EnchantmentView;
import org.bukkit.persistence.PersistentDataContainer;
import org.bukkit.plugin.Plugin;
import org.bukkit.scheduler.BukkitScheduler;
Expand Down Expand Up @@ -172,7 +172,7 @@ void testListener() {
var pdc = mock(PersistentDataContainer.class);
doReturn(pdc).when(player).getPersistentDataContainer();

var view = mock(InventoryView.class);
var view = mock(EnchantmentView.class);
var table = mock(Block.class);

var prepareEvent = new PrepareItemEnchantEvent(player, view, table, itemStack, new EnchantmentOffer[3], 15);
Expand Down

0 comments on commit 9da2f11

Please sign in to comment.