Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhance IterableSubject.containsAtLeastElementsIn().inOrder() to print an extra line that shows only the expected elements in their actual order. #982

Merged
merged 1 commit into from
May 6, 2022
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
33 changes: 18 additions & 15 deletions core/src/main/java/com/google/common/truth/IterableSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -298,22 +298,22 @@ public final Ordered containsAtLeastElementsIn(Iterable<?> expectedIterable) {
return failAtLeast(expected, missing);
}

/*
* TODO(cpovirk): In the NotInOrder case, also include a Fact that shows _only_ the required
* elements (that is, without any extras) but in the order they were actually found. That should
* make it easier for users to compare the actual order of the required elements to the expected
* order. Or, if that's too much trouble, at least try to find a better title for the full
* actual iterable than the default of "but was," which may _sound_ like it should show only the
* required elements, rather than the full actual iterable.
*/
return ordered
? IN_ORDER
: new Ordered() {
@Override
public void inOrder() {
failWithActual(
simpleFact("required elements were all found, but order was wrong"),
fact("expected order for required elements", expected));
ImmutableList.Builder<Fact> facts = ImmutableList.builder();
facts.add(simpleFact("required elements were all found, but order was wrong"));
facts.add(fact("expected order for required elements", expected));
List<Object> actualOrder = Lists.newArrayList(IterableSubject.this.actual);
if (actualOrder.retainAll(expected)) {
facts.add(fact("but order was", actualOrder));
facts.add(fullContents());
failWithoutActual(facts.build());
} else {
failWithActual(facts.build());
}
}
};
}
Expand Down Expand Up @@ -841,15 +841,19 @@ private void pairwiseCheck(String expectedFact, PairwiseChecker checker) {
}
}

/** @deprecated You probably meant to call {@link #containsNoneOf} instead. */
/**
* @deprecated You probably meant to call {@link #containsNoneOf} instead.
*/
@Override
@Deprecated
public void isNoneOf(
@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest) {
super.isNoneOf(first, second, rest);
}

/** @deprecated You probably meant to call {@link #containsNoneIn} instead. */
/**
* @deprecated You probably meant to call {@link #containsNoneIn} instead.
*/
@Override
@Deprecated
public void isNotIn(Iterable<?> iterable) {
Expand Down Expand Up @@ -1955,8 +1959,7 @@ private final class Pairer {
* Returns a {@link Pairing} of the given expected and actual values, or {@code null} if the
* expected values are not uniquely keyed.
*/
@Nullable
Pairing pair(
@Nullable Pairing pair(
List<? extends E> expectedValues,
List<? extends A> actualValues,
Correspondence.ExceptionStore exceptions) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,20 @@ public void iterableContainsAtLeastInOrderWithFailure() {
"expected order for required elements",
"but was");
assertFailureValue("expected order for required elements", "[null, 1, 3]");
assertFailureValue("but was", "[1, null, 3]");
}

@Test
public void iterableContainsAtLeastInOrderWithFailureWithActualOrder() {
expectFailureWhenTestingThat(asList(1, 2, null, 3, 4)).containsAtLeast(null, 1, 3).inOrder();
assertFailureKeys(
"required elements were all found, but order was wrong",
"expected order for required elements",
"but order was",
"full contents");
assertFailureValue("expected order for required elements", "[null, 1, 3]");
assertFailureValue("but order was", "[1, null, 3]");
assertFailureValue("full contents", "[1, 2, null, 3, 4]");
}

@Test
Expand Down