|
| 1 | +package resources |
| 2 | + |
| 3 | +import org.junit.Test |
| 4 | +import org.junit.Assert.assertTrue |
| 5 | +import org.w3c.dom.Element |
| 6 | +import java.io.File |
| 7 | +import javax.xml.parsers.DocumentBuilderFactory |
| 8 | + |
| 9 | +class StringsDocumentationTest { |
| 10 | + |
| 11 | + @Test |
| 12 | + fun `string resources and qq documentation are in sync`() { |
| 13 | + val projectRoot = File(System.getProperty("user.dir") ?: ".") |
| 14 | + val sourceFile = File(projectRoot, "src/main/res/values/strings.xml") |
| 15 | + val docFile = File(projectRoot, "src/main/res/values-qq/strings.xml") |
| 16 | + |
| 17 | + assertTrue("Source strings.xml not found", sourceFile.exists()) |
| 18 | + assertTrue("Documentation strings.xml (qq) not found", docFile.exists()) |
| 19 | + |
| 20 | + val sourceKeys = parseStringKeys(sourceFile) |
| 21 | + val docKeys = parseStringKeys(docFile) |
| 22 | + |
| 23 | + val extraDocs = docKeys - sourceKeys |
| 24 | + |
| 25 | + assertTrue( |
| 26 | + "Documentation exists for ${extraDocs.size} non-existent string(s): " + |
| 27 | + extraDocs.joinToString(", "), |
| 28 | + extraDocs.isEmpty() |
| 29 | + ) |
| 30 | + |
| 31 | + val missingDocs = sourceKeys - docKeys |
| 32 | + |
| 33 | + assertTrue( |
| 34 | + "Missing documentation for ${missingDocs.size} string(s): " + |
| 35 | + missingDocs.joinToString(", "), |
| 36 | + missingDocs.isEmpty() |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + private fun parseStringKeys(file: File): Set<String> { |
| 41 | + val factory = DocumentBuilderFactory.newInstance() |
| 42 | + val builder = factory.newDocumentBuilder() |
| 43 | + val doc = builder.parse(file) |
| 44 | + |
| 45 | + val keys = mutableSetOf<String>() |
| 46 | + |
| 47 | + listOf("string", "plurals").forEach { tagName -> |
| 48 | + val elements = doc.getElementsByTagName(tagName) |
| 49 | + for (i in 0 until elements.length) { |
| 50 | + val element = elements.item(i) as Element |
| 51 | + val name = element.getAttribute("name") |
| 52 | + if (name.isNotEmpty()) { |
| 53 | + keys.add(name) |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + return keys |
| 59 | + } |
| 60 | +} |
0 commit comments