From 306ee469e20d4aae8ed439dd214b663e9f68017e Mon Sep 17 00:00:00 2001 From: kak Date: Tue, 17 Nov 2015 14:23:58 -0800 Subject: [PATCH] Add MultimapSubject.containsExactlyEntriesIn(Multimap). containsExactly(Multimap) will be deprecated and removed. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=108079017 --- .../common/truth/ListMultimapSubject.java | 4 +-- .../google/common/truth/MultimapSubject.java | 25 +++++++++++---- .../common/truth/SetMultimapSubject.java | 4 +-- .../com/google/common/truth/MultimapTest.java | 32 +++++++++---------- 4 files changed, 39 insertions(+), 26 deletions(-) diff --git a/core/src/main/java/com/google/common/truth/ListMultimapSubject.java b/core/src/main/java/com/google/common/truth/ListMultimapSubject.java index 0752be37d..a35305f64 100644 --- a/core/src/main/java/com/google/common/truth/ListMultimapSubject.java +++ b/core/src/main/java/com/google/common/truth/ListMultimapSubject.java @@ -46,8 +46,8 @@ ListMultimapSubject, K, V, M> create( * @deprecated {@code #isEqualTo} A SetMultimap can never compare equal with a ListMultimap if * either Multimap is non-empty, because {@link java.util.Set} and {@link List} can never * compare equal. Prefer - * {@link MultimapSubject#containsExactly(com.google.common.collect.Multimap)} instead. - * Consult {@link com.google.common.collect.Multimap#equals} for more information. + * {@link MultimapSubject#containsExactlyEntriesIn(com.google.common.collect.Multimap)} + * instead. Consult {@link com.google.common.collect.Multimap#equals} for more information. */ @Deprecated public void isEqualTo(@Nullable SetMultimap other) { diff --git a/core/src/main/java/com/google/common/truth/MultimapSubject.java b/core/src/main/java/com/google/common/truth/MultimapSubject.java index c9a916d86..4259f1590 100644 --- a/core/src/main/java/com/google/common/truth/MultimapSubject.java +++ b/core/src/main/java/com/google/common/truth/MultimapSubject.java @@ -155,14 +155,15 @@ public void isEqualTo(@Nullable Object other) { } else { if (getSubject() instanceof ListMultimap) { // If we're comparing ListMultimaps, check for order - containsExactly((Multimap) other).inOrder(); + containsExactlyEntriesIn((Multimap) other).inOrder(); } else if (getSubject() instanceof SetMultimap) { // If we're comparing SetMultimaps, don't check for order - containsExactly((Multimap) other); + containsExactlyEntriesIn((Multimap) other); } - // This statement should generally never be reached because one of the two containsExactly - // calls above should throw an exception. It'll only be reached if we're looking at a - // non-ListMultimap and non-SetMultimap (e.g., a custom Multimap implementation). + // This statement should generally never be reached because one of the two + // containsExactlyEntriesIn calls above should throw an exception. It'll only be reached if + // we're looking at a non-ListMultimap and non-SetMultimap + // (e.g., a custom Multimap implementation). fail("is equal to", other); } } @@ -175,11 +176,23 @@ public void isEqualTo(@Nullable Object other) { * that the two Multimaps iterate fully in the same order. That is, their key sets iterate * in the same order, and the value collections for each key iterate in the same order. */ - public Ordered containsExactly(Multimap expectedMultimap) { + public Ordered containsExactlyEntriesIn(Multimap expectedMultimap) { checkNotNull(expectedMultimap, "expectedMultimap"); return containsExactly("contains exactly", expectedMultimap); } + /** + * Fails if the Multimap does not contain precisely the same entries as the argument Multimap. + * + *

A subsequent call to {@link Ordered#inOrder} may be made if the caller wishes to verify + * that the two Multimaps iterate fully in the same order. That is, their key sets iterate + * in the same order, and the value collections for each key iterate in the same order. + */ + // TODO(b/25742898): @deprecated Use {@link #containsExactlyEntiesIn} instead. + public Ordered containsExactly(Multimap expectedMultimap) { + return containsExactlyEntriesIn(expectedMultimap); + } + private Ordered containsExactly(String failVerb, Multimap expectedMultimap) { Multimap missing = difference(expectedMultimap, getSubject()); Multimap extra = difference(getSubject(), expectedMultimap); diff --git a/core/src/main/java/com/google/common/truth/SetMultimapSubject.java b/core/src/main/java/com/google/common/truth/SetMultimapSubject.java index 5723da7b0..d87edc21f 100644 --- a/core/src/main/java/com/google/common/truth/SetMultimapSubject.java +++ b/core/src/main/java/com/google/common/truth/SetMultimapSubject.java @@ -46,8 +46,8 @@ SetMultimapSubject, K, V, M> create( * @deprecated {@code #isEqualTo} A ListMultimap can never compare equal with a SetMultimap if * either Multimap is non-empty, because {@link java.util.List} and {@link java.util.Set} * can never compare equal. Prefer - * {@link MultimapSubject#containsExactly(com.google.common.collect.Multimap)} instead. - * Consult {@link com.google.common.collect.Multimap#equals} for more information. + * {@link MultimapSubject#containsExactlyEntriesIn(com.google.common.collect.Multimap)} + * instead. Consult {@link com.google.common.collect.Multimap#equals} for more information. */ @Deprecated public void isEqualTo(@Nullable ListMultimap other) { diff --git a/core/src/test/java/com/google/common/truth/MultimapTest.java b/core/src/test/java/com/google/common/truth/MultimapTest.java index b1ab93969..810613abd 100644 --- a/core/src/test/java/com/google/common/truth/MultimapTest.java +++ b/core/src/test/java/com/google/common/truth/MultimapTest.java @@ -324,12 +324,12 @@ public void valuesForKeyListMultimap() { } @Test - public void containsExactly() { + public void containsExactlyEntriesIn() { ImmutableListMultimap listMultimap = ImmutableListMultimap.of(3, "one", 3, "six", 3, "two", 4, "five", 4, "four"); ImmutableSetMultimap setMultimap = ImmutableSetMultimap.copyOf(listMultimap); - assertThat(listMultimap).containsExactly(setMultimap); + assertThat(listMultimap).containsExactlyEntriesIn(setMultimap); } @Test @@ -337,8 +337,8 @@ public void containsExactlyEmpty() { ImmutableListMultimap actual = ImmutableListMultimap.of(); ImmutableSetMultimap expected = ImmutableSetMultimap.of(); - assertThat(actual).containsExactly(expected); - assertThat(actual).containsExactly(expected).inOrder(); + assertThat(actual).containsExactlyEntriesIn(expected); + assertThat(actual).containsExactlyEntriesIn(expected).inOrder(); } @Test @@ -347,7 +347,7 @@ public void containsExactlyRejectsNull() { ImmutableMultimap.of(3, "one", 3, "six", 3, "two", 4, "five", 4, "four"); try { - assertThat(multimap).containsExactly(null); + assertThat(multimap).containsExactlyEntriesIn(null); fail("Should have thrown."); } catch (NullPointerException expected) { } @@ -360,7 +360,7 @@ public void containsExactlyRespectsDuplicates() { ImmutableListMultimap expected = ImmutableListMultimap.of(3, "two", 4, "five", 3, "one", 4, "five", 3, "one"); - assertThat(actual).containsExactly(expected); + assertThat(actual).containsExactlyEntriesIn(expected); } @Test @@ -370,7 +370,7 @@ public void containsExactlyRespectsDuplicatesFailure() { ImmutableSetMultimap expected = ImmutableSetMultimap.copyOf(actual); try { - assertThat(actual).containsExactly(expected); + assertThat(actual).containsExactlyEntriesIn(expected); fail("Should have thrown."); } catch (AssertionError e) { assertThat(e) @@ -392,7 +392,7 @@ public void containsExactlyFailureMissing() { actual.remove(4, "five"); try { - assertThat(actual).containsExactly(expected); + assertThat(actual).containsExactlyEntriesIn(expected); fail("Should have thrown."); } catch (AssertionError e) { assertThat(e) @@ -414,7 +414,7 @@ public void containsExactlyFailureExtra() { actual.put(5, "eight"); try { - assertThat(actual).containsExactly(expected); + assertThat(actual).containsExactlyEntriesIn(expected); fail("Should have thrown."); } catch (AssertionError e) { assertThat(e) @@ -438,7 +438,7 @@ public void containsExactlyFailureBoth() { actual.put(5, "eight"); try { - assertThat(actual).containsExactly(expected); + assertThat(actual).containsExactlyEntriesIn(expected); fail("Should have thrown."); } catch (AssertionError e) { assertThat(e) @@ -459,7 +459,7 @@ public void containsExactlyInOrder() { ImmutableMultimap expected = ImmutableMultimap.of(3, "one", 3, "six", 3, "two", 4, "five", 4, "four"); - assertThat(actual).containsExactly(expected).inOrder(); + assertThat(actual).containsExactlyEntriesIn(expected).inOrder(); } @Test @@ -468,7 +468,7 @@ public void containsExactlyInOrderDifferentTypes() { ImmutableListMultimap.of(3, "one", 3, "six", 3, "two", 4, "five", 4, "four"); ImmutableSetMultimap setMultimap = ImmutableSetMultimap.copyOf(listMultimap); - assertThat(listMultimap).containsExactly(setMultimap).inOrder(); + assertThat(listMultimap).containsExactlyEntriesIn(setMultimap).inOrder(); } @Test @@ -478,9 +478,9 @@ public void containsExactlyInOrderFailure() { ImmutableMultimap expected = ImmutableMultimap.of(4, "four", 3, "six", 4, "five", 3, "two", 3, "one"); - assertThat(actual).containsExactly(expected); + assertThat(actual).containsExactlyEntriesIn(expected); try { - assertThat(actual).containsExactly(expected).inOrder(); + assertThat(actual).containsExactlyEntriesIn(expected).inOrder(); fail("Should have thrown."); } catch (AssertionError e) { assertThat(e.getMessage()) @@ -497,9 +497,9 @@ public void containsExactlyInOrderFailureValuesOnly() { ImmutableMultimap expected = ImmutableMultimap.of(3, "six", 3, "two", 3, "one", 4, "five", 4, "four"); - assertThat(actual).containsExactly(expected); + assertThat(actual).containsExactlyEntriesIn(expected); try { - assertThat(actual).containsExactly(expected).inOrder(); + assertThat(actual).containsExactlyEntriesIn(expected).inOrder(); fail("Should have thrown."); } catch (AssertionError e) { assertThat(e)