Skip to content

Commit 75a4a5f

Browse files
core: pretty print receives console as param
1 parent 59a33d6 commit 75a4a5f

21 files changed

Lines changed: 150 additions & 83 deletions

File tree

webtau-browser/src/main/java/org/testingisdocumenting/webtau/browser/page/PageElement.java

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,18 @@
1818
package org.testingisdocumenting.webtau.browser.page;
1919

2020
import org.testingisdocumenting.webtau.browser.page.path.ElementsFinder;
21+
import org.testingisdocumenting.webtau.console.ConsoleOutput;
2122
import org.testingisdocumenting.webtau.console.ansi.Color;
2223
import org.testingisdocumenting.webtau.data.render.PrettyPrintable;
2324
import org.testingisdocumenting.webtau.expectation.ActualPathAndDescriptionAware;
24-
import org.testingisdocumenting.webtau.expectation.ActualValue;
2525
import org.testingisdocumenting.webtau.expectation.ActualValueExpectations;
26-
import org.testingisdocumenting.webtau.expectation.ValueMatcher;
27-
import org.testingisdocumenting.webtau.expectation.timer.ExpectationTimer;
2826
import org.testingisdocumenting.webtau.reporter.*;
2927
import org.openqa.selenium.WebElement;
3028

3129
import java.util.List;
3230
import java.util.regex.Pattern;
3331
import java.util.stream.Stream;
3432

35-
import static org.testingisdocumenting.webtau.Matchers.*;
36-
import static org.testingisdocumenting.webtau.console.ConsoleOutputs.out;
37-
3833
public interface PageElement extends
3934
ActualValueExpectations,
4035
PrettyPrintable,
@@ -98,25 +93,25 @@ default StepReportOptions shouldReportOption() {
9893
}
9994

