https://kotlinlang.org logo
#konsist
Title
# konsist
c

ceedee

09/21/2023, 9:48 AM
Another question regarding a test I found particularly interesting from the samples:
Copy code
@Test
fun `properties are declared before functions`() {
    Konsist
        .scopeFromProject()
        .classes()
        .assert {
            val lastKoPropertyDeclarationIndex = it
                .declarations()
                .indexOfLastInstance<KoPropertyDeclaration>()

            val firstKoFunctionDeclarationIndex = it
                .declarations()
                .indexOfFirstInstance<KoFunctionDeclaration>()

            if (lastKoPropertyDeclarationIndex != -1 && firstKoFunctionDeclarationIndex != -1) {
                lastKoPropertyDeclarationIndex < firstKoFunctionDeclarationIndex
            } else {
                true
            }
        }
}
It wanted to include it but found that I get a lot a false positives, e.g. for local variables in methods. Am I doing something wrong? Is there a revised example which is working?
n

Natalia Peterwas

09/21/2023, 10:48 AM
Sorry, we missed it after the last release when we change default value of all
includeNested
and
includeLocal
parameters from
false
to
true
. This test should work when you change
.declarations()
to
.declarations(includeNested = false, includeLocal = false)
:
Copy code
@Test
fun `properties are declared before functions`() {
    Konsist
        .scopeFromProject()
        .classes()
        .assert {
            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
            }
        }
}
Let me know if it works now 🙂 I will also correct this mistake in the documentation 🙂 Thanks for the contribution 🙏
c

ceedee

09/21/2023, 12:29 PM
Thank you for your quick and helpful response. The test now works 👍
❤️ 1
👍 1