1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.cli.example;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertThrows;
21
22 import java.io.IOException;
23 import java.io.StringReader;
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.Collections;
27 import java.util.List;
28
29 import org.apache.commons.cli.help.TableDefinition;
30 import org.apache.commons.cli.help.TextStyle;
31 import org.apache.commons.io.IOUtils;
32 import org.junit.jupiter.api.BeforeEach;
33 import org.junit.jupiter.api.Test;
34
35
36
37
38 class AptHelpAppendableTest {
39
40 private StringBuilder sb;
41 private AptHelpAppendable underTest;
42
43 @BeforeEach
44 public void beforeEach() {
45 sb = new StringBuilder();
46 underTest = new AptHelpAppendable(sb);
47 }
48
49 @Test
50 void testAppendFormatTest() throws IOException {
51 underTest.appendFormat("Big %s and Phantom %,d", "Joe", 309);
52 assertEquals(String.format("Big Joe and Phantom 309"), sb.toString());
53 }
54
55 @Test
56 void testAppendHeaderTest() throws IOException {
57 underTest.appendHeader(1, "Hello World");
58 assertEquals(String.format("* Hello World%n%n"), sb.toString());
59 sb.setLength(0);
60 underTest.appendHeader(2, "Hello World");
61 assertEquals(String.format("** Hello World%n%n"), sb.toString());
62 sb.setLength(0);
63 assertThrows(IllegalArgumentException.class, () -> underTest.appendHeader(0, "Hello World"));
64 }
65
66 @Test
67 void testAppendListTest() throws IOException {
68 final String[] entries = { "one", "two", "three" };
69 underTest.appendList(true, Arrays.asList(entries));
70 assertEquals(String.format(" [[1]] one%n [[2]] two%n [[3]] three%n%n"), sb.toString());
71 sb.setLength(0);
72 underTest.appendList(false, Arrays.asList(entries));
73 assertEquals(String.format(" * one%n * two%n * three%n%n"), sb.toString());
74 }
75
76 @Test
77 void testAppendParagraphFormatTest() throws IOException {
78 underTest.appendParagraphFormat("Hello %s World %,d", "Big Joe", 309);
79 assertEquals(String.format(" Hello Big Joe World 309%n%n"), sb.toString());
80 }
81
82 @Test
83 void testAppendParagraphTest() throws IOException {
84 underTest.appendParagraph("Hello World");
85 assertEquals(String.format(" Hello World%n%n"), sb.toString());
86 }
87
88 @Test
89 void testAppendTableTest() throws IOException {
90 final List<TextStyle> styles = Arrays.asList(TextStyle.DEFAULT, TextStyle.DEFAULT, TextStyle.DEFAULT);
91 final String[] headers = { "one", "two", "three" };
92
93 final List<List<String>> rows = Arrays.asList(
94 Arrays.asList(new String[]{"uno", "dos", "tres"}),
95 Arrays.asList(new String[]{"aon", "dhá", "trí"}),
96 Arrays.asList(new String[]{"واحد", "اثنين", "ثلاثة"})
97 );
98
99 List<String> expected = new ArrayList<>();
100 expected.add("*-----+-----+-------+");
101 expected.add("| one | two | three |");
102 expected.add("*-----+-----+-------+");
103 expected.add("| uno | dos | tres |");
104 expected.add("*-----+-----+-------+");
105 expected.add("| aon | dhá | trí |");
106 expected.add("*-----+-----+-------+");
107 expected.add("| واحد | اثنين | ثلاثة |");
108 expected.add("*-----+-----+-------+");
109 expected.add("The caption");
110 expected.add("");
111 TableDefinition table = TableDefinition.from("The caption", styles, Arrays.asList(headers), rows);
112 underTest.appendTable(table);
113 List<String> actual = IOUtils.readLines(new StringReader(sb.toString()));
114 assertEquals(expected, actual, "full table failed");
115 table = TableDefinition.from(null, styles, Arrays.asList(headers), rows);
116 expected.remove(9);
117 sb.setLength(0);
118 underTest.appendTable(table);
119 actual = IOUtils.readLines(new StringReader(sb.toString()));
120 assertEquals(expected, actual);
121 table = TableDefinition.from(null, styles, Arrays.asList(headers), Collections.emptyList());
122 expected = new ArrayList<>();
123 expected.add("*-----+-----+-------+");
124 expected.add("| one | two | three |");
125 expected.add("*-----+-----+-------+");
126 expected.add("");
127 sb.setLength(0);
128 underTest.appendTable(table);
129 actual = IOUtils.readLines(new StringReader(sb.toString()));
130 assertEquals(expected, actual, "no rows test failed");
131
132 }
133
134 @Test
135 void testAppendTitleTest() throws IOException {
136 sb.setLength(0);
137 underTest.appendTitle("Hello World");
138 assertEquals(String.format(" -----%n Hello World%n -----%n%nHello World%n%n"), sb.toString());
139 }
140 }