Skip to content

Commit 4cdeb7d

Browse files
committed
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.
1 parent 0c244f3 commit 4cdeb7d

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

app/src/main/res/values-qq/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,7 @@
413413
<string name="title_page_bookmarks_locations">Tab in bookmark view.</string>
414414
<string name="title_page_bookmarks_categories">Tab in bookmark view.</string>
415415
<string name="menu_bookmark">Call to action.</string>
416+
<string name="documentation_without_source">ONLY HERE TO TEST THAT IT WORKS IN GITHUB, MUST NOT BE MERGED.</string>
416417
<string name="provider_bookmarks">Provider name.</string>
417418
<string name="bookmark_empty">Notice when there are no bookmarks.</string>
418419
<string name="provider_bookmarks_location">Provider title.</string>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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

Comments
 (0)