Skip to content

Commit 70f1aeb

Browse files
matchers: list comparison missing/extra compact view (#1533)
1 parent 509281f commit 70f1aeb

4 files changed

Lines changed: 46 additions & 24 deletions

File tree

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@
2121
import org.testingisdocumenting.webtau.expectation.equality.CompareToComparator;
2222
import org.testingisdocumenting.webtau.expectation.equality.CompareToHandler;
2323

24+
import java.util.ArrayList;
2425
import java.util.Iterator;
26+
import java.util.List;
2527

2628
public class IterableCompareToHandler implements CompareToHandler {
2729
@Override
@@ -43,16 +45,24 @@ public void compareEqualOnly(CompareToComparator comparator, ValuePath actualPat
4345
idx++;
4446
}
4547

48+
List<Object> extraValues = new ArrayList<>();
4649
while (actualIt.hasNext()) {
4750
Object actualElement = actualIt.next();
48-
comparator.reportExtra(this, actualPath.index(idx), actualElement);
49-
idx++;
51+
extraValues.add(actualElement);
52+
}
53+
54+
if (!extraValues.isEmpty()) {
55+
comparator.reportExtra(this, actualPath, extraValues);
5056
}
5157

58+
List<Object> missingValues = new ArrayList<>();
5259
while (expectedIt.hasNext()) {
5360
Object expectedElement = expectedIt.next();
54-
comparator.reportMissing(this, actualPath.index(idx), expectedElement);
55-
idx++;
61+
missingValues.add(expectedElement);
62+
}
63+
64+
if (!missingValues.isEmpty()) {
65+
comparator.reportMissing(this, actualPath, missingValues);
5666
}
5767
}
5868
}

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

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,13 @@ package org.testingisdocumenting.webtau.expectation.equality.handlers
2020
import org.junit.Test
2121
import org.testingisdocumenting.webtau.data.ValuePath
2222
import org.testingisdocumenting.webtau.expectation.equality.CompareToComparator
23+
import org.testingisdocumenting.webtau.testutils.TestConsoleOutput
2324

2425
import static org.junit.Assert.*
2526
import static org.testingisdocumenting.webtau.Matchers.*
2627
import static org.testingisdocumenting.webtau.WebTauCore.*
2728
import static org.testingisdocumenting.webtau.expectation.equality.CompareToComparator.*
29+
import static org.testingisdocumenting.webtau.testutils.TestConsoleOutput.runExpectExceptionAndValidateOutput
2830

2931
class IterableCompareToHandlerTest {
3032
private static final ValuePath actualPath = createActualPath("value")
@@ -57,34 +59,39 @@ class IterableCompareToHandlerTest {
5759

5860
@Test
5961
void "should report mismatched elements"() {
60-
CompareToComparator comparator = comparator(AssertionMode.EQUAL)
61-
comparator.compareUsingEqualOnly(actualPath, [1, 2, 5], [3, 2, 4])
62-
63-
assertEquals(
64-
"value[0]: actual: 1 <java.lang.Integer>\n" +
65-
" expected: 3 <java.lang.Integer>\n" +
66-
"value[2]: actual: 5 <java.lang.Integer>\n" +
67-
" expected: 4 <java.lang.Integer>", comparator.generateEqualMismatchReport().toString())
62+
runExpectExceptionAndValidateOutput(AssertionError, "X failed expecting list to equal [3, 2, 4]:\n" +
63+
" list[0]: actual: 1 <java.lang.Integer>\n" +
64+
" expected: 3 <java.lang.Integer>\n" +
65+
" list[2]: actual: 5 <java.lang.Integer>\n" +
66+
" expected: 4 <java.lang.Integer> (Xms)\n" +
67+
" \n" +
68+
" [**1**, 2, **5**]") {
69+
actual([1, 2, 5], "list").should(equal([3, 2, 4]))
70+
}
6871
}
6972

7073
@Test
7174
void "should report missing elements"() {
72-
CompareToComparator comparator = comparator(AssertionMode.EQUAL)
73-
comparator.compareUsingEqualOnly(actualPath, [1, 2], [1, 2, 3])
74-
75-
assertEquals("missing, but expected values:\n" +
76-
"\n" +
77-
"value[2]: 3", comparator.generateEqualMismatchReport().toString())
75+
runExpectExceptionAndValidateOutput(AssertionError, "X failed expecting list to equal [1, 2, 4, 6, 8, 10, 12, 43, 1, 5]:\n" +
76+
" missing, but expected values:\n" +
77+
" \n" +
78+
" [4, 6, 8, 10, 12, 43, 1, 5] (Xms)\n" +
79+
" \n" +
80+
" [1, 2]") {
81+
actual([1, 2], "list").should(equal([1, 2, 4, 6, 8, 10, 12, 43, 1, 5]))
82+
}
7883
}
7984

8085
@Test
8186
void "should report extra elements"() {
82-
CompareToComparator comparator = comparator(AssertionMode.EQUAL)
83-
comparator.compareUsingEqualOnly(actualPath, [1, 2, 3], [1, 2])
84-
85-
assertEquals("unexpected values:\n" +
86-
"\n" +
87-
"value[2]: 3", comparator.generateEqualMismatchReport().toString())
87+
runExpectExceptionAndValidateOutput(AssertionError, "X failed expecting list to equal [1, 2]:\n" +
88+
" unexpected values:\n" +
89+
" \n" +
90+
" [3, 4, 12, 43, 23] (Xms)\n" +
91+
" \n" +
92+
" [1, 2, 3, 4, 12, 43, 23]") {
93+
actual([1, 2, 3, 4, 12, 43, 23], "list").should(equal([1, 2]))
94+
}
8895
}
8996

9097
@Test
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add: list comparison prints missing and extra elements in a more compact way

webtau-docs/znai/release-notes/2023.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
title: 2023 Releases
33
---
44

5+
# 2.2
6+
7+
:include-markdowns: 2.2
8+
59
# 2.1
610

711
:include-markdowns: 2.1

0 commit comments

Comments
 (0)