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

ceedee

09/22/2023, 12:23 PM
Is this the right way to check for inheritance or is there an out-of-the-box way using Konsist?
Copy code
@Test
fun `every class with a name ending on FragmentDelegate needs to be (indirectly) derived from BaseFragmentDelegate`() {
    Konsist.scopeFromProduction()
        .classes()
        .withNameEndingWith("FragmentDelegate")
        .takeIf { it.isNotEmpty() }
        ?.assert { klass ->
            val classToCheckForInheritance = Class.forName(klass.fullyQualifiedName)
            BaseFragmentDelegate::class.java.isAssignableFrom(classToCheckForInheritance)
        }
}
i

igor.wojda

09/22/2023, 11:41 PM
Copy code
@Test
fun `every class with a name ending on FragmentDelegate needs to be (indirectly) derived from BaseFragmentDelegate`() {
    Konsist.scopeFromProduction()
        .classes()
        .withNameEndingWith("FragmentDelegate")
        ?.assert {
            it.hasParentOf<BaseFragmentDelegate>()
        }
}
BTW
.takeIf { it.isNotEmpty() }
should not be required - please tell me whats happens if this is not present (this may be a bug) 🤔
2 Views