Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,24 @@ public SELF filteredOn(Predicate<? super ELEMENT> predicate) {
return internalFilteredOn(predicate::test);
}

@Override
@CheckReturnValue
public SELF filteredOnAssertions(Consumer<? super ELEMENT> elementAssertions) {
checkArgument(elementAssertions != null, "The element assertions should not be null");
return internalFilteredOn(byPassingAssertions(elementAssertions)::test);
}

protected static <T> Predicate<T> byPassingAssertions(Consumer<? super T> 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.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -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<String> assertFactory) {
assertThatNoException().isThrownBy(() -> assertFactory.fromElements("TOS", "TNG", "DS9", "VOY", "ENT")
.filteredOnAssertions((Consumer<String>) s -> assertThat(s).startsWith("T"))
.hasSize(2)
.containsOnly("TOS", "TNG"));
}

@RichIterableParameterizedTest
void filteredOnAssertions_consumer_noElementPassesAssertions_filtersEverything(RichIterableAssertFactory<String> assertFactory) {
assertThatNoException().isThrownBy(() -> assertFactory.fromElements("TOS", "TNG", "DS9", "VOY", "ENT")
.filteredOnAssertions((Consumer<String>) s -> assertThat(s).hasSize(4))
.isEmpty());
}

@SuppressWarnings({"rawtypes", "unchecked"})
@RichIterableParameterizedTest
void filteredOnAssertions_consumer_nullConsumer_throwsException(RichIterableAssertFactory<String> 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<String> assertFactory) {
assertThatNoException().isThrownBy(() -> assertFactory.fromElements("TOS", "TNG", "DS9", "VOY", "ENT")
.filteredOnAssertions((ThrowingConsumer<String>) s -> assertThat(s).startsWith("T"))
.hasSize(2)
.containsOnly("TOS", "TNG"));
}

@SuppressWarnings("RedundantCast")
@RichIterableParameterizedTest
void filteredOnAssertions_throwingConsumer_noElementPassesAssertions_filtersEverything(RichIterableAssertFactory<String> assertFactory) {
assertThatNoException().isThrownBy(() -> assertFactory.fromElements("TOS", "TNG", "DS9", "VOY", "ENT")
.filteredOnAssertions((ThrowingConsumer<String>) s -> assertThat(s).hasSize(4))
.isEmpty());
}

@SuppressWarnings({"RedundantCast", "rawtypes", "unchecked"})
@RichIterableParameterizedTest
void filteredOnAssertions_throwingConsumer_nullConsumer_throwsException(RichIterableAssertFactory<String> assertFactory) {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> assertFactory.fromElements("TOS", "TNG", "DS9", "VOY", "ENT").filteredOnAssertions((ThrowingConsumer) null))
.withMessageContaining("The element assertions should not be null");
}
}
Loading