Skip to content

Commit

Permalink
Improve error message for MultimapSubject.containsEntry()
Browse files Browse the repository at this point in the history
-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=127233406
  • Loading branch information
kluever authored and cpovirk committed Jul 12, 2016
1 parent c89b93d commit aabf07e
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 3 deletions.
24 changes: 22 additions & 2 deletions core/src/main/java/com/google/common/truth/MultimapSubject.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@
import com.google.common.collect.SetMultimap;
import com.google.common.collect.Sets;
import com.google.errorprone.annotations.CanIgnoreReturnValue;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;

import java.util.Map.Entry;
import java.util.Set;
import javax.annotation.Nullable;

/**
Expand Down Expand Up @@ -98,7 +98,27 @@ public void doesNotContainKey(@Nullable Object key) {

/** Fails if the multimap does not contain the given entry. */
public void containsEntry(@Nullable Object key, @Nullable Object value) {
// TODO(kak): Can we share any of this logic w/ MapSubject.containsEntry()?
if (!getSubject().containsEntry(key, value)) {
Entry<Object, Object> entry = Maps.immutableEntry(key, value);
if (getSubject().containsKey(key)) {
failWithRawMessage(
"Not true that %s contains entry <%s>. However, it has a mapping from <%s> to <%s>",
getDisplaySubject(), entry, key, ((Multimap<Object, Object>) getSubject()).get(key));
}
if (getSubject().containsValue(value)) {
Set<Object> keys = new LinkedHashSet<Object>();
for (Entry<Object, Object> actualEntry :
((Multimap<Object, Object>) getSubject()).entries()) {
if (Objects.equal(actualEntry.getValue(), value)) {
keys.add(actualEntry.getKey());
}
}
failWithRawMessage(
"Not true that %s contains entry <%s>. "
+ "However, the following keys are mapped to <%s>: %s",
getDisplaySubject(), entry, value, keys);
}
fail("contains entry", Maps.immutableEntry(key, value));
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,14 @@
import static com.google.common.truth.Truth.assertThat;
import static org.junit.Assert.fail;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.LinkedListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimap;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand Down Expand Up @@ -283,6 +283,56 @@ public void containsEntryFailure() {
}
}

@Test
public void containsEntryWithNullValueNullExpected() {
ListMultimap<String, String> actual = ArrayListMultimap.create();
actual.put("a", null);
assertThat(actual).containsEntry("a", null);
}

@Test
public void failContainsEntry() {
ImmutableMultimap<String, String> actual = ImmutableMultimap.of("a", "A");
try {
assertThat(actual).containsEntry("a", "a");
fail("Should have thrown.");
} catch (AssertionError e) {
assertThat(e)
.hasMessage(
"Not true that <{a=[A]}> contains entry <a=a>. "
+ "However, it has a mapping from <a> to <[A]>");
}
}

@Test
public void failContainsEntryWithNullValuePresentExpected() {
ListMultimap<String, String> actual = ArrayListMultimap.create();
actual.put("a", null);
try {
assertThat(actual).containsEntry("a", "A");
fail("Should have thrown.");
} catch (AssertionError e) {
assertThat(e)
.hasMessage(
"Not true that <{a=[null]}> contains entry <a=A>. "
+ "However, it has a mapping from <a> to <[null]>");
}
}

@Test
public void failContainsEntryWithPresentValueNullExpected() {
ImmutableMultimap<String, String> actual = ImmutableMultimap.of("a", "A");
try {
assertThat(actual).containsEntry("a", null);
fail("Should have thrown.");
} catch (AssertionError e) {
assertThat(e)
.hasMessage(
"Not true that <{a=[A]}> contains entry <a=null>. "
+ "However, it has a mapping from <a> to <[A]>");
}
}

@Test
public void doesNotContainEntry() {
ImmutableMultimap<String, String> multimap = ImmutableMultimap.of("kurt", "kluever");
Expand Down

0 comments on commit aabf07e

Please sign in to comment.