mirror of
https://github.com/StarWishsama/Slimefun4.git
synced 2024-09-20 19:55:48 +00:00
Added Unit Tests
This commit is contained in:
parent
2810019dbe
commit
1a7de82c45
@ -13,6 +13,7 @@ import org.bukkit.block.Block;
|
|||||||
import org.bukkit.block.BlockFace;
|
import org.bukkit.block.BlockFace;
|
||||||
import org.bukkit.event.block.BlockFromToEvent;
|
import org.bukkit.event.block.BlockFromToEvent;
|
||||||
|
|
||||||
|
import io.github.thebusybiscuit.slimefun4.api.MinecraftVersion;
|
||||||
import io.github.thebusybiscuit.slimefun4.implementation.items.androids.MinerAndroid;
|
import io.github.thebusybiscuit.slimefun4.implementation.items.androids.MinerAndroid;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -43,25 +44,47 @@ public enum InfiniteBlockGenerator implements Predicate<Block> {
|
|||||||
public static final InfiniteBlockGenerator[] values = values();
|
public static final InfiniteBlockGenerator[] values = values();
|
||||||
private static final BlockFace[] sameLevelFaces = { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST };
|
private static final BlockFace[] sameLevelFaces = { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST };
|
||||||
|
|
||||||
@Nullable
|
|
||||||
private final Material material;
|
private final Material material;
|
||||||
|
|
||||||
@ParametersAreNonnullByDefault
|
InfiniteBlockGenerator(@Nonnull String type) {
|
||||||
InfiniteBlockGenerator(String type) {
|
|
||||||
this.material = Material.matchMaterial(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
|
@Override
|
||||||
public boolean test(@Nonnull Block b) {
|
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!");
|
Validate.notNull(b, "Block cannot be null!");
|
||||||
|
|
||||||
// This will eliminate non-matching base materials
|
// This will eliminate non-matching base materials
|
||||||
// If we are on a version without Basalt, it will be null here and not match.
|
// 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) {
|
switch (this) {
|
||||||
case COBBLESTONE_GENERATOR:
|
case COBBLESTONE_GENERATOR:
|
||||||
return hasSurroundingMaterials(b, firesEvent, Material.WATER, Material.LAVA);
|
return hasSurroundingMaterials(b, firesEvent, Material.WATER, Material.LAVA);
|
||||||
@ -161,7 +184,7 @@ public enum InfiniteBlockGenerator implements Predicate<Block> {
|
|||||||
Validate.notNull(b, "Cannot find a generator without a Location!");
|
Validate.notNull(b, "Cannot find a generator without a Location!");
|
||||||
|
|
||||||
for (InfiniteBlockGenerator generator : values) {
|
for (InfiniteBlockGenerator generator : values) {
|
||||||
if (generator.test(b, firesEvent)) {
|
if (generator.testAndTrigger(b, firesEvent)) {
|
||||||
return generator;
|
return generator;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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<Arguments> provideFaces() {
|
||||||
|
BlockFace[] faces = { BlockFace.NORTH, BlockFace.EAST, BlockFace.SOUTH, BlockFace.WEST };
|
||||||
|
Stream<BlockFace> stream = Arrays.stream(faces);
|
||||||
|
return stream.flatMap(a -> Arrays.stream(faces).filter(b -> a != b).map(b -> Arguments.of(a, b)));
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user