Fixed occassional crashes of Power Tests and continued TDD

This commit is contained in:
Timmeey86 2018-11-27 21:27:35 +01:00
parent 11e071289b
commit f56e1933a6
7 changed files with 72 additions and 37 deletions

View file

@ -1,37 +1,57 @@
package power;
import io.anuke.mindustry.content.Liquids;
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.BurnerGenerator;
import io.anuke.mindustry.world.blocks.power.ItemGenerator;
import io.anuke.mindustry.world.blocks.power.ItemLiquidGenerator;
import io.anuke.mindustry.world.blocks.power.PowerGenerator;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;
/** 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 ItemLiquidGenerator generator;
private Tile tile;
private ItemGenerator.ItemGeneratorEntity entity;
private final float fakeLiquidPowerMultiplier = 2.0f;
private final float fakeMaxLiquidGenerate = 0.5f;
@BeforeEach
public void createItemLiquidGenerator(){
sut = new ItemLiquidGenerator("fakegen"){
@Override
protected float getLiquidEfficiency(Liquid liquid){
return liquid.flammability;
}
public void createBurnerGenerator(){
// Use a burner generator instead of a custom ItemLiquidGenerator subclass since we would implement abstract methods the same way.
generator = new BurnerGenerator("fakegen"){{
powerProduction = 0.1f;
itemDuration = 60f;
liquidPowerMultiplier = fakeLiquidPowerMultiplier;
maxLiquidGenerate = fakeMaxLiquidGenerate;
}};
@Override
protected float getItemEfficiency(Item item){
return item.flammability;
}
};
tile = createFakeTile(0, 0, sut);
tile = createFakeTile(0, 0, generator);
entity = tile.entity();
}
@Test
void detectCrashes(){
sut.update(tile);
void testLiquidConsumption(){
final float providedUsage = 0.1f;
final float expectedEfficiency = providedUsage / fakeMaxLiquidGenerate * fakeLiquidPowerMultiplier * Liquids.oil.flammability;
entity.liquids.add(Liquids.oil, providedUsage);
entity.cons.update(tile.entity);
assumeTrue(entity.cons.valid());
// Perform an update on the generator once - This should use up all oil and produce a fraction of what's possible
generator.update(tile);
assertEquals(0.0f, entity.liquids.get(Liquids.oil));
assertEquals(expectedEfficiency, entity.productionEfficiency);
}
}