Skip to content

Commit

Permalink
Added tests to cover #99
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Mar 2, 2023
1 parent a4fc496 commit e4bbb70
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -132,23 +132,29 @@ private void verify(Greenhouse gh) {

}

private void addMobs(Greenhouse gh) {
/**
* Try to spawn mobs in a greenhouse
* @param gh greenhouse
* @return true if mobs were spawned, false if not
*/
boolean addMobs(Greenhouse gh) {
final BoundingBox bb = gh.getBoundingBox();
if(gh.getLocation() == null || gh.getLocation().getWorld() == null || gh.getWorld() == null
|| !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMaxX()) >> 4, ((int) bb.getMaxZ()) >> 4) || !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMinX()) >> 4, ((int) bb.getMinZ()) >> 4)){
|| !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMaxX()) >> 4, ((int) bb.getMaxZ()) >> 4)
|| !gh.getLocation().getWorld().isChunkLoaded(((int) bb.getMinX()) >> 4, ((int) bb.getMinZ()) >> 4)){
// Skipping addmobs for unloaded greenhouse
return;
return false;
}
if (gh.getBiomeRecipe().noMobs()) {
return;
return false;
}
// Check greenhouse chunks are loaded
for (double blockX = bb.getMinX(); blockX < bb.getMaxX(); blockX+=16) {
for (double blockZ = bb.getMinZ(); blockZ < bb.getMaxZ(); blockZ+=16) {
int chunkX = (int)(blockX / 16);
int chunkZ = (int)(blockZ / 16);
if (!gh.getWorld().isChunkLoaded(chunkX, chunkZ)) {
return;
return false;
}
}
}
Expand All @@ -162,7 +168,7 @@ private void addMobs(Greenhouse gh) {
Iterator<GrowthBlock> it = list.iterator();
// Check if the greenhouse is full
if (sum >= gh.getBiomeRecipe().getMaxMob()) {
return;
return false;
}
while (it.hasNext() && (sum == 0 || gh.getArea() / sum >= gh.getBiomeRecipe().getMobLimit())) {
// Spawn something if chance says so
Expand All @@ -171,6 +177,7 @@ private void addMobs(Greenhouse gh) {
sum++;
}
}
return sum > 0;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package world.bentobox.greenhouses.managers;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
Expand All @@ -9,11 +11,11 @@
import static org.mockito.Mockito.when;

import java.util.List;
import java.util.Optional;

import org.bukkit.Bukkit;
import org.bukkit.Keyed;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.Tag;
import org.bukkit.World;
import org.bukkit.block.Block;
Expand All @@ -25,21 +27,21 @@
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.stubbing.OngoingStubbing;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

import world.bentobox.bentobox.BentoBox;
import world.bentobox.greenhouses.data.Greenhouse;
import world.bentobox.greenhouses.greenhouse.BiomeRecipe;
import world.bentobox.greenhouses.managers.EcoSystemManager.GrowthBlock;

/**
* @author tastybento
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BentoBox.class, Tag.class})
@PrepareForTest({Bukkit.class, BentoBox.class, Tag.class, RecipeManager.class})
public class EcoSystemManagerTest {

private Greenhouse gh;
Expand All @@ -53,6 +55,8 @@ public class EcoSystemManagerTest {
private Block liquid;
@Mock
private Block plant;
@Mock
private BiomeRecipe recipe;

// CUT
private EcoSystemManager eco;
Expand All @@ -62,6 +66,7 @@ public class EcoSystemManagerTest {
public void setUp() {
PowerMockito.mockStatic(Tag.class, Mockito.RETURNS_MOCKS);
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
@SuppressWarnings("unchecked")
Tag<Keyed> tag = mock(Tag.class);
when(Bukkit.getTag(anyString(), any(), any())).thenReturn(tag);

Expand Down Expand Up @@ -95,6 +100,12 @@ public void setUp() {
// Liquid false
when(block.getRelative(eq(BlockFace.UP))).thenReturn(air);

// Recipe
when(recipe.noMobs()).thenReturn(true);
PowerMockito.mockStatic(RecipeManager.class, Mockito.RETURNS_MOCKS);
when(RecipeManager.getBiomeRecipies(any())).thenReturn(Optional.of(recipe));


eco = new EcoSystemManager(null, null);
}

Expand Down Expand Up @@ -209,4 +220,57 @@ public void testGetAvailableBlocksAirAboveLiquidIgnoreLiquids() {
assertEquals(liquid, value.block());
}
}

/**
* Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#addMobs(Greenhouse)}.
*/
@Test
public void testAddMobsChunkNotLoaded() {
assertFalse(eco.addMobs(gh));
}

/**
* Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#addMobs(Greenhouse)}.
*/
@Test
public void testAddMobsChunkLoadedNoMobs() {
when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true);
assertFalse(eco.addMobs(gh));
}

/**
* Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#addMobs(Greenhouse)}.
*/
@Test
public void testAddMobsChunkLoadedWithMobsInRecipeMaxMobsZero() {
when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true);
when(recipe.noMobs()).thenReturn(false);
assertFalse(eco.addMobs(gh));
}

/**
* Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#addMobs(Greenhouse)}.
*/
@Test
public void testAddMobsChunkLoadedWithMobsInRecipeMaxMobsNotZero() {
// Nothing spawned here
when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true);
when(recipe.noMobs()).thenReturn(false);
when(recipe.getMaxMob()).thenReturn(10);
assertFalse(eco.addMobs(gh));
}

/**
* Test method for {@link world.bentobox.greenhouses.managers.EcoSystemManager#addMobs(Greenhouse)}.
*/
@Test
public void testAddMobsSpawnMob() {
// Nothing spawned here
when(world.isChunkLoaded(anyInt(), anyInt())).thenReturn(true);
when(recipe.noMobs()).thenReturn(false);
when(recipe.getMaxMob()).thenReturn(10);
when(recipe.spawnMob(any())).thenReturn(true);
assertTrue(eco.addMobs(gh));
}

}

0 comments on commit e4bbb70

Please sign in to comment.