Unit tests for TileInfo (#1959)

This commit is contained in:
JackRainy 2020-02-18 00:03:59 +02:00 committed by GitHub
parent 0b7a64db16
commit 7a96a96d73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -0,0 +1,85 @@
// Taken from https://github.com/TomGrill/gdx-testing
package com.unciv.logic.map
import com.unciv.logic.civilization.CivilizationInfo
import com.unciv.models.ruleset.Ruleset
import com.unciv.models.ruleset.RulesetCache
import com.unciv.testing.GdxTestRunner
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(GdxTestRunner::class)
class TileInfoTests {
private var tile = TileInfo()
private var civInfo = CivilizationInfo()
private var ruleSet = Ruleset()
@Before
fun initTheWorld() {
RulesetCache.loadRulesets()
ruleSet = RulesetCache.getBaseRuleset()
tile.ruleset = ruleSet
civInfo.tech.researchedTechnologies.addAll(ruleSet.technologies.values)
civInfo.tech.techsResearched.addAll(ruleSet.technologies.keys)
}
@Test
fun allTerrainSpecificImprovementsCanBeBuilt() {
for (improvement in ruleSet.tileImprovements.values) {
val terrain = improvement.terrainsCanBeBuiltOn.firstOrNull() ?: continue
tile.baseTerrain = terrain
tile.setTransients()
val canBeBuilt = tile.canBuildImprovement(improvement, civInfo)
Assert.assertTrue( improvement.name, canBeBuilt)
}
}
@Test
fun allResourceImprovementsCanBeBuilt() {
for (improvement in ruleSet.tileImprovements.values) {
tile.resource = ruleSet.tileResources.values.firstOrNull { it.improvement == improvement.name}?.name
if (tile.resource == null) continue
tile.baseTerrain = "Plains"
tile.setTransients()
val canBeBuilt = tile.canBuildImprovement(improvement, civInfo)
Assert.assertTrue( improvement.name, canBeBuilt)
}
}
@Test
fun coastalImprovementsCanBeBuilt() {
val map = TileMap()
tile.baseTerrain = "Plains"
tile.tileMap = map
tile.setTransients()
val otherTile = tile.clone()
otherTile.baseTerrain = "Coast"
otherTile.position.y = 1f
map.tileMatrix.add(arrayListOf(tile, otherTile))
for (improvement in ruleSet.tileImprovements.values) {
if (!improvement.uniques.contains("Can only be built on Coastal tiles")) continue
civInfo.civName = improvement.uniqueTo ?: "OtherCiv"
val canBeBuilt = tile.canBuildImprovement(improvement, civInfo)
Assert.assertTrue( improvement.name, canBeBuilt)
}
}
@Test
fun uniqueToOtherImprovementsCanNOTBeBuilt() {
for (improvement in ruleSet.tileImprovements.values) {
if (improvement.uniqueTo == null) continue
civInfo.civName = "OtherCiv"
tile.baseTerrain = "Plains"
tile.setTransients()
val canBeBuilt = tile.canBuildImprovement(improvement, civInfo)
Assert.assertFalse( improvement.name, canBeBuilt)
}
}
}