From 20db11e73feccd3ef370a8a40b87a3dec42ddf7e Mon Sep 17 00:00:00 2001 From: Yair Morgenstern Date: Sun, 27 Jun 2021 22:16:15 +0300 Subject: [PATCH] Added a test to ensure that translations don't crash --- .../unciv/models/translations/Translations.kt | 2 +- .../src/com/unciv/testing/TranslationTests.kt | 34 +++++++++++++++++-- 2 files changed, 32 insertions(+), 4 deletions(-) diff --git a/core/src/com/unciv/models/translations/Translations.kt b/core/src/com/unciv/models/translations/Translations.kt index 96ddadeb17..467a9ebc16 100644 --- a/core/src/com/unciv/models/translations/Translations.kt +++ b/core/src/com/unciv/models/translations/Translations.kt @@ -284,7 +284,7 @@ fun String.getPlaceholderText() = this.replace(squareBraceRegex, "[]") fun String.equalsPlaceholderText(str:String): Boolean { if (first() != str.first()) return false // for quick negative return 95% of the time - return this.getPlaceholderText() == str + return this.getPlaceholderText() == str } fun String.getPlaceholderParameters() = squareBraceRegex.findAll(this).map { it.groups[1]!!.value }.toList() diff --git a/tests/src/com/unciv/testing/TranslationTests.kt b/tests/src/com/unciv/testing/TranslationTests.kt index 552c76385b..e4e3133f66 100644 --- a/tests/src/com/unciv/testing/TranslationTests.kt +++ b/tests/src/com/unciv/testing/TranslationTests.kt @@ -2,12 +2,15 @@ package com.unciv.testing import com.badlogic.gdx.Gdx +import com.unciv.UncivGame import com.unciv.models.UnitActionType +import com.unciv.models.metadata.GameSettings import com.unciv.models.ruleset.Ruleset import com.unciv.models.ruleset.RulesetCache import com.unciv.models.translations.TranslationFileWriter import com.unciv.models.translations.Translations import com.unciv.models.translations.squareBraceRegex +import com.unciv.models.translations.tr import org.junit.Assert import org.junit.Before import org.junit.Test @@ -29,13 +32,10 @@ class TranslationTests { System.setOut(PrintStream(object : OutputStream() { override fun write(b: Int) {} })) -// System.setOut(TextWriter.Null) -// Console(). //(TextWriter.Null); translations.readAllLanguagesTranslation() RulesetCache.loadRulesets() ruleset = RulesetCache.getBaseRuleset() System.setOut(outputChannel) -// Console.SetOut() } @Test @@ -156,4 +156,32 @@ class TranslationTests { } Assert.assertFalse(failed) } + + + @Test + fun allStringsTranslate() { + // Needed for .tr() to work + UncivGame.Current = UncivGame("") + UncivGame.Current.settings = GameSettings() + + for ((key, value) in translations) + UncivGame.Current.translations[key] = value + + var allWordsTranslatedCorrectly = true + for (translationEntry in translations.values) { + for ((language, translation) in translationEntry) { + UncivGame.Current.settings.language = language + try { + translationEntry.entry.tr() + } catch (ex: Exception) { + allWordsTranslatedCorrectly = false + println("Crashed when translating ${translationEntry.entry} to $language") + } + } + } + Assert.assertTrue( + "This test will only pass when all phrases properly translate to their language", + allWordsTranslatedCorrectly + ) + } }