Skip to content

Commit

Permalink
Add MultimapSubject.containsExactlyEntriesIn(Multimap).
Browse files Browse the repository at this point in the history
containsExactly(Multimap) will be deprecated and removed.
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=108079017
  • Loading branch information
kluever authored and cpovirk committed Nov 19, 2015
1 parent 7574095 commit 306ee46
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 26 deletions.
Expand Up @@ -46,8 +46,8 @@ ListMultimapSubject<? extends ListMultimapSubject<?, K, V, M>, K, V, M> create(
* @deprecated {@code #isEqualTo} A SetMultimap can never compare equal with a ListMultimap if * @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 * either Multimap is non-empty, because {@link java.util.Set} and {@link List} can never
* compare equal. Prefer * compare equal. Prefer
* {@link MultimapSubject#containsExactly(com.google.common.collect.Multimap)} instead. * {@link MultimapSubject#containsExactlyEntriesIn(com.google.common.collect.Multimap)}
* Consult {@link com.google.common.collect.Multimap#equals} for more information. * instead. Consult {@link com.google.common.collect.Multimap#equals} for more information.
*/ */
@Deprecated @Deprecated
public void isEqualTo(@Nullable SetMultimap<?, ?> other) { public void isEqualTo(@Nullable SetMultimap<?, ?> other) {
Expand Down
25 changes: 19 additions & 6 deletions core/src/main/java/com/google/common/truth/MultimapSubject.java
Expand Up @@ -155,14 +155,15 @@ public void isEqualTo(@Nullable Object other) {
} else { } else {
if (getSubject() instanceof ListMultimap) { if (getSubject() instanceof ListMultimap) {
// If we're comparing ListMultimaps, check for order // If we're comparing ListMultimaps, check for order
containsExactly((Multimap<?, ?>) other).inOrder(); containsExactlyEntriesIn((Multimap<?, ?>) other).inOrder();
} else if (getSubject() instanceof SetMultimap) { } else if (getSubject() instanceof SetMultimap) {
// If we're comparing SetMultimaps, don't check for order // 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 // This statement should generally never be reached because one of the two
// calls above should throw an exception. It'll only be reached if we're looking at a // containsExactlyEntriesIn calls above should throw an exception. It'll only be reached if
// non-ListMultimap and non-SetMultimap (e.g., a custom Multimap implementation). // we're looking at a non-ListMultimap and non-SetMultimap
// (e.g., a custom Multimap implementation).
fail("is equal to", other); fail("is equal to", other);
} }
} }
Expand All @@ -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 * 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. * 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"); checkNotNull(expectedMultimap, "expectedMultimap");
return containsExactly("contains exactly", expectedMultimap); return containsExactly("contains exactly", expectedMultimap);
} }


/**
* Fails if the Multimap does not contain precisely the same entries as the argument Multimap.
*
* <p>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) { private Ordered containsExactly(String failVerb, Multimap<?, ?> expectedMultimap) {
Multimap<?, ?> missing = difference(expectedMultimap, getSubject()); Multimap<?, ?> missing = difference(expectedMultimap, getSubject());
Multimap<?, ?> extra = difference(getSubject(), expectedMultimap); Multimap<?, ?> extra = difference(getSubject(), expectedMultimap);
Expand Down
Expand Up @@ -46,8 +46,8 @@ SetMultimapSubject<? extends SetMultimapSubject<?, K, V, M>, K, V, M> create(
* @deprecated {@code #isEqualTo} A ListMultimap can never compare equal with a SetMultimap if * @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} * either Multimap is non-empty, because {@link java.util.List} and {@link java.util.Set}
* can never compare equal. Prefer * can never compare equal. Prefer
* {@link MultimapSubject#containsExactly(com.google.common.collect.Multimap)} instead. * {@link MultimapSubject#containsExactlyEntriesIn(com.google.common.collect.Multimap)}
* Consult {@link com.google.common.collect.Multimap#equals} for more information. * instead. Consult {@link com.google.common.collect.Multimap#equals} for more information.
*/ */
@Deprecated @Deprecated
public void isEqualTo(@Nullable ListMultimap<?, ?> other) { public void isEqualTo(@Nullable ListMultimap<?, ?> other) {
Expand Down
32 changes: 16 additions & 16 deletions core/src/test/java/com/google/common/truth/MultimapTest.java
Expand Up @@ -324,21 +324,21 @@ public void valuesForKeyListMultimap() {
} }


@Test @Test
public void containsExactly() { public void containsExactlyEntriesIn() {
ImmutableListMultimap<Integer, String> listMultimap = ImmutableListMultimap<Integer, String> listMultimap =
ImmutableListMultimap.of(3, "one", 3, "six", 3, "two", 4, "five", 4, "four"); ImmutableListMultimap.of(3, "one", 3, "six", 3, "two", 4, "five", 4, "four");
ImmutableSetMultimap<Integer, String> setMultimap = ImmutableSetMultimap.copyOf(listMultimap); ImmutableSetMultimap<Integer, String> setMultimap = ImmutableSetMultimap.copyOf(listMultimap);


assertThat(listMultimap).containsExactly(setMultimap); assertThat(listMultimap).containsExactlyEntriesIn(setMultimap);
} }


@Test @Test
public void containsExactlyEmpty() { public void containsExactlyEmpty() {
ImmutableListMultimap<Integer, String> actual = ImmutableListMultimap.of(); ImmutableListMultimap<Integer, String> actual = ImmutableListMultimap.of();
ImmutableSetMultimap<Integer, String> expected = ImmutableSetMultimap.of(); ImmutableSetMultimap<Integer, String> expected = ImmutableSetMultimap.of();


assertThat(actual).containsExactly(expected); assertThat(actual).containsExactlyEntriesIn(expected);
assertThat(actual).containsExactly(expected).inOrder(); assertThat(actual).containsExactlyEntriesIn(expected).inOrder();
} }


@Test @Test
Expand All @@ -347,7 +347,7 @@ public void containsExactlyRejectsNull() {
ImmutableMultimap.of(3, "one", 3, "six", 3, "two", 4, "five", 4, "four"); ImmutableMultimap.of(3, "one", 3, "six", 3, "two", 4, "five", 4, "four");


try { try {
assertThat(multimap).containsExactly(null); assertThat(multimap).containsExactlyEntriesIn(null);
fail("Should have thrown."); fail("Should have thrown.");
} catch (NullPointerException expected) { } catch (NullPointerException expected) {
} }
Expand All @@ -360,7 +360,7 @@ public void containsExactlyRespectsDuplicates() {
ImmutableListMultimap<Integer, String> expected = ImmutableListMultimap<Integer, String> expected =
ImmutableListMultimap.of(3, "two", 4, "five", 3, "one", 4, "five", 3, "one"); ImmutableListMultimap.of(3, "two", 4, "five", 3, "one", 4, "five", 3, "one");


assertThat(actual).containsExactly(expected); assertThat(actual).containsExactlyEntriesIn(expected);
} }


@Test @Test
Expand All @@ -370,7 +370,7 @@ public void containsExactlyRespectsDuplicatesFailure() {
ImmutableSetMultimap<Integer, String> expected = ImmutableSetMultimap.copyOf(actual); ImmutableSetMultimap<Integer, String> expected = ImmutableSetMultimap.copyOf(actual);


try { try {
assertThat(actual).containsExactly(expected); assertThat(actual).containsExactlyEntriesIn(expected);
fail("Should have thrown."); fail("Should have thrown.");
} catch (AssertionError e) { } catch (AssertionError e) {
assertThat(e) assertThat(e)
Expand All @@ -392,7 +392,7 @@ public void containsExactlyFailureMissing() {
actual.remove(4, "five"); actual.remove(4, "five");


try { try {
assertThat(actual).containsExactly(expected); assertThat(actual).containsExactlyEntriesIn(expected);
fail("Should have thrown."); fail("Should have thrown.");
} catch (AssertionError e) { } catch (AssertionError e) {
assertThat(e) assertThat(e)
Expand All @@ -414,7 +414,7 @@ public void containsExactlyFailureExtra() {
actual.put(5, "eight"); actual.put(5, "eight");


try { try {
assertThat(actual).containsExactly(expected); assertThat(actual).containsExactlyEntriesIn(expected);
fail("Should have thrown."); fail("Should have thrown.");
} catch (AssertionError e) { } catch (AssertionError e) {
assertThat(e) assertThat(e)
Expand All @@ -438,7 +438,7 @@ public void containsExactlyFailureBoth() {
actual.put(5, "eight"); actual.put(5, "eight");


try { try {
assertThat(actual).containsExactly(expected); assertThat(actual).containsExactlyEntriesIn(expected);
fail("Should have thrown."); fail("Should have thrown.");
} catch (AssertionError e) { } catch (AssertionError e) {
assertThat(e) assertThat(e)
Expand All @@ -459,7 +459,7 @@ public void containsExactlyInOrder() {
ImmutableMultimap<Integer, String> expected = ImmutableMultimap<Integer, String> expected =
ImmutableMultimap.of(3, "one", 3, "six", 3, "two", 4, "five", 4, "four"); ImmutableMultimap.of(3, "one", 3, "six", 3, "two", 4, "five", 4, "four");


assertThat(actual).containsExactly(expected).inOrder(); assertThat(actual).containsExactlyEntriesIn(expected).inOrder();
} }


@Test @Test
Expand All @@ -468,7 +468,7 @@ public void containsExactlyInOrderDifferentTypes() {
ImmutableListMultimap.of(3, "one", 3, "six", 3, "two", 4, "five", 4, "four"); ImmutableListMultimap.of(3, "one", 3, "six", 3, "two", 4, "five", 4, "four");
ImmutableSetMultimap<Integer, String> setMultimap = ImmutableSetMultimap.copyOf(listMultimap); ImmutableSetMultimap<Integer, String> setMultimap = ImmutableSetMultimap.copyOf(listMultimap);


assertThat(listMultimap).containsExactly(setMultimap).inOrder(); assertThat(listMultimap).containsExactlyEntriesIn(setMultimap).inOrder();
} }


@Test @Test
Expand All @@ -478,9 +478,9 @@ public void containsExactlyInOrderFailure() {
ImmutableMultimap<Integer, String> expected = ImmutableMultimap<Integer, String> expected =
ImmutableMultimap.of(4, "four", 3, "six", 4, "five", 3, "two", 3, "one"); ImmutableMultimap.of(4, "four", 3, "six", 4, "five", 3, "two", 3, "one");


assertThat(actual).containsExactly(expected); assertThat(actual).containsExactlyEntriesIn(expected);
try { try {
assertThat(actual).containsExactly(expected).inOrder(); assertThat(actual).containsExactlyEntriesIn(expected).inOrder();
fail("Should have thrown."); fail("Should have thrown.");
} catch (AssertionError e) { } catch (AssertionError e) {
assertThat(e.getMessage()) assertThat(e.getMessage())
Expand All @@ -497,9 +497,9 @@ public void containsExactlyInOrderFailureValuesOnly() {
ImmutableMultimap<Integer, String> expected = ImmutableMultimap<Integer, String> expected =
ImmutableMultimap.of(3, "six", 3, "two", 3, "one", 4, "five", 4, "four"); ImmutableMultimap.of(3, "six", 3, "two", 3, "one", 4, "five", 4, "four");


assertThat(actual).containsExactly(expected); assertThat(actual).containsExactlyEntriesIn(expected);
try { try {
assertThat(actual).containsExactly(expected).inOrder(); assertThat(actual).containsExactlyEntriesIn(expected).inOrder();
fail("Should have thrown."); fail("Should have thrown.");
} catch (AssertionError e) { } catch (AssertionError e) {
assertThat(e) assertThat(e)
Expand Down

0 comments on commit 306ee46

Please sign in to comment.