One thing I don’t like about the API is that `asse...
# konsist
m
One thing I don’t like about the API is that
assert
fails when called on an empty list. If I have a rule checking something about all functions in a class, then some classes are going to have no functions and that’s fine, obviously they can’t violate the rules, it’s vacuously true. I’d expect it to behave the same as calling
.all { ... }
on an empty collection - it returns true because no element violates the predicate.
👍 3
i
Please drop a snippet to demonstrate this (I need a bit more context to make sure I got it 100% right). Ae you talking about non-being able to assert the empty declaration lit ("Declaration list is empty. Please make sure that list of declarations contain items...")?
m
Yes that’s it. If the declaration list is empty I would expect the assertion to pass, not fail.
n
We’ve been thinking about this issue and find this exception helpful - especially when you’re starting out with Konsist. It can detect invalid tests (e.g. when we want to check
interfaces
, but in test we write
classes
etc.). But we will consider this problem again and try to solve it 🙂
We are thinking about two options: • adding another
assert
method that will behave as you said before (we don’t have a good name for this “new” assertion - maybe
assertRelaxed
?) • adding an additional parameter to our
assert
- then developer will be able to choose what behaviour he expects What do you think about it? Maybe you have another idea?
m
The normal recommended red/green process for writing tests is covering this: you see the test fail (even on purpose), and then you fix the failing code - ensuring the test actually works, as opposed to passing because of a mistake. There is also the "print" method Konsist provides that already makes it easy to confirm you are matching what you want, before you add the actual assertion. This is consistent with other test assertion libraries, for example in Kotest Assertions
collection.shouldContainOnlyNulls()
passes when the collection is empty. I think that should be the default behaviour, and failing on an empty collection could be another
assertNotEmpty()
to chain if needed.
i
Thx for sharing. I like the
assertNotEmpty
we will address this with the next release
This is the current API proposal: Deprecate methods:
Copy code
assert
assertNot
Add methods with a new behaviour where an empty list will pass. Add
strict = false
(default
false
) param (if someone wants this strict verification e.g for debugging):
Copy code
assertTrue (strict = true)
assertFalse (strict = true)