1
mirror of https://github.com/StarWishsama/Slimefun4.git synced 2024-09-20 03:35:51 +00:00

Some very basic unit tests

This commit is contained in:
TheBusyBiscuit 2021-02-06 10:48:30 +01:00
parent 0adc1c3192
commit eeb89c3c3b
4 changed files with 92 additions and 3 deletions

View File

@ -267,7 +267,7 @@ public abstract class AbstractAutoCrafter extends SlimefunItem implements Energy
if (inv.firstEmpty() != -1) {
Map<Integer, Integer> itemQuantities = new HashMap<>();
for (Predicate<ItemStack> predicate : recipe.getInputs()) {
for (Predicate<ItemStack> predicate : recipe.getIngredients()) {
// Check if any Item matches the Predicate
if (!matchesAny(inv, itemQuantities, predicate)) {
return false;

View File

@ -33,7 +33,7 @@ public abstract class AbstractRecipe {
}
@Nonnull
public Collection<Predicate<ItemStack>> getInputs() {
public Collection<Predicate<ItemStack>> getIngredients() {
return inputs;
}

View File

@ -0,0 +1,90 @@
package io.github.thebusybiscuit.slimefun4.testing.tests.items.auto_crafters;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.Tag;
import org.bukkit.inventory.FurnaceRecipe;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.RecipeChoice.MaterialChoice;
import org.bukkit.inventory.ShapedRecipe;
import org.bukkit.inventory.ShapelessRecipe;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import be.seeseemelk.mockbukkit.MockBukkit;
import io.github.thebusybiscuit.cscorelib2.item.CustomItem;
import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin;
import io.github.thebusybiscuit.slimefun4.implementation.items.electric.machines.auto_crafters.AbstractRecipe;
class TestAbstractRecipe {
private static SlimefunPlugin plugin;
@BeforeAll
public static void load() {
MockBukkit.mock();
plugin = MockBukkit.load(SlimefunPlugin.class);
}
@AfterAll
public static void unload() {
MockBukkit.unmock();
}
@Test
@DisplayName("Test ShapelessRecipe as AbstractRecipe")
void testShapelessRecipe() {
NamespacedKey key = new NamespacedKey(plugin, "shapeless_recipe_test");
ItemStack result = new CustomItem(Material.DIAMOND, "&6Special Diamond :o");
ShapelessRecipe recipe = new ShapelessRecipe(key, result);
recipe.addIngredient(new MaterialChoice(Material.IRON_NUGGET, Material.GOLD_NUGGET));
AbstractRecipe abstractRecipe = AbstractRecipe.of(recipe);
Assertions.assertNotNull(abstractRecipe);
Assertions.assertEquals(result, abstractRecipe.getResult());
Assertions.assertIterableEquals(recipe.getChoiceList(), abstractRecipe.getIngredients());
Assertions.assertEquals(key.toString(), abstractRecipe.toString());
}
@Test
@DisplayName("Test ShapedRecipe as AbstractRecipe")
void testShapedRecipe() {
NamespacedKey key = new NamespacedKey(plugin, "shaped_recipe_test");
ItemStack result = new CustomItem(Material.EMERALD, "&6Special Emerald :o");
ShapedRecipe recipe = new ShapedRecipe(key, result);
recipe.shape("OXO", " X ", "OXO");
List<MaterialChoice> choices = Arrays.asList(new MaterialChoice(Material.IRON_NUGGET), new MaterialChoice(Tag.PLANKS));
recipe.setIngredient('X', choices.get(0));
recipe.setIngredient('O', choices.get(1));
AbstractRecipe abstractRecipe = AbstractRecipe.of(recipe);
Assertions.assertNotNull(abstractRecipe);
Assertions.assertEquals(result, abstractRecipe.getResult());
Assertions.assertEquals(new HashSet<>(choices), new HashSet<>(abstractRecipe.getIngredients()));
Assertions.assertEquals(key.toString(), abstractRecipe.toString());
}
@Test
@DisplayName("Test invalid recipes as AbstractRecipe")
void testInvalidRecipes() {
NamespacedKey key = new NamespacedKey(plugin, "furnace_recipe_test");
ItemStack result = new CustomItem(Material.COAL, "&6Special Coal :o");
FurnaceRecipe recipe = new FurnaceRecipe(key, result, Material.COAL, 1, 1);
Assertions.assertNull(AbstractRecipe.of(recipe));
Assertions.assertNull(AbstractRecipe.of((ShapedRecipe) null));
}
}

View File

@ -10,7 +10,6 @@ import org.bukkit.entity.Player;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;