Skip to content

Commit

Permalink
noneSatisfy should report all failing elements. Fixes #1415
Browse files Browse the repository at this point in the history
  • Loading branch information
epeee authored and joel-costigliola committed Feb 4, 2019
1 parent 9692f7d commit dc7b271
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
Expand Up @@ -22,7 +22,7 @@ private NoElementsShouldSatisfy(Object actual, Object faultyElement) {
super("%n" +
"Expecting no elements of:%n" +
" <%s>%n" +
"to satisfy the given assertions requirements but this one did:%n" +
"to satisfy the given assertions requirements but these elements did:%n" +
" <%s>",
actual, faultyElement);
}
Expand Down
17 changes: 10 additions & 7 deletions src/main/java/org/assertj/core/internal/Iterables.java
Expand Up @@ -1188,19 +1188,22 @@ public <E> void assertAllMatch(AssertionInfo info, Iterable<? extends E> actual,
public <E> void assertNoneSatisfy(AssertionInfo info, Iterable<? extends E> actual, Consumer<? super E> restrictions) {
assertNotNull(info, actual);
requireNonNull(restrictions, "The Consumer<T> expressing the restrictions must not be null");
actual.forEach(element -> failIfElementSatisfyRestrictions(element, restrictions, actual, info));
List<E> erroneousElements = stream(actual).map(element -> failsRestrictions(element, restrictions))
.filter(Optional::isPresent)
.map(Optional::get)
.collect(toList());
if (erroneousElements.size() > 0) throw failures.failure(info, noElementsShouldSatisfy(actual, erroneousElements));
}

private <E> void failIfElementSatisfyRestrictions(E element, Consumer<? super E> restrictions, Iterable<? extends E> actual,
AssertionInfo info) {
private <E> Optional<E> failsRestrictions(E element, Consumer<? super E> restrictions) {
try {
restrictions.accept(element);
} catch (AssertionError e) {
// no problem, element is supposed not to meet the given restrictions
return;
// element is supposed not to meet the given restrictions
return Optional.empty();
}
// problem: element meets the given restrictions!
throw failures.failure(info, noElementsShouldSatisfy(actual, element));
// element meets the given restrictions!
return Optional.of(element);
}

public <E> void assertAnyMatch(AssertionInfo info, Iterable<? extends E> actual, Predicate<? super E> predicate,
Expand Down
Expand Up @@ -16,24 +16,38 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.error.NoElementsShouldSatisfy.noElementsShouldSatisfy;
import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION;
import static org.assertj.core.util.Lists.newArrayList;
import static org.assertj.core.util.Lists.list;

import org.assertj.core.description.TextDescription;
import org.junit.jupiter.api.Test;

public class NoElementsShouldSatisfy_create_Test {

@Test
public void should_create_error_message_any() {
public void should_create_error_message() {
// GIVEN
ErrorMessageFactory factory = noElementsShouldSatisfy(newArrayList("Luke", "Yoda"), "Vader");
ErrorMessageFactory factory = noElementsShouldSatisfy(list("Luke", "Leia", "Yoda"), list("Luke", "Leia"));
// WHEN
String message = factory.create(new TextDescription("Test"), STANDARD_REPRESENTATION);
// THEN
assertThat(message).isEqualTo(format("[Test] %n" +
"Expecting no elements of:%n" +
" <[\"Luke\", \"Yoda\"]>%n" +
"to satisfy the given assertions requirements but this one did:%n" +
" <\"Vader\">"));
" <[\"Luke\", \"Leia\", \"Yoda\"]>%n" +
"to satisfy the given assertions requirements but these elements did:%n" +
" <[\"Luke\", \"Leia\"]>"));
}

@Test
public void should_create_error_message_percent() {
// GIVEN
ErrorMessageFactory factory = noElementsShouldSatisfy(list("Luke", "Leia%s", "Yoda"), list("Luke", "Leia%s"));
// WHEN
String message = factory.create(new TextDescription("Test"), STANDARD_REPRESENTATION);
// THEN
assertThat(message).isEqualTo(format("[Test] %n" +
"Expecting no elements of:%n" +
" <[\"Luke\", \"Leia%%s\", \"Yoda\"]>%n" +
"to satisfy the given assertions requirements but these elements did:%n" +
" <[\"Luke\", \"Leia%%s\"]>"));
}
}
Expand Up @@ -15,11 +15,11 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.assertThatNullPointerException;
import static org.assertj.core.api.Assertions.catchThrowable;
import static org.assertj.core.error.NoElementsShouldSatisfy.noElementsShouldSatisfy;
import static org.assertj.core.test.TestData.someInfo;
import static org.assertj.core.util.AssertionsUtil.expectAssertionError;
import static org.assertj.core.util.FailureMessages.actualIsNull;
import static org.assertj.core.util.Lists.newArrayList;
import static org.assertj.core.util.Lists.list;
import static org.mockito.Mockito.verify;

import java.util.List;
Expand All @@ -30,7 +30,7 @@

public class Iterables_assertNoneSatisfy_Test extends IterablesBaseTest {

private List<String> actual = newArrayList("Luke", "Leia", "Yoda");
private List<String> actual = list("Luke", "Leia", "Yoda");

@Test
public void should_pass_when_no_elements_satisfy_the_given_single_restriction() {
Expand Down Expand Up @@ -65,12 +65,22 @@ public void should_fail_when_one_elements_satisfy_the_given_restrictions() {
// GIVEN
Consumer<String> restrictions = name -> assertThat(name).startsWith("Y");
// WHEN
Throwable assertionError = catchThrowable(() -> iterables.assertNoneSatisfy(someInfo(), actual, restrictions));
Throwable assertionError = expectAssertionError(() -> iterables.assertNoneSatisfy(someInfo(), actual, restrictions));
// THEN
verify(failures).failure(info, noElementsShouldSatisfy(actual, "Yoda"));
verify(failures).failure(info, noElementsShouldSatisfy(actual, list("Yoda")));
assertThat(assertionError).isNotNull();
}

@Test
public void should_fail_when_two_elements_satisfy_the_given_restrictions() {
// GIVEN
Consumer<String> restrictions = name -> assertThat(name).startsWith("L");
// WHEN
expectAssertionError(() -> iterables.assertNoneSatisfy(someInfo(), actual, restrictions));
// THEN
verify(failures).failure(info, noElementsShouldSatisfy(actual, list("Luke", "Leia")));
}

@Test
public void should_throw_error_if_consumer_restrictions_is_null() {
assertThatNullPointerException().isThrownBy(() -> iterables.assertNoneSatisfy(someInfo(), actual, null))
Expand All @@ -79,7 +89,7 @@ public void should_throw_error_if_consumer_restrictions_is_null() {

@Test
public void should_fail_if_actual_is_null() {
assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->{
assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> {
List<String> nullActual = null;
iterables.assertNoneSatisfy(someInfo(), nullActual, name -> assertThat(name).startsWith("Y"));
}).withMessage(actualIsNull());
Expand Down

0 comments on commit dc7b271

Please sign in to comment.