/** * Asserts that the given {@code Iterable} contains only the given values and nothing else, in any order. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param values the values that are expected to be in 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} does not contain the given values or if the given * {@code Iterable} contains values that are not in the given array. */ public void assertContainsOnly(AssertionInfo info, Iterable<?> actual, Object[] values) { if (commonCheckThatIterableAssertionSucceeds(info, actual, values)) return; // check for elements in values that are missing in actual. Set<Object> notExpected = setFromIterable(actual); Set<Object> notFound = containsOnly(notExpected, values); if (notExpected.isEmpty() && notFound.isEmpty()) { return; } throw failures.failure(info, shouldContainOnly(actual, values, notFound, notExpected, comparisonStrategy)); }
/** * Asserts that the given {@code Iterable} contains exactly the given values and nothing else, <b>in order</b>. * * @param info contains information about the assertion. * @param actual the given {@code Iterable}. * @param values the values that are expected to be in the given {@code Iterable} in order. * @throws NullPointerException if the array of values is {@code null}. * @throws AssertionError if the given {@code Iterable} is {@code null}. * @throws AssertionError if the given {@code Iterable} does not contain the given values or if the given * {@code Iterable} contains values that are not in the given array, in order. */ public void assertContainsExactly(AssertionInfo info, Iterable<?> actual, Object[] values) { checkIsNotNull(values); assertHasSameSizeAs(info, actual, values); // include check that actual is not null Set<Object> notExpected = setFromIterable(actual); Set<Object> notFound = containsOnly(notExpected, values); if (notExpected.isEmpty() && notFound.isEmpty()) { // actual and values have the same elements but are they in the same order. int i = 0; for (Object elementFromActual : actual) { if (!areEqual(elementFromActual, values[i])) { throw failures.failure(info, shouldContainExactly(elementFromActual, values[i], i, comparisonStrategy)); } i++; } return; } throw failures.failure(info, shouldContainExactly(actual, values, notFound, notExpected, comparisonStrategy)); }