### Verify Architecture Layer Dependencies Source: https://github.com/lemonappdev/konsist/blob/main/README.md A unit test example demonstrating how to define and enforce clean architecture layer dependencies using the assertArchitecture API. ```kotlin @Test fun `clean architecture layers have correct dependencies`() { Konsist .scopeFromProduction() .assertArchitecture { val domain = Layer("Domain", "com.myapp.domain..") val presentation = Layer("Presentation", "com.myapp.presentation..") val data = Layer("Data", "com.myapp.data..") domain.dependsOnNothing() presentation.dependsOn(domain) data.dependsOn(domain) } } ``` -------------------------------- ### Install Konsist Dependency Source: https://context7.com/lemonappdev/konsist/llms.txt Adds Konsist as a test dependency to your project using Gradle or Maven. This allows you to write architectural and structural rules as unit tests. ```kotlin // Gradle Kotlin DSL testImplementation("com.lemonappdev:konsist:0.17.3") // Gradle Groovy DSL testImplementation "com.lemonappdev:konsist:0.17.3" ``` ```xml com.lemonappdev konsist 0.17.3 test ``` -------------------------------- ### Enforce Package Naming Convention Source: https://github.com/lemonappdev/konsist/blob/main/README.md A unit test example that ensures all classes ending with 'UseCase' are located within the 'usecase' package. ```kotlin @Test fun `classes with 'UseCase' suffix should reside in 'usecase' package`() { Konsist.scopeFromProject() .classes() .withNameEndingWith("UseCase") .assertTrue { it.resideInPackage("..usecase..") } } ``` -------------------------------- ### Integrate Konsist with Kotest Source: https://context7.com/lemonappdev/konsist/llms.txt Provides an example of using Konsist within a Kotest FreeSpec. It demonstrates architectural layer dependency verification and custom test naming for better error reporting. ```kotlin import com.lemonappdev.konsist.api.Konsist import com.lemonappdev.konsist.api.ext.list.* import com.lemonappdev.konsist.api.verify.assertTrue import io.kotest.core.spec.style.FreeSpec class KonsistKotestSpec : FreeSpec({ "classes with UseCase suffix" - { "should reside in usecase package" { Konsist.scopeFromProject() .classes() .withNameEndingWith("UseCase") .assertTrue(testName = "classes with UseCase suffix should reside in usecase package") { it.resideInPackage("..usecase..") } } } "every ViewModel" - { "should have ViewModel suffix" { Konsist.scopeFromProject() .classes() .withAllParentsOf(ViewModel::class) .assertTrue(testName = "every ViewModel should have ViewModel suffix") { it.name.endsWith("ViewModel") } } } "architecture" - { "clean architecture layers should have correct dependencies" { Konsist.scopeFromProduction() .assertArchitecture(testName = "clean architecture layers") { val domain = Layer("Domain", "com.myapp.domain..") val presentation = Layer("Presentation", "com.myapp.presentation..") val data = Layer("Data", "com.myapp.data..") domain.dependsOnNothing() presentation.dependsOn(domain) data.dependsOn(domain) } } } }) ``` -------------------------------- ### Filter Kotlin Declarations by Text Starting With Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html This function filters a list of declarations based on whether their text starts with any of the specified prefixes. It takes a collection or a single string as input and returns a new list containing matching declarations. ```kotlin fun List.[withTextStartingWith](prefixes: Collection): List ``` ```kotlin fun List.[withTextStartingWith](prefix: String, vararg prefixes: String): List ``` -------------------------------- ### Get Package Declarations in Kotlin Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Retrieves a list of package declarations. This function requires the KoPackageProvider. ```kotlin val : KoPackageProvider> List.[packages]: List ``` -------------------------------- ### Filter Declarations by Name Starting With String Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Filters a list of declarations to include only those whose names start with any of the specified prefixes. It can take a collection of strings or a single string with varargs. The function returns a list of declarations that satisfy the condition. ```kotlin fun List.withNameStartingWith(prefixes: Collection): List ``` -------------------------------- ### Enforce Android ViewModel Naming Source: https://github.com/lemonappdev/konsist/blob/main/README.md A unit test example that verifies all classes extending the ViewModel base class have the 'ViewModel' suffix. ```kotlin @Test fun `classes extending 'ViewModel' should have 'ViewModel' suffix`() { Konsist.scopeFromProject() .classes() .withAllParentsOf(ViewModel::class) .assertTrue { it.name.endsWith("ViewModel") } } ``` -------------------------------- ### Enforce Spring Repository Naming Source: https://github.com/lemonappdev/konsist/blob/main/README.md A unit test example that ensures all interfaces annotated with @Repository have the 'Repository' suffix. ```kotlin @Test fun `interfaces with 'Repository' annotation should have 'Repository' suffix`() { Konsist .scopeFromProject() .interfaces() .withAllAnnotationsOf(Repository::class) .assertTrue { it.hasNameEndingWith("Repository") } } ``` -------------------------------- ### Get Init Block Declarations Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Retrieves a list of init block declarations from a list of elements. This extension is part of the KoInitBlockProvider interface and allows for the inspection of initializer blocks in Kotlin classes. ```kotlin val List.initBlocks: List where T : KoInitBlockProvider ``` -------------------------------- ### Get Primary Constructors - Kotlin Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Returns a list of primary constructor declarations. This extension property is available for types that implement `KoPrimaryConstructorProvider`. The output is a list of `KoPrimaryConstructorDeclaration` objects. ```Kotlin val List.primaryConstructors: List where T : KoPrimaryConstructorProvider ``` -------------------------------- ### Get Declarations Without Bare Source Type (Kotlin) Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html This function retrieves a list of declarations that do not have a bare source type. It takes a KClass and an optional array of KClasses as input, and returns a list of declarations. ```Kotlin fun (withoutBareSourceType: KoSourceTypeProvider) : List. withoutBareSourceTypeOf(kClass: KClass<*>, vararg kClasses: KClass<*>) : List ``` -------------------------------- ### Get Declarations: Kotlin List Extension Functions Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Provides extension functions for Kotlin Lists to retrieve various types of declarations. It allows filtering by nested and local declarations. Dependencies include Konsist's API for declarations and providers. ```kotlin fun List.declarations(includeNested: Boolean = true, includeLocal: Boolean = true): List ``` ```kotlin fun List.externalDeclarations(predicate: (KoExternalDeclaration) -> Boolean? = null): List ``` ```kotlin fun List.externalParents(indirectParents: Boolean = false): List ``` ```kotlin fun List.functions(includeNested: Boolean = true, includeLocal: Boolean = true): List ``` ```kotlin fun List.importAliasDeclarations(predicate: (KoImportAliasDeclaration) -> Boolean? = null): List ``` -------------------------------- ### Get Return Types (General) - Kotlin Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Provides a list of return type declarations. This extension property is defined for types that implement `KoReturnProvider`. It returns a list of `KoTypeDeclaration` objects representing the return types. ```Kotlin val List.returnTypes: List where T : KoReturnProvider ``` -------------------------------- ### Configure Konsist Dependencies Source: https://github.com/lemonappdev/konsist/blob/main/README.md Shows how to add the Konsist library to your project using different build systems including Gradle Kotlin, Gradle Groovy, and Maven. ```kotlin testImplementation("com.lemonappdev:konsist:0.17.3") ``` ```groovy testImplementation "com.lemonappdev:konsist:0.17.3" ``` ```xml com.lemonappdev konsist 0.17.3 test ``` -------------------------------- ### Get Return Types (Function) - Kotlin Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Gets the list of return types for function declarations. This extension property applies to lists of types implementing `KoFunctionTypeDeclarationProvider`. It returns a list of `KoTypeDeclaration` objects. ```Kotlin val List.returnTypes: List where T : KoFunctionTypeDeclarationProvider ``` -------------------------------- ### Filter Declarations by Prefix (Kotlin) Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html This function filters a list of declarations, removing those that start with any of the specified prefixes. It accepts a collection or a single string as prefixes and returns a new list with declarations that do not start with any of the given prefixes. ```kotlin fun List.[withoutTextStartingWith](prefixes: Collection): List fun List.[withoutTextStartingWith](prefix: String, vararg prefixes: String): List ``` -------------------------------- ### Define Architectural Layers Source: https://context7.com/lemonappdev/konsist/llms.txt Illustrates how to define architectural layers using the Layer class, utilizing package patterns and wildcards to map code structure to architectural concepts. ```kotlin import com.lemonappdev.konsist.api.architecture.Layer val domainLayer = Layer("Domain", "com.myapp.domain..") val presentationLayer = Layer("Presentation", "com.myapp.presentation..") val dataLayer = Layer("Data", "com.myapp.data..") val userFeatureLayer = Layer("UserFeature", "com.myapp.feature.user..") val authFeatureLayer = Layer("AuthFeature", "com.myapp.feature.auth..") val anyDomainLayer = Layer("Domain", "..domain..") val anyDataLayer = Layer("Data", "..data..") val coreLayer = Layer("Core", "com.myapp.core..") val commonLayer = Layer("Common", "com.myapp.common..") val apiLayer = Layer("API", "com.myapp.api..") ``` -------------------------------- ### Verify Package Residence with Konsist Source: https://context7.com/lemonappdev/konsist/llms.txt Demonstrates how to enforce architectural package rules using Konsist. It validates that specific classes and interfaces reside in designated packages based on their naming conventions. ```kotlin import com.lemonappdev.konsist.api.Konsist import com.lemonappdev.konsist.api.ext.list.* import com.lemonappdev.konsist.api.verify.assertTrue import org.junit.jupiter.api.Test class PackageTest { @Test fun `use cases should reside in usecase package`() { Konsist.scopeFromProject() .classes() .withNameEndingWith("UseCase") .assertTrue { it.resideInPackage("..usecase..") } } @Test fun `repositories should reside in repository or data package`() { Konsist.scopeFromProject() .interfaces() .withNameEndingWith("Repository") .assertTrue { it.resideInPackage("..repository..") || it.resideInPackage("..data..") } } @Test fun `view models should reside in presentation or ui package`() { Konsist.scopeFromProject() .classes() .withNameEndingWith("ViewModel") .assertTrue { it.resideInPackage("..presentation..") || it.resideInPackage("..ui..") || it.resideInPackage("..viewmodel..") } } @Test fun `DTOs should not reside in domain package`() { Konsist.scopeFromProject() .classes() .withNameEndingWith("DTO", "Dto") .assertTrue { it.resideOutsidePackage("..domain..") } } } ``` -------------------------------- ### Init Blocks Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Retrieves a list of declarations that have init block(s). ```APIDOC ## GET /declarations/init-blocks ### Description Retrieves a list of declarations that have init block(s). ### Method GET ### Endpoint /declarations/init-blocks ### Response #### Success Response (200) - **declarations** (List) - A list of declarations that have init block(s). #### Response Example ```json { "declarations": [ { "name": "MyClass", "initBlocks": [ { "statements": [ "println(\"Hello\")" ] } ] } ] } ``` ``` -------------------------------- ### Slicing and Filtering Konsist Scopes Source: https://context7.com/lemonappdev/konsist/llms.txt Demonstrates how to slice project scopes based on package names, file paths, and how to combine multiple source sets. These operations allow for targeted architectural verification within specific project modules. ```kotlin import com.lemonappdev.konsist.api.Konsist import com.lemonappdev.konsist.api.ext.list.* import com.lemonappdev.konsist.api.verify.assertTrue import org.junit.jupiter.api.Test class ScopeSlicingTest { @Test fun `filter scope to specific package`() { val featureScope = Konsist.scopeFromProject() .slice { it.resideInPackage("..feature..") } featureScope.classes() .assertTrue { it.hasNameEndingWith("Feature") || it.hasNameEndingWith("ViewModel") } } @Test fun `filter scope by file path`() { val apiScope = Konsist.scopeFromProject() .slice { it.path.contains("/api/") } apiScope.interfaces() .assertTrue { it.hasKDoc } } @Test fun `combine and filter scopes`() { val mainScope = Konsist.scopeFromSourceSet("main") val testScope = Konsist.scopeFromSourceSet("test") val combinedScope = mainScope + testScope val domainScope = combinedScope.slice { it.resideInPackage("..domain..") } domainScope.classes() .assertTrue { it.hasPublicOrDefaultModifier } } @Test fun `print scope for debugging`() { Konsist.scopeFromProject() .slice { it.resideInPackage("..domain..") } .print(prefix = "Domain files: ") .classes() .assertTrue { it.hasKDoc } } } ``` -------------------------------- ### Verify Declarations with assertTrue and assertFalse Source: https://context7.com/lemonappdev/konsist/llms.txt Demonstrates how to use assertTrue and assertFalse to enforce naming conventions, KDoc requirements, and architectural constraints like banning field injection or mutable properties in data classes. ```kotlin import com.lemonappdev.konsist.api.Konsist import com.lemonappdev.konsist.api.ext.list.* import com.lemonappdev.konsist.api.verify.assertTrue import com.lemonappdev.konsist.api.verify.assertFalse import org.junit.jupiter.api.Test class NamingConventionTest { @Test fun `classes with 'UseCase' suffix should reside in 'usecase' package`() { Konsist.scopeFromProject() .classes() .withNameEndingWith("UseCase") .assertTrue { it.resideInPackage("..usecase..") } } @Test fun `classes extending ViewModel should have ViewModel suffix`() { Konsist.scopeFromProject() .classes() .withAllParentsOf(ViewModel::class) .assertTrue { it.name.endsWith("ViewModel") } } @Test fun `interfaces with Repository annotation should have Repository suffix`() { Konsist.scopeFromProject() .interfaces() .withAllAnnotationsOf(Repository::class) .assertTrue { it.hasNameEndingWith("Repository") } } @Test fun `no class should use field injection`() { Konsist.scopeFromProject() .classes() .assertFalse { it.hasProperty { property -> property.hasAnnotationOf(Inject::class) } } } @Test fun `every public function should have KDoc`() { Konsist.scopeFromProject() .functions() .withPublicModifier() .assertTrue { it.hasKDoc } } @Test fun `data classes should not have mutable properties`() { Konsist.scopeFromProject() .classes() .filter { it.hasDataModifier } .flatMap { it.properties() } .assertFalse { it.hasVarModifier } } } ``` -------------------------------- ### withInitialized Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Filters a list of declarations to include only those that have been initialized. ```APIDOC ## withInitialized ### Description Filters a list of declarations to include only those that have been initialized. ### Method Extension function on List ### Response #### Success Response - Returns a List containing declarations that have been initialized. ``` -------------------------------- ### Get Local Declarations in Kotlin Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Retrieves a list of local declarations. This function requires the KoLocalDeclarationProvider. ```kotlin val : KoLocalDeclarationProvider> List.[localDeclarations]: List ``` -------------------------------- ### Get KDoc Declarations in Kotlin Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Retrieves a list of KDoc declarations. This function requires the KoKDocProvider. ```kotlin val : KoKDocProvider> List.[kDocs]: List ``` -------------------------------- ### Create Konsist Scopes Source: https://context7.com/lemonappdev/konsist/llms.txt Defines the parts of your project that Konsist will analyze. Scopes can be created from the entire project, specific modules, source sets, directories, or individual files. Scopes can also be combined or subtracted. ```kotlin import com.lemonappdev.konsist.api.Konsist // Create scope from entire project val projectScope = Konsist.scopeFromProject() // Create scope from specific module val moduleScope = Konsist.scopeFromModule("app") // Create scope from multiple modules val multiModuleScope = Konsist.scopeFromModule("app", "domain", "data") // Create scope from source set val mainScope = Konsist.scopeFromSourceSet("main") // Create scope from production code only (excludes test source sets) val productionScope = Konsist.scopeFromProduction() // Create scope from test code only val testScope = Konsist.scopeFromTest() // Create scope from specific package val packageScope = Konsist.scopeFromPackage("com.myapp.domain..") // Create scope from directory val directoryScope = Konsist.scopeFromDirectory("src/main/kotlin/feature") // Create scope from specific file val fileScope = Konsist.scopeFromFile("src/main/kotlin/MyClass.kt") // Combine scopes val combinedScope = Konsist.scopeFromModule("app") + Konsist.scopeFromModule("domain") // Subtract scopes val filteredScope = Konsist.scopeFromProject() - Konsist.scopeFromModule("test-utils") ``` -------------------------------- ### Get Object Declarations in Kotlin Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Retrieves a list of object declarations associated with types. This function requires the KoDeclarationCastProvider. ```kotlin val : KoDeclarationCastProvider> List.[objectDeclarations]: List ``` -------------------------------- ### List Extension Naming Conventions Source: https://github.com/lemonappdev/konsist/blob/main/DeveloperReadme.md Defines the naming patterns for properties and functions when working with List providers in Konsist. ```APIDOC ## Naming Conventions for List Extensions ### Description This section describes the standard naming conventions for extensions applied to providers containing a property of type List. The implementation varies based on whether the declaration implements KoNameProvider or falls into specific exception categories. ### Options Overview - **Option 1 (Generic):** Standard filtering and mapping functions. - **Option 2 (KoNameProvider):** Adds name-based filtering (e.g., `withNamed`, `hasWithName`). - **Option 3 (Exceptions):** Specialized handling for enums or specific providers like KoModifierProvider, replacing predicates with concrete instances. ### Functional Patterns - **Properties:** Mapping declarations to their items (plural name). - **Filtering Functions:** - `with`/`without` (singular/plural) - filtering based on predicates or specific instances. - `withAll`/`withoutAll` - bulk filtering logic. - `has`/`hasAll` - boolean checks for declaration existence. ### Example Usage ```kotlin // Example of Option 2 filtering declarations.withNamed("MyClass", "OtherClass") // Example of Option 3 check declarations.has(Modifier.PUBLIC) ``` ``` -------------------------------- ### Get Local Function Declarations in Kotlin Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Retrieves a list of local function declarations. This function requires the KoLocalFunctionProvider. ```kotlin val : KoLocalFunctionProvider> List.[localFunctions]: List ``` -------------------------------- ### Setters API Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Provides access to setter declarations. This endpoint is valuable for inspecting property setters and their implementations. ```APIDOC ## GET /api/setters ### Description Retrieves a list of all setter declarations. ### Method GET ### Endpoint /api/setters ### Parameters #### Query Parameters None ### Request Example ```json { "example": "No request body needed for GET request" } ``` ### Response #### Success Response (200) - **setters** (List) - A list of setter declarations. #### Response Example ```json { "setters": [ { "name": "setterForPropertyA", "parameters": [ { "name": "value", "type": "Int" } ] } ] } ``` ``` -------------------------------- ### Init Block Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Filters a list of declarations to include only those with init blocks satisfying the provided predicate. ```APIDOC ## GET /declarations/init-block ### Description Filters a list of declarations to include only those with init blocks satisfying the provided predicate. ### Method GET ### Endpoint /declarations/init-block ### Parameters #### Query Parameters - **predicate** (Function) - Required - A predicate function that takes a KoInitBlockDeclaration and returns a Boolean. Declarations with init blocks satisfying the predicate are returned. ### Request Example ```json { "query": { "predicate": "initBlock -> initBlock.statements.size > 5" } } ``` ### Response #### Success Response (200) - **declarations** (List) - A list of declarations that have at least one init block satisfying the provided predicate. #### Response Example ```json { "declarations": [ { "name": "MyClass", "initBlocks": [ { "statements": [ "println(\"Hello\")", "println(\"World\")" ] } ] } ] } ``` ``` -------------------------------- ### Get Local Class Declarations in Kotlin Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Retrieves a list of local class declarations. This function requires the KoLocalClassProvider. ```kotlin val : KoLocalClassProvider> List.[localClasses]: List ``` -------------------------------- ### Naming Conventions for List Extensions Source: https://github.com/lemonappdev/konsist/blob/main/DeveloperReadme.md Defines the naming conventions for creating list extensions in Konsist, differentiating between providers with KoXDeclaration types and other singular types (optional or always existing). ```APIDOC ## Naming Conventions For List Extensions ### When Provider Has Property With `KoXDeclaration` Type Examples of such providers: `KoTypeProvider`, `KoReturnTypeProvider`. We create extensions: - Properties: - With the name of the X item in the plural number. - Mapping declaration to its item. - Functions: - With prefix `with/without` and the name of the property in the singular number with `predicate` lambda parameter. - Filtering declarations that have/ not have item satisfying the provided predicate. ### When Provider Has Property With Other Singular Type #### Type always exists E.g. In `KoNameProvider`, the `name` property returns `String` (singular - it's one object), so we create two extensions: - `withName(name: String, vararg names: String)` - `withoutName(name: String, vararg names: String)` And if it makes sense: - `withName(predicate: (String) -> Boolean)` - `withoutName(predicate: (String) -> Boolean)` #### Type is optional E.g. In `KoAliasProvider`, the `alias` property returns `String?` (singular - it's one object: `String` or null ), so we create two extensions: - `withAlias(vararg names: String)` - `withoutAlias(vararg names: String)` The difference is that in the first case we force passing the parameter, in the second it is optional. ### If parameters of extensions is of `KClass` type, then we create analogous extensions like for providers not implementing `KoNameProvider` but with suffix `Of`! ``` -------------------------------- ### Get Interface Declarations in Kotlin Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Retrieves a list of interface declarations associated with types. This function requires the KoDeclarationCastProvider. ```kotlin val : KoDeclarationCastProvider> List.[interfaceDeclarations]: List ``` -------------------------------- ### Get Kotlin Type Declarations in Kotlin Source: https://github.com/lemonappdev/konsist/blob/main/docs/-konsist 0.17.0/com.lemonappdev.konsist.api.ext.list/index.html Retrieves a list of Kotlin type declarations associated with types. This function requires the KoDeclarationCastProvider. ```kotlin val : KoDeclarationCastProvider> List.[kotlinTypeDeclarations]: List ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.