Skip to content

Commit

Permalink
Enhance IterableSubject.containsAtLeastElementsIn().inOrder() to pr…
Browse files Browse the repository at this point in the history
…int an extra line that shows only the expected elements in their actual order.

But only if the full contents contains elements that are not required.

Fixes #599, #974.

RELNOTES=Enhanced `IterableSubject.containsAtLeastElementsIn().inOrder()` to print an extra line that shows only the expected elements in their actual order.
PiperOrigin-RevId: 446983641
  • Loading branch information
chaoren authored and Google Java Core Libraries committed May 6, 2022
1 parent a8c71f8 commit 9da7dd1
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
33 changes: 18 additions & 15 deletions core/src/main/java/com/google/common/truth/IterableSubject.java
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
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

0 comments on commit 9da7dd1

Please sign in to comment.