Archived
0

обновление теста ChunkdataPacketTest

This commit is contained in:
2018-10-28 20:48:05 +03:00
parent 58334591d0
commit 0ad933e57c
2 changed files with 49 additions and 38 deletions

View File

@@ -1,6 +1,5 @@
package mc.core.network.proto_1_12_2.packets; package mc.core.network.proto_1_12_2.packets;
import com.google.common.io.ByteStreams;
import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream; import mc.core.network.proto_1_12_2.ByteArrayOutputNetStream;
import mc.core.world.Biome; import mc.core.world.Biome;
import mc.core.world.World; import mc.core.world.World;
@@ -9,6 +8,7 @@ import mc.core.world.block.BlockFactory;
import mc.core.world.block.BlockType; import mc.core.world.block.BlockType;
import mc.core.world.chunk.Chunk; import mc.core.world.chunk.Chunk;
import mc.core.world.chunk.ChunkSection; import mc.core.world.chunk.ChunkSection;
import org.apache.commons.io.IOUtils;
import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@@ -18,6 +18,7 @@ import java.io.InputStream;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Matchers.anyInt; import static org.mockito.Matchers.anyInt;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
@@ -29,51 +30,61 @@ class ChunkdataPacketTest {
@BeforeAll @BeforeAll
static void beforeClassTest() throws IOException { static void beforeClassTest() throws IOException {
InputStream inputStream = ChunkdataPacketTest.class.getResourceAsStream("ChunkDataPacket.bin"); InputStream inputStream = ChunkdataPacketTest.class.getResourceAsStream("ChunkDataPacket.bin");
expectedPacketData = ByteStreams.toByteArray(inputStream); assertNotNull(inputStream);
expectedPacketData = IOUtils.toByteArray(inputStream);
assertEquals(12571, expectedPacketData.length);
}
private ChunkSection createChunkSection(int height) {
final ChunkSection chunkSection = mock(ChunkSection.class);
when(chunkSection.getBiomeLocal(anyInt(), anyInt())).thenReturn(Biome.PLAINS);
when(chunkSection.getSkyLightLocal(anyInt(), anyInt(), anyInt())).thenReturn(0);
when(chunkSection.getY()).thenReturn(height);
if (height == 0) {
when(chunkSection.getBlockLocal(anyInt(), anyInt(), anyInt())).thenAnswer(invocation -> {
Object[] args = invocation.getArguments();
int x = (int) args[0];
int y = (int) args[1];
int z = (int) args[2];
BlockFactory blockFactory = new BlockFactory();
if (y == 0) return blockFactory.create(BlockType.BEDROCK, x, y, z);
else return blockFactory.create(BlockType.DIRT, x, y, z);
});
} else {
when(chunkSection.getBlockLocal(anyInt(), anyInt(), anyInt())).thenAnswer(invocation -> {
Object[] args = invocation.getArguments();
int x = (int) args[0];
int y = (int) args[1];
int z = (int) args[2];
BlockFactory blockFactory = new BlockFactory();
if (y < 15) return blockFactory.create(BlockType.DIRT, x, y, z);
else return blockFactory.create(BlockType.GRASS, x, y, z);
});
}
return chunkSection;
} }
@BeforeEach @BeforeEach
void prepareWorld() { void prepareWorld() {
final ChunkSection chunkSection = mock(ChunkSection.class); final ChunkSection chunkSection0 = createChunkSection(0);
when(chunkSection.getSkyLight(anyInt(), anyInt(), anyInt())).thenAnswer(invocation -> { final ChunkSection chunkSection1 = createChunkSection(1);
int y = (int)invocation.getArguments()[1];
if (y <= 3) return 0; final Chunk chunk = mock(Chunk.class);
else return 15; when(chunk.getX()).thenReturn(0);
}); when(chunk.getZ()).thenReturn(0);
when(chunkSection.getBiome(anyInt(), anyInt())).thenReturn(Biome.PLAINS); when(chunk.getBiomeLocal(anyInt(), anyInt())).thenReturn(Biome.PLAINS);
when(chunkSection.getBlock(anyInt(), anyInt(), anyInt())).thenAnswer(invocation -> { when(chunk.getChunkSection(0)).thenReturn(chunkSection0);
Object[] args = invocation.getArguments(); when(chunk.getChunkSection(1)).thenReturn(chunkSection1);
int x = (int) args[0];
int y = (int) args[1];
int z = (int) args[2];
BlockFactory blockFactory = new BlockFactory();
if (y == 0) return blockFactory.create(BlockType.BEDROCK, x, y, z);
else if (y >= 1 && y <= 2) return blockFactory.create(BlockType.DIRT, x, y, z);
else if (y == 3) return blockFactory.create(BlockType.GRASS, x, y, z);
else return blockFactory.create(BlockType.AIR, x, y, z);
});
world = mock(World.class); world = mock(World.class);
when(world.getWorldType()).thenReturn(WorldType.FLAT); when(world.getWorldType()).thenReturn(WorldType.FLAT);
when(world.getChunk(anyInt(), anyInt())).thenAnswer(invocation -> { when(world.getChunk(0, 0)).thenReturn(chunk);
Object[] args = invocation.getArguments();
Chunk chunk = mock(Chunk.class);
when(chunk.getX()).thenReturn((int) args[0]);
when(chunk.getZ()).thenReturn((int) args[1]);
when(chunk.getBiome(anyInt(), anyInt())).thenReturn(Biome.PLAINS);
when(chunk.getChunkSection(anyInt())).thenAnswer(invocation1 -> {
int height = (int)invocation1.getArguments()[0];
if (height < 1) return chunkSection;
else return null;
});
return chunk;
});
} }
@Test @Test