Skip to content

Commit eff7eed

Browse files
matchers: extra message two empty maps shouldNot equal (#1560)
1 parent ea6eaa9 commit eff7eed

5 files changed

Lines changed: 38 additions & 4 deletions

File tree

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.util.Map;
2727
import java.util.Set;
2828

29+
import static org.testingisdocumenting.webtau.WebTauCore.*;
30+
2931
public class MapsCompareToHandler implements CompareToHandler {
3032
@Override
3133
public boolean handleEquality(Object actual, Object expected) {
@@ -64,6 +66,13 @@ private class Comparator {
6466
}
6567

6668
void compare() {
69+
if (compareToComparator.getAssertionMode() == CompareToComparator.AssertionMode.NOT_EQUAL) {
70+
if (actualMap.isEmpty() && expectedMap.isEmpty()) {
71+
compareToComparator.reportEqual(MapsCompareToHandler.this, actualPath,
72+
tokenizedMessage().error("both maps are empty"));
73+
}
74+
}
75+
6776
allKeys.forEach(this::handleKey);
6877
}
6978

webtau-core/src/test/groovy/org/testingisdocumenting/webtau/expectation/equality/handlers/MapsCompareToHandlerTest.groovy

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,13 @@ class MapsCompareToHandlerTest {
7676
actual([[k1: 'v1']], 'map').shouldNot(equal([k1: 'v1']))
7777
}
7878
}
79+
80+
@Test
81+
void "empty map should not equal empty map"() {
82+
runExpectExceptionAndValidateOutput(AssertionError.class, 'X failed expecting [value] to not equal {}: both maps are empty (Xms)\n' +
83+
' \n' +
84+
' {}') {
85+
actual([:]).shouldNot(equal([:]))
86+
}
87+
}
7988
}

webtau-core/src/test/java/org/testingisdocumenting/webtau/expectation/equality/SnapshotAwareDummyValue.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@
1818

1919
import org.testingisdocumenting.webtau.data.snapshot.SnapshotValueAware;
2020

21-
import java.util.HashMap;
22-
import java.util.List;
23-
import java.util.Map;
21+
import java.util.*;
2422

2523
import static org.testingisdocumenting.webtau.WebTauCore.*;
2624

2725
public class SnapshotAwareDummyValue implements SnapshotValueAware {
2826
private Object snapshot;
29-
private final List<Map<String, Object>> data;
27+
private List<Map<String, Object>> data;
3028
private boolean incrementValueIdx;
3129
private int currentValueIdx; // for wait testing
3230

@@ -42,6 +40,11 @@ public void doOperation() {
4240
data.set(0, map("id", "tid1", "name", "name1-changed"));
4341
}
4442

43+
public void makeEmpty() {
44+
data = new ArrayList<>();
45+
data.add(new HashMap<>());
46+
}
47+
4548
public void enableAutoIncrement() {
4649
incrementValueIdx = true;
4750
}

webtau-core/src/test/java/org/testingisdocumenting/webtau/expectation/equality/SnapshotChangeValueMatcherJavaTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,18 @@ public void change() {
3737
});
3838
}
3939

40+
@Test
41+
public void changeEmptyMap() {
42+
var value = new SnapshotAwareDummyValue();
43+
value.makeEmpty();
44+
value.takeSnapshot();
45+
46+
TestConsoleOutput.runExpectExceptionAndValidateOutput(AssertionError.class, """
47+
X failed expecting [value] to change: both maps are empty (Xms)""", () -> {
48+
actual(value).should(change);
49+
});
50+
}
51+
4052
@Test
4153
public void waitChange() {
4254
var value = new SnapshotAwareDummyValue();
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add: Extra message in case of two empty maps and `shouldNot` `equal`

0 commit comments

Comments
 (0)