Skip to content

Commit

Permalink
Make another code path resilient to hashCode implementations that t…
Browse files Browse the repository at this point in the history
…hrow an exception.

We'd rather they didn't, but let's do what we can... :(

(progress toward #176)

RELNOTES=n/a
PiperOrigin-RevId: 463143598
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed Jul 25, 2022
1 parent f149531 commit 2cdb458
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 3 deletions.
6 changes: 3 additions & 3 deletions core/src/main/java/com/google/common/truth/SubjectUtils.java
Expand Up @@ -25,13 +25,13 @@
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Optional;
import com.google.common.collect.HashMultimap;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import com.google.common.collect.LinkedHashMultiset;
import com.google.common.collect.Lists;
import com.google.common.collect.Multiset;
import com.google.common.collect.SetMultimap;
import com.google.common.collect.ListMultimap;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -262,7 +262,7 @@ static String iterableToStringWithTypeInfo(Iterable<?> itemsIterable) {
* <p>Example: {@code retainMatchingToString([1L, 2L, 2L], [2, 3]) == [2L, 2L]}
*/
static List<Object> retainMatchingToString(Iterable<?> items, Iterable<?> itemsToCheck) {
SetMultimap<String, Object> stringValueToItemsToCheck = HashMultimap.create();
ListMultimap<String, Object> stringValueToItemsToCheck = ArrayListMultimap.create();
for (Object itemToCheck : itemsToCheck) {
stringValueToItemsToCheck.put(String.valueOf(itemToCheck), itemToCheck);
}
Expand Down
Expand Up @@ -675,10 +675,24 @@ public void iterableContainsExactlyWithElementsThatThrowWhenYouCallHashCode() {
assertThat(asList(one, two)).containsExactly(one, two).inOrder();
assertThat(asList(one, two)).containsExactlyElementsIn(asList(two, one));
assertThat(asList(one, two)).containsExactlyElementsIn(asList(one, two)).inOrder();
}

@Test
public void iterableContainsExactlyWithElementsThatThrowWhenYouCallHashCodeFailureTooMany() {
HashCodeThrower one = new HashCodeThrower();
HashCodeThrower two = new HashCodeThrower();

expectFailureWhenTestingThat(asList(one, two)).containsExactly(one);
}

@Test
public void iterableContainsExactlyWithElementsThatThrowWhenYouCallHashCodeOneMismatch() {
HashCodeThrower one = new HashCodeThrower();
HashCodeThrower two = new HashCodeThrower();

expectFailureWhenTestingThat(asList(one, one)).containsExactly(one, two);
}

private static class HashCodeThrower {
@Override
public boolean equals(Object other) {
Expand Down

0 comments on commit 2cdb458

Please sign in to comment.