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)); }
private void checkNotNullIterables(AssertionInfo info, Iterable<?> actual, Object[] sequence) { checkIsNotNull(sequence); assertNotNull(info, actual); }
public <E> void assertAllMatch(AssertionInfo info, Iterable<? extends E> actual, Predicate<? super E> predicate, PredicateDescription predicateDescription) { assertNotNull(info, actual); predicates.assertIsNotNull(predicate); List<? extends E> nonMatches = stream(actual).filter(predicate.negate()).collect(toList()); if (!nonMatches.isEmpty()) { throw failures.failure(info, elementsShouldMatch(actual, nonMatches.size() == 1 ? nonMatches.get(0) : nonMatches, predicateDescription)); } }
public <E> void assertAllSatisfy(AssertionInfo info, Iterable<? extends E> actual, Consumer<? super E> requirements) { assertNotNull(info, actual); requireNonNull(requirements, "The Consumer<T> expressing the assertions requirements must not be null"); List<UnsatisfiedRequirement> unsatisfiedRequirements = stream(actual).map(element -> failsRequirements(requirements, element)) .filter(Optional::isPresent) .map(Optional::get) .collect(toList()); if (!unsatisfiedRequirements.isEmpty()) throw failures.failure(info, elementsShouldSatisfy(actual, unsatisfiedRequirements)); }
public <ACTUAL_ELEMENT, OTHER_ELEMENT> void assertZipSatisfy(AssertionInfo info, Iterable<? extends ACTUAL_ELEMENT> actual, Iterable<OTHER_ELEMENT> other, BiConsumer<? super ACTUAL_ELEMENT, OTHER_ELEMENT> zipRequirements) { assertNotNull(info, actual); requireNonNull(zipRequirements, "The BiConsumer expressing the assertions requirements must not be null"); requireNonNull(other, "The iterable to zip actual with must not be null"); assertHasSameSizeAs(info, actual, other); Iterator<OTHER_ELEMENT> otherIterator = other.iterator(); List<ZipSatisfyError> errors = stream(actual).map(actualElement -> failsZipRequirements(actualElement, otherIterator.next(), zipRequirements)) .filter(Optional::isPresent) .map(Optional::get) .collect(toList()); if (!errors.isEmpty()) throw failures.failure(info, zippedElementsShouldSatisfy(actual, other, errors)); }
public <E> void assertAnySatisfy(AssertionInfo info, Iterable<? extends E> actual, Consumer<? super E> requirements) { assertNotNull(info, actual); requireNonNull(requirements, "The Consumer<T> 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)); } }
public <E> void assertAnyMatch(AssertionInfo info, Iterable<? extends E> actual, Predicate<? super E> predicate, PredicateDescription predicateDescription) { assertNotNull(info, actual); predicates.assertIsNotNull(predicate); stream(actual).filter(predicate) .findFirst() .orElseGet(() -> { throw failures.failure(info, anyElementShouldMatch(actual, predicateDescription)); }); }
/** * Assert that the actual {@code Iterable} has the same size as the other {@code Iterable}. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param other the given {@code Iterable}. * @throws AssertionError if the actual group is {@code null}. * @throws AssertionError if the other group is {@code null}. * @throws AssertionError if actual and other {@code Iterable} don't have the same size. */ public void assertHasSameSizeAs(AssertionInfo info, Iterable<?> actual, Iterable<?> other) { assertNotNull(info, actual); hasSameSizeAsCheck(info, actual, other, sizeOf(actual)); }
public <E> void assertNoneMatch(AssertionInfo info, Iterable<? extends E> actual, Predicate<? super E> predicate, PredicateDescription predicateDescription) { assertNotNull(info, actual); predicates.assertIsNotNull(predicate); stream(actual).filter(predicate) .findFirst() .ifPresent(e -> { throw failures.failure(info, noElementsShouldMatch(actual, e, predicateDescription)); }); }
/** * Asserts that the number of elements in the given {@code Iterable} is equal to the expected one. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param expectedSize the expected size of {@code actual}. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the number of elements in the given {@code Iterable} is different than the expected one. */ public void assertHasSize(AssertionInfo info, Iterable<?> actual, int expectedSize) { assertNotNull(info, actual); checkSizes(actual, sizeOf(actual), expectedSize, info); }
/** * Asserts that the given {@code Iterable} contains all the elements of the other {@code Iterable}, in any order. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param other the other {@code Iterable}. * @throws NullPointerException if {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} does not contain all the elements of the other * {@code Iterable}, in any order. */ public void assertContainsAll(AssertionInfo info, Iterable<?> actual, Iterable<?> other) { if (other == null) throw iterableToLookForIsNull(); assertNotNull(info, actual); Object[] values = newArrayList(other).toArray(); assertIterableContainsGivenValues(actual, values, info); }
/** * Asserts that the given {@code Iterable} contains at least a null element. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} does not contain at least a null element. */ public void assertContainsNull(AssertionInfo info, Iterable<?> actual) { assertNotNull(info, actual); if (!iterableContains(actual, null)) throw failures.failure(info, shouldContainNull(actual)); }
/** * Verifies that the actual <code>Iterable</code> is a subset of values <code>Iterable</code>. <br> * Both actual and given iterable are treated as sets, therefore duplicates on either of them are ignored. * * @param info contains information about the assertion. * @param actual the actual {@code Iterable}. * @param values the {@code Iterable} that should contain all actual elements. * @throws AssertionError if the actual {@code Iterable} is {@code null}. * @throws NullPointerException if the given Iterable is {@code null}. * @throws AssertionError if the actual {@code Iterable} is not subset of set <code>{@link Iterable}</code> */ public void assertIsSubsetOf(AssertionInfo info, Iterable<?> actual, Iterable<?> values) { assertNotNull(info, actual); checkIterableIsNotNull(info, values); List<Object> extra = stream(actual).filter(actualElement -> !iterableContains(values, actualElement)) .collect(toList()); if (extra.size() > 0) throw failures.failure(info, shouldBeSubsetOf(actual, values, extra, comparisonStrategy)); }
/** * Asserts that the given {@code Iterable} is empty. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} is not empty. */ public void assertEmpty(AssertionInfo info, Iterable<?> actual) { assertNotNull(info, actual); if (!isNullOrEmpty(actual)) throw failures.failure(info, shouldBeEmpty(actual)); }
/** * Asserts that the given {@code Iterable} is not empty. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} is empty. */ public void assertNotEmpty(AssertionInfo info, Iterable<?> actual) { assertNotNull(info, actual); if (isNullOrEmpty(actual)) throw failures.failure(info, shouldNotBeEmpty()); }
/** * Asserts that the given {@code Iterable} does not contain null elements. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} contains a null element. */ public void assertDoesNotContainNull(AssertionInfo info, Iterable<?> actual) { assertNotNull(info, actual); if (iterableContains(actual, null)) throw failures.failure(info, shouldNotContainNull(actual)); }
/** * Asserts that the given {@code Iterable} contains only null elements and nothing else. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} does not contain at least a null element or if the given * {@code Iterable} contains values that are not null elements. */ public void assertContainsOnlyNulls(AssertionInfo info, Iterable<?> actual) { assertNotNull(info, actual); // empty => no null elements => failure if (sizeOf(actual) == 0) throw failures.failure(info, shouldContainOnlyNulls(actual)); // look for any non null elements List<Object> nonNullElements = stream(actual).filter(java.util.Objects::nonNull).collect(toList()); if (nonNullElements.size() > 0) throw failures.failure(info, shouldContainOnlyNulls(actual, nonNullElements)); }
/** * Assert that the actual {@code Iterable} has the same size as the other array. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param other the given array to compare. * @throws AssertionError if the actual group is {@code null}. * @throws AssertionError if the other group is {@code null}. * @throws AssertionError if actual {@code Iterable} and other array don't have the same size. */ public void assertHasSameSizeAs(AssertionInfo info, Iterable<?> actual, Object other) { assertNotNull(info, actual); assertIsArray(info, other); hasSameSizeAsCheck(info, actual, other, sizeOf(actual)); }
/** * Asserts that the given {@code Iterable} does not have duplicate values. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @throws NullPointerException if the array of values is {@code null}. * @throws IllegalArgumentException if the array of values is empty. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} contains duplicate values. */ public void assertDoesNotHaveDuplicates(AssertionInfo info, Iterable<?> actual) { assertNotNull(info, actual); Iterable<?> duplicates = comparisonStrategy.duplicatesFrom(actual); if (!isNullOrEmpty(duplicates)) throw failures.failure(info, shouldNotHaveDuplicates(actual, duplicates, comparisonStrategy)); }
public void assertContainsExactlyInAnyOrder(AssertionInfo info, Iterable<?> actual, Object[] values) { checkIsNotNull(values); assertNotNull(info, actual); List<Object> notExpected = newArrayList(actual); List<Object> notFound = newArrayList(values); for (Object value : values) { if (iterableContains(notExpected, value)) { iterablesRemoveFirst(notExpected, value); iterablesRemoveFirst(notFound, value); } } if (notExpected.isEmpty() && notFound.isEmpty()) return; throw failures.failure(info, shouldContainExactlyInAnyOrder(actual, values, notFound, notExpected, comparisonStrategy)); }