Additional unit tests (#9866)

* Additional unit tests

* Missing import

* Changing stuff solely to avoid an import

* Example of the test, in different orders

* New PromotionTree test

* Add functions to make PromotionTree easier to work with

* Changing values to be lateinit variables
This commit is contained in:
SeventhM
2023-08-16 23:17:23 -07:00
committed by GitHub
parent 1c85ca8c09
commit 67adabb78f
4 changed files with 104 additions and 4 deletions

View File

@ -22,7 +22,11 @@ class CityMovingTests {
enemy = testGame.addCiv()
// Required for enemy to utilize roads
enemy.tech.techsResearched.addAll(testGame.ruleset.technologies.keys)
for (tech in testGame.ruleset.technologies.keys)
enemy.tech.addTechnology(tech)
for (tech in testGame.ruleset.technologies.keys)
civInfo.tech.addTechnology(tech)
civInfo.diplomacyFunctions.makeCivilizationsMeet(enemy)
civInfo.getDiplomacyManager(enemy).declareWar()
@ -104,4 +108,24 @@ class CityMovingTests {
Assert.assertTrue(theirCapital.isCapital())
Assert.assertTrue(theirCapital.civ == civInfo)
}
@Test
fun moveTheirCityToUsWhenTheyHaveResources() {
val theirCapital = testGame.addCity(enemy, testGame.tileMap[2,0])
theirCapital.getCenterTile().resource = "Salt"
theirCapital.getCenterTile().resourceAmount = 1
val resourceTile = testGame.tileMap[0,1]
resourceTile.resource = "Iron"
resourceTile.resourceAmount = 3
resourceTile.changeImprovement("Mine")
theirCapital.expansion.takeOwnership(resourceTile)
theirCapital.moveToCiv(civInfo)
Assert.assertTrue(theirCapital.isCapital())
Assert.assertTrue(theirCapital.civ == civInfo)
Assert.assertTrue(civInfo.hasResource("Salt"))
Assert.assertEquals(civInfo.getResourceAmount("Iron"), 3)
}
}

View File

@ -28,6 +28,7 @@ import com.unciv.models.ruleset.nation.Nation
import com.unciv.models.ruleset.tile.TileImprovement
import com.unciv.models.ruleset.unique.UniqueType
import com.unciv.models.ruleset.unit.BaseUnit
import com.unciv.models.ruleset.unit.Promotion
import com.unciv.models.ruleset.unit.UnitType
class TestGame {
@ -207,4 +208,6 @@ class TestGame {
createRulesetObject(ruleset.tileImprovements, *uniques) { TileImprovement() }
fun createUnitType(vararg uniques: String) =
createRulesetObject(ruleset.unitTypes, *uniques) { UnitType() }
fun createUnitPromotion(vararg uniques: String) =
createRulesetObject(ruleset.unitPromotions, *uniques) { Promotion() }
}

View File

@ -5,6 +5,7 @@ import com.unciv.logic.map.mapunit.UnitTurnManager
import com.unciv.models.ruleset.unique.UniqueType
import com.unciv.models.translations.fillPlaceholders
import com.unciv.testing.GdxTestRunner
import com.unciv.ui.screens.pickerscreens.PromotionTree
import com.unciv.ui.screens.worldscreen.unit.actions.UnitActions
import com.unciv.ui.screens.worldscreen.unit.actions.UnitActions.getImprovementConstructionActions
import org.junit.Assert
@ -112,4 +113,70 @@ class UnitUniquesTests {
// This effectively tests whether the G&K rules have not been tampered with, but won't hurt
Assert.assertNotEquals("Great General stacked with a Hakkapeliitta should NOT have its normal movement points.", baseMovement, actualMovement)
}
@Test
fun testCanGetPromotionsWithXP() {
val civ = game.addCiv(isPlayer = true)
val centerTile = game.getTile(Vector2.Zero)
val unit = game.addUnit("Scout", civ, centerTile)
val tree = PromotionTree(unit)
Assert.assertFalse(tree.allNodes().any { !it.unreachable && tree.canBuyUpTo(it.promotion) })
unit.promotions.XP += 10
Assert.assertTrue(tree.allNodes().any { !it.unreachable && tree.canBuyUpTo(it.promotion) })
}
@Test
fun testPromotionTreeSetUp() {
val civ = game.addCiv(isPlayer = true)
//Creating the promotions
val promotionBranch1 = game.createUnitPromotion()
promotionBranch1.unitTypes = listOf("Scout")
val promotionBranch2 = game.createUnitPromotion()
promotionBranch2.unitTypes = listOf("Scout")
val promotionTestBranchA = game.createUnitPromotion()
promotionTestBranchA.unitTypes = listOf("Scout")
// intentional lists a promotion twice
promotionTestBranchA.prerequisites = listOf(promotionBranch1.name, promotionBranch2.name, promotionBranch1.name)
val promotionTestBranchB = game.createUnitPromotion()
promotionTestBranchB.unitTypes = listOf("Scout")
// intentional lists a promotion twice
promotionTestBranchB.prerequisites = listOf(promotionBranch1.name, promotionTestBranchA.name, promotionBranch2.name, promotionBranch1.name)
// add unit
val centerTile = game.tileMap[0,0]
val unit = game.addUnit("Scout", civ, centerTile)
var tree = PromotionTree(unit)
Assert.assertFalse("We shouldn't be able to get the promotion without XP",
tree.canBuyUpTo(promotionTestBranchB))
unit.promotions.XP += 30
unit.promotions.addPromotion(promotionBranch1.name)
// The Promotion tree needs to be refreshed to check it after gaining a promotion
tree.update()
Assert.assertTrue("Check if we can buy the Promotion now",
tree.canBuyUpTo(promotionTestBranchB))
// Make sure we only have the prerequisite promotions and that it's only listed once each
val promotionNode1 = tree.getNode(promotionTestBranchA)!!
Assert.assertEquals(promotionNode1.parents.size, 2)
Assert.assertTrue(
promotionNode1.parents.any { it.promotion == promotionBranch1 } &&
promotionNode1.parents.any { it.promotion == promotionBranch2 }
)
val promotionNode2 = tree.getNode(promotionTestBranchB)!!
Assert.assertEquals(promotionNode2.parents.size, 3)
Assert.assertTrue(
promotionNode2.parents.any { it.promotion == promotionBranch1 } &&
promotionNode2.parents.any { it.promotion == promotionTestBranchA } &&
promotionNode2.parents.any { it.promotion == promotionBranch2 }
)
}
}