Is there an easy way to check the entire parent tr...
# konsist
e
Is there an easy way to check the entire parent tree? I want to check all classes that implement
Repository
regardless of how high up the tree that is. Right now I have this:
Copy code
@Test
    fun `interfaces that implement 'Repository' should have 'Repository' suffix`() {
        Konsist
            .scopeFromProject()
            .interfaces()
            .withAllParentsOf(CoroutineCrudRepository::class)
            .assert { it.hasNameEndingWith("Repository") }
    }
but would prefer:
Copy code
@Test
    fun `interfaces that implement 'Repository' should have 'Repository' suffix`() {
        Konsist
            .scopeFromProject()
            .interfaces()
            .withAllParentsOf(Repository::class)
            .assert { it.hasNameEndingWith("Repository") }
    }
i
ATM there is now way of doing this. This feature is coming soon.
👍 1
e
also might be nice to be able to do
.withAnnotation<RestController>()
or
.withParent<Repository>()
. just a little sugar.
Just added some custom ext funs for that ^
Copy code
inline fun <reified A : Any> classesAnnotatedWith() =
    Konsist
        .scopeFromProject()
        .classes()
        .withAllAnnotationsOf(A::class)
Then usage is like:
Copy code
class EntityConsistencyTests {
    @Test
    fun `classes annotated with @Table should implement AuditedPersistable`() {
        classesAnnotatedWith<Table>()
            .assert { it.hasParents(AuditedPersistable::class.java.simpleName) }
    }

    @Test
    fun `classes annotated with @Table should be data classes`() {
        classesAnnotatedWith<Table>()
            .assert { it.hasDataModifier }
    }

    @Test
    fun `classes annotated with @Table should have getId function annotated with @Id`() {
        classesAnnotatedWith<Table>()
            .assert { it.containsFunction { f -> f.name == "getId" && f.hasAnnotationsOf(Id::class) } }
    }
}
m
+1 I have +50 modules and want to use konsist for all the modules
i
Konsist can be used for all modules by using
scopeFromProject
. This discussion is about supporting parent tree that seems unrelated to you message.
Thats being said this "parent tree" feature is currently under development
m
Ohh I think I see the problem now, all the modules needs to be included in the module that konsist is used
i
no - this is not true. Can you describe what you actually want to achieve?
Konsist.scopeFromProject()
should be a good start, but take a look at docs - there are various ways of creating Konsist scope and scope composition - all of this should be enough to fit your needs https://docs.konsist.lemonappdev.com/writing-tests/koscope
m
Copy code
Konsist.scopeFromProject()
    .classes()
    .withNameEndingWith("Service")
    .assert {
        println(it.name) // here
    }
so i can not get my all classes ends with
Service
here, only some of them visible, my project is KMP btw, not sure if it makes difference
i
All classes should be visible. If only some are visible then we may have a bug. Please share a small sample project where this behaviour can be repdoduced (I usually take a real project and delete most of the stuff to get minimal sample 😂).
m
Okay I will create an issue with a sample then 👍 Thank you for quick hands up! 🙂
❤️ 1
i
To debug this (e.g. nail down certain classes that are not included) you can follow this https://docs.konsist.lemonappdev.com/features/debug-konsist-test
👀 1
n
A new version v0.14.0 has been released. It contains parent references. See https://github.com/LemonAppDev/konsist/releases/tag/v0.14.0 Below solution for main message:
Copy code
Konsist
    .scopeFromProject()
    .interfaces()
    .withAllParentsOf(Repository::class, indirectParents = true)
    .assertTrue { it.hasNameEndingWith("Repository") }