Skip to content

Commit 5006a52

Browse files
petegronshapiro
authored andcommitted
Make all Fuzzy Truth assertions in MapSubject handle exceptions from Correspondence.formatDiff.
RELNOTES=All Fuzzy Truth assertions now handle exceptions thrown by Correspondence.formatDiff (see the javadoc of that method for details). ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=229710440
1 parent c900cb1 commit 5006a52

File tree

2 files changed

+57
-4
lines changed

2 files changed

+57
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,8 @@ public void containsEntry(@NullableDecl Object expectedKey, @NullableDecl E expe
497497
return;
498498
}
499499
// Found matching key with non-matching value.
500-
@NullableDecl String diff = correspondence.formatDiff(actualValue, expectedValue);
500+
@NullableDecl
501+
String diff = correspondence.safeFormatDiff(actualValue, expectedValue, exceptions);
501502
if (diff != null) {
502503
failWithoutActual(
503504
facts(
@@ -663,7 +664,7 @@ public boolean test(A actualValue, E expectedValue) {
663664
actualAsString(),
664665
correspondence,
665666
expectedMap,
666-
diff.describe(this.<V>valueDiffFormat()))))
667+
diff.describe(this.<V>valueDiffFormat(exceptions)))))
667668
.and(exceptions.describeAsAdditionalInfo()));
668669
return ALREADY_FAILED;
669670
}
@@ -672,12 +673,14 @@ public boolean test(A actualValue, E expectedValue) {
672673
* Returns a formatting function for value differences when compared using the current
673674
* correspondence.
674675
*/
675-
private final <V extends E> Function<ValueDifference<A, V>, String> valueDiffFormat() {
676+
private final <V extends E> Function<ValueDifference<A, V>, String> valueDiffFormat(
677+
final Correspondence.ExceptionStore exceptions) {
676678
return new Function<ValueDifference<A, V>, String>() {
677679
@Override
678680
public String apply(ValueDifference<A, V> values) {
679681
@NullableDecl
680-
String diffString = correspondence.formatDiff(values.actual, values.expected);
682+
String diffString =
683+
correspondence.safeFormatDiff(values.actual, values.expected, exceptions);
681684
if (diffString != null) {
682685
return lenientFormat(
683686
"(expected %s but got %s, diff: %s)", values.expected, values.actual, diffString);

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

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -922,6 +922,29 @@ public void comparingValuesUsing_containsEntry_diffExpectedKeyHasWrongValue() {
922922
+ "within 10 of <60>. However, it has a mapping from that key to <71> (diff: 11)");
923923
}
924924

925+
@Test
926+
public void comparingValuesUsing_containsEntry_handlesFormatDiffExceptions() {
927+
Map<String, Integer> actual = new LinkedHashMap<>();
928+
actual.put("abc", 35);
929+
actual.put("def", null);
930+
expectFailureWhenTestingThat(actual)
931+
.comparingValuesUsing(WITHIN_10_OF)
932+
.containsEntry("def", 60);
933+
assertFailureKeys(
934+
"Not true that <{abc=35, def=null}> contains an entry with key <def> and a value that is "
935+
+ "within 10 of <60>. However, it has a mapping from that key to <null>",
936+
"additionally, one or more exceptions were thrown while comparing values",
937+
"first exception",
938+
"additionally, one or more exceptions were thrown while formatting diffs",
939+
"first exception");
940+
assertThatFailure()
941+
.factValue("first exception", 0)
942+
.startsWith("compare(null, 60) threw java.lang.NullPointerException");
943+
assertThatFailure()
944+
.factValue("first exception", 1)
945+
.startsWith("formatDiff(null, 60) threw java.lang.NullPointerException");
946+
}
947+
925948
@Test
926949
public void comparingValuesUsing_containsEntry_handlesExceptions_expectedKeyHasWrongValue() {
927950
Map<Integer, String> actual = new LinkedHashMap<>();
@@ -1274,6 +1297,33 @@ public void comparingValuesUsing_containsExactlyEntriesIn_diffMissingAndExtraAnd
12741297
+ "{ghi=(expected 90 but got 101, diff: 11)}");
12751298
}
12761299

1300+
@Test
1301+
public void comparingValuesUsing_containsExactlyEntriesIn_handlesFormatDiffExceptions() {
1302+
ImmutableMap<String, Integer> expected = ImmutableMap.of("abc", 30, "def", 60, "ghi", 90);
1303+
Map<String, Integer> actual = new LinkedHashMap<>();
1304+
actual.put("abc", 35);
1305+
actual.put("def", null);
1306+
actual.put("ghi", 95);
1307+
expectFailureWhenTestingThat(actual)
1308+
.comparingValuesUsing(WITHIN_10_OF)
1309+
.containsExactlyEntriesIn(expected);
1310+
assertFailureKeys(
1311+
"Not true that <{abc=35, def=null, ghi=95}> contains exactly one entry that has a key that "
1312+
+ "is equal to and a value that is within 10 of the key and value of each entry of "
1313+
+ "<{abc=30, def=60, ghi=90}>. It has the following entries with matching keys but "
1314+
+ "different values: {def=(expected 60 but got null)}",
1315+
"additionally, one or more exceptions were thrown while comparing values",
1316+
"first exception",
1317+
"additionally, one or more exceptions were thrown while formatting diffs",
1318+
"first exception");
1319+
assertThatFailure()
1320+
.factValue("first exception", 0)
1321+
.startsWith("compare(null, 60) threw java.lang.NullPointerException");
1322+
assertThatFailure()
1323+
.factValue("first exception", 1)
1324+
.startsWith("formatDiff(null, 60) threw java.lang.NullPointerException");
1325+
}
1326+
12771327
@Test
12781328
public void comparingValuesUsing_containsExactlyEntriesIn_inOrder_failsOutOfOrder() {
12791329
ImmutableMap<String, Integer> expected = ImmutableMap.of("def", 456, "abc", 123);

0 commit comments

Comments
 (0)