From 898048513951b471a492afa23e46112d14bcb236 Mon Sep 17 00:00:00 2001 From: Norbert Preining Date: Thu, 5 Mar 2020 03:00:59 +0000 Subject: CTAN sync 202003050300 --- .../arara/localization/LanguageCoverageTest.kt | 102 +++++++++++++++ .../arara/localization/LanguageReport.kt | 143 +++++++++++++++++++++ .../islandoftex/arara/localization/LanguageTest.kt | 20 +++ 3 files changed, 265 insertions(+) create mode 100644 support/arara/source/src/test/kotlin/org/islandoftex/arara/localization/LanguageCoverageTest.kt create mode 100644 support/arara/source/src/test/kotlin/org/islandoftex/arara/localization/LanguageReport.kt create mode 100644 support/arara/source/src/test/kotlin/org/islandoftex/arara/localization/LanguageTest.kt (limited to 'support/arara/source/src/test/kotlin/org/islandoftex/arara/localization') diff --git a/support/arara/source/src/test/kotlin/org/islandoftex/arara/localization/LanguageCoverageTest.kt b/support/arara/source/src/test/kotlin/org/islandoftex/arara/localization/LanguageCoverageTest.kt new file mode 100644 index 0000000000..704d3e319d --- /dev/null +++ b/support/arara/source/src/test/kotlin/org/islandoftex/arara/localization/LanguageCoverageTest.kt @@ -0,0 +1,102 @@ +// SPDX-License-Identifier: BSD-3-Clause +package org.islandoftex.arara.localization + +import ch.qos.cal10n.verifier.MessageKeyVerifier +import io.kotlintest.inspectors.forAll +import io.kotlintest.matchers.collections.shouldNotBeEmpty +import io.kotlintest.shouldBe +import io.kotlintest.shouldThrow +import io.kotlintest.specs.ShouldSpec +import java.io.File +import java.io.IOException +import java.nio.file.Files +import java.nio.file.Path +import java.nio.file.Paths +import java.util.Locale +import java.util.stream.Collectors +import org.islandoftex.arara.model.AraraException + +class LanguageCoverageTest : ShouldSpec({ + "locale definitions" { + /** + * Helper method, checks the provided locale (all keys set?). + * + * @param locale The locale. + * @return The size of the error list. + */ + fun check(locale: Locale): Int { + val errors = MessageKeyVerifier(Messages::class.java).verify(locale) + errors.forEach(System.err::println) + return errors.size + } + + should("succeed instantiating known locale") { + Language("en").locale.language shouldBe "en" + } + should("throw on unknown locale instantiation") { + shouldThrow { + Language("quack") + } + } + + should("not error on known localizations") { + listOf("en", "de", "nl", "it").forAll { + check(Locale(it)) shouldBe 0 + } + check(Locale("en", "QN")) shouldBe 0 + check(Locale("pt", "BR")) shouldBe 0 + } + } + "language coverage" { + /* + * Tests the localized messages, checking if all messages are properly + * quoted (but not necessarily whether they are loadable). + */ + should("get all strings from every language") { + // get all files + val files = Files.list( + Paths.get("src/main/resources/org/islandoftex/arara/localization")) + .map { p: Path -> + val f = p.toFile() + if (f.name.endsWith("properties") && !f.isDirectory) f + else null + } + .collect(Collectors.toList()) + .toList() + .filterNotNull() + files.shouldNotBeEmpty() + + // for each report, print + // the corresponding entry + files.map { file: File -> + try { + LanguageReport.analyze(file) + } catch (exception: IOException) { + throw AssertionError( + "Fatal exception: an error was raised while " + + "trying to read one of the languages. Please " + + "make sure all languages in the provided " + + "directory have read permission.") + } + }.forEach { report -> + // debug output + println(report.reference.name + + "\t" + String.format(" %2.2f%%", report.coverage)) + + // if there are problematic lines, + // add the current language report + if (report.lines.isNotEmpty()) { + // legend: S = Simple message, single quotes should not be doubled + // P = Parametrized message, single quotes must be doubled + + // build the beginning of the line + println(report.reference.name) + // print error lines + println(report.lines) + } + + report.coverage shouldBe 100.0f + } + } + } +}) diff --git a/support/arara/source/src/test/kotlin/org/islandoftex/arara/localization/LanguageReport.kt b/support/arara/source/src/test/kotlin/org/islandoftex/arara/localization/LanguageReport.kt new file mode 100644 index 0000000000..addcd0262c --- /dev/null +++ b/support/arara/source/src/test/kotlin/org/islandoftex/arara/localization/LanguageReport.kt @@ -0,0 +1,143 @@ +// SPDX-License-Identifier: BSD-3-Clause +package org.islandoftex.arara.localization + +import java.io.File + +/** + * Implements the language report model. + * + * @author Island of TeX + * @version 5.0 + * @since 5.0 + */ +data class LanguageReport( + /** + * The file reference. + */ + val reference: File, + /** + * Total of checked lines. + */ + val total: Int, + /** + * List of problematic lines and their corresponding error types. + */ + val lines: Map +) { + /** + * Language coverage of the file. + */ + val coverage: Float + get() = if (lines.isEmpty()) { + 100f + } else { + (1f - lines.size.toFloat() / total) * 100f + } + + companion object { + /** + * Analyzes the list of lines. + * + * @param file The file to read. + * @return The language report. + */ + internal fun analyze(file: File): LanguageReport { + // holds the current line number + var number = 1 + // holds the number of checked lines + var checked = 0 + + // flag that holds the + // current analysis + var check: Int + + val reportLines = mutableMapOf() + + // check every line of the language file + file.forEachLine { line -> + // let's only analyze lines + // that are not comments + if (!line.trim().startsWith("#")) { + // increment the checked + // line counter + checked++ + + // line is a parametrized message + check = if (line.contains("{0}")) { + // check the corresponding pattern + checkParametrizedMessage(line) + } else { + // check the corresponding pattern + checkMessage(line) + } + + // we found an error, + // report it + if (check != 0) { + // add line and error type to the report + reportLines[number] = if (check == 1) 'P' else 'S' + } + } + + // let's move to the next line + number++ + } + + // return the language report + return LanguageReport( + reference = file, + total = checked, + lines = reportLines) + } + + /** + * Checks if the provided message follows the simple format. + * + * @param text Message. + * @return An integer value. + */ + private fun checkMessage(text: String): Int { + var i = 0 + var c: Char + for (element in text) { + c = element + i = if (c == '\'') { + if (i == 1) { + return 2 + } else { + 1 + } + } else { + 0 + } + } + return 0 + } + + /** + * Checks if the provided message follows the parametrized format. + * + * @param text Message. + * @return An integer value. + */ + private fun checkParametrizedMessage(text: String): Int { + var i = 0 + var c: Char + for (element in text) { + c = element + if (c == '\'') { + i += 1 + } else { + if (i != 0) { + if (i != 2) { + return 1 + } else { + i = 0 + } + } + } + } + return 0 + } + } +} diff --git a/support/arara/source/src/test/kotlin/org/islandoftex/arara/localization/LanguageTest.kt b/support/arara/source/src/test/kotlin/org/islandoftex/arara/localization/LanguageTest.kt new file mode 100644 index 0000000000..92d38ebc9b --- /dev/null +++ b/support/arara/source/src/test/kotlin/org/islandoftex/arara/localization/LanguageTest.kt @@ -0,0 +1,20 @@ +// SPDX-License-Identifier: BSD-3-Clause +package org.islandoftex.arara.localization + +import io.kotlintest.shouldBe +import io.kotlintest.shouldThrow +import io.kotlintest.specs.ShouldSpec +import java.util.Locale +import org.islandoftex.arara.model.AraraException + +class LanguageTest : ShouldSpec({ + should("instantiate with known code") { + Language("en").locale shouldBe Locale.ENGLISH + } + + should("throw on unknown language") { + shouldThrow { + Language("quack") + } + } +}) -- cgit v1.2.3