Bring back missing table tests
This commit is contained in:
@@ -25,7 +25,7 @@
|
||||
<artifactId>spring-shell-parent</artifactId>
|
||||
<version>3.0.0-SNAPSHOT</version>
|
||||
</parent>
|
||||
|
||||
|
||||
<description>Library to Display Fancy ASCII art Tables</description>
|
||||
|
||||
<dependencies>
|
||||
@@ -33,6 +33,11 @@
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-beans</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2015-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.table;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.InputStreamReader;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.TestInfo;
|
||||
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.FileCopyUtils;
|
||||
|
||||
/**
|
||||
* Base class that allows reading a sample result rendering of a table, based on the actual
|
||||
* class and method name of the test.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class AbstractTestWithSample {
|
||||
|
||||
private String testName;
|
||||
|
||||
@BeforeEach
|
||||
public void setup(TestInfo testInfo) {
|
||||
testName = testInfo.getDisplayName().replace("()", "");
|
||||
}
|
||||
|
||||
protected String sample() throws IOException {
|
||||
String sampleName = String.format("%s-%s.txt",
|
||||
this.getClass().getSimpleName(), testName);
|
||||
InputStream stream = TableTest.class.getResourceAsStream(sampleName);
|
||||
Assert.notNull(stream, "Can't find expected rendering result at " + sampleName);
|
||||
return FileCopyUtils.copyToString(new InputStreamReader(stream, "UTF-8")).replace("&", "");
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a simple rows x columns model made of chars.
|
||||
*/
|
||||
protected TableModel generate(int rows, int columns) {
|
||||
Character[][] data = new Character[rows][columns];
|
||||
for (int row = 0; row < rows; row++) {
|
||||
data[row] = new Character[columns];
|
||||
for (int column = 0; column < columns; column++) {
|
||||
data[row][column] = (char) ('a' + row * columns + column);
|
||||
}
|
||||
}
|
||||
return new ArrayTableModel(data);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* Copyright 2015-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.table;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
/**
|
||||
* Tests for ArrayTableModel.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class ArrayTableModelTest {
|
||||
|
||||
@Test
|
||||
public void testValid() {
|
||||
TableModel model = new ArrayTableModel(new String[][] {{"a", "b"}, {"c", "d"}});
|
||||
assertThat(model.getColumnCount()).isEqualTo(2);
|
||||
assertThat(model.getRowCount()).isEqualTo(2);
|
||||
assertThat(model.getValue(0, 1)).isEqualTo("b");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmpty() {
|
||||
TableModel model = new ArrayTableModel(new String[][] {});
|
||||
assertThat(model.getColumnCount()).isEqualTo(0);
|
||||
assertThat(model.getRowCount()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInvalidDimensions() {
|
||||
assertThatThrownBy(() -> {
|
||||
new ArrayTableModel(new String[][] {{"a", "b"}, {"c", "d", "e"}});
|
||||
}).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* Copyright 2015-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.table;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
/**
|
||||
* Test for BeanListTableModel.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class BeanListTableModelTest extends AbstractTestWithSample {
|
||||
|
||||
@Test
|
||||
public void testSimpleConstructor() throws IOException {
|
||||
|
||||
List<Person> data = data();
|
||||
|
||||
Table table = new TableBuilder(new BeanListTableModel<Person>(Person.class, data)).build();
|
||||
String result = table.render(80);
|
||||
assertThat(result).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExplicitPropertyNames() throws IOException {
|
||||
|
||||
List<Person> data = data();
|
||||
|
||||
Table table = new TableBuilder(new BeanListTableModel<Person>(data, "lastName", "firstName")).build();
|
||||
String result = table.render(80);
|
||||
assertThat(result).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeaderRow() throws IOException {
|
||||
|
||||
List<Person> data = data();
|
||||
|
||||
LinkedHashMap<String, Object> header = new LinkedHashMap<String, Object>();
|
||||
header.put("lastName", "Last Name");
|
||||
header.put("firstName", "First Name");
|
||||
|
||||
Table table = new TableBuilder(new BeanListTableModel<Person>(data, header)).build();
|
||||
String result = table.render(80);
|
||||
assertThat(result).isEqualTo(sample());
|
||||
}
|
||||
|
||||
private List<Person> data() {
|
||||
List<Person> data = new ArrayList<Person>();
|
||||
data.add(new Person("Alice", "Clark", 12));
|
||||
data.add(new Person("Bob", "Smith", 42));
|
||||
data.add(new Person("Sarah", "Connor", 38));
|
||||
return data;
|
||||
}
|
||||
|
||||
public static class Person {
|
||||
private int age;
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
public Person(String firstName, String lastName, int age) {
|
||||
this.age = age;
|
||||
this.firstName = firstName;
|
||||
this.lastName = lastName;
|
||||
}
|
||||
|
||||
public int getAge() {
|
||||
return age;
|
||||
}
|
||||
|
||||
public String getFirstName() {
|
||||
return firstName;
|
||||
}
|
||||
|
||||
public String getLastName() {
|
||||
return lastName;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
/*
|
||||
* Copyright 2015-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.table;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.shell.table.BorderStyle.fancy_double;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for convenience borders factory.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class BorderFactoryTest extends AbstractTestWithSample {
|
||||
|
||||
@Test
|
||||
public void testOutlineBorder() throws IOException {
|
||||
TableModel model = generate(3, 3);
|
||||
Table table = new TableBuilder(model).addOutlineBorder(fancy_double).build();
|
||||
String result = table.render(80);
|
||||
assertThat(result).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFullBorder() throws IOException {
|
||||
TableModel model = generate(3, 3);
|
||||
Table table = new TableBuilder(model).addFullBorder(fancy_double).build();
|
||||
String result = table.render(80);
|
||||
assertThat(result).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeaderBorder() throws IOException {
|
||||
TableModel model = generate(3, 3);
|
||||
Table table = new TableBuilder(model).addHeaderBorder(fancy_double).build();
|
||||
String result = table.render(80);
|
||||
assertThat(result).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeaderAndVerticalsBorder() throws IOException {
|
||||
TableModel model = generate(3, 3);
|
||||
Table table = new TableBuilder(model).addHeaderAndVerticalsBorders(fancy_double).build();
|
||||
String result = table.render(80);
|
||||
assertThat(result).isEqualTo(sample());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,136 @@
|
||||
/*
|
||||
* Copyright 2015-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.table;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for BorderStyle rendering and combinations.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class BorderStyleTests extends AbstractTestWithSample {
|
||||
|
||||
@Test
|
||||
public void testOldSchool() throws IOException {
|
||||
Table table = new TableBuilder(generate(2, 2)).addFullBorder(BorderStyle.oldschool).build();
|
||||
assertThat(table.render(10)).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFancySimple() throws IOException {
|
||||
Table table = new TableBuilder(generate(2, 2)).addFullBorder(BorderStyle.fancy_light).build();
|
||||
assertThat(table.render(10)).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFancyHeavy() throws IOException {
|
||||
Table table = new TableBuilder(generate(2, 2)).addFullBorder(BorderStyle.fancy_heavy).build();
|
||||
assertThat(table.render(10)).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFancyDouble() throws IOException {
|
||||
Table table = new TableBuilder(generate(2, 2)).addFullBorder(BorderStyle.fancy_double).build();
|
||||
assertThat(table.render(10)).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAir() throws IOException {
|
||||
Table table = new TableBuilder(generate(2, 2)).addFullBorder(BorderStyle.air).build();
|
||||
assertThat(table.render(10)).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMixedOldSchoolWithAir() throws IOException {
|
||||
Table table = new TableBuilder(generate(2, 2))
|
||||
.addFullBorder(BorderStyle.air)
|
||||
.addOutlineBorder(BorderStyle.oldschool)
|
||||
.build();
|
||||
assertThat(table.render(10)).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMixedFancyLightAndHeavy() throws IOException {
|
||||
Table table = new TableBuilder(generate(2, 2))
|
||||
.addFullBorder(BorderStyle.fancy_heavy)
|
||||
.addOutlineBorder(BorderStyle.fancy_light)
|
||||
.build();
|
||||
assertThat(table.render(10)).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMixedFancyHeavyAndLight() throws IOException {
|
||||
Table table = new TableBuilder(generate(2, 2))
|
||||
.addFullBorder(BorderStyle.fancy_light)
|
||||
.addOutlineBorder(BorderStyle.fancy_heavy)
|
||||
.build();
|
||||
assertThat(table.render(10)).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMixedDoubleAndSingle() throws IOException {
|
||||
Table table = new TableBuilder(generate(2, 2))
|
||||
.addFullBorder(BorderStyle.fancy_light)
|
||||
.addOutlineBorder(BorderStyle.fancy_double)
|
||||
.build();
|
||||
assertThat(table.render(10)).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMixedSingleAndDouble() throws IOException {
|
||||
Table table = new TableBuilder(generate(2, 2))
|
||||
.addFullBorder(BorderStyle.fancy_double)
|
||||
.addOutlineBorder(BorderStyle.fancy_light)
|
||||
.build();
|
||||
assertThat(table.render(10)).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMixedLightInternalAndHeavy() throws IOException {
|
||||
Table table = new TableBuilder(generate(3, 3))
|
||||
.addFullBorder(BorderStyle.fancy_heavy)
|
||||
.paintBorder(BorderStyle.fancy_light, BorderSpecification.OUTLINE).fromRowColumn(1, 1).toRowColumn(2, 2)
|
||||
.build();
|
||||
assertThat(table.render(10)).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMixedHeavyInternalAndLight() throws IOException {
|
||||
Table table = new TableBuilder(generate(3, 3))
|
||||
.addFullBorder(BorderStyle.fancy_light)
|
||||
.paintBorder(BorderStyle.fancy_heavy, BorderSpecification.OUTLINE).fromRowColumn(1, 1).toRowColumn(2, 2)
|
||||
.build();
|
||||
assertThat(table.render(10)).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHeavyOutlineAndHeader_LightVerticals_AirHorizontals() throws IOException {
|
||||
Table table = new TableBuilder(generate(4, 4))
|
||||
.addOutlineBorder(BorderStyle.fancy_heavy)
|
||||
.paintBorder(BorderStyle.fancy_light, BorderSpecification.INNER_VERTICAL).fromTopLeft().toBottomRight()
|
||||
.paintBorder(BorderStyle.air, BorderSpecification.INNER_HORIZONTAL).fromTopLeft().toBottomRight()
|
||||
.paintBorder(BorderStyle.fancy_heavy, BorderSpecification.OUTLINE).fromTopLeft().toRowColumn(1, 4)
|
||||
.build();
|
||||
assertThat(table.render(10)).isEqualTo(sample());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
/*
|
||||
* Copyright 2015-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.table;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for DelimiterTextWrapper.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class DelimiterTextWrapperTest {
|
||||
|
||||
private TextWrapper wrapper = new DelimiterTextWrapper();
|
||||
|
||||
@Test
|
||||
public void testNoWordSplit() {
|
||||
String[] text = new String[] {"the quick brown fox jumps over the lazy dog."};
|
||||
assertThat(wrapper.wrap(text, 10)).containsExactly("the quick ", "brown fox ", "jumps over", "the lazy ",
|
||||
"dog. ");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testWordSplit() {
|
||||
String[] text = new String[] {"the quick brown fox jumps over the lazy dog."};
|
||||
assertThat(wrapper.wrap(text, 4)).containsExactly("the ", "quic", "k ", "brow", "n ", "fox ", "jump",
|
||||
"s ", "over", "the ", "lazy", "dog.");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
/*
|
||||
* Copyright 2015-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.table;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests related to rendering Maps.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class KeyValueRenderingTests extends AbstractTestWithSample {
|
||||
|
||||
@Test
|
||||
public void testRenderConstrained() throws IOException {
|
||||
Map<String, String> values = new LinkedHashMap<String, String>();
|
||||
values.put("a", "b");
|
||||
values.put("long-key", "c");
|
||||
values.put("d", "long-value");
|
||||
TableModel model = new ArrayTableModel(new Object[][] {{"Thing", "Properties"}, {"Something", values}});
|
||||
TableBuilder tableBuilder = new TableBuilder(model)
|
||||
.addHeaderAndVerticalsBorders(BorderStyle.fancy_light);
|
||||
Tables.configureKeyValueRendering(tableBuilder, " = ");
|
||||
Table table = tableBuilder.build();
|
||||
String result = table.render(10);
|
||||
assertThat(result).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRenderUnconstrained() throws IOException {
|
||||
Map<String, String> values = new LinkedHashMap<String, String>();
|
||||
values.put("a", "b");
|
||||
values.put("long-key", "c");
|
||||
values.put("d", "long-value");
|
||||
TableModel model = new ArrayTableModel(new Object[][] {{"Thing", "Properties"}, {"Something", values}});
|
||||
TableBuilder tableBuilder = new TableBuilder(model)
|
||||
.addHeaderAndVerticalsBorders(BorderStyle.fancy_light);
|
||||
Tables.configureKeyValueRendering(tableBuilder, " = ");
|
||||
Table table = tableBuilder.build();
|
||||
String result = table.render(80);
|
||||
assertThat(result).isEqualTo(sample());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
/*
|
||||
* Copyright 2015-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.table;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
/**
|
||||
* Tests for TableModelBuilder.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class TableModelBuilderTests {
|
||||
|
||||
@Test
|
||||
public void emptyModel() {
|
||||
TableModelBuilder<Number> builder = new TableModelBuilder<Number>();
|
||||
TableModel model = builder.build();
|
||||
assertThat(model.getColumnCount()).isEqualTo(0);
|
||||
assertThat(model.getColumnCount()).isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testFrozen() {
|
||||
assertThatThrownBy(() -> {
|
||||
TableModelBuilder<Number> builder = new TableModelBuilder<Number>();
|
||||
builder.addRow().addValue(5);
|
||||
builder.build();
|
||||
builder.addRow();
|
||||
}).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddingTooManyValues() {
|
||||
assertThatThrownBy(() -> {
|
||||
TableModelBuilder<Number> builder = new TableModelBuilder<Number>();
|
||||
builder.addRow().addValue(5);
|
||||
builder.addRow().addValue(1).addValue(2);
|
||||
builder.build();
|
||||
}).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddingLessValues() {
|
||||
assertThatThrownBy(() -> {
|
||||
TableModelBuilder<Number> builder = new TableModelBuilder<Number>();
|
||||
builder.addRow().addValue(1).addValue(2);
|
||||
builder.addRow().addValue(5);
|
||||
builder.addRow();
|
||||
builder.build();
|
||||
}).isInstanceOf(IllegalArgumentException.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleBuild() {
|
||||
TableModelBuilder<Number> builder = new TableModelBuilder<Number>();
|
||||
builder
|
||||
.addRow()
|
||||
.addValue(7).addValue(2)
|
||||
.addRow()
|
||||
.addValue(3).addValue(5.5)
|
||||
.addRow()
|
||||
.addValue(1).addValue(4);
|
||||
|
||||
TableModel model = builder.build();
|
||||
assertThat(model.getColumnCount()).isEqualTo(2);
|
||||
assertThat(model.getRowCount()).isEqualTo(3);
|
||||
assertThat(model.getValue(1, 1)).isEqualTo(5.5);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
/*
|
||||
* Copyright 2015-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.table;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Tests for TableModel.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class TableModelTest {
|
||||
|
||||
@Test
|
||||
public void testTranspose() {
|
||||
TableModel model = new ArrayTableModel(new String[][] {{"a", "b", "c"}, {"d", "e", "f"}});
|
||||
|
||||
assertThat(model.transpose().getColumnCount()).isEqualTo(2);
|
||||
assertThat(model.transpose().getRowCount()).isEqualTo(3);
|
||||
assertThat(model.transpose().getValue(2, 1)).isEqualTo("f");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* Copyright 2015-2022 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.shell.table;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.springframework.shell.table.SimpleHorizontalAligner.*;
|
||||
import static org.springframework.shell.table.SimpleVerticalAligner.*;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for Table rendering.
|
||||
*
|
||||
* @author Eric Bottard
|
||||
*/
|
||||
public class TableTest extends AbstractTestWithSample {
|
||||
|
||||
@Test
|
||||
public void testEmptyModel() {
|
||||
TableModel model = new ArrayTableModel(new Object[0][0]);
|
||||
Table table = new TableBuilder(model).build();
|
||||
String result = table.render(80);
|
||||
assertThat(result).isEqualTo("");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testPreformattedModel() {
|
||||
TableModel model = generate(2, 2);
|
||||
Table table = new TableBuilder(model).build();
|
||||
String result = table.render(80);
|
||||
assertThat(result).isEqualTo("ab\ncd\n");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testExpandingColumns() throws IOException {
|
||||
TableModel model = new ArrayTableModel(new String[][] {{"a", "b"}, {"ccc", "d"}});
|
||||
Table table = new TableBuilder(model).build();
|
||||
String result = table.render(80);
|
||||
assertThat(result).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRightAlignment() throws IOException {
|
||||
TableModel model = new ArrayTableModel(new String[][] {{"a\na\na", "bbb"}, {"ccc", "d"}});
|
||||
Table table = new TableBuilder(model).on(CellMatchers.column(1)).addAligner(right).build();
|
||||
String result = table.render(80);
|
||||
assertThat(result).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testVerticalAlignment() throws IOException {
|
||||
TableModel model = new ArrayTableModel(new String[][] {{"a\na\na", "bbb"}, {"ccc", "d"}});
|
||||
Table table = new TableBuilder(model).on(CellMatchers.row(0)).addAligner(middle).build();
|
||||
String result = table.render(80);
|
||||
assertThat(result).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAutoWrapping() throws IOException {
|
||||
TableModel model = new ArrayTableModel(new String[][] {{"this is a long line", "bbb"}, {"ccc", "d"}});
|
||||
Table table = new TableBuilder(model).build();
|
||||
String result = table.render(10);
|
||||
assertThat(result).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testOverflow() throws IOException {
|
||||
TableModel model = new ArrayTableModel(new String[][] {{"this is a long line", "bbb"}, {"ccc", "d"}});
|
||||
Table table = new TableBuilder(model).build();
|
||||
String result = table.render(3);
|
||||
assertThat(result).isEqualTo(sample());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testEmptyCellsVerticalAligner() throws IOException {
|
||||
TableModel model = new ArrayTableModel(new String[][] {{"a", "b"}, {null, null}});
|
||||
Table table = new TableBuilder(model).on(CellMatchers.table()).addAligner(SimpleVerticalAligner.middle).build();
|
||||
table.render(3);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
Clark Alice&
|
||||
Smith Bob &
|
||||
ConnorSarah&
|
||||
@@ -0,0 +1,4 @@
|
||||
Last NameFirst Name&
|
||||
Clark Alice &
|
||||
Smith Bob &
|
||||
Connor Sarah &
|
||||
@@ -0,0 +1,3 @@
|
||||
12AliceClark &
|
||||
42Bob Smith &
|
||||
38SarahConnor&
|
||||
@@ -0,0 +1,7 @@
|
||||
╔═╦═╦═╗
|
||||
║a║b║c║
|
||||
╠═╬═╬═╣
|
||||
║d║e║f║
|
||||
╠═╬═╬═╣
|
||||
║g║h║i║
|
||||
╚═╩═╩═╝
|
||||
@@ -0,0 +1,6 @@
|
||||
╔═╦═╦═╗
|
||||
║a║b║c║
|
||||
╠═╬═╬═╣
|
||||
║d║e║f║
|
||||
║g║h║i║
|
||||
╚═╩═╩═╝
|
||||
@@ -0,0 +1,6 @@
|
||||
╔═══╗
|
||||
║abc║
|
||||
╠═══╣
|
||||
║def║
|
||||
║ghi║
|
||||
╚═══╝
|
||||
@@ -0,0 +1,5 @@
|
||||
╔═══╗
|
||||
║abc║
|
||||
║def║
|
||||
║ghi║
|
||||
╚═══╝
|
||||
@@ -0,0 +1,5 @@
|
||||
&
|
||||
a b &
|
||||
&
|
||||
c d &
|
||||
&
|
||||
@@ -0,0 +1,5 @@
|
||||
╔═╦═╗
|
||||
║a║b║
|
||||
╠═╬═╣
|
||||
║c║d║
|
||||
╚═╩═╝
|
||||
@@ -0,0 +1,5 @@
|
||||
┏━┳━┓
|
||||
┃a┃b┃
|
||||
┣━╋━┫
|
||||
┃c┃d┃
|
||||
┗━┻━┛
|
||||
@@ -0,0 +1,5 @@
|
||||
┌─┬─┐
|
||||
│a│b│
|
||||
├─┼─┤
|
||||
│c│d│
|
||||
└─┴─┘
|
||||
@@ -0,0 +1,9 @@
|
||||
┏━┯━┯━┯━┓
|
||||
┃a│b│c│d┃
|
||||
┣━┿━┿━┿━┫
|
||||
┃e│f│g│h┃
|
||||
┃ │ │ │ ┃
|
||||
┃i│j│k│l┃
|
||||
┃ │ │ │ ┃
|
||||
┃m│n│o│p┃
|
||||
┗━┷━┷━┷━┛
|
||||
@@ -0,0 +1,5 @@
|
||||
╔═╤═╗
|
||||
║a│b║
|
||||
╟─┼─╢
|
||||
║c│d║
|
||||
╚═╧═╝
|
||||
@@ -0,0 +1,5 @@
|
||||
┏━┯━┓
|
||||
┃a│b┃
|
||||
┠─┼─┨
|
||||
┃c│d┃
|
||||
┗━┷━┛
|
||||
@@ -0,0 +1,5 @@
|
||||
┌─┰─┐
|
||||
│a┃b│
|
||||
┝━╋━┥
|
||||
│c┃d│
|
||||
└─┸─┘
|
||||
@@ -0,0 +1,7 @@
|
||||
┌─┬─┬─┐
|
||||
│a│b│c│
|
||||
├─╆━╅─┤
|
||||
│d┃e┃f│
|
||||
├─╄━╃─┤
|
||||
│g│h│i│
|
||||
└─┴─┴─┘
|
||||
@@ -0,0 +1,7 @@
|
||||
┏━┳━┳━┓
|
||||
┃a┃b┃c┃
|
||||
┣━╃─╄━┫
|
||||
┃d│e│f┃
|
||||
┣━╅─╆━┫
|
||||
┃g┃h┃i┃
|
||||
┗━┻━┻━┛
|
||||
@@ -0,0 +1,5 @@
|
||||
+---+
|
||||
|a b|
|
||||
| |
|
||||
|c d|
|
||||
+---+
|
||||
@@ -0,0 +1,5 @@
|
||||
┌─╥─┐
|
||||
│a║b│
|
||||
╞═╬═╡
|
||||
│c║d│
|
||||
└─╨─┘
|
||||
@@ -0,0 +1,5 @@
|
||||
+-+-+
|
||||
|a|b|
|
||||
+-+-+
|
||||
|c|d|
|
||||
+-+-+
|
||||
@@ -0,0 +1,9 @@
|
||||
┌─────────┬──────────┐
|
||||
│Thing │Properties│
|
||||
├─────────┼──────────┤
|
||||
│Something│a = b │
|
||||
│ │long-key │
|
||||
│ │ = c │
|
||||
│ │d = │
|
||||
│ │long-value│
|
||||
└─────────┴──────────┘
|
||||
@@ -0,0 +1,7 @@
|
||||
┌─────────┬─────────────────────┐
|
||||
│Thing │Properties │
|
||||
├─────────┼─────────────────────┤
|
||||
│Something│ a = b │
|
||||
│ │long-key = c │
|
||||
│ │ d = long-value│
|
||||
└─────────┴─────────────────────┘
|
||||
@@ -0,0 +1,4 @@
|
||||
this isbbb&
|
||||
a long &
|
||||
line &
|
||||
ccc d &
|
||||
@@ -0,0 +1,2 @@
|
||||
a b&
|
||||
cccd&
|
||||
@@ -0,0 +1,5 @@
|
||||
thisbbb&
|
||||
is a &
|
||||
long &
|
||||
line &
|
||||
ccc d &
|
||||
@@ -0,0 +1,4 @@
|
||||
a bbb&
|
||||
a &
|
||||
a &
|
||||
ccc d&
|
||||
@@ -0,0 +1,4 @@
|
||||
a &
|
||||
a bbb&
|
||||
a &
|
||||
cccd &
|
||||
Reference in New Issue
Block a user