10095
@Override
101-
default void prettyPrint() {
96+
default void prettyPrint(ConsoleOutput console) {
10297
TokenizedMessageToAnsiConverter toAnsiConverter = IntegrationTestsMessageBuilder.getConverter();
10398

10499
if (!isPresent()) {
105-
out(Stream.concat(
100+
console.out(Stream.concat(
106101
Stream.of(Color.RED, "element is not present: "),
107102
toAnsiConverter.convert(locationDescription()).stream()).toArray());
108103
return;
109104
}
110105

111-
out(Stream.concat(
106+
console.out(Stream.concat(
112107
Stream.of(Color.GREEN, "element is found: "),
113108
toAnsiConverter.convert(locationDescription()).stream()).toArray());
114109

115-
out(Color.YELLOW, " getText(): ", Color.GREEN, getText());
116-
out(Color.YELLOW, "getUnderlyingValue(): ", Color.GREEN, getUnderlyingValue());
110+
console.out(Color.YELLOW, " getText(): ", Color.GREEN, getText());
111+
console.out(Color.YELLOW, "getUnderlyingValue(): ", Color.GREEN, getUnderlyingValue());
117112
Integer count = getCount().get();
118113
if (count > 1) {
119-
out(Color.YELLOW, " count: ", Color.GREEN, count);
114+
console.out(Color.YELLOW, " count: ", Color.GREEN, count);
120115
}
121116
}
122117
}

webtau-browser/src/main/java/org/testingisdocumenting/webtau/browser/page/PageUrl.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,14 @@
1717

1818
package org.testingisdocumenting.webtau.browser.page;
1919

20+
import org.testingisdocumenting.webtau.console.ConsoleOutput;
2021
import org.testingisdocumenting.webtau.console.ansi.Color;
2122
import org.testingisdocumenting.webtau.data.render.PrettyPrintable;
2223

2324
import java.net.MalformedURLException;
2425
import java.net.URL;
2526
import java.util.function.Supplier;
2627

27-
import static org.testingisdocumenting.webtau.console.ConsoleOutputs.out;
28-
2928
public class PageUrl implements PrettyPrintable {
3029
private static final BrowserContext browserContext = new BrowserContext();
3130
private final Supplier<String> currentUrlSupplier;
@@ -87,10 +86,10 @@ public String toString() {
8786
}
8887

8988
@Override
90-
public void prettyPrint() {
91-
out(Color.YELLOW, " full: ", Color.GREEN, full.get());
92-
out(Color.YELLOW, " path: ", Color.GREEN, path.get());
93-
out(Color.YELLOW, "query: ", Color.GREEN, query.get());
94-
out(Color.YELLOW, " ref: ", Color.GREEN, ref.get());
89+
public void prettyPrint(ConsoleOutput console) {
90+
console.out(Color.YELLOW, " full: ", Color.GREEN, full.get());
91+
console.out(Color.YELLOW, " path: ", Color.GREEN, path.get());
92+
console.out(Color.YELLOW, "query: ", Color.GREEN, query.get());
93+
console.out(Color.YELLOW, " ref: ", Color.GREEN, ref.get());
9594
}
9695
}

webtau-browser/src/main/java/org/testingisdocumenting/webtau/browser/reporter/ScreenshotStepOutput.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.testingisdocumenting.webtau.browser.reporter;
1919

20+
import org.testingisdocumenting.webtau.console.ConsoleOutput;
2021
import org.testingisdocumenting.webtau.reporter.WebTauStepOutput;
2122

2223
import java.util.Collections;
@@ -39,6 +40,6 @@ public String getBase64png() {
3940
}
4041

4142
@Override
42-
public void prettyPrint() {
43+
public void prettyPrint(ConsoleOutput console) {
4344
}
4445
}

webtau-cli/src/main/java/org/testingisdocumenting/webtau/cli/CliValidationResult.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.testingisdocumenting.webtau.cli;
1919

20+
import org.testingisdocumenting.webtau.console.ConsoleOutput;
2021
import org.testingisdocumenting.webtau.reporter.WebTauStepOutput;
2122
import org.testingisdocumenting.webtau.reporter.WebTauStepPayload;
2223

@@ -125,6 +126,6 @@ public CliDocumentationArtifact createDocumentationArtifact() {
125126
}
126127

127128
@Override
128-
public void prettyPrint() {
129+
public void prettyPrint(ConsoleOutput console) {
129130
}
130131
}

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,15 @@
2626
import java.util.ArrayList;
2727
import java.util.Arrays;
2828
import java.util.Collection;
29-
import java.util.Collections;
3029
import java.util.LinkedHashMap;
3130
import java.util.List;
3231
import java.util.Map;
3332
import java.util.Optional;
3433
import java.util.function.Supplier;
3534
import java.util.stream.Collectors;
3635
import java.util.stream.Stream;
36+
37+
import org.testingisdocumenting.webtau.console.ConsoleOutput;
3738
import org.testingisdocumenting.webtau.console.ConsoleOutputs;
3839
import org.testingisdocumenting.webtau.console.ansi.Color;
3940
import org.testingisdocumenting.webtau.console.ansi.FontStyle;
@@ -304,10 +305,10 @@ public String toString() {
304305
}
305306

306307
public void printEnumerated() {
307-
printConfig(enumeratedCfgValues.values());
308+
printConfig(ConsoleOutputs.asCombinedConsoleOutput(), enumeratedCfgValues.values());
308309
}
309310

310-
private void printConfig(Collection<ConfigValue> configValues) {
311+
private void printConfig(ConsoleOutput console, Collection<ConfigValue> configValues) {
311312
int maxKeyLength = configValues.stream()
312313
.filter(ConfigValue::nonDefault)
313314
.map(v -> v.getKey().length()).max(Integer::compareTo).orElse(0);
@@ -317,13 +318,13 @@ private void printConfig(Collection<ConfigValue> configValues) {
317318
.map(v -> v.getAsString().length()).max(Integer::compareTo).orElse(0);
318319

319320
configValues.stream().filter(ConfigValue::nonDefault).forEach(v -> {
320-
String valueAsText = v.getAsString();
321-
int valuePadding = maxValueLength - valueAsText.length();
321+
String valueAsText = v.getAsString();
322+
int valuePadding = maxValueLength - valueAsText.length();
322323

323-
ConsoleOutputs.out(Color.BLUE, String.format("%" + maxKeyLength + "s", v.getKey()), ": ",
324-
Color.YELLOW, valueAsText,
325-
StringUtils.createIndentation(valuePadding),
326-
FontStyle.NORMAL, " // from ", v.getSource());
324+
console.out(Color.BLUE, String.format("%" + maxKeyLength + "s", v.getKey()), ": ",
325+
Color.YELLOW, valueAsText,
326+
StringUtils.createIndentation(valuePadding),
327+
FontStyle.NORMAL, " // from ", v.getSource());
327328
}
328329
);
329330
}
@@ -407,9 +408,9 @@ private Map<String, ConfigValue> enumerateRegisteredConfigValues() {
407408
}
408409

409410
@Override
410-
public void prettyPrint() {
411-
printConfig(freeFormCfgValues);
412-
printConfig(enumeratedCfgValues.values());
411+
public void prettyPrint(ConsoleOutput console) {
412+
printConfig(console, freeFormCfgValues);
413+
printConfig(console, enumeratedCfgValues.values());
413414
}
414415

415416
private static class CfgInstanceHolder {

webtau-console/src/main/java/org/testingisdocumenting/webtau/console/ConsoleOutput.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright 2021 webtau maintainers
23
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -16,7 +17,46 @@
1617

1718
package org.testingisdocumenting.webtau.console;
1819

20+
import org.testingisdocumenting.webtau.console.ansi.Color;
21+
22+
import java.util.List;
23+
import java.util.function.Function;
24+
1925
public interface ConsoleOutput {
2026
void out(Object... styleOrValues);
2127
void err(Object... styleOrValues);
28+
29+
/**
30+
* output multiple lines, where line is defined by converting function
31+
* @param lines list of lines to print
32+
* @param styleOrValueExtractor function to convert a line to a style and values
33+
* @param <E> type of a line
34+
*/
35+
default <E> void outLines(List<E> lines, Function<E, Object[]> styleOrValueExtractor) {
36+
lines.forEach(l -> ConsoleOutputs.out(styleOrValueExtractor.apply(l)));
37+
}
38+
39+
/**
40+
* output multiple lines by limiting the number of lines printed,
41+
* where line is defined by converting function
42+
* @param lines list of lines to print
43+
* @param limit max number of lines to print
44+
* @param styleOrValueExtractor function to convert a line to a style and values
45+
* @param <E> type of a line
46+
*/
47+
default <E> void outLinesWithLimit(List<E> lines,
48+
int limit,
49+
Function<E, Object[]> styleOrValueExtractor) {
50+
if (limit == -1 || lines.size() <= limit) {
51+
outLines(lines, styleOrValueExtractor);
52+
return;
53+
}
54+
55+
int firstHalfNumberOfLines = limit / 2;
56+
int secondHalfNumberOfLines = firstHalfNumberOfLines + limit % 2;
57+
58+
outLines(lines.subList(0, firstHalfNumberOfLines), styleOrValueExtractor);
59+
ConsoleOutputs.out(Color.YELLOW, "...");
60+
outLines(lines.subList(lines.size() - secondHalfNumberOfLines, lines.size()), styleOrValueExtractor);
61+
}
2262
}

webtau-console/src/main/java/org/testingisdocumenting/webtau/console/ConsoleOutputs.java

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
/*
2+
* Copyright 2021 webtau maintainers
23
* Copyright 2019 TWO SIGMA OPEN SOURCE, LLC
34
*
45
* Licensed under the Apache License, Version 2.0 (the "License");
@@ -26,6 +27,7 @@
2627
import java.util.stream.Stream;
2728

2829
public class ConsoleOutputs {
30+
private static final ConsoleOutput combined = new CombinedConsoleOutput();
2931
private static final ConsoleOutput defaultOutput = new AnsiConsoleOutput();
3032

3133
private static final List<ConsoleOutput> outputs = ServiceLoaderUtils.load(ConsoleOutput.class);
@@ -38,27 +40,33 @@ public static void err(Object... styleOrValues) {
3840
getOutputsStream().forEach(o -> o.err(styleOrValues));
3941
}
4042

43+
public static ConsoleOutput asCombinedConsoleOutput() {
44+
return combined;
45+
}
46+
47+
/**
48+
* output multiple lines, where line is defined by converting function
49+
* @param lines list of lines to print
50+
* @param styleOrValueExtractor function to convert a line to a style and values
51+
* @param <E> type of a line
52+
*/
53+
public static <E> void outLines(List<E> lines, Function<E, Object[]> styleOrValueExtractor) {
54+
getOutputsStream().forEach(o -> o.outLines(lines, styleOrValueExtractor));
55+
56+
}
57+
4158
/**
42-
* prints lines limiting output to a given value. Prints values from the start and from the end, omitting middle.
43-
*
44-
* @param lines lines to print and limit
45-
* @param limit max number of lines to print, -1 to print all
46-
* @param styleOrValueExtractor function to extract valueOrStyle from given list
59+
* output multiple lines by limiting the number of lines printed,
60+
* where line is defined by converting function
61+
* @param lines list of lines to print
62+
* @param limit max number of lines to print
63+
* @param styleOrValueExtractor function to convert a line to a style and values
64+
* @param <E> type of a line
4765
*/
4866
public static <E> void outLinesWithLimit(List<E> lines,
4967
int limit,
5068
Function<E, Object[]> styleOrValueExtractor) {
51-
if (limit == -1 || lines.size() <= limit) {
52-
outLines(lines, styleOrValueExtractor);
53-
return;
54-
}
55-
56-
int firstHalfNumberOfLines = limit / 2;
57-
int secondHalfNumberOfLines = firstHalfNumberOfLines + limit % 2;
58-
59-
outLines(lines.subList(0, firstHalfNumberOfLines), styleOrValueExtractor);
60-
ConsoleOutputs.out(Color.YELLOW, "...");
61-
outLines(lines.subList(lines.size() - secondHalfNumberOfLines, lines.size()), styleOrValueExtractor);
69+
getOutputsStream().forEach(o -> o.outLinesWithLimit(lines, limit, styleOrValueExtractor));
6270
}
6371

6472
public static void add(ConsoleOutput consoleOutput) {
@@ -77,7 +85,15 @@ private static Stream<ConsoleOutput> getOutputsStream() {
7785
return outputs.stream();
7886
}
7987

80-
private static <E> void outLines(List<E> lines, Function<E, Object[]> styleOrValueExtractor) {
81-
lines.forEach(l -> ConsoleOutputs.out(styleOrValueExtractor.apply(l)));
88+
private static class CombinedConsoleOutput implements ConsoleOutput {
89+
@Override
90+
public void out(Object... styleOrValues) {
91+
ConsoleOutputs.out((styleOrValues));
92+
}
93+
94+
@Override
95+
public void err(Object... styleOrValues) {
96+
ConsoleOutputs.err((styleOrValues));
97+
}
8298
}
8399
}

webtau-core-groovy/src/test/groovy/org/testingisdocumenting/webtau/data/table/TableDataGroovyTest.groovy

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ class TableDataGroovyTest implements ConsoleOutput {
134134
20 | "world"
135135
30 | null }
136136

137-
table.prettyPrint()
137+
table.prettyPrint(ConsoleOutputs.asCombinedConsoleOutput())
138138

139139
capturedOutLines.join("\n").should == "\u001B[33mcolumn A\u001B[33m, \u001B[0m\u001B[33mcolumn B\u001B[0m\n" +
140140
"\u001B[36m 10\u001B[0m\u001B[33m, \u001B[0m\"hello\" \n" +

webtau-core/src/main/java/org/testingisdocumenting/webtau/data/render/PrettyPrintable.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@
1616

1717
package org.testingisdocumenting.webtau.data.render;
1818

19+
import org.testingisdocumenting.webtau.console.ConsoleOutput;
20+
1921
/**
2022
* Print value to the console using ANSI colors
21-
* Used in e.g. REPL
23+
* Used in e.g. REPL, {@link org.testingisdocumenting.webtau.reporter.ConsoleStepReporter}
2224
*/
2325
public interface PrettyPrintable {
24-
void prettyPrint();
26+
void prettyPrint(ConsoleOutput console);
2527
}

webtau-core/src/main/java/org/testingisdocumenting/webtau/data/table/TableData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
package org.testingisdocumenting.webtau.data.table;
1919

20-
import org.testingisdocumenting.webtau.console.ConsoleOutputs;
20+
import org.testingisdocumenting.webtau.console.ConsoleOutput;
2121
import org.testingisdocumenting.webtau.data.render.PrettyPrintable;
2222
import org.testingisdocumenting.webtau.data.render.TableDataRenderer;
2323
import org.testingisdocumenting.webtau.data.table.header.CompositeKey;
@@ -243,7 +243,7 @@ private static List<String> extractColumnNames(Stream<?> columnNameAndValues) {
243243
}
244244

245245
@Override
246-
public void prettyPrint() {
247-
ConsoleOutputs.out(PrettyPrintTableRenderer.render(this));
246+
public void prettyPrint(ConsoleOutput console) {
247+
console.out(PrettyPrintTableRenderer.render(this));
248248
}
249249
}

0 commit comments

Comments
 (0)