1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-19 19:25:48 +00:00

Added Unit Tests for Categories

This commit is contained in:
TheBusyBiscuit 2020-05-07 19:56:45 +02:00
parent 220d5ef41f
commit 9e63f1ce79
4 changed files with 248 additions and 102 deletions

View File

@ -6,6 +6,7 @@ import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import org.apache.commons.lang.Validate;
import org.bukkit.ChatColor;
import org.bukkit.Keyed;
import org.bukkit.NamespacedKey;
@ -64,6 +65,9 @@ public class Category implements Keyed {
* the {@link SlimefunGuide}
*/
public Category(NamespacedKey key, ItemStack item, int tier) {
Validate.notNull(key, "A Category's NamespacedKey must not be null!");
Validate.notNull(item, "A Category's ItemStack must not be null!");
this.item = item;
this.key = key;

View File

@ -1,103 +1,103 @@
{
"GOLD_PAN": "Gold-Pan",
"SIFTED_ORE": "Sifted-Ore",
"SMELTERY": "Smeltery",
"DIET_COOKIE": "Diet-Cookie",
"ENHANCED_CRAFTING_TABLE": "Enhanced-Crafting-Table",
"FORTUNE_COOKIE": "Fortune-Cookie",
"TABLE_SAW": "Table-Saw",
"APPLE_JUICE": "Juices",
"GOLDEN_APPLE_JUICE": "Juices",
"CARROT_JUICE": "Juices",
"MELON_JUICE": "Juices",
"PUMPKIN_JUICE": "Juices",
"SWEET_BERRY_JUICE": "Juices",
"MAGIC_SUGAR": "Magic-Sugar",
"MONSTER_JERKY": "Monster-Jerky",
"OUTPUT_CHEST": "Output-Chest",
"BEEF_JERKY": "Meat-Jerky",
"PORK_JERKY": "Meat-Jerky",
"CHICKEN_JERKY": "Meat-Jerky",
"MUTTON_JERKY": "Meat-Jerky",
"RABBIT_JERKY": "Meat-Jerky",
"FISH_JERKY": "Meat-Jerky",
"KELP_COOKIE": "Kelp-Cookie",
"ANCIENT_ALTAR": "Ancient-Altar",
"ANCIENT_PEDESTAL": "Ancient-Pedestal",
"BLADE_OF_VAMPIRES": "Blade-of-Vampires",
"BROKEN_SPAWNER": "Broken-Spawner",
"REPAIRED_SPAWNER": "Reinforced-Spawner",
"NETHER_GOLD_PAN": "Nether-Gold-Pan",
"PICKAXE_OF_CONTAINMENT": "Pickaxe-of-Containment",
"SEISMIC_AXE": "Seismic-Axe",
"SMELTERS_PICKAXE": "Smelter's-Pickaxe",
"MAGNET": "Magnet",
"BASIC_CIRCUIT_BOARD": "Circuit-Boards",
"ADVANCED_CIRCUIT_BOARD": "Circuit-Boards",
"BATTERY": "Battery",
"STEEL_THRUSTER": "Steel-Thruster",
"POWER_CRYSTAL": "Power-Crystal",
"SOLAR_PANEL": "Solar-Panel",
"ELECTRO_MAGNET": "Electromagnet",
"ELECTRIC_MOTOR": "Electric-Motor",
"HEATING_COIL": "Heating-Coil",
"COPPER_WIRE": "Copper-Wire",
"HARDENED_GLASS": "Hardened-Glass",
"COOLING_UNIT": "Cooling-Unit",
"WITHER_PROOF_OBSIDIAN": "Wither-Proof-Blocks",
"WITHER_PROOF_GLASS": "Wither-Proof-Blocks",
"REACTOR_COOLANT_CELL": "Coolant-Cells",
"NETHER_ICE_COOLANT_CELL": "Coolant-Cells",
"IRON_DUST": "Iron-Dust",
"GOLD_DUST": "Gold-Dust",
"GOLD_4K": "Gold-Ingot",
"GOLD_6K": "Gold-Ingot",
"GOLD_8K": "Gold-Ingot",
"GOLD_10K": "Gold-Ingot",
"GOLD_12K": "Gold-Ingot",
"GOLD_14K": "Gold-Ingot",
"GOLD_16K": "Gold-Ingot",
"GOLD_18K": "Gold-Ingot",
"GOLD_20K": "Gold-Ingot",
"GOLD_22K": "Gold-Ingot",
"GOLD_24K": "Gold-Ingot",
"ENERGY_REGULATOR": "Energy-Regulator",
"SMALL_CAPACITOR": "Energy-Capacitors",
"MEDIUM_CAPACITOR": "Energy-Capacitors",
"BIG_CAPACITOR": "Energy-Capacitors",
"LARGE_CAPACITOR": "Energy-Capacitors",
"CARBONADO_EDGED_CAPACITOR": "Energy-Capacitors",
"SOLAR_GENERATOR": "Solar-Generator",
"SOLAR_GENERATOR_2": "Solar-Generator",
"SOLAR_GENERATOR_3": "Solar-Generator",
"SOLAR_GENERATOR_4": "Solar-Generator",
"COAL_GENERATOR": "Coal-Generator",
"COAL_GENERATOR_2": "Coal-Generator",
"COBALT_PICKAXE": "Cobalt-Pickaxe",
"EXPLOSIVE_PICKAXE": "Explosive-Pickaxe",
"EXPLOSIVE_SHOVEL": "Explosive-Shovel",
"GRAPPLING_HOOK": "Grappling-Hook",
"HERCULES_PICKAXE": "Hercules'-Pickaxe",
"LUMBER_AXE": "Lumber-Axe",
"PICKAXE_OF_VEIN_MINING": "Pickaxe-of-Vein-Mining",
"PICKAXE_OF_THE_SEEKER": "Pickaxe-of-the-Seeker",
"CARGO_OUTPUT_ADVANCED": "Advanced-Output-Node",
"CARGO_MANAGER": "Cargo-Manager",
"CARGO_MOTOR": "Cargo-Motor",
"CARGO_NODE": "Connector-Node",
"CARGO_INPUT": "Input-Node",
"CARGO_OUTPUT": "Output-Node",
"TRASH_CAN": "Trash-Can",
"ORE_WASHER": "Ore-Washer",
"SCUBA_HELMET": "Hazmat-Suit",
"HAZMAT_CHESTPLATE": "Hazmat-Suit",
"HAZMAT_LEGGINGS": "Hazmat-Suit",
"RUBBER_BOOTS": "Hazmat-Suit",
"ARMOR_FORGE": "Armor-Forge",
"AUTOMATED_PANNING_MACHINE": "Automated-Panning-Machine",
"COMPRESSOR": "Compressor",
"ORE_CRUSHER": "Ore-Crusher",
"PRESSURE_CHAMBER": "Pressure-Chamber",
"GRIND_STONE": "Grind-Stone",
"MAGIC_WORKBENCH": "Magic-Workbench"
"GOLD_PAN" : "Gold-Pan",
"SIFTED_ORE" : "Sifted-Ore",
"SMELTERY" : "Smeltery",
"DIET_COOKIE" : "Diet-Cookie",
"ENHANCED_CRAFTING_TABLE" : "Enhanced-Crafting-Table",
"FORTUNE_COOKIE" : "Fortune-Cookie",
"TABLE_SAW" : "Table-Saw",
"APPLE_JUICE" : "Juices",
"GOLDEN_APPLE_JUICE" : "Juices",
"CARROT_JUICE" : "Juices",
"MELON_JUICE" : "Juices",
"PUMPKIN_JUICE" : "Juices",
"SWEET_BERRY_JUICE" : "Juices",
"MAGIC_SUGAR" : "Magic-Sugar",
"MONSTER_JERKY" : "Monster-Jerky",
"OUTPUT_CHEST" : "Output-Chest",
"BEEF_JERKY" : "Meat-Jerky",
"PORK_JERKY" : "Meat-Jerky",
"CHICKEN_JERKY" : "Meat-Jerky",
"MUTTON_JERKY" : "Meat-Jerky",
"RABBIT_JERKY" : "Meat-Jerky",
"FISH_JERKY" : "Meat-Jerky",
"KELP_COOKIE" : "Kelp-Cookie",
"ANCIENT_ALTAR" : "Ancient-Altar",
"ANCIENT_PEDESTAL" : "Ancient-Pedestal",
"BLADE_OF_VAMPIRES" : "Blade-of-Vampires",
"BROKEN_SPAWNER" : "Broken-Spawner",
"REPAIRED_SPAWNER" : "Reinforced-Spawner",
"NETHER_GOLD_PAN" : "Nether-Gold-Pan",
"PICKAXE_OF_CONTAINMENT" : "Pickaxe-of-Containment",
"SEISMIC_AXE" : "Seismic-Axe",
"SMELTERS_PICKAXE" : "Smelter's-Pickaxe",
"MAGNET" : "Magnet",
"BASIC_CIRCUIT_BOARD" : "Circuit-Boards",
"ADVANCED_CIRCUIT_BOARD" : "Circuit-Boards",
"BATTERY" : "Battery",
"STEEL_THRUSTER" : "Steel-Thruster",
"POWER_CRYSTAL" : "Power-Crystal",
"SOLAR_PANEL" : "Solar-Panel",
"ELECTRO_MAGNET" : "Electromagnet",
"ELECTRIC_MOTOR" : "Electric-Motor",
"HEATING_COIL" : "Heating-Coil",
"COPPER_WIRE" : "Copper-Wire",
"HARDENED_GLASS" : "Hardened-Glass",
"COOLING_UNIT" : "Cooling-Unit",
"WITHER_PROOF_OBSIDIAN" : "Wither-Proof-Blocks",
"WITHER_PROOF_GLASS" : "Wither-Proof-Blocks",
"REACTOR_COOLANT_CELL" : "Coolant-Cells",
"NETHER_ICE_COOLANT_CELL" : "Coolant-Cells",
"IRON_DUST" : "Iron-Dust",
"GOLD_DUST" : "Gold-Dust",
"GOLD_4K" : "Gold-Ingot",
"GOLD_6K" : "Gold-Ingot",
"GOLD_8K" : "Gold-Ingot",
"GOLD_10K" : "Gold-Ingot",
"GOLD_12K" : "Gold-Ingot",
"GOLD_14K" : "Gold-Ingot",
"GOLD_16K" : "Gold-Ingot",
"GOLD_18K" : "Gold-Ingot",
"GOLD_20K" : "Gold-Ingot",
"GOLD_22K" : "Gold-Ingot",
"GOLD_24K" : "Gold-Ingot",
"ENERGY_REGULATOR" : "Energy-Regulator",
"SMALL_CAPACITOR" : "Energy-Capacitors",
"MEDIUM_CAPACITOR" : "Energy-Capacitors",
"BIG_CAPACITOR" : "Energy-Capacitors",
"LARGE_CAPACITOR" : "Energy-Capacitors",
"CARBONADO_EDGED_CAPACITOR" : "Energy-Capacitors",
"SOLAR_GENERATOR" : "Solar-Generator",
"SOLAR_GENERATOR_2" : "Solar-Generator",
"SOLAR_GENERATOR_3" : "Solar-Generator",
"SOLAR_GENERATOR_4" : "Solar-Generator",
"COAL_GENERATOR" : "Coal-Generator",
"COAL_GENERATOR_2" : "Coal-Generator",
"COBALT_PICKAXE" : "Cobalt-Pickaxe",
"EXPLOSIVE_PICKAXE" : "Explosive-Pickaxe",
"EXPLOSIVE_SHOVEL" : "Explosive-Shovel",
"GRAPPLING_HOOK" : "Grappling-Hook",
"HERCULES_PICKAXE" : "Hercules'-Pickaxe",
"LUMBER_AXE" : "Lumber-Axe",
"PICKAXE_OF_VEIN_MINING" : "Pickaxe-of-Vein-Mining",
"PICKAXE_OF_THE_SEEKER" : "Pickaxe-of-the-Seeker",
"CARGO_OUTPUT_ADVANCED" : "Advanced-Output-Node",
"CARGO_MANAGER" : "Cargo-Manager",
"CARGO_MOTOR" : "Cargo-Motor",
"CARGO_NODE" : "Connector-Node",
"CARGO_INPUT" : "Input-Node",
"CARGO_OUTPUT" : "Output-Node",
"TRASH_CAN" : "Trash-Can",
"ORE_WASHER" : "Ore-Washer",
"SCUBA_HELMET" : "Hazmat-Suit",
"HAZMAT_CHESTPLATE" : "Hazmat-Suit",
"HAZMAT_LEGGINGS" : "Hazmat-Suit",
"RUBBER_BOOTS" : "Hazmat-Suit",
"ARMOR_FORGE" : "Armor-Forge",
"AUTOMATED_PANNING_MACHINE" : "Automated-Panning-Machine",
"COMPRESSOR" : "Compressor",
"ORE_CRUSHER" : "Ore-Crusher",
"PRESSURE_CHAMBER" : "Pressure-Chamber",
"GRIND_STONE" : "Grind-Stone",
"MAGIC_WORKBENCH" : "Magic-Workbench"
}

View File

@ -9,6 +9,7 @@ import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemStack;
import org.mockito.Mockito;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Objects.Category;
@ -16,7 +17,7 @@ import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
public final class SlimefunMocks {
private static final Category category = new Category(new NamespacedKey(SlimefunPlugin.instance, "test"), new ItemStack(Material.EMERALD));
private static final Category category = new Category(new NamespacedKey(SlimefunPlugin.instance, "test"), new CustomItem(Material.EMERALD, "&4Test Category"));
private SlimefunMocks() {}

View File

@ -0,0 +1,141 @@
package io.github.thebusybiscuit.slimefun4.tests.items;
import java.time.LocalDate;
import java.time.Month;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import be.seeseemelk.mockbukkit.MockBukkit;
import be.seeseemelk.mockbukkit.ServerMock;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.core.categories.SeasonalCategory;
import io.github.thebusybiscuit.slimefun4.implementation.items.VanillaItem;
import io.github.thebusybiscuit.slimefun4.mocks.SlimefunMocks;
import me.mrCookieSlime.Slimefun.SlimefunPlugin;
import me.mrCookieSlime.Slimefun.Objects.Category;
import me.mrCookieSlime.Slimefun.Objects.LockedCategory;
import me.mrCookieSlime.Slimefun.Objects.SlimefunItem.SlimefunItem;
public class TestCategories {
private static ServerMock server;
private static SlimefunPlugin plugin;
@BeforeAll
public static void load() {
server = MockBukkit.mock();
plugin = MockBukkit.load(SlimefunPlugin.class);
}
@AfterAll
public static void unload() {
MockBukkit.unmock();
}
@Test
public void testCategoryGetters() {
Category category = SlimefunMocks.getCategory();
Assertions.assertEquals(3, category.getTier());
Assertions.assertEquals(new NamespacedKey(SlimefunPlugin.instance, "test"), category.getKey());
Assertions.assertEquals("Test Category", category.getUnlocalizedName());
Assertions.assertEquals(0, category.getItems().size());
SlimefunItem item = SlimefunMocks.mockSlimefunItem("CATEGORY_TEST_ITEM", new CustomItem(Material.BAMBOO, "&6Test Bamboo"));
item.register(plugin);
item.load();
Assertions.assertEquals(1, category.getItems().size());
Assertions.assertTrue(category.getItems().contains(item));
}
@Test
public void testHidden() {
Category category = new Category(new NamespacedKey(plugin, "testCategory2"), new ItemStack(Material.BEACON));
Player player = server.addPlayer();
// Empty Categories are also hidden
Assertions.assertTrue(category.isHidden(player));
VanillaItem vanillaItem = SlimefunMocks.mockVanillaItem(Material.BEETROOT, false);
vanillaItem.setCategory(category);
vanillaItem.register(plugin);
vanillaItem.load();
// A disabled Item should also make the Category hide
Assertions.assertTrue(category.isHidden(player));
SlimefunItem item = SlimefunMocks.mockSlimefunItem("CATEGORY_HIDDEN_TEST", new CustomItem(Material.BAMBOO, "&6Test Bamboo"));
item.setCategory(category);
item.setHidden(true);
item.register(plugin);
item.load();
// A hidden Item should also make the Category hide
Assertions.assertTrue(category.isHidden(player));
item.setHidden(false);
Assertions.assertFalse(category.isHidden(player));
}
@Test
public void testContains() {
Category category = SlimefunMocks.getCategory();
SlimefunItem item = SlimefunMocks.mockSlimefunItem("CATEGORY_TEST_ITEM_2", new CustomItem(Material.BOW, "&6Test Bow"));
item.register(plugin);
item.load();
Assertions.assertTrue(category.contains(item));
Assertions.assertFalse(category.contains(null));
Assertions.assertFalse(category.contains(SlimefunMocks.mockSlimefunItem("NULL", new ItemStack(Material.BEDROCK))));
}
@Test
public void testLockedCategories() {
Assertions.assertThrows(IllegalArgumentException.class, () -> new LockedCategory(new NamespacedKey(plugin, "locked"), new CustomItem(Material.GOLD_NUGGET, "&6Locked Test"), (NamespacedKey) null));
Category category = SlimefunMocks.getCategory();
LockedCategory locked = new LockedCategory(new NamespacedKey(plugin, "locked"), new CustomItem(Material.GOLD_NUGGET, "&6Locked Test"), category.getKey());
locked.register();
Assertions.assertTrue(locked.getParents().contains(category));
locked.removeParent(category);
Assertions.assertFalse(locked.getParents().contains(category));
Assertions.assertThrows(IllegalArgumentException.class, () -> locked.addParent(locked));
Assertions.assertThrows(IllegalArgumentException.class, () -> locked.addParent(null));
locked.addParent(category);
Assertions.assertTrue(locked.getParents().contains(category));
}
@Test
public void testSeasonalCategories() {
// Category with current Month
Month month = LocalDate.now().getMonth();
SeasonalCategory category = new SeasonalCategory(new NamespacedKey(plugin, "seasonal"), month, 1, new CustomItem(Material.NETHER_STAR, "&cSeasonal Test"));
SlimefunItem item = SlimefunMocks.mockSlimefunItem("SEASONAL_ITEM", new CustomItem(Material.NETHER_STAR, "&dSeasonal Test Star"));
item.setCategory(category);
item.register(plugin);
item.load();
Player player = server.addPlayer();
Assertions.assertEquals(month, category.getMonth());
System.out.println(month);
System.out.println(category.getMonth());
Assertions.assertFalse(category.isHidden(player));
// Category with future Month
SeasonalCategory category2 = new SeasonalCategory(category.getKey(), month.plus(6), 1, new CustomItem(Material.MILK_BUCKET, "&dSeasonal Test"));
Assertions.assertTrue(category2.isHidden(player));
}
}