diff --git a/src/main/java/org/assertj/core/error/ElementsShouldSatisfy.java b/src/main/java/org/assertj/core/error/ElementsShouldSatisfy.java index d30cd559d1..7f95c8939e 100644 --- a/src/main/java/org/assertj/core/error/ElementsShouldSatisfy.java +++ b/src/main/java/org/assertj/core/error/ElementsShouldSatisfy.java @@ -19,30 +19,26 @@ public class ElementsShouldSatisfy extends BasicErrorMessageFactory { - public static ErrorMessageFactory elementsShouldSatisfyAny(Object actual) { - return new ElementsShouldSatisfy(actual); + public static ErrorMessageFactory elementsShouldSatisfyAny(Object actual, + List elementsNotSatisfyingRequirements) { + return new ElementsShouldSatisfy("%n" + + "Expecting any element of:%n" + + " <%s>%n" + + "to satisfy the given assertions requirements but none did:%n%n", + actual, elementsNotSatisfyingRequirements); } public static ErrorMessageFactory elementsShouldSatisfy(Object actual, List elementsNotSatisfyingRestrictions) { - return new ElementsShouldSatisfy(actual, elementsNotSatisfyingRestrictions); + return new ElementsShouldSatisfy("%n" + + "Expecting all elements of:%n" + + " <%s>%n" + + "to satisfy given requirements, but these elements did not:%n%n", + actual, elementsNotSatisfyingRestrictions); } - private ElementsShouldSatisfy(Object actual) { - super("%n" + - "Expecting any element of:%n" + - " <%s>%n" + - "to satisfy the given assertions requirements but none did.", - actual); - } - - private ElementsShouldSatisfy(Object actual, List elementsNotSatisfyingRequirements) { - super("%n" + - "Expecting all elements of:%n" + - " <%s>%n" + - "to satisfy given requirements, but these elements did not:%n%n" + - describeErrors(elementsNotSatisfyingRequirements), - actual); + private ElementsShouldSatisfy(String message, Object actual, List elementsNotSatisfyingRequirements) { + super(message + describeErrors(elementsNotSatisfyingRequirements), actual); } private static String describeErrors(List elementsNotSatisfyingRequirements) { @@ -51,6 +47,10 @@ private static String describeErrors(List elementsNotSat .collect(joining(String.format("%n%n")))); } + public static UnsatisfiedRequirement unsatisfiedRequirement(Object elementNotSatisfyingRequirements, String errorMessage) { + return new ElementsShouldSatisfy.UnsatisfiedRequirement(elementNotSatisfyingRequirements, errorMessage); + } + public static class UnsatisfiedRequirement { private final Object elementNotSatisfyingRequirements; diff --git a/src/main/java/org/assertj/core/internal/Iterables.java b/src/main/java/org/assertj/core/internal/Iterables.java index bbc0dc8508..e01a9c2819 100644 --- a/src/main/java/org/assertj/core/internal/Iterables.java +++ b/src/main/java/org/assertj/core/internal/Iterables.java @@ -1160,17 +1160,14 @@ private Optional failsZipRequir public void assertAnySatisfy(AssertionInfo info, Iterable actual, Consumer requirements) { assertNotNull(info, actual); requireNonNull(requirements, "The Consumer expressing the assertions requirements must not be null"); - boolean anyMatch = stream(actual).anyMatch(e -> { - try { - requirements.accept(e); - } catch (AssertionError ex) { - return false; - } - return true; - }); - if (!anyMatch) { - throw failures.failure(info, elementsShouldSatisfyAny(actual)); + List unsatisfiedRequirements = stream(actual).map(element -> failsRequirements(requirements, element)) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(toList()); + if (unsatisfiedRequirements.size() == sizeOf(actual)) { + // all elements have failed the requirements! + throw failures.failure(info, elementsShouldSatisfyAny(actual, unsatisfiedRequirements)); } } diff --git a/src/main/java/org/assertj/core/internal/Maps.java b/src/main/java/org/assertj/core/internal/Maps.java index 5755ec466b..0dd28ee807 100644 --- a/src/main/java/org/assertj/core/internal/Maps.java +++ b/src/main/java/org/assertj/core/internal/Maps.java @@ -92,8 +92,7 @@ public static Maps instance() { Conditions conditions = Conditions.instance(); @VisibleForTesting - Maps() { - } + Maps() {} public void assertAllSatisfy(AssertionInfo info, Map actual, BiConsumer entryRequirements) { @@ -122,16 +121,16 @@ public void assertAnySatisfy(AssertionInfo info, Map actual, BiConsumer entryRequirements) { checkNotNull(entryRequirements, "The BiConsumer expressing the assertions requirements must not be null"); assertNotNull(info, actual); - boolean anyMatch = actual.entrySet().stream().anyMatch(e -> { - try { - entryRequirements.accept(e.getKey(), e.getValue()); - } catch (AssertionError ex) { - return false; - } - return true; - }); - if (!anyMatch) throw failures.failure(info, elementsShouldSatisfyAny(actual)); + List unsatisfiedRequirements = actual.entrySet().stream() + .map(entry -> failsRequirements(entryRequirements, entry)) + .filter(Optional::isPresent) + .map(Optional::get) + .collect(toList()); + if (unsatisfiedRequirements.size() == actual.size()) { + // all elements have failed the requirements! + throw failures.failure(info, elementsShouldSatisfyAny(actual, unsatisfiedRequirements)); + } } /** diff --git a/src/test/java/org/assertj/core/api/iterable/IterableAssert_anyMatch_Test.java b/src/test/java/org/assertj/core/api/iterable/IterableAssert_anyMatch_Test.java index 3aac37579f..c100529dfc 100644 --- a/src/test/java/org/assertj/core/api/iterable/IterableAssert_anyMatch_Test.java +++ b/src/test/java/org/assertj/core/api/iterable/IterableAssert_anyMatch_Test.java @@ -12,31 +12,32 @@ */ package org.assertj.core.api.iterable; +import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.verify; -import java.util.function.Predicate; +import java.util.function.Consumer; import org.assertj.core.api.ConcreteIterableAssert; import org.assertj.core.api.IterableAssertBaseTest; -import org.assertj.core.presentation.PredicateDescription; import org.junit.jupiter.api.BeforeEach; public class IterableAssert_anyMatch_Test extends IterableAssertBaseTest { - private Predicate predicate; + private Consumer restrictions; @BeforeEach - public void beforeOnce() { - predicate = o -> o != null; + public void beforeEach() { + restrictions = o -> assertThat(o).isNotNull(); } @Override protected ConcreteIterableAssert invoke_api_method() { - return assertions.anyMatch(predicate); + return assertions.anySatisfy(restrictions); } @Override protected void verify_internal_effects() { - verify(iterables).assertAnyMatch(getInfo(assertions), getActual(assertions), predicate, PredicateDescription.GIVEN); + verify(iterables).assertAnySatisfy(getInfo(assertions), getActual(assertions), restrictions); + } } diff --git a/src/test/java/org/assertj/core/api/iterable/IterableAssert_anySatisfy_Test.java b/src/test/java/org/assertj/core/api/iterable/IterableAssert_anySatisfy_Test.java new file mode 100644 index 0000000000..5c8232112d --- /dev/null +++ b/src/test/java/org/assertj/core/api/iterable/IterableAssert_anySatisfy_Test.java @@ -0,0 +1,42 @@ +/* + * 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 + * + * http://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. + * + * Copyright 2012-2018 the original author or authors. + */ +package org.assertj.core.api.iterable; + +import static org.mockito.Mockito.verify; + +import java.util.function.Predicate; + +import org.assertj.core.api.ConcreteIterableAssert; +import org.assertj.core.api.IterableAssertBaseTest; +import org.assertj.core.presentation.PredicateDescription; +import org.junit.jupiter.api.BeforeEach; + +public class IterableAssert_anySatisfy_Test extends IterableAssertBaseTest { + + private Predicate predicate; + + @BeforeEach + public void beforeOnce() { + predicate = o -> o != null; + } + + @Override + protected ConcreteIterableAssert invoke_api_method() { + return assertions.anyMatch(predicate); + } + + @Override + protected void verify_internal_effects() { + verify(iterables).assertAnyMatch(getInfo(assertions), getActual(assertions), predicate, PredicateDescription.GIVEN); + } +} diff --git a/src/test/java/org/assertj/core/error/ElementsShouldSatisfy_create_Test.java b/src/test/java/org/assertj/core/error/ElementsShouldSatisfy_create_Test.java index a7aa62316e..58f5f56b9f 100644 --- a/src/test/java/org/assertj/core/error/ElementsShouldSatisfy_create_Test.java +++ b/src/test/java/org/assertj/core/error/ElementsShouldSatisfy_create_Test.java @@ -16,6 +16,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.error.ElementsShouldSatisfy.elementsShouldSatisfy; import static org.assertj.core.error.ElementsShouldSatisfy.elementsShouldSatisfyAny; +import static org.assertj.core.error.ElementsShouldSatisfy.unsatisfiedRequirement; import static org.assertj.core.presentation.StandardRepresentation.STANDARD_REPRESENTATION; import static org.assertj.core.util.Lists.list; @@ -30,8 +31,8 @@ public class ElementsShouldSatisfy_create_Test { @Test public void should_create_error_message_all() { // GIVEN - List unsatisfiedRequirements = list(new UnsatisfiedRequirement("Leia", "Leia mistake."), - new UnsatisfiedRequirement("Luke", "Luke mistake.")); + List unsatisfiedRequirements = list(unsatisfiedRequirement("Leia", "Leia mistake."), + unsatisfiedRequirement("Luke", "Luke mistake.")); ErrorMessageFactory factory = elementsShouldSatisfy(list("Leia", "Luke", "Yoda"), unsatisfiedRequirements); // WHEN String message = factory.create(new TextDescription("Test"), STANDARD_REPRESENTATION); @@ -47,8 +48,8 @@ public void should_create_error_message_all() { @Test public void should_create_error_message_all_and_escape_percent_correctly() { // GIVEN - List unsatisfiedRequirements = list(new UnsatisfiedRequirement("Leia%s", "Leia mistake."), - new UnsatisfiedRequirement("Luke", "Luke mistake.")); + List unsatisfiedRequirements = list(unsatisfiedRequirement("Leia%s", "Leia mistake."), + unsatisfiedRequirement("Luke", "Luke mistake.")); ErrorMessageFactory factory = elementsShouldSatisfy(list("Leia%s", "Luke", "Yoda"), unsatisfiedRequirements); // WHEN String message = factory.create(new TextDescription("Test"), STANDARD_REPRESENTATION); @@ -64,26 +65,34 @@ public void should_create_error_message_all_and_escape_percent_correctly() { @Test public void should_create_error_message_any() { // GIVEN - ErrorMessageFactory factory = elementsShouldSatisfyAny(list("Luke", "Yoda")); + List unsatisfiedRequirements = list(unsatisfiedRequirement("Leia", "Leia mistake."), + unsatisfiedRequirement("Luke", "Luke mistake.")); + ErrorMessageFactory factory = elementsShouldSatisfyAny(list("Luke", "Yoda"), unsatisfiedRequirements); // WHEN String message = factory.create(new TextDescription("Test"), STANDARD_REPRESENTATION); // THEN assertThat(message).isEqualTo(format("[Test] %n" + "Expecting any element of:%n" + " <[\"Luke\", \"Yoda\"]>%n" + - "to satisfy the given assertions requirements but none did.")); + "to satisfy the given assertions requirements but none did:%n%n" + + " Leia mistake.%n%n" + + " Luke mistake.")); } @Test public void should_create_error_message_any_and_escape_percent_correctly() { // GIVEN - ErrorMessageFactory factory = elementsShouldSatisfyAny(list("Lu%dke", "Yoda")); + List unsatisfiedRequirements = list(unsatisfiedRequirement("Leia", "Leia mistake."), + unsatisfiedRequirement("Luke", "Luke mistake.")); + ErrorMessageFactory factory = elementsShouldSatisfyAny(list("Lu%dke", "Yoda"), unsatisfiedRequirements); // WHEN String message = factory.create(new TextDescription("Test"), STANDARD_REPRESENTATION); // THEN assertThat(message).isEqualTo(format("[Test] %n" + "Expecting any element of:%n" + " <[\"Lu%%dke\", \"Yoda\"]>%n" + - "to satisfy the given assertions requirements but none did.")); + "to satisfy the given assertions requirements but none did:%n%n" + + " Leia mistake.%n%n" + + " Luke mistake.")); } } diff --git a/src/test/java/org/assertj/core/internal/iterables/Iterables_assertAnySatisfy_Test.java b/src/test/java/org/assertj/core/internal/iterables/Iterables_assertAnySatisfy_Test.java index 2b0567dfae..db6ea4dcc4 100644 --- a/src/test/java/org/assertj/core/internal/iterables/Iterables_assertAnySatisfy_Test.java +++ b/src/test/java/org/assertj/core/internal/iterables/Iterables_assertAnySatisfy_Test.java @@ -12,18 +12,23 @@ */ package org.assertj.core.internal.iterables; +import static java.lang.String.format; 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.error.ElementsShouldSatisfy.elementsShouldSatisfyAny; +import static org.assertj.core.error.ElementsShouldSatisfy.unsatisfiedRequirement; import static org.assertj.core.test.TestData.someInfo; import static org.assertj.core.test.TestFailures.failBecauseExpectedAssertionErrorWasNotThrown; +import static org.assertj.core.util.AssertionsUtil.expectAssertionError; import static org.assertj.core.util.FailureMessages.actualIsNull; +import static org.assertj.core.util.Lists.emptyList; +import static org.assertj.core.util.Lists.list; import static org.assertj.core.util.Lists.newArrayList; import static org.mockito.Mockito.verify; import java.util.List; +import org.assertj.core.error.ElementsShouldSatisfy; import org.assertj.core.internal.IterablesBaseTest; import org.junit.jupiter.api.Test; @@ -60,7 +65,26 @@ iterables. assertAnySatisfy(someInfo(), actual, s -> { assertThat(s).contains("W"); }); } catch (AssertionError e) { - verify(failures).failure(info, elementsShouldSatisfyAny(actual)); + List errors = list(unsatisfiedRequirement("Luke", format("%n" + + "Expecting:%n" + + " <\"Luke\">%n" + + "to contain:%n" + + " <\"W\"> ")), + unsatisfiedRequirement("Leia", format("%n" + + "Expecting:%n" + + " <\"Leia\">%n" + + "to contain:%n" + + " <\"W\"> ")), + unsatisfiedRequirement("Yoda", format("%n" + + "Expecting:%n" + + " <\"Yoda\">%n" + + "to contain:%n" + + " <\"W\"> ")), + unsatisfiedRequirement("Obiwan", format("%n" + + "Expected size:<4> but was:<6> in:%n" + + + "<\"Obiwan\">"))); + verify(failures).failure(info, elementsShouldSatisfyAny(actual, errors)); return; } failBecauseExpectedAssertionErrorWasNotThrown(); @@ -72,7 +96,8 @@ public void should_fail_if_the_iterable_under_test_is_empty_whatever_the_asserti try { iterables. assertAnySatisfy(someInfo(), actual, $ -> assertThat(true).isTrue()); } catch (AssertionError e) { - verify(failures).failure(info, elementsShouldSatisfyAny(actual)); + List errors = emptyList(); + verify(failures).failure(info, elementsShouldSatisfyAny(actual, errors)); return; } failBecauseExpectedAssertionErrorWasNotThrown(); @@ -86,9 +111,9 @@ public void should_fail_if_consumer_is_null() { @Test public void should_fail_if_actual_is_null() { - assertThatExceptionOfType(AssertionError.class).isThrownBy(() ->{ - actual = null; - assertThat(actual).anySatisfy(null); - }).withMessage(actualIsNull()); + // WHEN + AssertionError error = expectAssertionError(() -> iterables.assertAnySatisfy(someInfo(), null, $ -> {})); + // THEN + assertThat(error).hasMessage(actualIsNull()); } -} \ No newline at end of file +} diff --git a/src/test/java/org/assertj/core/internal/maps/Maps_assertAnySatisfyingConsumer_Test.java b/src/test/java/org/assertj/core/internal/maps/Maps_assertAnySatisfyingConsumer_Test.java index eca7fe8fec..d87dd445e0 100644 --- a/src/test/java/org/assertj/core/internal/maps/Maps_assertAnySatisfyingConsumer_Test.java +++ b/src/test/java/org/assertj/core/internal/maps/Maps_assertAnySatisfyingConsumer_Test.java @@ -12,17 +12,23 @@ */ package org.assertj.core.internal.maps; +import static java.lang.String.format; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatNullPointerException; import static org.assertj.core.data.MapEntry.entry; import static org.assertj.core.error.ElementsShouldSatisfy.elementsShouldSatisfyAny; +import static org.assertj.core.error.ElementsShouldSatisfy.unsatisfiedRequirement; import static org.assertj.core.test.Maps.mapOf; 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.emptyList; +import static org.assertj.core.util.Lists.list; +import java.util.List; import java.util.Map; +import org.assertj.core.error.ElementsShouldSatisfy; import org.assertj.core.internal.MapsBaseTest; import org.assertj.core.test.Player; import org.junit.jupiter.api.BeforeEach; @@ -55,7 +61,7 @@ public void should_fail_if_the_map_under_test_is_empty_whatever_the_assertions_r AssertionError error = expectAssertionError(() -> maps.assertAnySatisfy(someInfo(), actual, ($1, $2) -> assertThat(true).isTrue())); // THEN - assertThat(error).hasMessage(elementsShouldSatisfyAny(actual).create()); + assertThat(error).hasMessage(elementsShouldSatisfyAny(actual, emptyList()).create()); } @Test @@ -64,7 +70,21 @@ public void should_fail_if_no_entry_satisfies_the_given_requirements() { AssertionError error = expectAssertionError(() -> maps.assertAnySatisfy(someInfo(), actual, ($1, $2) -> assertThat(true).isFalse())); // THEN - assertThat(error).hasMessage(elementsShouldSatisfyAny(actual).create()); + List errors = list(unsatisfiedRequirement("name=Yoda", + format("%n" + + "Expecting:%n" + + " %n" + + "to be equal to:%n" + + " %n" + + "but was not.")), + unsatisfiedRequirement("color=green", + format("%n" + + "Expecting:%n" + + " %n" + + "to be equal to:%n" + + " %n" + + "but was not."))); + assertThat(error).hasMessage(elementsShouldSatisfyAny(actual, errors).create()); } @Test