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

Yonatan Karp-Rudin

10/06/2023, 4:03 PM
I'm not sure how valuable this would be to anybody besides me, but I'm currently playing with integrating konsist to a repository template that I'm using for my pet projects. I wanted to add some test validations that should always pass regardless of the project (e.g. all controllers are with the suffix
Controller
). However, for the best of my understanding the
assert
extension function expects the list of results to not be empty. Does it make any sense to either extend the functionality or add additional assertion like
assertOrEmpty
as in my specific case I might not have any
Repository
or
Service
implemented on the template repository yet I'll add code snippet in the 🧵
What I'm currently doing is something like this:
Copy code
class AnnotationValidationTest {
    @Test
    fun `classes with 'RestController' annotation should have 'Controller' suffix`() {
        Konsist
            .scopeFromProject()
            .classes()
            .withAnnotationOf(RestController::class)
            .assert { it.hasNameEndingWithOrNone("Controller") }
    }

    @Test
    fun `classes with 'Service' annotation should have 'Service' suffix`() {
        Konsist
            .scopeFromProject()
            .classes()
            .withAnnotationOf(Service::class)
            .assert { it.hasNameEndingWithOrNone("Service") }
    }

    @Test
    fun `classes with 'Repository' annotation should have 'Repository' suffix`() {
        Konsist
            .scopeFromProject()
            .classes()
            .withAnnotationOf(Repository::class)
            .assert { it.hasNameEndingWithOrNone("Repository") }
    }

    private fun KoClassDeclaration.hasNameEndingWithOrNone(vararg names: String): Boolean {
        if (this.name.isEmpty()) {
            return true
        }

        return names.all { name ->
            LocationUtil.resideInLocation("..$name", name)
        }
    }
}
However the extension function is never called as the list of results is empty. Also, if the assertion would accept empty as a valid result I could completely remove my extension function 🙂
i

igor.wojda

10/06/2023, 8:19 PM
In the new version v013.0 coming next week, this behaviour will be changed - konsist will pass on an empty list to match behaviour of methods in kotlin collection processing - you will be also able to control this behavior (pass / fail when lost is empty)
💯 1
🙏 1
y

Yonatan Karp-Rudin

10/06/2023, 8:21 PM
Amazing, thanks!