Bus tests.

This commit is contained in:
Florian Nücke
2020-11-30 18:37:10 +01:00
parent bbd98e9a84
commit f5745c4a16

View File

@@ -23,6 +23,8 @@ import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
public class DeviceBusTests {
private static final BlockPos CONTROLLER_POS = new BlockPos(0, 0, 0);
@Mock
private Capability<DeviceBusElement> busElementCapability;
private World world;
@@ -42,14 +44,14 @@ public class DeviceBusTests {
@Test
public void scanPendingWhenTileEntityNotLoaded() {
Assertions.assertEquals(DeviceBusControllerImpl.State.SCAN_PENDING,
controller.scan(world, new BlockPos(0, 0, 0)));
controller.scan(world, CONTROLLER_POS));
}
@Test
public void scanCompletesWhenNoNeighbors() {
when(world.chunkExists(anyInt(), anyInt())).thenReturn(true);
Assertions.assertEquals(DeviceBusControllerImpl.State.READY,
controller.scan(world, new BlockPos(0, 0, 0)));
controller.scan(world, CONTROLLER_POS));
}
@Test
@@ -57,7 +59,7 @@ public class DeviceBusTests {
when(world.chunkExists(anyInt(), anyInt())).thenReturn(true);
final TileEntity tileEntity = mock(TileEntity.class);
when(world.getTileEntity(eq(new BlockPos(0, 0, 0)))).thenReturn(tileEntity);
when(world.getTileEntity(eq(CONTROLLER_POS))).thenReturn(tileEntity);
final DeviceBusElement busElement = mock(DeviceBusElement.class);
when(tileEntity.getCapability(eq(busElementCapability), any())).thenReturn(LazyOptional.of(() -> busElement));
@@ -68,9 +70,40 @@ public class DeviceBusTests {
when(device.getUniqueId()).thenReturn(UUID.randomUUID());
Assertions.assertEquals(DeviceBusControllerImpl.State.READY,
controller.scan(world, new BlockPos(0, 0, 0)));
controller.scan(world, CONTROLLER_POS));
verify(busElement).setController(controller);
Assertions.assertTrue(controller.getDevices().contains(device));
}
@Test
public void scanSuccessfulWithMultipleElements() {
when(world.chunkExists(anyInt(), anyInt())).thenReturn(true);
final TileEntity tileEntityController = mock(TileEntity.class);
when(world.getTileEntity(eq(CONTROLLER_POS))).thenReturn(tileEntityController);
final DeviceBusElement busElementController = mock(DeviceBusElement.class);
when(tileEntityController.getCapability(eq(busElementCapability), any())).thenReturn(LazyOptional.of(() -> busElementController));
final TileEntity tileEntityBusElement1 = mock(TileEntity.class);
when(world.getTileEntity(eq(CONTROLLER_POS.west()))).thenReturn(tileEntityBusElement1);
final DeviceBusElement busElement1 = mock(DeviceBusElement.class);
when(tileEntityBusElement1.getCapability(eq(busElementCapability), any())).thenReturn(LazyOptional.of(() -> busElement1));
when(busElement1.getLocalDevices()).thenReturn(Collections.emptyList());
final TileEntity tileEntityBusElement2 = mock(TileEntity.class);
when(world.getTileEntity(eq(CONTROLLER_POS.west().west()))).thenReturn(tileEntityBusElement2);
final DeviceBusElement busElement2 = mock(DeviceBusElement.class);
when(tileEntityBusElement2.getCapability(eq(busElementCapability), any())).thenReturn(LazyOptional.of(() -> busElement2));
when(busElement2.getLocalDevices()).thenReturn(Collections.emptyList());
Assertions.assertEquals(DeviceBusControllerImpl.State.READY,
controller.scan(world, CONTROLLER_POS));
verify(busElement1).setController(controller);
verify(busElement2).setController(controller);
}
}