Yokan / rebuild
Course 1 8624561

SettingsSearchAnchorsTest.kt

composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/ui/screens/settings/SettingsSearchAnchorsTest.kt

test / fixture36 lines
1package eu.friedcutlet.yokan.ui.screens.settings23import java.io.File4import kotlin.test.Test5import kotlin.test.assertTrue67class SettingsSearchAnchorsTest {8    private val settingsDir = File("src/commonMain/kotlin/eu/friedcutlet/yokan/ui/screens/settings")910    @Test fun everyAnchoredSearchOptionMatchesALazyItemKey() {11        val anchored = Regex("""SettingsSearchOption\("([^"]+)".*anchored = true""")12            .findAll(settingsDir.resolve("SettingsSearch.kt").readText())13            .map { it.groupValues[1] }14            .toList()1516        val keys = settingsDir.walkTopDown()17            .filter { it.extension == "kt" }18            .flatMap { lazyItemKeys(it.readText()) }19            .toSet()2021        assertTrue(anchored.isNotEmpty())22        val missing = anchored.filterNot { it in keys }23        assertTrue(missing.isEmpty(), "Anchored settings search ids without a matching item key: $missing")24    }2526    /** Literal `key = "id"` values plus `key = "$variable-suffix"` templates expanded with the string literals assigned to `val variable`. */27    private fun lazyItemKeys(source: String): Sequence<String> {28        val literals = Regex("""key = "([^"$]+)"""").findAll(source).map { it.groupValues[1] }29        val templates = Regex("""key = "\$(\w+)(-[^"$]+)"""").findAll(source).flatMap { match ->30            val (variable, suffix) = match.destructured31            val assignment = Regex("""val $variable = (.+)""").find(source)?.groupValues?.get(1).orEmpty()32            Regex(""""([^"$]+)"""").findAll(assignment).map { it.groupValues[1] + suffix }33        }34        return literals + templates35    }36}37

Search chapters, reference and source filenames.