Reorganized power tests ...

... and changed creation of fake tiles so update() implementations have all required dependencies
This commit is contained in:
Timmeey86 2018-11-27 08:51:19 +01:00
parent a9517096f9
commit 11e071289b
4 changed files with 92 additions and 17 deletions

View File

@ -1,4 +1,5 @@
import com.badlogic.gdx.Gdx;
package power;
import io.anuke.mindustry.core.ThreadHandler;
import io.anuke.ucore.core.Timers;

View 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);
}
}

View File

@ -1,18 +1,31 @@
package power;
import io.anuke.mindustry.Vars;
import io.anuke.mindustry.content.blocks.Blocks;
import io.anuke.mindustry.core.ContentLoader;
import io.anuke.mindustry.world.Block;
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.PowerGenerator;
import io.anuke.mindustry.world.modules.PowerModule;
import org.junit.jupiter.api.BeforeAll;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
/** This class provides objects commonly used by power related unit tests.
* For now, this is a helper with static methods, but this might change.
* */
public class PowerTestFixture{
@BeforeAll
static void initializeDependencies(){
Vars.content = new ContentLoader();
Vars.content.load();
Vars.threads = new FakeThreadHandler();
}
protected static PowerGenerator createFakeProducerBlock(float producedPower){
return new PowerGenerator("fakegen"){{
powerProduction = producedPower;
@ -26,13 +39,13 @@ public class PowerTestFixture{
}
protected static Block createFakeDirectConsumer(float powerPerTick, float minimumSatisfaction){
return new Block("fakedirectconsumer"){{
return new PowerBlock("fakedirectconsumer"){{
consumes.powerDirect(powerPerTick, minimumSatisfaction);
}};
}
protected static Block createFakeBufferedConsumer(float capacity, float ticksToFill){
return new Block("fakebufferedconsumer"){{
return new PowerBlock("fakebufferedconsumer"){{
consumes.powerBuffered(capacity, ticksToFill);
}};
}
@ -47,6 +60,10 @@ public class PowerTestFixture{
try{
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.setAccessible(true);
field.set(tile, block);
@ -55,8 +72,10 @@ public class PowerTestFixture{
field.setAccessible(true);
field.set(tile, Blocks.sand);
tile.entity = block.newEntity();
tile.entity.power = new PowerModule();
Method method = Tile.class.getDeclaredMethod("changed");
method.setAccessible(true);
method.invoke(tile);
return tile;
}catch(Exception ex){
return null;

View File

@ -1,28 +1,22 @@
package power;
import com.badlogic.gdx.math.MathUtils;
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.world.Tile;
import io.anuke.mindustry.world.blocks.power.PowerGraph;
import io.anuke.mindustry.world.consumers.ConsumePower;
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.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.junit.jupiter.api.DynamicTest.dynamicTest;
public class PowerTests extends PowerTestFixture{
@BeforeAll
static void initializeDependencies(){
Vars.content = new ContentLoader();
Vars.content.load();
Vars.threads = new FakeThreadHandler();
}
@BeforeEach
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");
}
}
/** 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()));
}
}
}
}