Links
Comment on page

Android Snippets

Konsist can be used to guard the consistency of the Android project.
The android-showcase project contains set of Konsist tests.

1. Classes Extending ViewModel Should Have ViewModel Suffix

@Test
fun `classes extending 'ViewModel' should have 'ViewModel' suffix`() {
Konsist
.scopeFromProject()
.classes()
.withParentOf(ViewModel::class)
.assertTrue { it.name.endsWith("ViewModel") }
}

2. Every ViewModel Public Property Has Flow Type

@Test
fun `Every 'ViewModel' public property has 'Flow' type`() {
Konsist
.scopeFromProject()
.classes()
.withParentOf(ViewModel::class)
.properties()
.assertTrue {
it.hasPublicOrDefaultModifier && it.hasType { type -> type.name == "kotlinx.coroutines.flow.Flow" }
}
}

3. Repository Classes Should Reside In repository Package

@Test
fun `'Repository' classes should reside in 'repository' package`() {
Konsist
.scopeFromProject()
.classes()
.withNameEndingWith("Repository")
.assertTrue { it.resideInPackage("..repository..") }
}

4. No Class Should Use Android Util Logging

@Test
fun `no class should use Android util logging`() {
Konsist
.scopeFromProject()
.files
.assertFalse { it.hasImport { import -> import.name == "android.util.Log" } }
}

5. All JetPack Compose Previews Contain Preview In Method Name

@Test
fun `All JetPack Compose previews contain 'Preview' in method name`() {
Konsist
.scopeFromProject()
.functions()
.withAnnotationOf(Preview::class)
.assertTrue {
it.name.contains("Preview")
}
}