From fd8528af7a9a94fd3de4bef7682d409b03cf9e3e Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Thu, 26 Dec 2019 12:01:23 +0200 Subject: [PATCH] All tests are properly Koltinized! --- .../unciv/logic/automation/UnitAutomation.kt | 12 +- tests/src/com/unciv/testing/BasicTests.kt | 47 +++++ .../unciv/testing}/GdxTestRunner.java | 2 +- .../src/com/unciv/testing/TranslationTests.kt | 149 +++++++++++++++ .../testing}/TutorialTranslationTests.kt | 10 +- .../gdxtesting/examples/BasicTests.java | 67 ------- .../gdxtesting/examples/TranslationTests.java | 174 ------------------ .../gdxtesting/examples/UnitTestExample.java | 31 ---- 8 files changed, 208 insertions(+), 284 deletions(-) create mode 100644 tests/src/com/unciv/testing/BasicTests.kt rename tests/src/{de/tomgrill/gdxtesting => com/unciv/testing}/GdxTestRunner.java (98%) create mode 100644 tests/src/com/unciv/testing/TranslationTests.kt rename tests/src/{de/tomgrill/gdxtesting => com/unciv/testing}/TutorialTranslationTests.kt (88%) delete mode 100644 tests/src/de/tomgrill/gdxtesting/examples/BasicTests.java delete mode 100644 tests/src/de/tomgrill/gdxtesting/examples/TranslationTests.java delete mode 100644 tests/src/de/tomgrill/gdxtesting/examples/UnitTestExample.java diff --git a/core/src/com/unciv/logic/automation/UnitAutomation.kt b/core/src/com/unciv/logic/automation/UnitAutomation.kt index 06b70306..0b349690 100644 --- a/core/src/com/unciv/logic/automation/UnitAutomation.kt +++ b/core/src/com/unciv/logic/automation/UnitAutomation.kt @@ -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 } diff --git a/tests/src/com/unciv/testing/BasicTests.kt b/tests/src/com/unciv/testing/BasicTests.kt new file mode 100644 index 00000000..2391a683 --- /dev/null +++ b/tests/src/com/unciv/testing/BasicTests.kt @@ -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 = 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) + } +} \ No newline at end of file diff --git a/tests/src/de/tomgrill/gdxtesting/GdxTestRunner.java b/tests/src/com/unciv/testing/GdxTestRunner.java similarity index 98% rename from tests/src/de/tomgrill/gdxtesting/GdxTestRunner.java rename to tests/src/com/unciv/testing/GdxTestRunner.java index bd26ec09..7f98b5c8 100644 --- a/tests/src/de/tomgrill/gdxtesting/GdxTestRunner.java +++ b/tests/src/com/unciv/testing/GdxTestRunner.java @@ -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; diff --git a/tests/src/com/unciv/testing/TranslationTests.kt b/tests/src/com/unciv/testing/TranslationTests.kt new file mode 100644 index 00000000..261e44be --- /dev/null +++ b/tests/src/com/unciv/testing/TranslationTests.kt @@ -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 = 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 = HashSet() + for (building in ruleSet.Buildings.values) { + strings.addAll(building.uniques) + } + val allStringsHaveTranslation = allStringAreTranslated(strings) + Assert.assertTrue(allStringsHaveTranslation) + } + + @Test + fun allBuildingQuotesHaveTranslation() { + val strings: MutableSet = 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 = 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 = 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 = HashSet() + for (improvement in ruleSet.TileImprovements.values) { + strings.addAll(improvement.uniques) + } + val allStringsHaveTranslation = allStringAreTranslated(strings) + Assert.assertTrue(allStringsHaveTranslation) + } + + @Test + fun allTechnologiesHaveTranslation() { + val strings: Set = 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 = 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 = 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): 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().javaClass, file.path()) + } + Assert.assertTrue("This test will only pass when there is a translation for all promotions", + true) + } +} \ No newline at end of file diff --git a/tests/src/de/tomgrill/gdxtesting/TutorialTranslationTests.kt b/tests/src/com/unciv/testing/TutorialTranslationTests.kt similarity index 88% rename from tests/src/de/tomgrill/gdxtesting/TutorialTranslationTests.kt rename to tests/src/com/unciv/testing/TutorialTranslationTests.kt index 6c377d32..2585caef 100644 --- a/tests/src/de/tomgrill/gdxtesting/TutorialTranslationTests.kt +++ b/tests/src/com/unciv/testing/TutorialTranslationTests.kt @@ -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)) } } diff --git a/tests/src/de/tomgrill/gdxtesting/examples/BasicTests.java b/tests/src/de/tomgrill/gdxtesting/examples/BasicTests.java deleted file mode 100644 index d5ddd05c..00000000 --- a/tests/src/de/tomgrill/gdxtesting/examples/BasicTests.java +++ /dev/null @@ -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 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); -// } - -} \ No newline at end of file diff --git a/tests/src/de/tomgrill/gdxtesting/examples/TranslationTests.java b/tests/src/de/tomgrill/gdxtesting/examples/TranslationTests.java deleted file mode 100644 index fd3cdc21..00000000 --- a/tests/src/de/tomgrill/gdxtesting/examples/TranslationTests.java +++ /dev/null @@ -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 strings = new HashSet(); - 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 strings = new HashSet(); - for(Building building: ruleSet.getBuildings().values()){ - strings.addAll(building.getUniques()); - } - Boolean allStringsHaveTranslation = allStringAreTranslated(strings); - assertTrue(allStringsHaveTranslation); - } - - - @Test - public void allBuildingQuotesHaveTranslation() { - Set strings = new HashSet(); - 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 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 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 strings = new HashSet(); - for(TileImprovement improvement: ruleSet.getTileImprovements().values()){ - strings.addAll(improvement.getUniques()); - } - Boolean allStringsHaveTranslation = allStringAreTranslated(strings); - assertTrue(allStringsHaveTranslation); - } - - - @Test - public void allTechnologiesHaveTranslation() { - Set 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 strings = new HashSet(); - 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 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 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().getClass(), file.path()); - } - assertTrue("This test will only pass when there is a translation for all promotions", - true); - } - -} \ No newline at end of file diff --git a/tests/src/de/tomgrill/gdxtesting/examples/UnitTestExample.java b/tests/src/de/tomgrill/gdxtesting/examples/UnitTestExample.java deleted file mode 100644 index e4e0dd4b..00000000 --- a/tests/src/de/tomgrill/gdxtesting/examples/UnitTestExample.java +++ /dev/null @@ -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); - } - -} -