Skip to content

Commit a306352

Browse files
Merge pull request #128 from MichaelMcKibbin/CsvTableFormatterTest-2
Add unit tests for CsvTableFormatter FINAL
2 parents 2f03ade + bc53388 commit a306352

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
package com.group5.csv.io;
2+
3+
import com.group5.csv.core.Row;
4+
import com.group5.csv.testutils.VirtualReader;
5+
import org.junit.jupiter.api.Test;
6+
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import static org.junit.jupiter.api.Assertions.*;
11+
12+
/**
13+
* Test class for CsvTableFormatter
14+
*/
15+
class CsvTableFormatterTest {
16+
17+
/**
18+
* Helper to read all rows from a CsvReader.
19+
*/
20+
private List<Row> readAll(CsvReader reader) throws Exception {
21+
List<Row> rows = new ArrayList<>();
22+
Row row;
23+
while ((row = reader.readRow()) != null) {
24+
rows.add(row);
25+
}
26+
return rows;
27+
}
28+
29+
@Test
30+
void formatTable_withHeadersAndRows_producesBorderedTable() throws Exception {
31+
String csv = """
32+
name,age,city
33+
Alice,30,Dublin
34+
Bob,25,Cork
35+
""";
36+
37+
CsvConfig config = CsvConfig.builder()
38+
.setHasHeader(true)
39+
.build();
40+
41+
String table;
42+
try (CsvReader reader = new CsvReader(new VirtualReader(csv), config)) {
43+
List<Row> rows = readAll(reader);
44+
45+
// Use the reader-based constructor so formatter picks up the same newline
46+
CsvTableFormatter formatter = new CsvTableFormatter(reader);
47+
48+
table = formatter.formatTable(rows);
49+
}
50+
51+
assertNotNull(table);
52+
assertFalse(table.isEmpty(), "Table output should not be empty");
53+
54+
// Basic structure checks
55+
assertTrue(table.startsWith("+"), "Table should start with a separator line");
56+
assertTrue(table.trim().endsWith("+"), "Table should end with a separator line");
57+
assertTrue(table.contains("|"), "Table should use '|' as column borders");
58+
59+
// Headers present
60+
assertTrue(table.contains("name"), "Header 'name' should be present");
61+
assertTrue(table.contains("age"), "Header 'age' should be present");
62+
assertTrue(table.contains("city"), "Header 'city' should be present");
63+
64+
// Data present
65+
assertTrue(table.contains("Alice"), "Data row should contain 'Alice'");
66+
assertTrue(table.contains("Bob"), "Data row should contain 'Bob'");
67+
}
68+
69+
@Test
70+
void formatTable_emptyList_returnsEmptyString() {
71+
CsvTableFormatter formatter = new CsvTableFormatter("\n");
72+
String table = formatter.formatTable(List.of());
73+
74+
assertNotNull(table);
75+
assertEquals("", table, "Empty input should produce an empty string");
76+
}
77+
78+
@Test
79+
void formatRow_multilineCell_producesMultipleLinesWithBorders() throws Exception {
80+
String csv = """
81+
name,comment
82+
Alice,"Hello
83+
World"
84+
""";
85+
86+
CsvConfig config = CsvConfig.builder()
87+
.setHasHeader(true)
88+
.build();
89+
90+
String formatted;
91+
try (CsvReader reader = new CsvReader(new VirtualReader(csv), config)) {
92+
// Directly read all data rows (header is handled inside CsvReader)
93+
List<Row> rows = readAll(reader);
94+
assertEquals(1, rows.size(), "Expected one data row");
95+
96+
CsvTableFormatter formatter = new CsvTableFormatter(reader);
97+
formatted = formatter.formatRow(rows.get(0));
98+
}
99+
100+
assertNotNull(formatted);
101+
102+
// It should contain both lines of the multi-line cell
103+
assertTrue(formatted.contains("Hello"), "First line of cell should appear");
104+
assertTrue(formatted.contains("World"), "Second line of cell should appear");
105+
106+
// Split on line breaks and check borders
107+
String[] lines = formatted.split("\\R");
108+
assertTrue(lines.length >= 2, "Multiline cell should produce multiple lines");
109+
110+
for (String line : lines) {
111+
assertTrue(line.startsWith("|"), "Each formatted line should start with '|'");
112+
assertTrue(line.endsWith("|"), "Each formatted line should end with '|'");
113+
}
114+
}
115+
}

0 commit comments

Comments
 (0)