From 1a7de82c4556de698110b35982529f35346eb7d3 Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Tue, 15 Sep 2020 19:42:52 +0200 Subject: [PATCH] Added Unit Tests --- .../utils/InfiniteBlockGenerator.java | 37 +++- .../utils/TestInfiniteBlockGenerators.java | 162 ++++++++++++++++++ 2 files changed, 192 insertions(+), 7 deletions(-) create mode 100644 src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/utils/TestInfiniteBlockGenerators.java diff --git a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/InfiniteBlockGenerator.java b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/InfiniteBlockGenerator.java index 1e8208ea3..e0c2e5e18 100644 --- a/src/main/java/io/github/thebusybiscuit/slimefun4/utils/InfiniteBlockGenerator.java +++ b/src/main/java/io/github/thebusybiscuit/slimefun4/utils/InfiniteBlockGenerator.java @@ -13,6 +13,7 @@ import org.bukkit.block.Block; import org.bukkit.block.BlockFace; import org.bukkit.event.block.BlockFromToEvent; +import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion; import io.github.thebusybiscuit.slimefun4.implementation.items.androids.MinerAndroid; /** @@ -43,25 +44,47 @@ public enum InfiniteBlockGenerator implements Predicate { public static final InfiniteBlockGenerator[] values = values(); private static final BlockFace[] sameLevelFaces = { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST }; - @Nullable private final Material material; - @ParametersAreNonnullByDefault - InfiniteBlockGenerator(String type) { + InfiniteBlockGenerator(@Nonnull String type) { this.material = Material.matchMaterial(type); } + /** + * This returns the generated {@link Material} of this {@link InfiniteBlockGenerator}. + * This method can return null if the associated {@link Material} is not available in the current + * {@link MinecraftVersion}. + * + * @return The generated {@link Material} or null + */ + @Nullable + public Material getGeneratedMaterial() { + return material; + } + @Override public boolean test(@Nonnull Block b) { - return test(b, false); + return testAndTrigger(b, false); } - public boolean test(@Nonnull Block b, boolean firesEvent) { + /** + * Similar to {@link #test(Block)} this tests whether this {@link InfiniteBlockGenerator} + * exists at the given {@link Block}. + * Optionally this will also trigger a {@link BlockFromToEvent}. + * + * @param b + * The {@link Block} + * @param firesEvent + * Whether or not to to trigger a {@link BlockFromToEvent}. + * + * @return Whether this {@link InfiniteBlockGenerator} exists at the given {@link Block} + */ + public boolean testAndTrigger(@Nonnull Block b, boolean firesEvent) { Validate.notNull(b, "Block cannot be null!"); // This will eliminate non-matching base materials // If we are on a version without Basalt, it will be null here and not match. - if (b.getType() == material) { + if (b.getType() == getGeneratedMaterial()) { switch (this) { case COBBLESTONE_GENERATOR: return hasSurroundingMaterials(b, firesEvent, Material.WATER, Material.LAVA); @@ -161,7 +184,7 @@ public enum InfiniteBlockGenerator implements Predicate { Validate.notNull(b, "Cannot find a generator without a Location!"); for (InfiniteBlockGenerator generator : values) { - if (generator.test(b, firesEvent)) { + if (generator.testAndTrigger(b, firesEvent)) { return generator; } } diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/utils/TestInfiniteBlockGenerators.java b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/utils/TestInfiniteBlockGenerators.java new file mode 100644 index 000000000..c023473a0 --- /dev/null +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/utils/TestInfiniteBlockGenerators.java @@ -0,0 +1,162 @@ +package io.github.thebusybiscuit.slimefun4.testing.tests.utils; + +import java.util.Arrays; +import java.util.stream.Stream; + +import org.bukkit.Material; +import org.bukkit.World; +import org.bukkit.block.Block; +import org.bukkit.block.BlockFace; +import org.bukkit.event.block.BlockFromToEvent; +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 org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.EnumSource; +import org.junit.jupiter.params.provider.MethodSource; + +import be.seeseemelk.mockbukkit.MockBukkit; +import be.seeseemelk.mockbukkit.ServerMock; +import be.seeseemelk.mockbukkit.WorldMock; +import io.github.thebusybiscuit.slimefun4.implementation.SlimefunPlugin; +import io.github.thebusybiscuit.slimefun4.utils.InfiniteBlockGenerator; + +class TestInfiniteBlockGenerators { + + private static ServerMock server; + + @BeforeAll + public static void load() { + server = MockBukkit.mock(); + MockBukkit.load(SlimefunPlugin.class); + } + + @AfterAll + public static void unload() { + MockBukkit.unmock(); + } + + @Test + @DisplayName("Test if invalid Cobblestone generators are ignored") + void testInvalidCobblestoneGenerator() { + World world = new WorldMock(); + Block block = world.getBlockAt(0, 100, 0); + + block.setType(Material.STONE); + Assertions.assertFalse(InfiniteBlockGenerator.COBBLESTONE_GENERATOR.test(block)); + Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false)); + + block.setType(Material.COBBLESTONE); + Assertions.assertFalse(InfiniteBlockGenerator.COBBLESTONE_GENERATOR.test(block)); + Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false)); + } + + @ParameterizedTest + @MethodSource(value = "provideFaces") + @DisplayName("Test if a Cobblestone Generator can be detected") + void testValidCobblestoneGenerator(BlockFace water, BlockFace lava) { + InfiniteBlockGenerator generator = InfiniteBlockGenerator.COBBLESTONE_GENERATOR; + + World world = new WorldMock(); + Block block = world.getBlockAt(0, 100, 0); + + block.setType(Material.COBBLESTONE); + block.getRelative(water).setType(Material.WATER); + block.getRelative(lava).setType(Material.LAVA); + + Assertions.assertTrue(generator.testAndTrigger(block, true)); + server.getPluginManager().assertEventFired(BlockFromToEvent.class); + server.getPluginManager().clearEvents(); + + Assertions.assertEquals(generator, InfiniteBlockGenerator.findAt(block, false)); + } + + @Test + @DisplayName("Test if invalid Basalt generators are ignored") + void testInvalidBasaltGenerator() { + World world = new WorldMock(); + Block block = world.getBlockAt(0, 100, 0); + + block.setType(Material.COBBLESTONE); + Assertions.assertFalse(InfiniteBlockGenerator.BASALT_GENERATOR.test(block)); + Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false)); + + block.setType(Material.BASALT); + Assertions.assertFalse(InfiniteBlockGenerator.BASALT_GENERATOR.test(block)); + Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false)); + + block.getRelative(BlockFace.DOWN).setType(Material.SOUL_SOIL); + Assertions.assertFalse(InfiniteBlockGenerator.BASALT_GENERATOR.test(block)); + Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false)); + } + + @ParameterizedTest + @MethodSource(value = "provideFaces") + @DisplayName("Test if a Basalt Generator can be detected") + void testValidBasaltGenerator(BlockFace ice, BlockFace lava) { + InfiniteBlockGenerator generator = InfiniteBlockGenerator.BASALT_GENERATOR; + + World world = new WorldMock(); + Block block = world.getBlockAt(0, 100, 0); + + block.setType(Material.BASALT); + block.getRelative(BlockFace.DOWN).setType(Material.SOUL_SOIL); + block.getRelative(ice).setType(Material.BLUE_ICE); + block.getRelative(lava).setType(Material.LAVA); + + Assertions.assertTrue(generator.testAndTrigger(block, true)); + server.getPluginManager().assertEventFired(BlockFromToEvent.class); + server.getPluginManager().clearEvents(); + + Assertions.assertEquals(generator, InfiniteBlockGenerator.findAt(block, false)); + } + + @Test + @DisplayName("Test if invalid Stone generators are ignored") + void testInvalidStoneGenerator() { + World world = new WorldMock(); + Block block = world.getBlockAt(0, 100, 0); + + block.setType(Material.COBBLESTONE); + Assertions.assertFalse(InfiniteBlockGenerator.STONE_GENERATOR.test(block)); + Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false)); + + block.setType(Material.STONE); + Assertions.assertFalse(InfiniteBlockGenerator.STONE_GENERATOR.test(block)); + Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false)); + + block.getRelative(BlockFace.UP).setType(Material.LAVA); + Assertions.assertFalse(InfiniteBlockGenerator.STONE_GENERATOR.test(block)); + Assertions.assertNull(InfiniteBlockGenerator.findAt(block, false)); + } + + @ParameterizedTest + @EnumSource(value = BlockFace.class, names = { "NORTH", "EAST", "SOUTH", "WEST" }) + @DisplayName("Test if a Stone Generator can be detected") + void testValidStoneGenerator(BlockFace water) { + InfiniteBlockGenerator generator = InfiniteBlockGenerator.STONE_GENERATOR; + + World world = new WorldMock(); + Block block = world.getBlockAt(0, 100, 0); + + block.setType(Material.STONE); + block.getRelative(BlockFace.UP).setType(Material.LAVA); + block.getRelative(water).setType(Material.WATER); + + Assertions.assertTrue(generator.testAndTrigger(block, true)); + server.getPluginManager().assertEventFired(BlockFromToEvent.class); + server.getPluginManager().clearEvents(); + + Assertions.assertEquals(generator, InfiniteBlockGenerator.findAt(block, false)); + } + + private static Stream provideFaces() { + BlockFace[] faces = { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST }; + Stream stream = Arrays.stream(faces); + return stream.flatMap(a -> Arrays.stream(faces).filter(b -> a != b).map(b -> Arguments.of(a, b))); + } + +}