LayeringRulesTest.kt
composeApp/src/desktopTest/kotlin/eu/friedcutlet/yokan/LayeringRulesTest.kt
1package eu.friedcutlet.yokan23import java.io.File4import kotlin.test.Test5import kotlin.test.assertTrue67/** Enforces CODE_STYLE's package layering across every main source set. */8class LayeringRulesTest {9 private val mainSourceSets = File("src").listFiles { file -> file.isDirectory && file.name.endsWith("Main") }.orEmpty()1011 @Test fun lowerLayersNeverReferenceUiOrApp() {12 assertNoReferences(packages = listOf("data", "database", "domain", "services"), forbidden = Regex("""\beu\.friedcutlet\.yokan\.(ui|app)\."""))13 }1415 @Test fun domainDependsOnlyOnItselfAndUtil() {16 assertNoReferences(packages = listOf("domain"), forbidden = Regex("""\beu\.friedcutlet\.yokan\.(data|database|services)\."""))17 }1819 private fun assertNoReferences(packages: List<String>, forbidden: Regex) {20 val roots = mainSourceSets.flatMap { set -> packages.map { set.resolve("kotlin/eu/friedcutlet/yokan/$it") } }21 assertTrue(roots.any { it.isDirectory }, "No source roots found under ${File("src").absolutePath}")22 val violations = roots.filter { it.isDirectory }23 .flatMap { root -> root.walkTopDown().filter { it.extension == "kt" }.toList() }24 .flatMap { file ->25 file.readLines().mapIndexedNotNull { index, line ->26 "${file.path}:${index + 1}: ${line.trim()}".takeIf { forbidden.containsMatchIn(line) }27 }28 }29 assertTrue(violations.isEmpty(), "Layering violations:\n${violations.joinToString("\n")}")30 }31}32