Skip to content

Commit 2887753

Browse files
core: indent step input and output in console reporter
1 parent baef9b6 commit 2887753

4 files changed

Lines changed: 172 additions & 4 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2021 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.console;
18+
19+
import org.testingisdocumenting.webtau.utils.StringUtils;
20+
21+
import java.util.Arrays;
22+
23+
import java.util.stream.Stream;
24+
25+
public class IndentedConsoleOutput implements ConsoleOutput {
26+
private final ConsoleOutput original;
27+
private final String indentation;
28+
29+
public IndentedConsoleOutput(ConsoleOutput original, Integer indentationSize) {
30+
this.original = original;
31+
this.indentation = StringUtils.createIndentation(indentationSize);
32+
}
33+
34+
@Override
35+
public void out(Object... styleOrValues) {
36+
original.out(Stream.concat(Stream.of(indentation), Arrays.stream(styleOrValues)).toArray());
37+
}
38+
39+
@Override
40+
public void err(Object... styleOrValues) {
41+
original.err(Stream.concat(Stream.of(indentation), Arrays.stream(styleOrValues)).toArray());
42+
}
43+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2021 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.console
18+
19+
import org.junit.Before
20+
import org.junit.Test
21+
22+
class IndentedConsoleOutputTest {
23+
TestConsoleOutput console
24+
25+
@Before
26+
void init() {
27+
console = new TestConsoleOutput()
28+
}
29+
30+
@Test
31+
void "should indent output"() {
32+
def indented =new IndentedConsoleOutput(console, 4)
33+
indented.out('hello', 200L, 'world')
34+
35+
assert console.out == [[' ', 'hello', 200L, 'world']]
36+
assert console.err == []
37+
}
38+
39+
@Test
40+
void "should indent error"() {
41+
def indented =new IndentedConsoleOutput(console, 4)
42+
indented.err('hello', 200L, 'world')
43+
44+
assert console.err == [[' ', 'hello', 200L, 'world']]
45+
assert console.out == []
46+
}
47+
48+
private static class TestConsoleOutput implements ConsoleOutput {
49+
private List<List<Object>> out = new ArrayList<>()
50+
private List<List<Object>> err = new ArrayList<>()
51+
52+
53+
@Override
54+
void out(Object... styleOrValues) {
55+
out.push(Arrays.asList(styleOrValues))
56+
}
57+
58+
@Override
59+
void err(Object... styleOrValues) {
60+
err.push(Arrays.asList(styleOrValues))
61+
}
62+
}
63+
}

webtau-core/src/main/java/org/testingisdocumenting/webtau/reporter/ConsoleStepReporter.java

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package org.testingisdocumenting.webtau.reporter;
1919

2020
import org.testingisdocumenting.webtau.console.ConsoleOutputs;
21+
import org.testingisdocumenting.webtau.console.IndentedConsoleOutput;
2122
import org.testingisdocumenting.webtau.console.ansi.Color;
2223
import org.testingisdocumenting.webtau.utils.StringUtils;
2324

@@ -114,15 +115,20 @@ private void printStepInput(WebTauStep step) {
114115
return;
115116
}
116117

117-
step.getInput().prettyPrint(ConsoleOutputs.asCombinedConsoleOutput());
118+
step.getInput().prettyPrint(createIndentedConsoleOutput(step));
118119
}
119120

120121
private void printStepOutput(WebTauStep step) {
121122
if (skipRenderRequestResponse()) {
122123
return;
123124
}
124125

125-
step.getOutput().prettyPrint(ConsoleOutputs.asCombinedConsoleOutput());
126+
step.getOutput().prettyPrint(createIndentedConsoleOutput(step));
127+
}
128+
129+
private IndentedConsoleOutput createIndentedConsoleOutput(WebTauStep step) {
130+
return new IndentedConsoleOutput(ConsoleOutputs.asCombinedConsoleOutput(),
131+
numberOfSpacedForIndentLevel(step.getNumberOfParents()));
126132
}
127133

128134
private boolean skipRenderRequestResponse() {
@@ -178,8 +184,12 @@ private boolean isLastTokenError(TokenizedMessage completionMessage) {
178184
IntegrationTestsMessageBuilder.TokenTypes.ERROR.getType());
179185
}
180186

181-
private String createIndentation(int indentation) {
182-
return StringUtils.createIndentation(indentation * 2);
187+
private String createIndentation(int indentLevel) {
188+
return StringUtils.createIndentation(numberOfSpacedForIndentLevel(indentLevel));
189+
}
190+
191+
private int numberOfSpacedForIndentLevel(int indentLevel) {
192+
return indentLevel * 2;
183193
}
184194

185195
private void executeIfWithinVerboseLevel(WebTauStep step, Runnable code) {

webtau-core/src/test/groovy/org/testingisdocumenting/webtau/reporter/ConsoleStepReporterTest.groovy

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,32 @@ class ConsoleStepReporterTest implements ConsoleOutput {
142142
}
143143
}
144144

145+
@Test
146+
void "should indent step input pretty print"() {
147+
def topLevelStep = WebTauStep.createStep(null, TokenizedMessage.tokenizedMessage(action("top level action")),
148+
{ -> TokenizedMessage.tokenizedMessage(action("top level action completed")) }) {
149+
150+
def secondLevelStepSuccess = WebTauStep.createStep(null, TokenizedMessage.tokenizedMessage(action("second level action")),
151+
{ -> TokenizedMessage.tokenizedMessage(action("second level action completed")) }) {
152+
}
153+
154+
secondLevelStepSuccess.setInput(new TestStepInput())
155+
secondLevelStepSuccess.setOutput(new TestStepOutput())
156+
secondLevelStepSuccess.execute(StepReportOptions.REPORT_ALL)
157+
}
158+
159+
expectReport(Integer.MAX_VALUE, '> top level action\n' +
160+
' > second level action\n' +
161+
' hello input\n' +
162+
' world\n' +
163+
' hello output\n' +
164+
' world\n' +
165+
' . second level action completed (0ms)\n' +
166+
'. top level action completed (0ms)') {
167+
topLevelStep.execute(StepReportOptions.REPORT_ALL)
168+
}
169+
}
170+
145171
@Test
146172
void "should not render step if a nest level is greater than provided max"() {
147173
expectReport(0, '') {
@@ -236,4 +262,30 @@ class ConsoleStepReporterTest implements ConsoleOutput {
236262
void err(Object... styleOrValues) {
237263
lines.add(new IgnoreAnsiString(styleOrValues).toString())
238264
}
265+
266+
private static class TestStepInput implements WebTauStepInput {
267+
@Override
268+
void prettyPrint(ConsoleOutput console) {
269+
console.out("hello input")
270+
console.out("world")
271+
}
272+
273+
@Override
274+
Map<String, ?> toMap() {
275+
return Collections.emptyMap()
276+
}
277+
}
278+
279+
private static class TestStepOutput implements WebTauStepOutput {
280+
@Override
281+
void prettyPrint(ConsoleOutput console) {
282+
console.out("hello output")
283+
console.out("world")
284+
}
285+
286+
@Override
287+
Map<String, ?> toMap() {
288+
return Collections.emptyMap()
289+
}
290+
}
239291
}

0 commit comments

Comments
 (0)