mirror of
https://github.com/Anuken/Mindustry.git
synced 2025-07-11 00:07:46 +07:00
Reorganized power tests ...
... and changed creation of fake tiles so update() implementations have all required dependencies
This commit is contained in:
@ -1,4 +1,5 @@
|
|||||||
import com.badlogic.gdx.Gdx;
|
package power;
|
||||||
|
|
||||||
import io.anuke.mindustry.core.ThreadHandler;
|
import io.anuke.mindustry.core.ThreadHandler;
|
||||||
import io.anuke.ucore.core.Timers;
|
import io.anuke.ucore.core.Timers;
|
||||||
|
|
37
tests/src/test/java/power/ItemLiquidGeneratorTests.java
Normal file
37
tests/src/test/java/power/ItemLiquidGeneratorTests.java
Normal file
@ -0,0 +1,37 @@
|
|||||||
|
package power;
|
||||||
|
|
||||||
|
import io.anuke.mindustry.type.Item;
|
||||||
|
import io.anuke.mindustry.type.Liquid;
|
||||||
|
import io.anuke.mindustry.world.Tile;
|
||||||
|
import io.anuke.mindustry.world.blocks.power.ItemLiquidGenerator;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
/** This class tests the abstract ItemLiquidGenerator class and maybe some of its dependencies. */
|
||||||
|
public class ItemLiquidGeneratorTests extends PowerTestFixture{
|
||||||
|
|
||||||
|
private ItemLiquidGenerator sut; // system under test (https://en.wikipedia.org/wiki/System_under_test)
|
||||||
|
private Tile tile;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void createItemLiquidGenerator(){
|
||||||
|
sut = new ItemLiquidGenerator("fakegen"){
|
||||||
|
@Override
|
||||||
|
protected float getLiquidEfficiency(Liquid liquid){
|
||||||
|
return liquid.flammability;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected float getItemEfficiency(Item item){
|
||||||
|
return item.flammability;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
tile = createFakeTile(0, 0, sut);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void detectCrashes(){
|
||||||
|
sut.update(tile);
|
||||||
|
}
|
||||||
|
}
|
@ -1,18 +1,31 @@
|
|||||||
|
package power;
|
||||||
|
|
||||||
|
import io.anuke.mindustry.Vars;
|
||||||
import io.anuke.mindustry.content.blocks.Blocks;
|
import io.anuke.mindustry.content.blocks.Blocks;
|
||||||
|
import io.anuke.mindustry.core.ContentLoader;
|
||||||
import io.anuke.mindustry.world.Block;
|
import io.anuke.mindustry.world.Block;
|
||||||
import io.anuke.mindustry.world.Tile;
|
import io.anuke.mindustry.world.Tile;
|
||||||
import io.anuke.mindustry.world.blocks.Floor;
|
import io.anuke.mindustry.world.blocks.PowerBlock;
|
||||||
import io.anuke.mindustry.world.blocks.power.Battery;
|
import io.anuke.mindustry.world.blocks.power.Battery;
|
||||||
import io.anuke.mindustry.world.blocks.power.PowerGenerator;
|
import io.anuke.mindustry.world.blocks.power.PowerGenerator;
|
||||||
import io.anuke.mindustry.world.modules.PowerModule;
|
import io.anuke.mindustry.world.modules.PowerModule;
|
||||||
|
import org.junit.jupiter.api.BeforeAll;
|
||||||
|
|
||||||
import java.lang.reflect.Field;
|
import java.lang.reflect.Field;
|
||||||
|
import java.lang.reflect.Method;
|
||||||
|
|
||||||
/** This class provides objects commonly used by power related unit tests.
|
/** This class provides objects commonly used by power related unit tests.
|
||||||
* For now, this is a helper with static methods, but this might change.
|
* For now, this is a helper with static methods, but this might change.
|
||||||
* */
|
* */
|
||||||
public class PowerTestFixture{
|
public class PowerTestFixture{
|
||||||
|
|
||||||
|
@BeforeAll
|
||||||
|
static void initializeDependencies(){
|
||||||
|
Vars.content = new ContentLoader();
|
||||||
|
Vars.content.load();
|
||||||
|
Vars.threads = new FakeThreadHandler();
|
||||||
|
}
|
||||||
|
|
||||||
protected static PowerGenerator createFakeProducerBlock(float producedPower){
|
protected static PowerGenerator createFakeProducerBlock(float producedPower){
|
||||||
return new PowerGenerator("fakegen"){{
|
return new PowerGenerator("fakegen"){{
|
||||||
powerProduction = producedPower;
|
powerProduction = producedPower;
|
||||||
@ -26,13 +39,13 @@ public class PowerTestFixture{
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected static Block createFakeDirectConsumer(float powerPerTick, float minimumSatisfaction){
|
protected static Block createFakeDirectConsumer(float powerPerTick, float minimumSatisfaction){
|
||||||
return new Block("fakedirectconsumer"){{
|
return new PowerBlock("fakedirectconsumer"){{
|
||||||
consumes.powerDirect(powerPerTick, minimumSatisfaction);
|
consumes.powerDirect(powerPerTick, minimumSatisfaction);
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
|
||||||
protected static Block createFakeBufferedConsumer(float capacity, float ticksToFill){
|
protected static Block createFakeBufferedConsumer(float capacity, float ticksToFill){
|
||||||
return new Block("fakebufferedconsumer"){{
|
return new PowerBlock("fakebufferedconsumer"){{
|
||||||
consumes.powerBuffered(capacity, ticksToFill);
|
consumes.powerBuffered(capacity, ticksToFill);
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
@ -47,6 +60,10 @@ public class PowerTestFixture{
|
|||||||
try{
|
try{
|
||||||
Tile tile = new Tile(x, y);
|
Tile tile = new Tile(x, y);
|
||||||
|
|
||||||
|
// Using the Tile(int, int, byte, byte) constructor would require us to register any fake block or tile we create
|
||||||
|
// Since this part shall not be part of the test and would require more work anyway, we manually set the block and floor
|
||||||
|
// and call the private changed() method through reflections.
|
||||||
|
|
||||||
Field field = Tile.class.getDeclaredField("wall");
|
Field field = Tile.class.getDeclaredField("wall");
|
||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
field.set(tile, block);
|
field.set(tile, block);
|
||||||
@ -55,8 +72,10 @@ public class PowerTestFixture{
|
|||||||
field.setAccessible(true);
|
field.setAccessible(true);
|
||||||
field.set(tile, Blocks.sand);
|
field.set(tile, Blocks.sand);
|
||||||
|
|
||||||
tile.entity = block.newEntity();
|
Method method = Tile.class.getDeclaredMethod("changed");
|
||||||
tile.entity.power = new PowerModule();
|
method.setAccessible(true);
|
||||||
|
method.invoke(tile);
|
||||||
|
|
||||||
return tile;
|
return tile;
|
||||||
}catch(Exception ex){
|
}catch(Exception ex){
|
||||||
return null;
|
return null;
|
@ -1,28 +1,22 @@
|
|||||||
|
package power;
|
||||||
|
|
||||||
import com.badlogic.gdx.math.MathUtils;
|
import com.badlogic.gdx.math.MathUtils;
|
||||||
import io.anuke.mindustry.Vars;
|
import io.anuke.mindustry.Vars;
|
||||||
import io.anuke.mindustry.content.blocks.PowerBlocks;
|
|
||||||
import io.anuke.mindustry.content.blocks.ProductionBlocks;
|
|
||||||
import io.anuke.mindustry.core.ContentLoader;
|
import io.anuke.mindustry.core.ContentLoader;
|
||||||
import io.anuke.mindustry.world.Tile;
|
import io.anuke.mindustry.world.Tile;
|
||||||
import io.anuke.mindustry.world.blocks.power.PowerGraph;
|
import io.anuke.mindustry.world.blocks.power.PowerGraph;
|
||||||
|
import io.anuke.mindustry.world.consumers.ConsumePower;
|
||||||
import org.junit.jupiter.api.*;
|
import org.junit.jupiter.api.*;
|
||||||
import org.junit.jupiter.params.provider.ValueSource;
|
|
||||||
import org.junit.jupiter.params.ParameterizedTest;
|
|
||||||
|
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertAll;
|
||||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
import static org.junit.jupiter.api.Assumptions.assumeTrue;
|
||||||
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
|
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
|
||||||
|
|
||||||
public class PowerTests extends PowerTestFixture{
|
public class PowerTests extends PowerTestFixture{
|
||||||
|
|
||||||
@BeforeAll
|
|
||||||
static void initializeDependencies(){
|
|
||||||
Vars.content = new ContentLoader();
|
|
||||||
Vars.content.load();
|
|
||||||
Vars.threads = new FakeThreadHandler();
|
|
||||||
}
|
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void initTest(){
|
void initTest(){
|
||||||
}
|
}
|
||||||
@ -143,5 +137,29 @@ public class PowerTests extends PowerTestFixture{
|
|||||||
assertEquals(expectedSatisfaction, directConsumerTile.entity.power.satisfaction, MathUtils.FLOAT_ROUNDING_ERROR, parameterDescription + ": Satisfaction of direct consumer did not match");
|
assertEquals(expectedSatisfaction, directConsumerTile.entity.power.satisfaction, MathUtils.FLOAT_ROUNDING_ERROR, parameterDescription + ": Satisfaction of direct consumer did not match");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Makes sure a direct consumer stops working after power production is set to zero. */
|
||||||
|
@Test
|
||||||
|
void testDirectConsumptionStopsWithNoPower(){
|
||||||
|
Tile producerTile = createFakeTile(0, 0, createFakeProducerBlock(10.0f));
|
||||||
|
Tile consumerTile = createFakeTile(0, 1, createFakeDirectConsumer(5.0f, 0.6f));
|
||||||
|
|
||||||
|
PowerGraph powerGraph = new PowerGraph();
|
||||||
|
powerGraph.add(producerTile);
|
||||||
|
powerGraph.add(consumerTile);
|
||||||
|
powerGraph.update();
|
||||||
|
|
||||||
|
assertEquals(1.0f, consumerTile.entity.power.satisfaction, MathUtils.FLOAT_ROUNDING_ERROR);
|
||||||
|
|
||||||
|
powerGraph.remove(producerTile);
|
||||||
|
powerGraph.add(consumerTile);
|
||||||
|
powerGraph.update();
|
||||||
|
|
||||||
|
assertEquals(0.0f, consumerTile.entity.power.satisfaction, MathUtils.FLOAT_ROUNDING_ERROR);
|
||||||
|
if(consumerTile.block().consumes.has(ConsumePower.class)){
|
||||||
|
ConsumePower consumePower = consumerTile.block().consumes.get(ConsumePower.class);
|
||||||
|
assertFalse(consumePower.valid(consumerTile.block(), consumerTile.entity()));
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Reference in New Issue
Block a user