Skip to content

Commit a508d23

Browse files
repl: PrettyPrintable interface
1 parent e776dfe commit a508d23

9 files changed

Lines changed: 280 additions & 28 deletions

File tree

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import org.testingisdocumenting.webtau.console.ConsoleOutputs;
2121
import org.testingisdocumenting.webtau.console.ansi.Color;
2222
import org.testingisdocumenting.webtau.console.ansi.FontStyle;
23+
import org.testingisdocumenting.webtau.data.render.PrettyPrintable;
2324
import org.testingisdocumenting.webtau.expectation.timer.SystemTimerConfig;
2425
import org.testingisdocumenting.webtau.persona.Persona;
2526
import org.testingisdocumenting.webtau.utils.ServiceLoaderUtils;
@@ -37,7 +38,7 @@
3738
import static org.testingisdocumenting.webtau.cfg.ConfigValue.declareBoolean;
3839
import static org.testingisdocumenting.webtau.documentation.DocumentationArtifactsLocation.DEFAULT_DOC_ARTIFACTS_DIR_NAME;
3940

40-
public class WebTauConfig {
41+
public class WebTauConfig implements PrettyPrintable {
4142
private static final String SOURCE_MANUAL = "manual";
4243

4344
public static final String CONFIG_FILE_DEPRECATED_DEFAULT = "webtau.cfg";
@@ -309,11 +310,6 @@ public void printEnumerated() {
309310
printConfig(enumeratedCfgValues.values());
310311
}
311312

312-
public void printAll() {
313-
printConfig(freeFormCfgValues);
314-
printConfig(enumeratedCfgValues.values());
315-
}
316-
317313
private void printConfig(Collection<ConfigValue> configValues) {
318314
int maxKeyLength = configValues.stream()
319315
.filter(ConfigValue::nonDefault)
@@ -410,6 +406,12 @@ private Map<String, ConfigValue> enumerateRegisteredConfigValues() {
410406
.collect(Collectors.toMap(ConfigValue::getKey, v -> v, (o, n) -> n, LinkedHashMap::new));
411407
}
412408

409+
@Override
410+
public void prettyPrint() {
411+
printConfig(freeFormCfgValues);
412+
printConfig(enumeratedCfgValues.values());
413+
}
414+
413415
private static class CfgInstanceHolder {
414416
private static final WebTauConfig INSTANCE = new WebTauConfig();
415417
}

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

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

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

20+
import org.junit.After
21+
import org.junit.Before
2022
import org.junit.Test
23+
import org.testingisdocumenting.webtau.console.ConsoleOutput
24+
import org.testingisdocumenting.webtau.console.ConsoleOutputs
2125

2226
import java.time.LocalDate
2327

@@ -29,7 +33,20 @@ import static org.testingisdocumenting.webtau.data.table.TableDataJavaTestValida
2933
import static org.testingisdocumenting.webtau.data.table.TableDataJavaTestValidations.validateSimpleTableData
3034
import static org.testingisdocumenting.webtau.data.table.TableDataJavaTestValidations.validateSimpleTableDataAfterReplace
3135

32-
class TableDataGroovyTest {
36+
class TableDataGroovyTest implements ConsoleOutput {
37+
private List<String> capturedOutLines = []
38+
39+
@Before
40+
void init() {
41+
capturedOutLines = []
42+
ConsoleOutputs.add(this)
43+
}
44+
45+
@After
46+
void clean() {
47+
ConsoleOutputs.remove(this)
48+
}
49+
3350
@Test
3451
void "should register header and values using pipes"() {
3552
def tableData = createTableWithUnderscore()
@@ -109,6 +126,22 @@ class TableDataGroovyTest {
109126
"{id=id3, Name=N, Type=T}")
110127
}
111128

129+
@Test
130+
void "should pretty print"() {
131+
def table = ["column A" | "column B"] {
132+
____________________________
133+
10 | "hello"
134+
20 | "world"
135+
30 | null }
136+
137+
table.prettyPrint()
138+
139+
capturedOutLines.join("\n").should == "\u001B[33mcolumn A\u001B[33m, \u001B[0m\u001B[33mcolumn B\u001B[0m\n" +
140+
"\u001B[36m 10\u001B[0m\u001B[33m, \u001B[0m\"hello\" \n" +
141+
"\u001B[36m 20\u001B[0m\u001B[33m, \u001B[0m\"world\" \n" +
142+
"\u001B[36m 30\u001B[0m\u001B[33m, \u001B[0m\u001B[33m[null] \u001B[0m\n"
143+
}
144+
112145
private static TableData replaceValue(TableData tableData) {
113146
tableData.replace("v1b", "v1b_")
114147
}
@@ -195,4 +228,14 @@ class TableDataGroovyTest {
195228
"id2" | "N2" | "T2"
196229
"id3" | "N" | "T" }
197230
}
231+
232+
@Override
233+
void out(Object... styleOrValues) {
234+
capturedOutLines << styleOrValues.join("")
235+
}
236+
237+
@Override
238+
void err(Object... styleOrValues) {
239+
240+
}
198241
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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.data.render;
18+
19+
/**
20+
* Print value to the console using ANSI colors
21+
* Used in e.g. REPL
22+
*/
23+
public interface PrettyPrintable {
24+
void prettyPrint();
25+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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.data.table;
18+
19+
import org.testingisdocumenting.webtau.console.ansi.Color;
20+
import org.testingisdocumenting.webtau.data.table.render.TableRenderStyle;
21+
22+
class PrettyPrintReplTableRenderStyle implements TableRenderStyle {
23+
private static final String ANSI_COMMA = Color.YELLOW.toString() + ", " + Color.RESET.toString();
24+
25+
@Override
26+
public String headerMidLeft() {
27+
return Color.YELLOW.toString();
28+
}
29+
30+
@Override
31+
public String headerMidMid() {
32+
return ANSI_COMMA + Color.YELLOW;
33+
}
34+
35+
@Override
36+
public String headerMidRight() {
37+
return Color.RESET.toString();
38+
}
39+
40+
@Override
41+
public String headerBotLeft() {
42+
return "*";
43+
}
44+
45+
@Override
46+
public String headerBotMid() {
47+
return "*";
48+
}
49+
50+
@Override
51+
public String headerBotRight() {
52+
return "*";
53+
}
54+
55+
@Override
56+
public String headerBotFill() {
57+
return null;
58+
}
59+
60+
@Override
61+
public String bodyMidLeft() {
62+
return "";
63+
}
64+
65+
@Override
66+
public String bodyMidMid() {
67+
return ANSI_COMMA;
68+
}
69+
70+
@Override
71+
public String bodyMidRight() {
72+
return "";
73+
}
74+
75+
@Override
76+
public String bodyBotLeft() {
77+
return "";
78+
}
79+
80+
@Override
81+
public String bodyBotMid() {
82+
return "";
83+
}
84+
85+
@Override
86+
public String bodyBotRight() {
87+
return "";
88+
}
89+
90+
@Override
91+
public String bodyBotFill() {
92+
return null;
93+
}
94+
}
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.data.table;
18+
19+
import org.apache.commons.lang3.StringUtils;
20+
import org.testingisdocumenting.webtau.console.ansi.Color;
21+
import org.testingisdocumenting.webtau.data.render.DataRenderers;
22+
import org.testingisdocumenting.webtau.data.table.render.TableCellDataRenderer;
23+
24+
class PrettyPrintTableCellDataRenderer implements TableCellDataRenderer {
25+
@Override
26+
public String renderCell(Object value) {
27+
return DataRenderers.render(value);
28+
}
29+
30+
@Override
31+
public Integer valueWidth(Object value) {
32+
return DataRenderers.render(value).length();
33+
}
34+
35+
@Override
36+
public String wrapBeforeRender(Object original, String rendered) {
37+
if (original == null) {
38+
return Color.YELLOW + rendered + Color.RESET;
39+
}
40+
41+
42+
if (original instanceof Number) {
43+
return Color.CYAN + rendered + Color.RESET.toString();
44+
}
45+
46+
47+
return rendered;
48+
}
49+
50+
@Override
51+
public String align(Object original, String rendered, Integer width, String fill) {
52+
if (original instanceof Number) {
53+
return StringUtils.leftPad(rendered, width, fill);
54+
}
55+
56+
return StringUtils.rightPad(rendered, width, fill);
57+
}
58+
59+
@Override
60+
public boolean useDefaultWidth() {
61+
return true;
62+
}
63+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
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.data.table;
18+
19+
import org.testingisdocumenting.webtau.data.table.render.TableRenderer;
20+
21+
class PrettyPrintTableRenderer {
22+
public static String render(TableData tableData) {
23+
return TableRenderer.render(tableData,
24+
new PrettyPrintTableCellDataRenderer(),
25+
new PrettyPrintReplTableRenderStyle());
26+
}
27+
}

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

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

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

20+
import org.testingisdocumenting.webtau.console.ConsoleOutputs;
21+
import org.testingisdocumenting.webtau.data.render.PrettyPrintable;
2022
import org.testingisdocumenting.webtau.data.render.TableDataRenderer;
2123
import org.testingisdocumenting.webtau.data.table.header.CompositeKey;
2224
import org.testingisdocumenting.webtau.data.table.header.TableDataHeader;
@@ -32,7 +34,7 @@
3234
/**
3335
* Represents a set of rows with named columns to be used as part of test input preparation and/or test output validation
3436
*/
35-
public class TableData implements Iterable<Record> {
37+
public class TableData implements Iterable<Record>, PrettyPrintable {
3638
private final List<Record> rows;
3739
private final Map<CompositeKey, Record> rowsByKey;
3840
private final Map<CompositeKey, Integer> rowIdxByKey;
@@ -239,4 +241,9 @@ private static List<String> extractColumnNames(Stream<?> columnNameAndValues) {
239241

240242
return result;
241243
}
244+
245+
@Override
246+
public void prettyPrint() {
247+
ConsoleOutputs.out(PrettyPrintTableRenderer.render(this));
248+
}
242249
}

0 commit comments

Comments
 (0)