feat: add allElementsAreEqual assertion for iterables - #4330
seonwooj0810 wants to merge 1 commit into
Conversation
|
Circling back on this assertion addition — happy to tweak the naming or API shape if you have preferences. |
|
Hi @seonwooj0810, thanks for your contribution! I'd like to discuss an alternative name for this feature; see #4302 (comment). |
|
Thanks @scordio! Happy to rename. One thing to flag: in #4302 @joel-costigliola steered away from "same" since it could read as reference identity, which is why I went with |
|
Fair enough, comment updated for consistency. WDYT @joel-costigliola? |
|
@seonwooj0810 just FYI, there is no need to polish your PR description or comments through an LLM. We much prefer direct and authentic conversation, as keeping things direct makes the review process faster and more natural for everyone. |
|
Understood, thanks for the feedback. I’ll keep the discussion brief and direct. |
Closes #4302
Why
There is currently no chaining-friendly way to assert that all elements of an iterable are equal to each other. The workaround
assertThat(list).allMatch(e -> e.equals(list.get(0)))requires a direct reference to the first element, which is awkward when the iterable is produced by a chain offilteredOn/extractingcalls and cannot be easily referenced. As discussed in the issue, @joel-costigliola agreed to addallElementsAreEqual(equality-based, not reference-based).What
Adds
allElementsAreEqual()toAbstractIterableAssert, so it is available on allIterable-based assertions:allMatch/allSatisfy.usingElementComparator(...)(the assertion goes through the internalcomparisonStrategy).New error factory
ShouldHaveAllElementsEqualproduces a message such as:Tests
Iterables_assertAllElementsAreEqual_Test— engine behaviour: all-equal, single-element and empty pass; null actual; failure reports all unequal elements; readable error message; and the custom (case-insensitive) comparison-strategy pass/fail paths.IterableAssert_allElementsAreEqual_Test— verifies the public API delegates to the internal engine (IterableAssertBaseTest).Verification done: ran
./mvnw -pl assertj-core testfor both new test classes (10 tests, all green);spotless:applyandlicense:formatproduce no further changes.I followed the discussion in #4302; if you would prefer this also exposed on
ObjectEnumerableAssert(arrays) or want the pair/fold assertions tracked separately, happy to adjust.