Comment on page
Clean Architecture Snippets
@Test
fun `clean architecture layers have correct dependencies`() {
Konsist
.scopeFromProduction()
.assertArchitecture {
// Define layers
val domain = Layer("Domain", "com.myapp.domain..")
val presentation = Layer("Presentation", "com.myapp.presentation..")
val data = Layer("Data", "com.myapp.data..")
// Define architecture assertions
domain.dependsOnNothing()
presentation.dependsOn(domain)
data.dependsOn(domain)
}
}
@Test
fun `classes with 'UseCase' suffix should reside in 'domain' and 'usecase' package`() {
Konsist
.scopeFromProject()
.classes()
.withNameEndingWith("UseCase")
.assertTrue { it.resideInPackage("..domain..usecase..") }
}
@Test
fun `classes with 'UseCase' suffix should have single 'public operator' method named 'invoke'`() {
Konsist
.scopeFromProject()
.classes()
.withNameEndingWith("UseCase")
.assertTrue {
val hasSingleInvokeOperatorMethod = it.hasFunction { function ->
function.name == "invoke" && function.hasPublicOrDefaultModifier && function.hasOperatorModifier
}
hasSingleInvokeOperatorMethod && it.countFunctions { item -> item.hasPublicOrDefaultModifier } == 1
}
}
@Test
fun `interfaces with 'Repository' annotation should reside in 'data' package`() {
Konsist
.scopeFromProject()
.interfaces()
.withAnnotationOf(Repository::class)
.assertTrue { it.resideInPackage("..data..") }
}
@Test
fun `every UseCase class has test`() {
Konsist
.scopeFromProduction()
.classes()
.withNameEndingWith("UseCase")
.assertTrue { it.hasTestClass() }
}
Last modified 1mo ago