Comment on page
General Snippets
@Test
fun `files in 'ext' package must have name ending with 'Ext'`() {
Konsist
.scopeFromProject()
.files
.withPackage("..ext..")
.assertTrue { it.hasNameEndingWith("Ext") }
}
@Test
fun `properties are declared before functions`() {
Konsist
.scopeFromProject()
.classes()
.assertTrue {
val lastKoPropertyDeclarationIndex = it
.declarations(includeNested = false, includeLocal = false)
.indexOfLastInstance<KoPropertyDeclaration>()
val firstKoFunctionDeclarationIndex = it
.declarations(includeNested = false, includeLocal = false)
.indexOfFirstInstance<KoFunctionDeclaration>()
if (lastKoPropertyDeclarationIndex != -1 && firstKoFunctionDeclarationIndex != -1) {
lastKoPropertyDeclarationIndex < firstKoFunctionDeclarationIndex
} else {
true
}
}
}
@Test
fun `every constructor parameter has name derived from parameter type`() {
Konsist
.scopeFromProject()
.classes()
.constructors
.parameters
.assertTrue {
val nameTitleCase = it.name.replaceFirstChar { char -> char.titlecase(Locale.getDefault()) }
nameTitleCase == it.type.sourceType
}
}
@Test
fun `every class constructor has alphabetically ordered parameters`() {
Konsist
.scopeFromProject()
.classes()
.constructors
.assertTrue {
val names = it.parameters.map { parameter -> parameter.name }
val sortedNames = names.sorted()
names == sortedNames
}
}
@Test
fun `companion object is last declaration in the class`() {
Konsist
.scopeFromProject()
.classes()
.assertTrue {
val companionObject = it.objects(includeNested = false).lastOrNull { obj ->
obj.hasModifier(KoModifier.COMPANION)
}
if (companionObject != null) {
it.declarations(includeNested = false, includeLocal = false).last() == companionObject
} else {
true
}
}
}
@Test
fun `every value class has parameter named 'value'`() {
Konsist
.scopeFromProject()
.classes()
.withValueModifier()
.primaryConstructors
.assertTrue { it.hasParameterWithName("value") }
}
@Test
fun `no empty files allowed`() {
Konsist
.scopeFromProject()
.files
.assertFalse { it.text.isEmpty() }
}
@Test
fun `no field should have 'm' prefix`() {
Konsist
.scopeFromProject()
.classes()
.properties()
.assertFalse {
val secondCharacterIsUppercase = it.name.getOrNull(1)?.isUpperCase() ?: false
it.name.startsWith('m') && secondCharacterIsUppercase
}
}
@Test
fun `no class should use field injection`() {
Konsist
.scopeFromProject()
.classes()
.properties()
.assertFalse { it.hasAnnotationOf<Inject>() }
}
@Test
fun `no class should use Java util logging`() {
Konsist
.scopeFromProject()
.files
.assertFalse { it.hasImport { import -> import.name == "java.util.logging.." } }
}
@Test
fun `package name must match file path`() {
Konsist
.scopeFromProject()
.packages
.assertTrue { it.hasMatchingPath }
}
@Test
fun `no wildcard imports allowed`() {
Konsist
.scopeFromProject()
.imports
.assertFalse { it.isWildcard }
}
@Test
fun `forbid the usage of 'forbiddenString' in file`() {
Konsist
.scopeFromProject()
.files
.assertFalse { it.text.contains("forbiddenString") }
}
Last modified 1mo ago