Olaf Gottschalk
09/16/2025, 6:48 AMshouldHaveSingleElement(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!Klitos Kyriacou
09/16/2025, 10:41 AMcollection.shouldBeSingleton()
collection.shouldBeSingleton { block }Olaf Gottschalk
09/16/2025, 10:49 AMshouldBeSingleton 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...Olaf Gottschalk
09/16/2025, 12:03 PMinfix modifier. But changing the behaviour is something that should be discussed...Alex Kuznetsov
09/16/2025, 6:08 PMshouldHaveSingleElement?Olaf Gottschalk
09/16/2025, 6:10 PMshouldBeSingleton as well, maybe shouldHaveSingleElement should NOT at all be about singleton check, but only about assuring that one of the elements matches.Alex Kuznetsov
09/16/2025, 6:19 PMshouldHaveSingleElement { block... }
to
shouldHaveSingleElementMatching { block... }
and deprecate the old one.Olaf Gottschalk
09/16/2025, 6:20 PMAlex Kuznetsov
09/16/2025, 6:20 PMshouldHaveSingleElement 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?Alex Kuznetsov
09/16/2025, 6:20 PMAlex Kuznetsov
09/16/2025, 6:21 PMcollection.shouldBeSingleton { block } needs some attention as well? in a separate PR