From 1c1f1190d2b905e2d6826b3eadca0c14392f7b07 Mon Sep 17 00:00:00 2001 From: "Amir E. Aharoni" Date: Mon, 13 Oct 2025 10:03:38 -0400 Subject: [PATCH] WIP DON'T MERGE Add a test for missing string documentation THIS IS SUPPOSED TO FAIL AT THE MOMENT. Documentation for all the strings is already written in translatewiki (see #6457), but not yet imported into GitHub, so it's supposed to fail now, which is good because you shouldn't trust tests that never failed :) Once all the documentation strings are imported, this pull request should be rebased, and then it's supposed to pass. The extra string in values-qq/string.xml is also there just to check that it fails as it should, and will be removed. Resolves #6485. --- .../resources/StringsDocumentationTest.kt | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 app/src/test/kotlin/resources/StringsDocumentationTest.kt diff --git a/app/src/test/kotlin/resources/StringsDocumentationTest.kt b/app/src/test/kotlin/resources/StringsDocumentationTest.kt new file mode 100644 index 000000000..72f56668f --- /dev/null +++ b/app/src/test/kotlin/resources/StringsDocumentationTest.kt @@ -0,0 +1,60 @@ +package resources + +import org.junit.Test +import org.junit.Assert.assertTrue +import org.w3c.dom.Element +import java.io.File +import javax.xml.parsers.DocumentBuilderFactory + +class StringsDocumentationTest { + + @Test + fun `string resources and qq documentation are in sync`() { + val projectRoot = File(System.getProperty("user.dir") ?: ".") + val sourceFile = File(projectRoot, "src/main/res/values/strings.xml") + val docFile = File(projectRoot, "src/main/res/values-qq/strings.xml") + + assertTrue("Source strings.xml not found", sourceFile.exists()) + assertTrue("Documentation strings.xml (qq) not found", docFile.exists()) + + val sourceKeys = parseStringKeys(sourceFile) + val docKeys = parseStringKeys(docFile) + + val extraDocs = docKeys - sourceKeys + + assertTrue( + "Documentation exists for ${extraDocs.size} non-existent string(s): " + + extraDocs.joinToString(", "), + extraDocs.isEmpty() + ) + + val missingDocs = sourceKeys - docKeys + + assertTrue( + "Missing documentation for ${missingDocs.size} string(s): " + + missingDocs.joinToString(", "), + missingDocs.isEmpty() + ) + } + + private fun parseStringKeys(file: File): Set { + val factory = DocumentBuilderFactory.newInstance() + val builder = factory.newDocumentBuilder() + val doc = builder.parse(file) + + val keys = mutableSetOf() + + listOf("string", "plurals").forEach { tagName -> + val elements = doc.getElementsByTagName(tagName) + for (i in 0 until elements.length) { + val element = elements.item(i) as Element + val name = element.getAttribute("name") + if (name.isNotEmpty()) { + keys.add(name) + } + } + } + + return keys + } +}