8000 Enhance `IterableSubject.containsAtLeastElementsIn().inOrder()` to pr… · google/truth@9da7dd1 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9da7dd1

Browse files
chaorenGoogle Java Core Libraries
authored andcommitted
Enhance IterableSubject.containsAtLeastElementsIn().inOrder() to print 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
1 parent a8c71f8 commit 9da7dd1

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

core/src/main/java/com/google/common/truth/IterableSubject.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -298,22 +298,22 @@ public final Ordered containsAtLeastElementsIn(Iterable<?> expectedIterable) {
298298
return failAtLeast(expected, missing);
299299
}
300300

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

844-
/** @deprecated You probably meant to call {@link #containsNoneOf} instead. */
844+
/**
845+
* @deprecated You probably meant to call {@link #containsNoneOf} instead.
846+
*/
845847
@Override
846848
@Deprecated
847849
public void isNoneOf(
848850
@Nullable Object first, @Nullable Object second, @Nullable Object @Nullable ... rest) {
849851
super.isNoneOf(first, second, rest);
850852
}
851853

852-
/** @deprecated You probably meant to call {@link #containsNoneIn} instead. */
854+
/**
855+
* @deprecated You probably meant to call {@link #containsNoneIn} instead.
856+
*/
853857
@Override
854858
@Deprecated
855859
public void isNotIn(Iterable<?> iterable) {
@@ -1955,8 +1959,7 @@ private final class Pairer {
19551959
* Returns a {@link Pairing} of the given expected and actual values, or {@code null} if the
19561960
* expected values are not uniquely keyed.
19571961
*/
1958-
@Nullable
1959-
Pairing pair(
1962+
@Nullable Pairing pair(
19601963
List<? extends E> expectedValues,
19611964
List<? extends A> actualValues,
19621965
Correspondence.ExceptionStore exceptions) {

core/src/test/java/com/google/common/truth/IterableSubjectTest.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,20 @@ public void iterableContainsAtLeastInOrderWithFailure() {
390390
"expected order for required elements",
391391
"but was");
392392
assertFailureValue("expected order for required elements", "[null, 1, 3]");
393+
assertFailureValue("but was", "[1, null, 3]");
394+
}
395+
396+
@Test
397+
public void iterableContainsAtLeastInOrderWithFailureWithActualOrder() {
398+
expectFailureWhenTestingThat(asList(1, 2, null, 3, 4)).containsAtLeast(null, 1, 3).inOrder();
399+
assertFailureKeys(
400+
"required elements were all found, but order was wrong",
401+
"expected order for required elements",
402+
"but order was",
403+
"full contents");
404+
assertFailureValue("expected order for required elements", "[null, 1, 3]");
405+
assertFailureValue("but order was", "[1, null, 3]");
406+
assertFailureValue("full contents", "[1, 2, null, 3, 4]");
393407
}
394408

395409
@Test

0 commit comments

Comments
 (0)
0