Skip to content

Commit 6f7e18f

Browse files
matchers: limit report details output (#1538)
1 parent 6ae8163 commit 6f7e18f

5 files changed

Lines changed: 93 additions & 14 deletions

File tree

webtau-core/src/main/java/org/testingisdocumenting/webtau/cfg/WebTauConfig.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ public class WebTauConfig implements PrettyPrintable {
5959

6060
private final ConfigValue tableVerticalSeparator = declare("tableVerticalSeparator", "string to use as a vertical separator when print TableData", () -> " \u2502 ");
6161

62+
private final ConfigValue matchersReportEntriesLimit = declare("matchersReportEntriesLimit",
63+
"max number of entries for <mismatches>/<missing> to display when matcher fails", () -> 10);
6264
private final ConfigValue consolePayloadOutputLimit = declare("consolePayloadOutputLimit",
6365
"max number of lines to display in console for outputs (e.g. http response)", () -> 500);
6466

@@ -163,6 +165,10 @@ public boolean getFullStackTrace() {
163165
return fullStackTrace.getAsBoolean();
164166
}
165167

168+
public int getMatchersReportEntriesLimit() {
169+
return matchersReportEntriesLimit.getAsInt();
170+
}
171+
166172
public int getConsolePayloadOutputLimit() {
167173
return consolePayloadOutputLimit.getAsInt();
168174
}

webtau-core/src/main/java/org/testingisdocumenting/webtau/expectation/TokenizedReportUtils.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.testingisdocumenting.webtau.expectation;
1818

19+
import org.testingisdocumenting.webtau.cfg.WebTauConfig;
1920
import org.testingisdocumenting.webtau.data.ValuePath;
2021
import org.testingisdocumenting.webtau.expectation.equality.ValuePathMessage;
2122
import org.testingisdocumenting.webtau.reporter.TokenizedMessage;
@@ -61,7 +62,14 @@ public static TokenizedMessage combineReportParts(TokenizedMessage... parts) {
6162
return result;
6263
}
6364

64-
public static TokenizedMessage generateReportPartWithoutLabel(ValuePath topLevelActualPath, Stream<List<ValuePathMessage>> messagesGroupsStream) {
65+
public static TokenizedMessage generateReportPartWithoutLabel(ValuePath topLevelActualPath,
66+
Stream<List<ValuePathMessage>> messagesGroupsStream) {
67+
return generateReportPartWithoutLabel(topLevelActualPath, messagesGroupsStream, WebTauConfig.getCfg().getMatchersReportEntriesLimit());
68+
}
69+
70+
public static TokenizedMessage generateReportPartWithoutLabel(ValuePath topLevelActualPath,
71+
Stream<List<ValuePathMessage>> messagesGroupsStream,
72+
int maxNumberOfEntries) {
6573
List<List<ValuePathMessage>> messagesGroups = messagesGroupsStream.filter(group -> !group.isEmpty()).toList();
6674
if (messagesGroups.isEmpty()) {
6775
return tokenizedMessage();
@@ -70,7 +78,7 @@ public static TokenizedMessage generateReportPartWithoutLabel(ValuePath topLevel
7078
TokenizedMessage result = tokenizedMessage();
7179
int groupIdx = 0;
7280
for (List<ValuePathMessage> group : messagesGroups) {
73-
TokenizedReportUtils.appendToReport(result, topLevelActualPath, group);
81+
TokenizedReportUtils.appendToReport(result, topLevelActualPath, group, maxNumberOfEntries);
7482

7583
boolean isLastGroup = groupIdx == messagesGroups.size() - 1;
7684
if (!isLastGroup) {
@@ -83,9 +91,19 @@ public static TokenizedMessage generateReportPartWithoutLabel(ValuePath topLevel
8391
return result;
8492
}
8593

86-
public static TokenizedMessage appendToReport(TokenizedMessage report, ValuePath topLevelActualPath, List<ValuePathMessage> messages) {
94+
private static void appendToReport(TokenizedMessage report,
95+
ValuePath topLevelActualPath,
96+
List<ValuePathMessage> messages,
97+
int maxNumberOfEntries) {
98+
boolean needToLimit = messages.size() > maxNumberOfEntries;
8799
int messageIdx = 0;
88100
for (ValuePathMessage message : messages) {
101+
boolean reachedLimit = needToLimit && messageIdx == maxNumberOfEntries;
102+
if (reachedLimit) {
103+
report.delimiter("...");
104+
return;
105+
}
106+
89107
boolean useFullMessage = !message.getActualPath().equals(topLevelActualPath);
90108
report.add(useFullMessage ? message.getFullMessage() : message.getMessage());
91109

@@ -95,7 +113,5 @@ public static TokenizedMessage appendToReport(TokenizedMessage report, ValuePath
95113
}
96114
messageIdx++;
97115
}
98-
99-
return report;
100116
}
101117
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2023 webtau maintainers
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.testingisdocumenting.webtau.expectation
18+
19+
import org.junit.Test
20+
import org.testingisdocumenting.webtau.data.ValuePath
21+
import org.testingisdocumenting.webtau.expectation.equality.ValuePathMessage
22+
23+
import java.util.stream.Stream
24+
25+
import static org.testingisdocumenting.webtau.WebTauCore.*
26+
27+
class TokenizedReportUtilsTest {
28+
@Test
29+
void "should limit number of details when specified"() {
30+
def messages = [
31+
new ValuePathMessage(new ValuePath("p1"), tokenizedMessage().error("message 1")),
32+
new ValuePathMessage(new ValuePath("p2"), tokenizedMessage().error("message 2")),
33+
new ValuePathMessage(new ValuePath("p3"), tokenizedMessage().error("message 3")),
34+
]
35+
36+
def report = TokenizedReportUtils.generateReportPartWithoutLabel(new ValuePath("root"), Stream.of(messages), 2)
37+
actual(report.toString()).should equal("p1: message 1\n" +
38+
"p2: message 2\n" +
39+
"...")
40+
}
41+
42+
@Test
43+
void "should not limit number of details when exact match in count"() {
44+
def messages = [
45+
new ValuePathMessage(new ValuePath("p1"), tokenizedMessage().error("message 1")),
46+
new ValuePathMessage(new ValuePath("p2"), tokenizedMessage().error("message 2")),
47+
new ValuePathMessage(new ValuePath("p3"), tokenizedMessage().error("message 3")),
48+
]
49+
50+
def report = TokenizedReportUtils.generateReportPartWithoutLabel(new ValuePath("root"), Stream.of(messages), 3)
51+
actual(report.toString()).should equal("p1: message 1\n" +
52+
"p2: message 2\n" +
53+
"p3: message 3")
54+
}
55+
}

webtau-core/src/test/java/org/example/domain/CustomComplexDataTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -67,15 +67,16 @@ public void complexDataEqualsEachOtherUsingWebTau() {
6767
expected.addRow(3, 5);
6868

6969
runExpectExceptionCaptureAndValidateOutput(AssertionError.class, "custom-complex-data-webtau-fail-output",
70-
"X failed expecting [value] to equal cA │ cB\n" +
71-
" 1 │ 2\n" +
72-
" 3 │ 5:\n" +
73-
" [value][1].cB: actual: 4 <java.lang.Integer>\n" +
74-
" expected: 5 <java.lang.Integer> (Xms)\n" +
75-
" \n" +
76-
" cA │ cB \n" +
77-
" 1 │ 2\n" +
78-
" 3 │ **4**", () -> {
70+
"""
71+
X failed expecting [value] to equal cA │ cB
72+
1 │ 2
73+
3 │ 5:
74+
[value][1].cB: actual: 4 <java.lang.Integer>
75+
expected: 5 <java.lang.Integer> (Xms)
76+
\s
77+
cA │ cB \s
78+
1 │ 2
79+
3 │ **4**""", () -> {
7980
// webtau-assertion
8081
actual(calculated).should(equal(expected));
8182
// webtau-assertion
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* Add: Limit [Matchers](matchers/introduction) number of mismatch details printed by default via config `matchersReportEntriesLimit `

0 commit comments

Comments
 (0)