All tests are properly Koltinized!

This commit is contained in:
Yair Morgenstern 2019-12-26 12:01:23 +02:00
parent 7f06ae0cce
commit fd8528af7a
8 changed files with 208 additions and 284 deletions

View file

@ -14,8 +14,8 @@ import com.unciv.models.ruleset.unit.UnitType
import com.unciv.ui.worldscreen.unit.UnitAction
import com.unciv.ui.worldscreen.unit.UnitActions
const val CLOSE_TURNS_AWAY_LIMIT = 3f
const val CLOSE_TILES_AWAY_LIMIT = 5
const val CLOSE_ENEMY_TURNS_AWAY_LIMIT = 3f
const val CLOSE_ENEMY_TILES_AWAY_LIMIT = 5
class UnitAutomation{
@ -247,19 +247,19 @@ class UnitAutomation{
/** Move towards the closest attackable enemy of the [unit].
*
* Limited by [CLOSE_TURNS_AWAY_LIMIT] and [CLOSE_TILES_AWAY_LIMIT].
* Limited by [CLOSE_ENEMY_TURNS_AWAY_LIMIT] and [CLOSE_ENEMY_TILES_AWAY_LIMIT].
* Tiles attack from which would result in instant death of the [unit] are ignored. */
private fun tryAdvanceTowardsCloseEnemy(unit: MapUnit): Boolean {
// this can be sped up if we check each layer separately
val unitDistanceToTiles = unit.movement.getDistanceToTilesWithinTurn(
unit.getTile().position,
unit.getMaxMovement() * CLOSE_TURNS_AWAY_LIMIT
unit.getMaxMovement() * CLOSE_ENEMY_TURNS_AWAY_LIMIT
)
var closeEnemies = getAttackableEnemies(
unit,
unitDistanceToTiles,
tilesToCheck = unit.getTile().getTilesInDistance(CLOSE_TILES_AWAY_LIMIT)
).filter {
tilesToCheck = unit.getTile().getTilesInDistance(CLOSE_ENEMY_TILES_AWAY_LIMIT)
).filter { // Ignore units that would 1-shot you if you attacked
BattleDamage().calculateDamageToAttacker(MapUnitCombatant(unit),
Battle(unit.civInfo.gameInfo).getMapCombatantOfTile(it.tileToAttack)!!) < unit.health
}

View file

@ -0,0 +1,47 @@
// Taken from https://github.com/TomGrill/gdx-testing
package com.unciv.testing
import com.badlogic.gdx.Gdx
import com.unciv.UncivGame
import com.unciv.models.ruleset.Ruleset
import com.unciv.models.ruleset.unit.BaseUnit
import org.junit.Assert
import org.junit.Test
import org.junit.runner.RunWith
@RunWith(GdxTestRunner::class)
class BasicTests {
@Test
fun gamePngExists() {
Assert.assertTrue("This test will only pass when the game.png exists",
Gdx.files.local("game.png").exists())
}
@Test
fun loadRuleset() {
Assert.assertTrue("This test will only pass when the jsons can be loaded",
Ruleset(true).Buildings.size > 0)
}
@Test
fun gameIsNotRunWithDebugModes() {
val game = UncivGame("", null)
Assert.assertTrue("This test will only pass if the game is not run with debug modes",
!game.superchargedForDebug && !game.viewEntireMapForDebug)
}
// If there's a unit that obsoletes with no upgrade then when it obsoletes
// and we try to work on its upgrade, we'll get an exception - see techManager
@Test
fun allObsoletingUnitsHaveUpgrades() {
val units: Collection<BaseUnit> = Ruleset(true).Units.values
var allObsoletingUnitsHaveUpgrades = true
for (unit in units) {
if (unit.obsoleteTech != null && unit.upgradesTo == null) {
println(unit.name + " obsoletes but has no upgrade")
allObsoletingUnitsHaveUpgrades = false
}
}
Assert.assertTrue(allObsoletingUnitsHaveUpgrades)
}
}

View file

@ -14,7 +14,7 @@
* limitations under the License.
******************************************************************************/
package de.tomgrill.gdxtesting;
package com.unciv.testing;
import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;

View file

@ -0,0 +1,149 @@
// Taken from https://github.com/TomGrill/gdx-testing
package com.unciv.testing
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.utils.Array
import com.unciv.models.ruleset.Nation
import com.unciv.models.ruleset.Ruleset
import com.unciv.models.translations.Translations
import org.junit.Assert
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import java.util.*
@RunWith(GdxTestRunner::class)
class TranslationTests {
var translations = Translations()
var ruleSet = Ruleset(true)
@Before
fun loadTranslations() {
translations.readAllLanguagesTranslation()
}
@Test
fun translationsLoad() {
Assert.assertTrue("This test will only pass there are translations",
translations.size > 0)
}
@Test
fun allUnitsHaveTranslation() {
val allUnitsHaveTranslation = allStringAreTranslated(ruleSet.Units.keys)
Assert.assertTrue("This test will only pass when there is a translation for all units",
allUnitsHaveTranslation)
}
@Test
fun allUnitUniquesHaveTranslation() {
val strings: MutableSet<String> = HashSet()
for (unit in ruleSet.Units.values) for (unique in unit.uniques) if (!unique.startsWith("Bonus")
&& !unique.startsWith("Penalty")
&& !unique.contains("[")) // templates
strings.add(unique)
val allStringsHaveTranslation = allStringAreTranslated(strings)
Assert.assertTrue(allStringsHaveTranslation)
}
@Test
fun allBuildingsHaveTranslation() {
val allBuildingsHaveTranslation = allStringAreTranslated(ruleSet.Buildings.keys)
Assert.assertTrue("This test will only pass when there is a translation for all buildings",
allBuildingsHaveTranslation)
}
@Test
fun allBuildingUniquesHaveTranslation() {
val strings: MutableSet<String> = HashSet()
for (building in ruleSet.Buildings.values) {
strings.addAll(building.uniques)
}
val allStringsHaveTranslation = allStringAreTranslated(strings)
Assert.assertTrue(allStringsHaveTranslation)
}
@Test
fun allBuildingQuotesHaveTranslation() {
val strings: MutableSet<String> = HashSet()
for (building in ruleSet.Buildings.values) {
if (building.quote == "") continue
strings.add(building.quote)
}
val allStringsHaveTranslation = allStringAreTranslated(strings)
Assert.assertTrue(allStringsHaveTranslation)
}
@Test
fun allTerrainsHaveTranslation() {
val strings: Set<String> = ruleSet.Terrains.keys
val allStringsHaveTranslation = allStringAreTranslated(strings)
Assert.assertTrue("This test will only pass when there is a translation for all buildings",
allStringsHaveTranslation)
}
@Test
fun allImprovementsHaveTranslation() {
val strings: Set<String> = ruleSet.TileImprovements.keys
val allStringsHaveTranslation = allStringAreTranslated(strings)
Assert.assertTrue("This test will only pass when there is a translation for all improvements",
allStringsHaveTranslation)
}
@Test
fun allImprovementUniquesHaveTranslation() {
val strings: MutableSet<String> = HashSet()
for (improvement in ruleSet.TileImprovements.values) {
strings.addAll(improvement.uniques)
}
val allStringsHaveTranslation = allStringAreTranslated(strings)
Assert.assertTrue(allStringsHaveTranslation)
}
@Test
fun allTechnologiesHaveTranslation() {
val strings: Set<String> = ruleSet.Technologies.keys
val allStringsHaveTranslation = allStringAreTranslated(strings)
Assert.assertTrue("This test will only pass when there is a translation for all technologies",
allStringsHaveTranslation)
}
@Test
fun allTechnologiesQuotesHaveTranslation() {
val strings: MutableSet<String> = HashSet()
for (tech in ruleSet.Technologies.values) {
strings.add(tech.quote)
}
val allStringsHaveTranslation = allStringAreTranslated(strings)
Assert.assertTrue("This test will only pass when there is a translation for all technologies",
allStringsHaveTranslation)
}
@Test
fun allPromotionsHaveTranslation() {
val strings: Set<String> = ruleSet.UnitPromotions.keys
val allStringsHaveTranslation = allStringAreTranslated(strings)
Assert.assertTrue("This test will only pass when there is a translation for all promotions",
allStringsHaveTranslation)
}
private fun allStringAreTranslated(strings: Set<String>): Boolean {
var allBuildingsHaveTranslation = true
for (unitName in strings) {
if (!translations.containsKey(unitName)) {
allBuildingsHaveTranslation = false
println(unitName)
}
}
return allBuildingsHaveTranslation
}
@Test
fun allTranslatedNationsFilesAreSerializable() {
for (file in Gdx.files.internal("jsons/Nations").list()) {
ruleSet.getFromJson(Array<Nation>().javaClass, file.path())
}
Assert.assertTrue("This test will only pass when there is a translation for all promotions",
true)
}
}

View file

@ -1,4 +1,4 @@
package de.tomgrill.gdxtesting
package com.unciv.testing
import com.badlogic.gdx.Gdx
import com.badlogic.gdx.utils.Array
@ -23,7 +23,7 @@ class TutorialTranslationTests {
)
@Test
fun testValidNumberOfTranslations() {
fun testValidNumberOfTranslationTutorials() {
languages.forEach { language ->
val keys = getKeysForLanguage(language)
assertTrue("$language tutorial does not match", keys.size == tutorialCount)
@ -31,10 +31,10 @@ class TutorialTranslationTests {
}
@Test
fun testKeyValidity() {
languages.forEach { language ->
fun testTutorialKeyValidity() {
for (language in languages) {
val keys = getKeysForLanguage(language)
tutorialKeyNames.forEach { key ->
for (key in tutorialKeyNames) {
assertTrue("$language tutorial does not have $key", keys.contains(key))
}
}

View file

@ -1,67 +0,0 @@
// Taken from https://github.com/TomGrill/gdx-testing
package de.tomgrill.gdxtesting.examples;
import com.badlogic.gdx.Gdx;
import com.unciv.UncivGame;
import com.unciv.models.ruleset.Ruleset;
import com.unciv.models.ruleset.unit.BaseUnit;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.Collection;
import de.tomgrill.gdxtesting.GdxTestRunner;
import static org.junit.Assert.assertTrue;
// DO NOT attempt to Kotlinize until you're sure that running gradle tests:test actually checks stuff!
@RunWith(GdxTestRunner.class)
public class BasicTests {
@Test
public void gamePngExists() {
assertTrue("This test will only pass when the game.png exists",
Gdx.files.local("game.png").exists());
}
@Test
public void loadRuleset() {
assertTrue("This test will only pass when the jsons can be loaded",
new Ruleset(true).getBuildings().size() > 0);
}
@Test
public void gameIsNotRunWithDebugModes() {
assertTrue("This test will only pass if the game is not run with debug modes",
!new UncivGame("", null).getSuperchargedForDebug()
&& !new UncivGame("", null).getViewEntireMapForDebug());
}
// If there's a unit that obsoletes with no upgrade then when it obsoletes
// and we try to work on its upgrade, we'll get an exception - see techManager
@Test
public void allObsoletingUnitsHaveUpgrades() {
Collection<BaseUnit> units = new Ruleset(true).getUnits().values();
boolean allObsoletingUnitsHaveUpgrades = true;
for(BaseUnit unit : units){
if(unit.getObsoleteTech()!=null && unit.getUpgradesTo()==null) {
System.out.println(unit.name+" obsoletes but has no upgrade");
allObsoletingUnitsHaveUpgrades=false;
}
}
assertTrue(allObsoletingUnitsHaveUpgrades);
}
// @Test
// public void setMapEditorScreen() {
// new UncivGame("").create(); // sets the current
// UncivGame.Current.setScreen(new MapEditorScreen(UncivGame.Current.getGameInfo().getTileMap()));
// assertTrue("This test will only pass when we can open the map editor screen",
// GameBasics.INSTANCE.getBuildings().size() > 0);
// }
}

View file

@ -1,174 +0,0 @@
// Taken from https://github.com/TomGrill/gdx-testing
package de.tomgrill.gdxtesting.examples;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.utils.Array;
import com.unciv.models.ruleset.Building;
import com.unciv.models.ruleset.Nation;
import com.unciv.models.ruleset.Ruleset;
import com.unciv.models.ruleset.tech.Technology;
import com.unciv.models.ruleset.tile.TileImprovement;
import com.unciv.models.ruleset.unit.BaseUnit;
import com.unciv.models.translations.Translations;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import java.util.HashSet;
import java.util.Set;
import de.tomgrill.gdxtesting.GdxTestRunner;
import static org.junit.Assert.assertTrue;
// DO NOT attempt to Kotlinize until you're sure that running gradle tests:test actually checks stuff!
@RunWith(GdxTestRunner.class)
public class TranslationTests {
Translations translations = new Translations();
Ruleset ruleSet = new Ruleset(true);
@Before
public void loadTranslations(){
translations.readAllLanguagesTranslation();
}
@Test
public void translationsLoad() {
assertTrue("This test will only pass there are translations",
translations.size() > 0);
}
@Test
public void allUnitsHaveTranslation() {
Boolean allUnitsHaveTranslation = allStringAreTranslated(ruleSet.getUnits().keySet());
assertTrue("This test will only pass when there is a translation for all units",
allUnitsHaveTranslation);
}
@Test
public void allUnitUniquesHaveTranslation() {
Set<String> strings = new HashSet<String>();
for (BaseUnit unit : ruleSet.getUnits().values())
for (String unique : unit.getUniques())
if (!unique.startsWith("Bonus")
&& !unique.startsWith("Penalty")
&& !unique.contains("[")) // templates
strings.add(unique);
Boolean allStringsHaveTranslation = allStringAreTranslated(strings);
assertTrue(allStringsHaveTranslation);
}
@Test
public void allBuildingsHaveTranslation() {
Boolean allBuildingsHaveTranslation = allStringAreTranslated(ruleSet.getBuildings().keySet());
assertTrue("This test will only pass when there is a translation for all buildings",
allBuildingsHaveTranslation);
}
@Test
public void allBuildingUniquesHaveTranslation() {
Set<String> strings = new HashSet<String>();
for(Building building: ruleSet.getBuildings().values()){
strings.addAll(building.getUniques());
}
Boolean allStringsHaveTranslation = allStringAreTranslated(strings);
assertTrue(allStringsHaveTranslation);
}
@Test
public void allBuildingQuotesHaveTranslation() {
Set<String> strings = new HashSet<String>();
for(Building building: ruleSet.getBuildings().values()){
if(building.getQuote().equals("")) continue;
strings.add(building.getQuote());
}
Boolean allStringsHaveTranslation = allStringAreTranslated(strings);
assertTrue(allStringsHaveTranslation);
}
@Test
public void allTerrainsHaveTranslation() {
Set<String> strings = ruleSet.getTerrains().keySet();
Boolean allStringsHaveTranslation = allStringAreTranslated(strings);
assertTrue("This test will only pass when there is a translation for all buildings",
allStringsHaveTranslation);
}
@Test
public void allImprovementsHaveTranslation() {
Set<String> strings = ruleSet.getTileImprovements().keySet();
Boolean allStringsHaveTranslation = allStringAreTranslated(strings);
assertTrue("This test will only pass when there is a translation for all improvements",
allStringsHaveTranslation);
}
@Test
public void allImprovementUniquesHaveTranslation() {
Set<String> strings = new HashSet<String>();
for(TileImprovement improvement: ruleSet.getTileImprovements().values()){
strings.addAll(improvement.getUniques());
}
Boolean allStringsHaveTranslation = allStringAreTranslated(strings);
assertTrue(allStringsHaveTranslation);
}
@Test
public void allTechnologiesHaveTranslation() {
Set<String> strings = ruleSet.getTechnologies().keySet();
Boolean allStringsHaveTranslation = allStringAreTranslated(strings);
assertTrue("This test will only pass when there is a translation for all technologies",
allStringsHaveTranslation);
}
@Test
public void allTechnologiesQuotesHaveTranslation() {
Set<String> strings = new HashSet<String>();
for(Technology tech : ruleSet.getTechnologies().values()){
strings.add(tech.getQuote());
}
Boolean allStringsHaveTranslation = allStringAreTranslated(strings);
assertTrue("This test will only pass when there is a translation for all technologies",
allStringsHaveTranslation);
}
@Test
public void allPromotionsHaveTranslation() {
Set<String> strings = ruleSet.getUnitPromotions().keySet();
Boolean allStringsHaveTranslation = allStringAreTranslated(strings);
assertTrue("This test will only pass when there is a translation for all promotions",
allStringsHaveTranslation);
}
private Boolean allStringAreTranslated(Set<String> strings) {
boolean allBuildingsHaveTranslation = true;
for (String unitName : strings) {
if (!translations.containsKey(unitName)) {
allBuildingsHaveTranslation = false;
System.out.println(unitName);
}
}
return allBuildingsHaveTranslation;
}
@Test
public void allTranslatedNationsFilesAreSerializable() {
for(FileHandle file : Gdx.files.internal("jsons/Nations").list()){
ruleSet.getFromJson(new Array<Nation>().getClass(), file.path());
}
assertTrue("This test will only pass when there is a translation for all promotions",
true);
}
}

View file

@ -1,31 +0,0 @@
/*******************************************************************************
* Copyright 2015 See AUTHORS file.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package de.tomgrill.gdxtesting.examples;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
public class UnitTestExample {
@Test
public void oneEqualsOne() {
assertEquals(1, 1);
}
}