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

bartek.t

09/20/2023, 11:46 AM
One question. Assuming I would like to check that all classes implementing only one interface should have postfix in name would look like as below
Copy code
@Test
fun `classes implementing one interface should have postfix 'X' in name`() {
    Konsist.scopeFromProduction()
        .classes()
        .filter { !it.hasDataModifier && !it.hasAbstractModifier && it.parents.size == 1 && !it.hasNameEndingWith("X") }
        .flatMap { it.parents }
        .flatMap {
            Konsist.scopeFromFile(it.projectPath)
                .interfaces()
        }
        .assert { false } // if assertion is invoked then actually such an element exist when for successful test it shouldn't
// Currently in case the check is true and nothing meets criteria then test also will return false
}
...but in current scenario I don't have proper assertion at the end as you can see. Probably something like
.assertNone()
would be needed here or maybe my test is incorrect? What do you think? Current assertion implementations have check
_checkIfLocalListIsEmpty_(this, _getTestMethodNameFromFourthIndex_())
which will produce an error in case there's no items for such an criteria.
I guess this could be partially related - https://github.com/LemonAppDev/konsist/pull/564
i

igor.wojda

09/20/2023, 12:22 PM
Few takeaways: 1. Indeed this is the ticket. We will improve asserts with the next release - assert will by default pass on empty lists (new behaviour) and it will have a
strict
parameter to throw an exception (current behaviour) 2. Currently referring to a parent may be a bit tricky. We have a ticket to add a "complex references" - parents will return actual instance of KoDeclaration, not a string. This will give is way to check if given parent is class or an interface. ATM it may be hard to achieve. 3.
projectPath
refers to the file where declaration is located (not the file path of the parent). 4.
flatMap
is no required nowadays as a lot of properties can be directly retrieved 5. Consider using
with
/
without
rather than
filter
- idea is to have all methods in the Konsist API and fallback to kotlin collection methods (filter is still fine, but things may be simple with Konsist API):
Copy code
Konsist.scopeFromProduction()
            .classes()
            .withoutModifier(KoModifier.DATA, KoModifier.ABSTRACT)
            .withParents { it.count() == 1 }
            .withParent { it.hasNameEndingWith("X") }
b

bartek.t

09/20/2023, 12:57 PM
Ad. 1️⃣ It wouldn't be needed if I would rework this to something alike (if 3️⃣ wasn't true of course 😅):
Copy code
Konsist.scopeFromProduction()
    .classes()
    .withoutModifier(KoModifier.DATA, KoModifier.ABSTRACT)
    .withoutNameEndingWith("Impl")
    .withParents { it.count() == 1 }
    .assert {
        Konsist.scopeFromFile(it.parents[0].projectPath)
            .interfaces().isEmpty()
    }
Ad. 2️⃣ I was thinking about it, that this could be like desired improvement. Ok, so it will solve 3️⃣ too. Ad. 4️⃣ 💯 Ad. 5️⃣ I know, with the API functions code is looking great to me but I just wanted to have single filtering, but with API functions it's definitely much more readable.
Nevertheless, this is a great library, I hope it will become a standard! 🙂
❤️ 1
🙂 1