Skip to content

Commit b515e71

Browse files
cpovirkGoogle Java Core Libraries
authored andcommitted
Show only the duplicate keys in the exception message, not all keys.
Arguably we should also: - Show the entire map. - Fail the assertion instead of throwing. But this CL seems like a strict improvement. RELNOTES=n/a PiperOrigin-RevId: 799515840
1 parent 534518e commit b515e71

File tree

2 files changed

+23
-5
lines changed

2 files changed

+23
-5
lines changed

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

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import static com.google.common.base.Preconditions.checkArgument;
1919
import static com.google.common.base.Preconditions.checkNotNull;
20+
import static com.google.common.base.Strings.lenientFormat;
2021
import static com.google.common.collect.Maps.immutableEntry;
2122
import static com.google.common.truth.Fact.fact;
2223
import static com.google.common.truth.Fact.simpleFact;
@@ -28,6 +29,7 @@
2829
import static com.google.common.truth.SubjectUtils.retainMatchingToString;
2930
import static java.util.Collections.singletonList;
3031

32+
import com.google.common.collect.FluentIterable;
3133
import com.google.common.collect.ImmutableList;
3234
import com.google.common.collect.LinkedHashMultiset;
3335
import com.google.common.collect.Maps;
@@ -260,11 +262,14 @@ public final Ordered containsAtLeast(
260262
expectedMap.put(key, rest[i + 1]);
261263
keys.add(key);
262264
}
263-
checkArgument(
264-
keys.size() == expectedMap.size(),
265-
"Duplicate keys (%s) cannot be passed to %s().",
266-
keys,
267-
functionName);
265+
if (expectedMap.size() != keys.size()) {
266+
List<Multiset.Entry<@Nullable Object>> duplicateKeys =
267+
FluentIterable.from(keys.entrySet()).filter(e -> e.getCount() > 1).toList();
268+
throw new IllegalArgumentException(
269+
lenientFormat(
270+
// TODO(cpovirk): Consider a format like SubjectUtils.entryString instead.
271+
"Duplicate keys (%s) cannot be passed to %s().", duplicateKeys, functionName));
272+
}
268273
return expectedMap;
269274
}
270275

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,19 @@ public void containsExactlyMultipleDuplicateKeys() {
160160
.isEqualTo("Duplicate keys ([jan x 2, feb x 2]) cannot be passed to containsExactly().");
161161
}
162162

163+
@Test
164+
public void containsExactlyBothDuplicateAndNonDuplicateKeys() {
165+
ImmutableMap<String, Integer> actual = ImmutableMap.of("jan", 1, "feb", 2);
166+
167+
IllegalArgumentException expected =
168+
assertThrows(
169+
IllegalArgumentException.class,
170+
() -> assertThat(actual).containsExactly("jan", 1, "jan", 1, "feb", 2));
171+
assertThat(expected)
172+
.hasMessageThat()
173+
.isEqualTo("Duplicate keys ([jan x 2]) cannot be passed to containsExactly().");
174+
}
175+
163176
@Test
164177
public void containsExactlyExtraKey() {
165178
ImmutableMap<String, Integer> actual = ImmutableMap.of("jan", 1, "feb", 2, "march", 3);

0 commit comments

Comments
 (0)