Add tests that print CSVRecords.

This commit is contained in:
Gary Gregory 2021-07-06 13:11:43 -04:00
parent d59f0866ce
commit abfdf83f11
1 changed files with 62 additions and 0 deletions

View File

@ -1261,6 +1261,68 @@ public class CSVPrinterTest {
} }
} }
@Test
public void testPrintCSVParser() throws IOException {
final String code = "a1,b1\n" // 1)
+ "a2,b2\n" // 2)
+ "a3,b3\n" // 3)
+ "a4,b4\n"// 4)
;
final String[][] res = {{"a1", "b1"}, {"a2", "b2"}, {"a3", "b3"}, {"a4", "b4"}};
CSVFormat format = CSVFormat.DEFAULT;
StringWriter sw = new StringWriter();
try (final CSVPrinter printer = format.print(sw); final CSVParser parser = CSVParser.parse(code, format)) {
printer.printRecords(parser);
}
try (final CSVParser parser = CSVParser.parse(sw.toString(), format)) {
final List<CSVRecord> records = parser.getRecords();
assertFalse(records.isEmpty());
Utils.compare("Fail", res, records);
}
}
@Test
public void testPrintCSVRecord() throws IOException {
final String code = "a1,b1\n" // 1)
+ "a2,b2\n" // 2)
+ "a3,b3\n" // 3)
+ "a4,b4\n"// 4)
;
final String[][] res = {{"a1", "b1"}, {"a2", "b2"}, {"a3", "b3"}, {"a4", "b4"}};
CSVFormat format = CSVFormat.DEFAULT;
StringWriter sw = new StringWriter();
try (final CSVPrinter printer = format.print(sw); final CSVParser parser = CSVParser.parse(code, format)) {
for (CSVRecord record : parser) {
printer.printRecord(record);
}
}
try (final CSVParser parser = CSVParser.parse(sw.toString(), format)) {
final List<CSVRecord> records = parser.getRecords();
assertFalse(records.isEmpty());
Utils.compare("Fail", res, records);
}
}
@Test
public void testPrintCSVRecords() throws IOException {
final String code = "a1,b1\n" // 1)
+ "a2,b2\n" // 2)
+ "a3,b3\n" // 3)
+ "a4,b4\n"// 4)
;
final String[][] res = {{"a1", "b1"}, {"a2", "b2"}, {"a3", "b3"}, {"a4", "b4"}};
CSVFormat format = CSVFormat.DEFAULT;
StringWriter sw = new StringWriter();
try (final CSVPrinter printer = format.print(sw); final CSVParser parser = CSVParser.parse(code, format)) {
printer.printRecords(parser.getRecords());
}
try (final CSVParser parser = CSVParser.parse(sw.toString(), format)) {
final List<CSVRecord> records = parser.getRecords();
assertFalse(records.isEmpty());
Utils.compare("Fail", res, records);
}
}
@Test @Test
public void testPrintCustomNullValues() throws IOException { public void testPrintCustomNullValues() throws IOException {
final StringWriter sw = new StringWriter(); final StringWriter sw = new StringWriter();