From 11e071289bb60f0f00a5b9010b2a1a5bf2a77f0f Mon Sep 17 00:00:00 2001 From: Timmeey86 Date: Tue, 27 Nov 2018 08:51:19 +0100 Subject: [PATCH] Reorganized power tests ... ... and changed creation of fake tiles so update() implementations have all required dependencies --- .../java/{ => power}/FakeThreadHandler.java | 3 +- .../java/power/ItemLiquidGeneratorTests.java | 37 +++++++++++++++++ .../java/{ => power}/PowerTestFixture.java | 29 +++++++++++--- .../src/test/java/{ => power}/PowerTests.java | 40 ++++++++++++++----- 4 files changed, 92 insertions(+), 17 deletions(-) rename tests/src/test/java/{ => power}/FakeThreadHandler.java (94%) create mode 100644 tests/src/test/java/power/ItemLiquidGeneratorTests.java rename tests/src/test/java/{ => power}/PowerTestFixture.java (67%) rename tests/src/test/java/{ => power}/PowerTests.java (88%) diff --git a/tests/src/test/java/FakeThreadHandler.java b/tests/src/test/java/power/FakeThreadHandler.java similarity index 94% rename from tests/src/test/java/FakeThreadHandler.java rename to tests/src/test/java/power/FakeThreadHandler.java index 3588205e98..9b2676c49b 100644 --- a/tests/src/test/java/FakeThreadHandler.java +++ b/tests/src/test/java/power/FakeThreadHandler.java @@ -1,4 +1,5 @@ -import com.badlogic.gdx.Gdx; +package power; + import io.anuke.mindustry.core.ThreadHandler; import io.anuke.ucore.core.Timers; diff --git a/tests/src/test/java/power/ItemLiquidGeneratorTests.java b/tests/src/test/java/power/ItemLiquidGeneratorTests.java new file mode 100644 index 0000000000..930f257cd9 --- /dev/null +++ b/tests/src/test/java/power/ItemLiquidGeneratorTests.java @@ -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); + } +} diff --git a/tests/src/test/java/PowerTestFixture.java b/tests/src/test/java/power/PowerTestFixture.java similarity index 67% rename from tests/src/test/java/PowerTestFixture.java rename to tests/src/test/java/power/PowerTestFixture.java index 2ecd5a7dc7..e2bc95ec4e 100644 --- a/tests/src/test/java/PowerTestFixture.java +++ b/tests/src/test/java/power/PowerTestFixture.java @@ -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; diff --git a/tests/src/test/java/PowerTests.java b/tests/src/test/java/power/PowerTests.java similarity index 88% rename from tests/src/test/java/PowerTests.java rename to tests/src/test/java/power/PowerTests.java index 3b93652c0e..48b5cf8c6f 100644 --- a/tests/src/test/java/PowerTests.java +++ b/tests/src/test/java/power/PowerTests.java @@ -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())); + } + } } }