Add conflict api test

This commit is contained in:
alexcrea 2024-10-31 10:46:16 +01:00
parent 4c7ee760a6
commit 309dedc383
No known key found for this signature in database
GPG key ID: 43FD265DB0DBF91F
8 changed files with 116 additions and 12 deletions

View file

@ -2,7 +2,7 @@ package io.delilaheve;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import xyz.alexcrea.cuanvil.DefaultCustomAnvilTest;
import xyz.alexcrea.cuanvil.tests.DefaultCustomAnvilTest;
public class CustomAnvilTests extends DefaultCustomAnvilTest {

View file

@ -7,7 +7,7 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import xyz.alexcrea.cuanvil.DefaultCustomAnvilTest;
import xyz.alexcrea.cuanvil.tests.DefaultCustomAnvilTest;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.util.AnvilFuseTestData;
import xyz.alexcrea.cuanvil.util.AnvilFuseTestUtil;

View file

@ -0,0 +1,99 @@
package xyz.alexcrea.cuanvil.api;
import be.seeseemelk.mockbukkit.entity.PlayerMock;
import org.bukkit.enchantments.Enchantment;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.AnvilInventory;
import org.bukkit.inventory.Inventory;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import xyz.alexcrea.cuanvil.tests.ConfigResetCustomAnvilTest;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.group.EnchantConflictGroup;
import xyz.alexcrea.cuanvil.util.AnvilFuseTestData;
import xyz.alexcrea.cuanvil.util.AnvilFuseTestUtil;
import xyz.alexcrea.cuanvil.util.CommonItemUtil;
import java.util.List;
public class ConflictApiTests extends ConfigResetCustomAnvilTest {
private AnvilInventory anvil;
private PlayerMock player;
@Override
@BeforeEach
public void setUp() {
super.setUp();
// Mock used player & open anvil
player = server.addPlayer();
Inventory anvil = server.createInventory(player, InventoryType.ANVIL);
this.anvil = (AnvilInventory) anvil;
player.openInventory(anvil);
ConfigHolder.DEFAULT_CONFIG.getConfig().set("debug_log", true);
ConfigHolder.DEFAULT_CONFIG.getConfig().set("debug_log_verbose", true);
}
@Test
public void testConflict(){
ItemStack sharpness1 = CommonItemUtil.sharpness(1);
ItemStack arthropods1 = CommonItemUtil.bane_of_arthropods(1);
ItemStack illegalResult = AnvilFuseTestUtil.prepareItem(
Material.DIAMOND_SWORD,
List.of("bane_of_arthropods", "sharpness"),
1, 1
);
AnvilFuseTestData nullResultData = new AnvilFuseTestData(
sharpness1, arthropods1,
null
);
AnvilFuseTestData legalResultData = new AnvilFuseTestData(
sharpness1, arthropods1,
illegalResult
// TODO add expected price
);
CAEnchantment sharpness = EnchantmentApi.getByKey(Enchantment.SHARPNESS.getKey());
Assertions.assertNotNull(sharpness);
// Testing default conflict (illegal item should not be produced)
AnvilFuseTestUtil.executeAnvilTest(anvil, player, nullResultData);
// Try to find & remove conflict
EnchantConflictGroup conflict = null;
for (EnchantConflictGroup enchantConflictGroup : ConflictAPI.getRegisteredConflict()) {
if("sword_enchant_conflict".equalsIgnoreCase(enchantConflictGroup.getName())){
conflict = enchantConflictGroup;
break;
}
}
Assertions.assertNotNull(conflict, "Could not find conflict.");
// Test what happen when we remove the conflict (illegal item should be allowed)
ConflictAPI.removeConflict(conflict);
AnvilFuseTestUtil.executeAnvilTest(anvil, player, legalResultData);
// We create and add a new conflict
ConflictBuilder builder = new ConflictBuilder("sword_enchant_conflict");
builder.addEnchantment("bane_of_arthropods").addEnchantment(sharpness); //TODO maybe add "add by bukkit enchantment"
builder.setMaxBeforeConflict(1);
// Nothing should change as it is not new: it was previously deleted
Assertions.assertFalse(builder.registerIfNew());
AnvilFuseTestUtil.executeAnvilTest(anvil, player, legalResultData);
// Now the conflict should be registered and conflict should exist
Assertions.assertTrue(builder.registerIfAbsent());
AnvilFuseTestUtil.executeAnvilTest(anvil, player, nullResultData);
}
}

View file

@ -9,7 +9,7 @@ import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import xyz.alexcrea.cuanvil.DefaultCustomAnvilTest;
import xyz.alexcrea.cuanvil.tests.DefaultCustomAnvilTest;
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry;
import xyz.alexcrea.cuanvil.enchant.EnchantmentRarity;

View file

@ -1,11 +1,11 @@
package xyz.alexcrea.cuanvil;
package xyz.alexcrea.cuanvil.tests;
import org.junit.jupiter.api.AfterEach;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import java.io.File;
public class ConfigResetCustomAnvilTest extends DefaultCustomAnvilTest {
public abstract class ConfigResetCustomAnvilTest extends DefaultCustomAnvilTest {
@Override
@AfterEach

View file

@ -1,19 +1,17 @@
package xyz.alexcrea.cuanvil;
package xyz.alexcrea.cuanvil.tests;
import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.ServerMock;
import io.delilaheve.CustomAnvil;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import xyz.alexcrea.cuanvil.config.ConfigHolder;
import xyz.alexcrea.cuanvil.enchant.CAEnchantment;
import xyz.alexcrea.cuanvil.enchant.CAEnchantmentRegistry;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
public class DefaultCustomAnvilTest {
public abstract class DefaultCustomAnvilTest {
protected ServerMock server;
protected CustomAnvil plugin;

View file

@ -85,6 +85,7 @@ public class AnvilFuseTestUtil {
"Openned inventory is not anvil");
// Test with only the left item
anvil.setItem(1, null); // We clear the right slot in case something was there
testPlacingItem(anvil, player,
0, data.expectedPriceAfterLeftPlaced(),
data.leftItem(), data.expectedAfterLeftPlaced());
@ -116,12 +117,12 @@ public class AnvilFuseTestUtil {
AnvilFuseTestUtil.imitateAnvilUpdate(player, anvil);
ItemStack result = anvil.getItem(2);
assertEqual(result, expectedResult);
assertEqual(expectedResult, result);
assertPriceEqual(expectedPrice, anvil.getRepairCost());
}
public static void assertEqual(@Nullable ItemStack item1, @Nullable ItemStack item2) {
if(isAir(item1)) Assertions.assertTrue(isAir(item2));
if(isAir(item1)) Assertions.assertTrue(isAir(item2),"Item "+item2+" was not AIR but was expected to be air");
else Assertions.assertEquals(item1, item2);
}

View file

@ -15,7 +15,13 @@ public class CommonItemUtil {
);
}
public static ItemStack bane_of_arthropods(int level){
return AnvilFuseTestUtil.prepareItem(
Material.DIAMOND_SWORD,
List.of("bane_of_arthropods"),
level
);
}
}