Skip to content

Commit 14ba1e2

Browse files
matchers: CompareToComparator lazy creation of converted actual map (#1572)
1 parent c37f885 commit 14ba1e2

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

webtau-core/src/main/java/org/testingisdocumenting/webtau/expectation/equality/CompareToComparator.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public String getMessage() {
6161
private final CompareToResult compareToResult = new CompareToResult();
6262

6363
// when actual value was converted for comparison, e.g. bean to Map, it will go into this map
64-
private final Map<ValuePath, Object> convertedActualByPath = new HashMap<>();
64+
private Map<ValuePath, Object> convertedActualByPath;
6565
private Object topLevelExpected;
6666
private Object convertedTopLevelExpected;
6767

@@ -86,12 +86,14 @@ public ValueConverter createValueConverter() {
8686
return convertedTopLevelExpected;
8787
}
8888

89-
return convertedActualByPath.getOrDefault(path, original);
89+
return convertedActualByPath == null ? original :
90+
convertedActualByPath.getOrDefault(path, original);
9091
};
9192
}
9293

9394
public Map<ValuePath, Object> getConvertedActualByPath() {
94-
return Collections.unmodifiableMap(convertedActualByPath);
95+
return convertedActualByPath == null ? Collections.emptyMap() :
96+
Collections.unmodifiableMap(convertedActualByPath);
9597
}
9698

9799
public boolean compareIsEqual(ValuePath actualPath, Object actual, Object expected) {
@@ -339,6 +341,10 @@ private void recordConvertedActual(ValuePath actualPath, Object actual, Object c
339341
return;
340342
}
341343

344+
if (convertedActualByPath == null) {
345+
convertedActualByPath = new HashMap<>();
346+
}
347+
342348
convertedActualByPath.put(actualPath, convertedActual);
343349
}
344350

@@ -369,7 +375,13 @@ private void validateAssertionModeIsPresent() {
369375

370376
private void mergeResults(CompareToComparator comparator) {
371377
compareToResult.merge(comparator.compareToResult);
372-
convertedActualByPath.putAll(comparator.convertedActualByPath);
378+
if (comparator.convertedActualByPath != null) {
379+
if (convertedActualByPath == null) {
380+
convertedActualByPath = new HashMap<>();
381+
}
382+
383+
convertedActualByPath.putAll(comparator.convertedActualByPath);
384+
}
373385
}
374386

375387
private static CompareToHandler findCompareToGreaterLessHandler(Object actual, Object expected) {

webtau-core/src/main/java/org/testingisdocumenting/webtau/expectation/equality/ValuePathLazyMessageList.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public void add(ValuePathMessage message) {
4040
if (singleMessage == null) {
4141
singleMessage = message;
4242
} else {
43-
messages = new ArrayList<>();
43+
messages = new ArrayList<>(SIZE_LIMIT);
4444
messages.add(singleMessage);
4545
messages.add(message);
4646
singleMessage = null;
@@ -69,8 +69,12 @@ public void addAll(Iterable<ValuePathMessage> messages) {
6969
}
7070

7171
public void merge(ValuePathLazyMessageList list) {
72-
for (ValuePathMessage message : list) {
73-
add(message);
72+
if (list.size == 1) {
73+
add(list.first());
74+
} else {
75+
for (ValuePathMessage message : list) {
76+
add(message);
77+
}
7478
}
7579
}
7680

0 commit comments

Comments
 (0)