Hey Kotesters! I struggle a lot with the different...
# kotest
o
Hey Kotesters! I struggle a lot with the different concepts of the two functions:
Copy code
shouldHaveSingleElement(element)
shouldHaveSingleElement { block... }
Every time I want to use them, I have to go back to documentation and/or code to see for myself that the first one, which takes an element actually does assert that the collection contains just a single element and the latter, which takes a predicate, does not do that, instead it only checks the collection to contain one single element that passes the predicate, but could potentially contain many more elements. The documentation piece I found here looks like this:
collection.shouldHaveSingleElement(element)	Asserts that the collection only contains a single element and that that element is the given one.
collection.shouldHaveSingleElement { block }	Asserts that the collection contains a single element by a given predicate.
This is very error prone as most of the time my assumption is I need a check that the collection contains a single element and the next step is only to either be able to provide the element for equals test OR a predicate to check some aspects of the element - but never I expect different behaviour in the main aspect of the function. Can somebody explain to my why we have this and whether it really is intentionally? Maybe I don't see it... Thanks!
k
May I confuse you even further 😈 with:
collection.shouldBeSingleton()
collection.shouldBeSingleton { block }
🙈 1
o
@Klitos Kyriacou Please, don't! 😉 Reason I did not even see
shouldBeSingleton
right next to
shouldHaveSingleElement
is that it lacks its
infix
modifier! I am so used to using functions with only a predicate lambda using infix style, I only type a space and look at possible completions... So, why does
shouldHaveSingleElement
have
infix
, but
shouldBeSingleton
does not? Why does
shouldBeEmpty
work on nullable collections/iterables whereas these functions require an additional
shouldNotBeNull
check? ...so many questions...
I created the necessary changes for the
infix
modifier. But changing the behaviour is something that should be discussed...
a
which name(s) would be less confusing than
shouldHaveSingleElement
?
o
It's not about the name. It's about the fact that all functions named identical should behave identically. Name is fine, but seeing that there is
shouldBeSingleton
as well, maybe
shouldHaveSingleElement
should NOT at all be about singleton check, but only about assuring that one of the elements matches.
a
I'd suggest renaming
shouldHaveSingleElement { block... }
to
shouldHaveSingleElementMatching { block... }
and deprecate the old one.
o
Yes, I agree. I can propose a PR, I have already started to rework this...
a
regarding "maybe
shouldHaveSingleElement
should NOT at all be about singleton check, but only about assuring that one of the elements matches" - do we need that function at all?
shouldHaveSingleElementMatching { it == ... }
should do it, right?
makes sense, feel free to cut a PR
and maybe
collection.shouldBeSingleton { block }
needs some attention as well? in a separate PR