|
| 1 | +package net.tfminecraft.simplefactions.map; |
| 2 | + |
| 3 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 5 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | + |
| 8 | +import java.util.HashMap; |
| 9 | +import java.util.Map; |
| 10 | + |
| 11 | +import org.junit.jupiter.api.AfterEach; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import com.google.gson.JsonObject; |
| 16 | + |
| 17 | +import net.tfminecraft.simplefactions.Cache; |
| 18 | +import net.tfminecraft.simplefactions.enums.Terrain; |
| 19 | +import net.tfminecraft.simplefactions.guild.Guild; |
| 20 | +import net.tfminecraft.simplefactions.map.provinces.Province; |
| 21 | +import net.tfminecraft.simplefactions.map.provinces.ProvinceDataEntry; |
| 22 | + |
| 23 | +class CompilerProvinceExportTest { |
| 24 | + private final Map<Terrain, Double> previousCarry = new HashMap<>(Cache.tradeCarry); |
| 25 | + private final double previousFull = Cache.infrastructureFull; |
| 26 | + private final double previousTarget = Cache.infrastructureTarget; |
| 27 | + |
| 28 | + @BeforeEach |
| 29 | + void setUp() { |
| 30 | + Cache.infrastructureFull = 20; |
| 31 | + Cache.infrastructureTarget = 0.75; |
| 32 | + Cache.tradeCarry.clear(); |
| 33 | + Cache.tradeCarry.put(Terrain.BOG, 0.40); |
| 34 | + Cache.tradeCarry.put(Terrain.PLAINS, 0.75); |
| 35 | + Cache.tradeCarry.put(Terrain.SEA, 0.60); |
| 36 | + Cache.tradeCarry.put(Terrain.WATER, 0.75); |
| 37 | + } |
| 38 | + |
| 39 | + @AfterEach |
| 40 | + void restore() { |
| 41 | + Cache.infrastructureFull = previousFull; |
| 42 | + Cache.infrastructureTarget = previousTarget; |
| 43 | + Cache.tradeCarry.clear(); |
| 44 | + Cache.tradeCarry.putAll(previousCarry); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + void bogWithInfrastructureExportsFillAndEffectiveTerrain() { |
| 49 | + Province province = new Province(1, "bog", 0); |
| 50 | + province.setInfrastructure(12); |
| 51 | + |
| 52 | + JsonObject json = Compiler.provinceToJson(province, null); |
| 53 | + |
| 54 | + assertEquals("bog", json.get("terrain").getAsString()); |
| 55 | + assertEquals(0.40, json.get("terrain_value").getAsDouble()); |
| 56 | + assertEquals(12, json.get("infrastructure").getAsDouble()); |
| 57 | + assertEquals(0.60, json.get("infrastructure_fill").getAsDouble()); |
| 58 | + assertEquals(0.61, json.get("effective_terrain").getAsDouble()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + void bogWithoutInfrastructureOmitsInfrastructureFields() { |
| 63 | + JsonObject json = Compiler.provinceToJson(new Province(1, "bog", 0), null); |
| 64 | + |
| 65 | + assertEquals(0.40, json.get("effective_terrain").getAsDouble()); |
| 66 | + assertFalse(json.has("infrastructure")); |
| 67 | + assertFalse(json.has("infrastructure_fill")); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void infrastructureDoesNotRaisePlainsAboveItsTerrainValue() { |
| 72 | + Province province = new Province(1, "plains", 0); |
| 73 | + province.setInfrastructure(20); |
| 74 | + |
| 75 | + assertEquals(0.75, Compiler.provinceToJson(province, null).get("effective_terrain").getAsDouble()); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void seaAndWaterOmitAllNewTerrainFields() { |
| 80 | + for (String terrain : new String[] {"sea", "water"}) { |
| 81 | + JsonObject json = Compiler.provinceToJson(new Province(1, terrain, 0), null); |
| 82 | + assertFalse(json.has("terrain")); |
| 83 | + assertFalse(json.has("terrain_value")); |
| 84 | + assertFalse(json.has("infrastructure")); |
| 85 | + assertFalse(json.has("infrastructure_fill")); |
| 86 | + assertFalse(json.has("effective_terrain")); |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + void tradeObjectAndExistingFieldOrderArePreserved() { |
| 92 | + Province province = new Province(1, "bog", 0); |
| 93 | + Guild guild = mock(Guild.class); |
| 94 | + province.setData("guild-id", new ProvinceDataEntry(guild, 1.234, 0.567)); |
| 95 | + |
| 96 | + JsonObject json = Compiler.provinceToJson(province, "occupier"); |
| 97 | + |
| 98 | + assertEquals("{\"trade\":1.23,\"production\":0.57}", json.getAsJsonObject("trade") |
| 99 | + .getAsJsonObject("guild-id").toString()); |
| 100 | + assertEquals("id", json.keySet().toArray()[0]); |
| 101 | + assertEquals("prosperity", json.keySet().toArray()[1]); |
| 102 | + assertEquals("occupied_by", json.keySet().toArray()[2]); |
| 103 | + assertEquals("trade", json.keySet().toArray()[3]); |
| 104 | + assertTrue(json.has("effective_terrain")); |
| 105 | + } |
| 106 | +} |
0 commit comments