diff --git a/src/main/java/rickwporter/prettytable/BuilderConfig.java b/src/main/java/rickwporter/prettytable/BuilderConfig.java index 7c2812c..c4dc6e6 100644 --- a/src/main/java/rickwporter/prettytable/BuilderConfig.java +++ b/src/main/java/rickwporter/prettytable/BuilderConfig.java @@ -27,6 +27,7 @@ public final class BuilderConfig { private List keyFields = new ArrayList(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 columns = null; public String getItemsLabel() { @@ -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 getColumns() { return this.columns; } diff --git a/src/main/java/rickwporter/prettytable/TableBuilder.java b/src/main/java/rickwporter/prettytable/TableBuilder.java index 4ac9f0f..c4ec65f 100644 --- a/src/main/java/rickwporter/prettytable/TableBuilder.java +++ b/src/main/java/rickwporter/prettytable/TableBuilder.java @@ -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; @@ -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); diff --git a/src/test/java/rickwporter/prettytable/BuilderConfigTest.java b/src/test/java/rickwporter/prettytable/BuilderConfigTest.java index e1d2d49..0c152fc 100644 --- a/src/test/java/rickwporter/prettytable/BuilderConfigTest.java +++ b/src/test/java/rickwporter/prettytable/BuilderConfigTest.java @@ -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()); } @@ -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 columns = new ArrayList(Arrays.asList("service", "name")); Assertions.assertSame(config, config.setColumns(columns)); columns.add("id"); @@ -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()); } } diff --git a/src/test/java/rickwporter/prettytable/TableBuilderTest.java b/src/test/java/rickwporter/prettytable/TableBuilderTest.java index ba6bf2a..b941a5a 100644 --- a/src/test/java/rickwporter/prettytable/TableBuilderTest.java +++ b/src/test/java/rickwporter/prettytable/TableBuilderTest.java @@ -106,7 +106,10 @@ private List 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) { @@ -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()); @@ -260,6 +295,7 @@ public void testTruncated() { @Test public void testConfigTruncated() { BuilderConfig options = new BuilderConfig() + .setFormat(OutputFormat.TEXT) .setUrlMaxLen(16) .setValueMaxLen(20) .setKeyMaxLen(4); @@ -269,6 +305,7 @@ public void testConfigTruncated() { @Test public void testConfigLabels() { BuilderConfig options = new BuilderConfig() + .setFormat(OutputFormat.TEXT) .setUrlMaxLen(16) .setValueMaxLen(50) .setKeyMaxLen(100) @@ -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); @@ -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 items = new ArrayList(); items.add(mapOf("name", "a", "x", 1, "y", 2)); - PrettyTable table = new TableBuilder(new BuilderConfig().setColumns(new ArrayList())).build(items); + PrettyTable table = new TableBuilder(new BuilderConfig().setFormat(OutputFormat.TEXT).setColumns(new ArrayList())).build(items); Assertions.assertEquals(Arrays.asList("Name", "Properties"), table.getHeaders()); } @@ -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); @@ -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); } @@ -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 @@ -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, () -> { diff --git a/src/test/resources/TableBuilder_inner.csv b/src/test/resources/TableBuilder_inner.csv new file mode 100644 index 0000000..3dce6fc --- /dev/null +++ b/src/test/resources/TableBuilder_inner.csv @@ -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 diff --git a/src/test/resources/TableBuilder_inner.html b/src/test/resources/TableBuilder_inner.html new file mode 100644 index 0000000..c79e584 --- /dev/null +++ b/src/test/resources/TableBuilder_inner.html @@ -0,0 +1,125 @@ + + + + + + + + + + + + + + + + + + + + + +
PropertyValue
prop 1simple
prOp B + + + + + + + + + + + + + + + + + + + + + + + + + +
NameProperties
sna + + + + + + + + + + + + + + + + + +
PropertyValue
abcdef
ghiTrue
+
foo + + + + + + + + + + + + + + + + + +
PropertyValue
abcdef
ghiNone
+
bar + + + + + + + + + + + + + + + + + +
PropertyValue
abcdef
ghi1.2345
+
Unknown + + + + + + + + + + + + + + + + + +
PropertyValue
abcdef
ghiblah
+
+
Prop IIINone
diff --git a/src/test/resources/TableBuilder_inner.json b/src/test/resources/TableBuilder_inner.json new file mode 100644 index 0000000..6fa9af5 --- /dev/null +++ b/src/test/resources/TableBuilder_inner.json @@ -0,0 +1,91 @@ +[ + [ + "Property", + "Value" + ], + { + "Property": "prop 1", + "Value": "simple" + }, + { + "Property": "prOp B", + "Value": [ + [ + "Name", + "Properties" + ], + { + "Name": "sna", + "Properties": [ + [ + "Property", + "Value" + ], + { + "Property": "abc", + "Value": "def" + }, + { + "Property": "ghi", + "Value": "True" + } + ] + }, + { + "Name": "foo", + "Properties": [ + [ + "Property", + "Value" + ], + { + "Property": "abc", + "Value": "def" + }, + { + "Property": "ghi", + "Value": "None" + } + ] + }, + { + "Name": "bar", + "Properties": [ + [ + "Property", + "Value" + ], + { + "Property": "abc", + "Value": "def" + }, + { + "Property": "ghi", + "Value": "1.2345" + } + ] + }, + { + "Name": "Unknown", + "Properties": [ + [ + "Property", + "Value" + ], + { + "Property": "abc", + "Value": "def" + }, + { + "Property": "ghi", + "Value": "blah" + } + ] + } + ] + }, + { + "Property": "Prop III", + "Value": "None" + } +] diff --git a/src/test/resources/TableBuilder_inner.txt b/src/test/resources/TableBuilder_inner.txt new file mode 100644 index 0000000..b8a908b --- /dev/null +++ b/src/test/resources/TableBuilder_inner.txt @@ -0,0 +1,14 @@ +┏━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━━━━┓ +┃ Property ┃ Value ┃ +┡━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━━━━┩ +│ prop 1 │ simple │ +│ prOp B │ sna abc def │ +│ │ ghi True │ +│ │ foo abc def │ +│ │ ghi None │ +│ │ bar abc def │ +│ │ ghi 1.2345 │ +│ │ Unknown abc def │ +│ │ ghi blah │ +│ Prop III │ None │ +└──────────┴──────────────────────────┘ diff --git a/src/test/resources/TableBuilder_simple_dict.txt b/src/test/resources/TableBuilder_simple_dict.txt new file mode 100644 index 0000000..af148e5 --- /dev/null +++ b/src/test/resources/TableBuilder_simple_dict.txt @@ -0,0 +1,10 @@ +┏━━━━━━━━━━┳━━━━━━━━━━━━━━━┓ +┃ Property ┃ Value ┃ +┡━━━━━━━━━━╇━━━━━━━━━━━━━━━┩ +│ abc │ def │ +│ ghi │ False │ +│ jkl │ mno, pqr, stu │ +│ vwx │ 1, 2, 4 │ +│ 2 │ 3 │ +│ yxa │ None │ +└──────────┴───────────────┘