|
| 1 | +package blackbox.reader; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.junit.jupiter.api.Test; |
| 9 | +import org.junit.jupiter.params.ParameterizedTest; |
| 10 | +import org.junit.jupiter.params.provider.ValueSource; |
| 11 | + |
| 12 | +import de.siegmar.fastcsv.reader.AbstractBaseCsvCallbackHandler; |
| 13 | +import de.siegmar.fastcsv.reader.CsvReader; |
| 14 | +import de.siegmar.fastcsv.reader.CsvRecordHandler; |
| 15 | +import de.siegmar.fastcsv.reader.FieldModifiers; |
| 16 | +import de.siegmar.fastcsv.reader.RecordWrapper; |
| 17 | +import testutil.CsvRecordAssert; |
| 18 | + |
| 19 | +class SkipRecordsTest { |
| 20 | + |
| 21 | + private final CsvReader.CsvReaderBuilder crb = CsvReader.builder(); |
| 22 | + |
| 23 | + @Test |
| 24 | + void singleRecordNoSkipEmpty() { |
| 25 | + crb.skipEmptyLines(false); |
| 26 | + assertThat(crb.ofCsvRecord("").iterator()).isExhausted(); |
| 27 | + } |
| 28 | + |
| 29 | + @Test |
| 30 | + void multipleRecordsNoSkipEmpty() { |
| 31 | + crb.skipEmptyLines(false); |
| 32 | + |
| 33 | + assertThat(crb.ofCsvRecord("\n\na").iterator()).toIterable() |
| 34 | + .satisfiesExactly( |
| 35 | + item1 -> CsvRecordAssert.assertThat(item1).isStartingLineNumber(1).fields().containsExactly(""), |
| 36 | + item2 -> CsvRecordAssert.assertThat(item2).isStartingLineNumber(2).fields().containsExactly(""), |
| 37 | + item3 -> CsvRecordAssert.assertThat(item3).isStartingLineNumber(3).fields().containsExactly("a")); |
| 38 | + } |
| 39 | + |
| 40 | + @ParameterizedTest |
| 41 | + @ValueSource(strings = {",\nfoo\n", ",,\nfoo\n", "''\nfoo\n", "' '\nfoo\n"}) |
| 42 | + void notEmpty(final String input) { |
| 43 | + crb.quoteCharacter('\''); |
| 44 | + final var cbh = new CsvRecordHandler(FieldModifiers.TRIM); |
| 45 | + assertThat(crb.build(cbh, input).stream()).hasSize(2); |
| 46 | + } |
| 47 | + |
| 48 | + @ParameterizedTest |
| 49 | + @ValueSource(strings = {",\nfoo\n", ",,\nfoo\n", "''\nfoo\n", "' '\nfoo\n"}) |
| 50 | + void notEmptyCustomCallback(final String input) { |
| 51 | + crb.quoteCharacter('\''); |
| 52 | + final AbstractBaseCsvCallbackHandler<String[]> cbh = new AbstractBaseCsvCallbackHandler<>() { |
| 53 | + private final List<String> fields = new ArrayList<>(); |
| 54 | + |
| 55 | + @Override |
| 56 | + protected void handleBegin(final long startingLineNumber) { |
| 57 | + fields.clear(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + protected void handleField(final int fieldIdx, final char[] buf, |
| 62 | + final int offset, final int len, final boolean quoted) { |
| 63 | + fields.add(new String(buf, offset, len).trim()); |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + protected RecordWrapper<String[]> buildRecord() { |
| 68 | + return wrapRecord(fields.toArray(new String[0])); |
| 69 | + } |
| 70 | + }; |
| 71 | + assertThat(crb.build(cbh, input).stream()).hasSize(2); |
| 72 | + } |
| 73 | + |
| 74 | +} |
0 commit comments