From 8401e3678da5622f080487d1132bcb039f2094b2 Mon Sep 17 00:00:00 2001 From: Matt Bertolini Date: Wed, 12 Aug 2026 20:35:49 -0400 Subject: [PATCH] Add filteredOnAssertions method --- .../api/AbstractRichIterableAssert.java | 18 +++++ ...rableAssert_FilteredOnAssertions_Test.java | 74 +++++++++++++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/test/java/org/assertj/eclipse/collections/api/richiterable/AbstractRichIterableAssert_FilteredOnAssertions_Test.java diff --git a/src/main/java/org/assertj/eclipse/collections/api/AbstractRichIterableAssert.java b/src/main/java/org/assertj/eclipse/collections/api/AbstractRichIterableAssert.java index 4601b04..074429e 100644 --- a/src/main/java/org/assertj/eclipse/collections/api/AbstractRichIterableAssert.java +++ b/src/main/java/org/assertj/eclipse/collections/api/AbstractRichIterableAssert.java @@ -308,6 +308,24 @@ public SELF filteredOn(Predicate predicate) { return internalFilteredOn(predicate::test); } + @Override + @CheckReturnValue + public SELF filteredOnAssertions(Consumer elementAssertions) { + checkArgument(elementAssertions != null, "The element assertions should not be null"); + return internalFilteredOn(byPassingAssertions(elementAssertions)::test); + } + + protected static Predicate byPassingAssertions(Consumer assertions) { + return objectToTest -> { + try { + assertions.accept(objectToTest); + return true; + } catch (AssertionError e) { + return false; + } + }; + } + /** * Verifies that the size of the actual RichIterable is equal to the size of the given iterable. * diff --git a/src/test/java/org/assertj/eclipse/collections/api/richiterable/AbstractRichIterableAssert_FilteredOnAssertions_Test.java b/src/test/java/org/assertj/eclipse/collections/api/richiterable/AbstractRichIterableAssert_FilteredOnAssertions_Test.java new file mode 100644 index 0000000..85c51d1 --- /dev/null +++ b/src/test/java/org/assertj/eclipse/collections/api/richiterable/AbstractRichIterableAssert_FilteredOnAssertions_Test.java @@ -0,0 +1,74 @@ +/* + * Copyright 2025-2026 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.assertj.eclipse.collections.api.richiterable; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatExceptionOfType; +import static org.assertj.core.api.Assertions.assertThatNoException; + +import java.util.function.Consumer; + +import org.assertj.core.api.ThrowingConsumer; + +class AbstractRichIterableAssert_FilteredOnAssertions_Test { + @RichIterableParameterizedTest + void filteredOnAssertions_consumer_passes(RichIterableAssertFactory assertFactory) { + assertThatNoException().isThrownBy(() -> assertFactory.fromElements("TOS", "TNG", "DS9", "VOY", "ENT") + .filteredOnAssertions((Consumer) s -> assertThat(s).startsWith("T")) + .hasSize(2) + .containsOnly("TOS", "TNG")); + } + + @RichIterableParameterizedTest + void filteredOnAssertions_consumer_noElementPassesAssertions_filtersEverything(RichIterableAssertFactory assertFactory) { + assertThatNoException().isThrownBy(() -> assertFactory.fromElements("TOS", "TNG", "DS9", "VOY", "ENT") + .filteredOnAssertions((Consumer) s -> assertThat(s).hasSize(4)) + .isEmpty()); + } + + @SuppressWarnings({"rawtypes", "unchecked"}) + @RichIterableParameterizedTest + void filteredOnAssertions_consumer_nullConsumer_throwsException(RichIterableAssertFactory assertFactory) { + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> assertFactory.fromElements("TOS", "TNG", "DS9", "VOY", "ENT").filteredOnAssertions((Consumer) null)) + .withMessageContaining("The element assertions should not be null"); + } + + @SuppressWarnings("RedundantCast") + @RichIterableParameterizedTest + void filteredOnAssertions_throwingConsumer_passes(RichIterableAssertFactory assertFactory) { + assertThatNoException().isThrownBy(() -> assertFactory.fromElements("TOS", "TNG", "DS9", "VOY", "ENT") + .filteredOnAssertions((ThrowingConsumer) s -> assertThat(s).startsWith("T")) + .hasSize(2) + .containsOnly("TOS", "TNG")); + } + + @SuppressWarnings("RedundantCast") + @RichIterableParameterizedTest + void filteredOnAssertions_throwingConsumer_noElementPassesAssertions_filtersEverything(RichIterableAssertFactory assertFactory) { + assertThatNoException().isThrownBy(() -> assertFactory.fromElements("TOS", "TNG", "DS9", "VOY", "ENT") + .filteredOnAssertions((ThrowingConsumer) s -> assertThat(s).hasSize(4)) + .isEmpty()); + } + + @SuppressWarnings({"RedundantCast", "rawtypes", "unchecked"}) + @RichIterableParameterizedTest + void filteredOnAssertions_throwingConsumer_nullConsumer_throwsException(RichIterableAssertFactory assertFactory) { + assertThatExceptionOfType(IllegalArgumentException.class) + .isThrownBy(() -> assertFactory.fromElements("TOS", "TNG", "DS9", "VOY", "ENT").filteredOnAssertions((ThrowingConsumer) null)) + .withMessageContaining("The element assertions should not be null"); + } +}