1. Files In ext Package Must Have Name Ending With Ext
@Testfun`files in 'ext' package must have name ending with 'Ext'`() { Konsist .scopeFromProject() .files .withPackage("..ext..") .assertTrue { it.hasNameEndingWith("Ext") }}
2. All Data Class Properties Are Defined In Constructor
@Testfun`all data class properties are defined in constructor`() { Konsist .scopeFromProject() .classes() .withModifier(KoModifier.DATA) .properties() .assertTrue { it.isConstructorDefined }}
3. Every Class Has Test
@Testfun`every class has test`() { Konsist .scopeFromProduction() .classes() .assertTrue { it.hasTestClasses() }}
4. Every Class - Except Data And Value Class - Has Test
@Testfun`every class - except data and value class - has test`() { Konsist .scopeFromProduction() .classes() .withoutModifier(KoModifier.DATA, KoModifier.VALUE) .assertTrue { it.hasTestClasses() }}
5. Properties Are Declared Before Functions
@Testfun`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 } }}
6. Every Constructor Parameter Has Name Derived From Parameter Type
@Testfun`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 }}
7. Every Class Constructor Has Alphabetically Ordered Parameters
@Testfun`every class constructor has alphabetically ordered parameters`() { Konsist .scopeFromProject() .classes() .constructors .assertTrue { it.parameters.isSortedByName() }}
9. Companion Object Is Last Declaration In The Class
@Testfun`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 } }}
10. Every Value Class Has Parameter Named value
@Testfun`every value class has parameter named 'value'`() { Konsist .scopeFromProject() .classes() .withValueModifier() .primaryConstructors .assertTrue { it.hasParameterWithName("value") }}
@Testfun`forbid the usage of 'forbiddenString' in file`() { Konsist .scopeFromProject() .files .assertFalse { it.hasTextContaining("forbiddenString") }}
18. All Function Parameters Are Interfaces
@Testfun`all function parameters are interfaces`() { Konsist .scopeFromProject() .functions() .parameters .types .assertTrue { it.sourceDeclaration?.isInterface }}