Skip to content

Commit 6462a23

Browse files
matchers: map contain analyzer refactor (#1522)
1 parent 3853205 commit 6462a23

2 files changed

Lines changed: 14 additions & 32 deletions

File tree

webtau-core/src/main/java/org/testingisdocumenting/webtau/expectation/contain/handlers/MapContainHandler.java

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -51,47 +51,29 @@ private void analyze(ContainAnalyzer containAnalyzer, ValuePath actualPath,
5151
Object expectedKey = expectedEntry.getKey();
5252

5353
ValuePath propertyPath = actualPath.property(expectedKey.toString());
54-
55-
if (isNegative) {
56-
analyzeNegative(containAnalyzer, actualMap, propertyPath, expectedEntry);
57-
} else {
58-
analyzePositive(containAnalyzer, actualMap, propertyPath, expectedEntry);
59-
}
54+
analyzePositiveNegative(containAnalyzer, actualMap, propertyPath, expectedEntry, isNegative);
6055
}
6156
}
6257

63-
private void analyzePositive(ContainAnalyzer containAnalyzer,
64-
Map<?, ?> actualMap,
65-
ValuePath propertyPath,
66-
Map.Entry<?, ?> expectedEntry) {
58+
private void analyzePositiveNegative(ContainAnalyzer containAnalyzer,
59+
Map<?, ?> actualMap,
60+
ValuePath propertyPath,
61+
Map.Entry<?, ?> expectedEntry,
62+
boolean isNegative) {
6763
if (!actualMap.containsKey(expectedEntry.getKey())) {
6864
containAnalyzer.reportMismatch(this, propertyPath, tokenizedMessage().matcher("is missing"));
6965
} else {
7066
CompareToComparator comparator = CompareToComparator.comparator();
7167

7268
Object actualValue = actualMap.get(expectedEntry.getKey());
73-
boolean actualValueEqual = comparator.compareIsEqual(propertyPath,
74-
actualValue, expectedEntry.getValue());
69+
boolean actualValueEqual = isNegative ?
70+
!comparator.compareIsNotEqual(propertyPath, actualValue, expectedEntry.getValue()):
71+
comparator.compareIsEqual(propertyPath, actualValue, expectedEntry.getValue());
7572

7673
if (!actualValueEqual) {
7774
containAnalyzer.reportMismatch(this, propertyPath, comparator.generateEqualMismatchReport());
78-
}
79-
}
80-
}
81-
82-
private void analyzeNegative(ContainAnalyzer containAnalyzer,
83-
Map<?, ?> actualMap,
84-
ValuePath propertyPath,
85-
Map.Entry<?, ?> expectedEntry) {
86-
if (actualMap.containsKey(expectedEntry.getKey())) {
87-
CompareToComparator comparator = CompareToComparator.comparator();
88-
89-
Object actualValue = actualMap.get(expectedEntry.getKey());
90-
boolean actualValueNotEqual = comparator.compareIsNotEqual(propertyPath,
91-
actualValue, expectedEntry.getValue());
92-
93-
if (!actualValueNotEqual) {
94-
containAnalyzer.reportMatch(this, propertyPath, comparator.generateNotEqualMismatchReport());
75+
} else {
76+
containAnalyzer.reportMatch(this, propertyPath, comparator.generateEqualMatchReport());
9577
}
9678
}
9779
}

webtau-core/src/main/java/org/testingisdocumenting/webtau/expectation/contain/handlers/StringContainHandler.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ public boolean handle(Object actual, Object expected) {
3131

3232
@Override
3333
public void analyzeContain(ContainAnalyzer containAnalyzer, ValuePath actualPath, Object actual, Object expected) {
34-
analyze(containAnalyzer, actualPath, actual, expected, false);
34+
analyze(containAnalyzer, actualPath, actual, expected);
3535
}
3636

3737
@Override
3838
public void analyzeNotContain(ContainAnalyzer containAnalyzer, ValuePath actualPath, Object actual, Object expected) {
39-
analyze(containAnalyzer, actualPath, actual, expected, true);
39+
analyze(containAnalyzer, actualPath, actual, expected);
4040
}
4141

42-
private void analyze(ContainAnalyzer containAnalyzer, ValuePath actualPath, Object actual, Object expected, boolean isNegative) {
42+
private void analyze(ContainAnalyzer containAnalyzer, ValuePath actualPath, Object actual, Object expected) {
4343
CharSequence actualText = (CharSequence) actual;
4444
CharSequence expectedText = (CharSequence) expected;
4545

0 commit comments

Comments
 (0)