-
-
Notifications
You must be signed in to change notification settings - Fork 683
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix ignoringFields with different map size #3003
base: 3.x
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ | |
import static java.lang.String.format; | ||
import static java.util.Objects.deepEquals; | ||
import static java.util.stream.Collectors.joining; | ||
import static java.util.stream.Collectors.toMap; | ||
import static org.assertj.core.api.recursive.comparison.ComparisonDifference.rootComparisonDifference; | ||
import static org.assertj.core.api.recursive.comparison.DualValue.DEFAULT_ORDERED_COLLECTION_TYPES; | ||
import static org.assertj.core.api.recursive.comparison.FieldLocation.rootFieldLocation; | ||
|
@@ -44,7 +45,6 @@ | |
import java.util.concurrent.atomic.AtomicReference; | ||
import java.util.concurrent.atomic.AtomicReferenceArray; | ||
import java.util.stream.Stream; | ||
|
||
import org.assertj.core.internal.DeepDifference; | ||
|
||
/** | ||
|
@@ -506,9 +506,9 @@ private static <K, V> void compareSortedMap(DualValue dualValue, ComparisonState | |
return; | ||
} | ||
|
||
Map<?, ?> actualMap = (Map<?, ?>) dualValue.actual; | ||
@SuppressWarnings("unchecked") | ||
Map<K, V> expectedMap = (Map<K, V>) dualValue.expected; | ||
Map<?, ?> actualMap = filterIgnoringFields((Map<?, ?>)dualValue.actual, comparisonState); | ||
Map<K, V> expectedMap = (Map<K, V>) filterIgnoringFields((Map<?, ?>) dualValue.expected, comparisonState); | ||
|
||
if (actualMap.size() != expectedMap.size()) { | ||
comparisonState.addDifference(dualValue, format(DIFFERENT_SIZE_ERROR, "sorted maps", actualMap.size(), expectedMap.size())); | ||
// no need to inspect entries, maps are not equal as they don't have the same size | ||
|
@@ -535,8 +535,10 @@ private static void compareUnorderedMap(DualValue dualValue, ComparisonState com | |
return; | ||
} | ||
|
||
Map<?, ?> actualMap = (Map<?, ?>) dualValue.actual; | ||
Map<?, ?> expectedMap = (Map<?, ?>) dualValue.expected; | ||
Set<String> ignoredFields = comparisonState.recursiveComparisonConfiguration.getIgnoredFields(); | ||
Map<?, ?> actualMap = filterIgnoringFields((Map<?, ?>) dualValue.actual, comparisonState); | ||
Map<?, ?> expectedMap = filterIgnoringFields((Map<?, ?>) dualValue.expected, comparisonState); | ||
|
||
if (actualMap.size() != expectedMap.size()) { | ||
comparisonState.addDifference(dualValue, format(DIFFERENT_SIZE_ERROR, "maps", actualMap.size(), expectedMap.size())); | ||
// no need to inspect entries, maps are not equal as they don't have the same size | ||
|
@@ -557,6 +559,15 @@ private static void compareUnorderedMap(DualValue dualValue, ComparisonState com | |
} | ||
} | ||
|
||
private static Map<?, ?> filterIgnoringFields(Map<?, ?> dualValue, ComparisonState comparisonState) { | ||
Set<String> ignoredFields = comparisonState.recursiveComparisonConfiguration.getIgnoredFields(); | ||
if(ignoredFields.isEmpty()){ | ||
return dualValue; | ||
} | ||
return dualValue.entrySet() | ||
.stream().filter(e -> !ignoredFields.contains(e.getKey())) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. rename There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we are comparing the map key interpreted as field against the ignored fields, this is not entirely correct because we need to compare the field location ( Let's say we want to ignore the Moreover the recursive comparison supports ignoring fields by regex. Have a look at We should also add test cases in where we ignore for maps:
|
||
.collect(toMap(Map.Entry::getKey, Map.Entry::getValue));} | ||
|
||
private static FieldLocation keyFieldLocation(FieldLocation parentFieldLocation, Object key) { | ||
return key == null ? parentFieldLocation : parentFieldLocation.field(key.toString()); | ||
} | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let's rename
dualValue
tomap
since this is not a dualvalue