Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions src/main/java/rickwporter/prettytable/BuilderConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public final class BuilderConfig {
private List<String> keyFields = new ArrayList<String>(Arrays.asList("name", "id"));
private int keyMaxLen = DEFAULT_KEY_MAX_LEN;
private int valueMaxLen = DEFAULT_VALUE_MAX_LEN;
private OutputFormat format = OutputFormat.RICH;
private List<String> columns = null;

public String getItemsLabel() {
Expand Down Expand Up @@ -182,6 +183,15 @@ public BuilderConfig setValueMaxLen(int maxLen) {
return this;
}

public OutputFormat getFormat() {
return this.format;
}

public BuilderConfig setFormat(OutputFormat outputFormat) {
this.format = Objects.requireNonNull(outputFormat);
return this;
}

public List<String> getColumns() {
return this.columns;
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/java/rickwporter/prettytable/TableBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public TableBuilder(BuilderConfig options) {
this.config = options == null ? new BuilderConfig() : options;
}

public PrettyTable build(OutputFormat format, Object data) {
OutputFormat original = this.config.getFormat();
this.config.setFormat(format);

// store the result while we set the format back to the original
PrettyTable result = this.build(data);
this.config.setFormat(original);
return result;
}

public PrettyTable build(Object data) {
if (isNothing(data)) {
return null;
Expand Down Expand Up @@ -171,6 +181,7 @@ private Object cellValue(Object obj) {
}

private void configure(PrettyTable result, boolean outer, boolean dividers, String caption) {
result.setOutputFormat(this.config.getFormat());
result.setShowHeader(outer);
result.setShowLines(outer);
result.setRowDividers(dividers);
Expand Down
7 changes: 7 additions & 0 deletions src/test/java/rickwporter/prettytable/BuilderConfigTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public void testDefaults() {
Assertions.assertEquals(Arrays.asList("name", "id"), config.getKeyFields());
Assertions.assertEquals(35, config.getKeyMaxLen());
Assertions.assertEquals(50, config.getValueMaxLen());
Assertions.assertEquals(OutputFormat.RICH, config.getFormat());
Assertions.assertNull(config.getColumns());
}

Expand Down Expand Up @@ -90,6 +91,9 @@ public void testSetters() {
Assertions.assertSame(config, config.setValueMaxLen(3));
Assertions.assertEquals(3, config.getValueMaxLen());

Assertions.assertSame(config, config.setFormat(OutputFormat.JSON));
Assertions.assertEquals(OutputFormat.JSON, config.getFormat());

List<String> columns = new ArrayList<String>(Arrays.asList("service", "name"));
Assertions.assertSame(config, config.setColumns(columns));
columns.add("id");
Expand Down Expand Up @@ -147,5 +151,8 @@ public void testSettersRejectNull() {

Assertions.assertThrows(NullPointerException.class, () -> config.setKeyFields(null));
Assertions.assertEquals(Arrays.asList("name", "id"), config.getKeyFields());

Assertions.assertThrows(NullPointerException.class, () -> config.setFormat(null));
Assertions.assertEquals(OutputFormat.RICH, config.getFormat());
}
}
65 changes: 59 additions & 6 deletions src/test/java/rickwporter/prettytable/TableBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,10 @@ private List<Object> columnList() {
}

private void assertRendered(String filename, Object data) {
this.assertRendered(filename, data, null);
// original TableBuilder tests were done with text, so use that as the default
BuilderConfig config = new BuilderConfig();
config.setFormat(OutputFormat.TEXT);
this.assertRendered(filename, data, config);
}

private void assertRendered(String filename, Object data, BuilderConfig options) {
Expand Down Expand Up @@ -218,6 +221,38 @@ public void testInnerList() {
Assertions.assertEquals("True", props.getCell(1, 1));
}

@Test
public void testInnerListFormats() {
TableBuilder builder = new TableBuilder();
String actual;
String expected;

actual = builder.build(OutputFormat.RICH, this.innerList()).toString();
expected = this.loadFileContent("TableBuilder_inner.txt");
Assertions.assertEquals(expected, actual);

actual = builder.build(OutputFormat.TEXT, this.innerList()).toString();
expected = this.loadFileContent("TableBuilder_inner.text");
Assertions.assertEquals(expected, actual);

actual = builder.build(OutputFormat.JSON, this.innerList()).toString();
expected = this.loadFileContent("TableBuilder_inner.json");
Assertions.assertEquals(expected, actual);

actual = builder.build(OutputFormat.HTML, this.innerList()).toString();
expected = this.loadFileContent("TableBuilder_inner.html");
Assertions.assertEquals(expected, actual);

actual = builder.build(OutputFormat.CSV, this.innerList()).toString();
expected = this.loadFileContent("TableBuilder_inner.csv");
Assertions.assertEquals(expected, actual);

// still at RICH output
actual = builder.build(this.innerList()).toString();
expected = this.loadFileContent("TableBuilder_inner.txt");
Assertions.assertEquals(expected, actual);
}

@Test
public void testReadmeList() {
this.assertRendered("TableBuilder_readme.text", this.readmeList());
Expand Down Expand Up @@ -260,6 +295,7 @@ public void testTruncated() {
@Test
public void testConfigTruncated() {
BuilderConfig options = new BuilderConfig()
.setFormat(OutputFormat.TEXT)
.setUrlMaxLen(16)
.setValueMaxLen(20)
.setKeyMaxLen(4);
Expand All @@ -269,6 +305,7 @@ public void testConfigTruncated() {
@Test
public void testConfigLabels() {
BuilderConfig options = new BuilderConfig()
.setFormat(OutputFormat.TEXT)
.setUrlMaxLen(16)
.setValueMaxLen(50)
.setKeyMaxLen(100)
Expand All @@ -281,6 +318,7 @@ public void testConfigLabels() {
@Test
public void testConfigInnerList() {
BuilderConfig options = new BuilderConfig()
.setFormat(OutputFormat.TEXT)
.setKeyFields(Arrays.asList("ghi"))
.setPropertiesLabel("Different");
this.assertRendered("TableBuilder_config_inner.text", this.innerList(), options);
Expand All @@ -289,18 +327,18 @@ public void testConfigInnerList() {
@Test
public void testColumns() {
this.assertRendered("TableBuilder_columns.text", this.columnList(),
new BuilderConfig().setColumns(Arrays.asList("service", "name")));
new BuilderConfig().setFormat(OutputFormat.TEXT).setColumns(Arrays.asList("service", "name")));
this.assertRendered("TableBuilder_columns_wild.text", this.columnList(),
new BuilderConfig().setColumns(Arrays.asList("service", "*", "id")));
new BuilderConfig().setFormat(OutputFormat.TEXT).setColumns(Arrays.asList("service", "*", "id")));
this.assertRendered("TableBuilder_columns_missing.text", this.columnList(),
new BuilderConfig().setColumns(Arrays.asList("id", "missing", "name")));
new BuilderConfig().setFormat(OutputFormat.TEXT).setColumns(Arrays.asList("id", "missing", "name")));
}

@Test
public void testEmptyColumnsUsesDefaultList() {
List<Object> items = new ArrayList<Object>();
items.add(mapOf("name", "a", "x", 1, "y", 2));
PrettyTable table = new TableBuilder(new BuilderConfig().setColumns(new ArrayList<String>())).build(items);
PrettyTable table = new TableBuilder(new BuilderConfig().setFormat(OutputFormat.TEXT).setColumns(new ArrayList<String>())).build(items);
Assertions.assertEquals(Arrays.asList("Name", "Properties"), table.getHeaders());
}

Expand All @@ -314,6 +352,7 @@ public void testEmptyListValue() {
@Test
public void testCaptionWrap() {
BuilderConfig options = new BuilderConfig()
.setFormat(OutputFormat.TEXT)
.setItemsLabel("Simple stuff")
.setItemsCaption("Got {} simple things");
this.assertRendered("TableBuilder_caption.text", this.simpleList(), options);
Expand All @@ -322,6 +361,7 @@ public void testCaptionWrap() {
@Test
public void testCaptionLong() {
BuilderConfig options = new BuilderConfig()
.setFormat(OutputFormat.TEXT)
.setItemsCaption("ABCDEFGHIJKLMNOPQRSTUVWXYZ");
this.assertRendered("TableBuilder_caption_long.text", Arrays.asList("a"), options);
}
Expand Down Expand Up @@ -368,13 +408,14 @@ public void testDoesNotMutateInput() {
@Test
public void testColumnsIgnoredForMap() {
BuilderConfig options = new BuilderConfig()
.setFormat(OutputFormat.TEXT)
.setColumns(Arrays.asList("abc"));
this.assertRendered("TableBuilder_simple_dict.text", this.simpleDict(), options);
}

@Test
public void testNullConfigUsesDefaults() {
this.assertRendered("TableBuilder_simple_dict.text", this.simpleDict(), null);
this.assertRendered("TableBuilder_simple_dict.txt", this.simpleDict(), null);
}

@Test
Expand Down Expand Up @@ -422,6 +463,18 @@ public void testWildcardIgnoresPropertiesLabel() {
Assertions.assertFalse(rendered.contains("Different"));
}

@Test
public void testOutputFormat() {
PrettyTable defaults = new TableBuilder().build(this.innerList());
Assertions.assertEquals(OutputFormat.RICH, defaults.getDefaultOutput());
Assertions.assertEquals(OutputFormat.RICH, ((PrettyTable) defaults.getCell(1, 1)).getDefaultOutput());

BuilderConfig options = new BuilderConfig().setFormat(OutputFormat.JSON);
PrettyTable table = new TableBuilder(options).build(this.innerList());
Assertions.assertEquals(OutputFormat.JSON, table.getDefaultOutput());
Assertions.assertEquals(OutputFormat.JSON, ((PrettyTable) table.getCell(1, 1)).getDefaultOutput());
}

@Test
public void testUnsupportedType() {
IllegalArgumentException ex = Assertions.assertThrows(IllegalArgumentException.class, () -> {
Expand Down
21 changes: 21 additions & 0 deletions src/test/resources/TableBuilder_inner.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
Property,Value
prop 1,simple
prOp B,"Name,Properties
sna,""Property,Value
abc,def
ghi,True
""
foo,""Property,Value
abc,def
ghi,None
""
bar,""Property,Value
abc,def
ghi,1.2345
""
Unknown,""Property,Value
abc,def
ghi,blah
""
"
Prop III,None
125 changes: 125 additions & 0 deletions src/test/resources/TableBuilder_inner.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<table>
<thead>
<tr>
<th style="text-align:left">Property</th>
<th style="text-align:left">Value</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">prop 1</td>
<td style="text-align:left">simple</td>
</tr>
<tr>
<td style="text-align:left">prOp B</td>
<td style="text-align:left">
<table>
<thead>
<tr>
<th style="text-align:left">Name</th>
<th style="text-align:left">Properties</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">sna</td>
<td style="text-align:left">
<table>
<thead>
<tr>
<th style="text-align:left">Property</th>
<th style="text-align:left">Value</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">abc</td>
<td style="text-align:left">def</td>
</tr>
<tr>
<td style="text-align:left">ghi</td>
<td style="text-align:left">True</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td style="text-align:left">foo</td>
<td style="text-align:left">
<table>
<thead>
<tr>
<th style="text-align:left">Property</th>
<th style="text-align:left">Value</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">abc</td>
<td style="text-align:left">def</td>
</tr>
<tr>
<td style="text-align:left">ghi</td>
<td style="text-align:left">None</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td style="text-align:left">bar</td>
<td style="text-align:left">
<table>
<thead>
<tr>
<th style="text-align:left">Property</th>
<th style="text-align:left">Value</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">abc</td>
<td style="text-align:left">def</td>
</tr>
<tr>
<td style="text-align:left">ghi</td>
<td style="text-align:left">1.2345</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td style="text-align:left">Unknown</td>
<td style="text-align:left">
<table>
<thead>
<tr>
<th style="text-align:left">Property</th>
<th style="text-align:left">Value</th>
</tr>
</thead>
<tbody>
<tr>
<td style="text-align:left">abc</td>
<td style="text-align:left">def</td>
</tr>
<tr>
<td style="text-align:left">ghi</td>
<td style="text-align:left">blah</td>
</tr>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td style="text-align:left">Prop III</td>
<td style="text-align:left">None</td>
</tr>
</tbody>
</table>
Loading
Loading