bartek.t
09/20/2023, 11:46 AM@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.igor.wojda
09/20/2023, 12:22 PMstrict
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):
Konsist.scopeFromProduction()
.classes()
.withoutModifier(KoModifier.DATA, KoModifier.ABSTRACT)
.withParents { it.count() == 1 }
.withParent { it.hasNameEndingWith("X") }
bartek.t
09/20/2023, 12:57 PMKonsist.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.