Yokan / rebuild
Course 1 8624561

HardcodedUiStringsTest.kt

composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/ui/HardcodedUiStringsTest.kt

test / fixture31 lines
1package eu.friedcutlet.yokan.ui23import java.io.File4import kotlin.test.Test5import kotlin.test.assertTrue67/**8 * Ratchet for hardcoded user-visible text: per-file counts under `ui/` may only go down.9 * New text belongs in `composeResources` (`stringResource(Res.string.…)`); after moving text,10 * lower the file's count in `src/desktopTest/resources/hardcoded-ui-strings.txt`.11 */12class HardcodedUiStringsTest {13    private val uiRoot = File("src/commonMain/kotlin/eu/friedcutlet/yokan/ui")14    private val baselineFile = File("src/desktopTest/resources/hardcoded-ui-strings.txt")15    private val literal = Regex("""\bText\(\s*"[A-Za-z]|\b(?:title|label|text|subtitle|message|contentDescription|placeholder)\s*=\s*"[A-Za-z]""")1617    @Test fun hardcodedUiStringsOnlyDecrease() {18        val baseline = baselineFile.readLines()19            .filter { it.isNotBlank() && !it.startsWith("#") }20            .associate { line -> line.substringBeforeLast(' ') to line.substringAfterLast(' ').toInt() }21        val actual = uiRoot.walkTopDown()22            .filter { it.extension == "kt" }23            .associate { file -> file.relativeTo(uiRoot).invariantSeparatorsPath to file.readLines().sumOf { literal.findAll(it).count() } }24            .filterValues { it > 0 }2526        val grown = actual.filter { (path, count) -> count > (baseline[path] ?: 0) }27        assertTrue(grown.isEmpty(), "New hardcoded UI strings (move them to composeResources): $grown")28        val shrunk = baseline.filter { (path, count) -> (actual[path] ?: 0) < count }29        assertTrue(shrunk.isEmpty(), "Hardcoded UI strings went down; lower these counts in ${baselineFile.path}: $shrunk")30    }31}32

Search chapters, reference and source filenames.