Skip to content

Commit 5744ee8

Browse files
committed
Separate testing empty line handling from comment recognition
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1465722 13f79535-47bb-0310-9956-ffa450edef68
1 parent 449e9a8 commit 5744ee8

1 file changed

Lines changed: 41 additions & 22 deletions

File tree

src/test/java/org/apache/commons/csv/CSVLexerTest.java

Lines changed: 41 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -73,41 +73,60 @@ public void testIgnoreSurroundingSpacesAreDeleted() throws IOException {
7373
@Test
7474
public void testIgnoreEmptyLines() throws IOException {
7575
final String code =
76-
"1,2,3,\n"+ // 1
76+
"first,line,\n"+
7777
"\n"+
7878
"\n"+
79-
"a,b x,c#no-comment\n"+ // 2
79+
"second,line\n"+
8080
"\n"+
8181
"\n"+
82-
"# foo \n"+ // 3
83-
"\n"+ // 4
84-
"d,e,#no-comment\n"+ // 5
82+
"third line \n"+
8583
"\n"+
8684
"\n"+
87-
"# penultimate comment\n"+ // 6
85+
"last, line \n"+
8886
"\n"+
8987
"\n"+
90-
"# Final comment\n"; // 7
91-
final CSVFormat format = CSVFormat.newBuilder().withCommentStart('#').withIgnoreEmptyLines(true).build();
88+
"\n";
89+
final CSVFormat format = CSVFormat.newBuilder().withIgnoreEmptyLines(true).build();
9290
final Lexer parser = getLexer(code, format);
9391

94-
assertThat(parser.nextToken(new Token()), matches(TOKEN, "1"));
95-
assertThat(parser.nextToken(new Token()), matches(TOKEN, "2"));
96-
assertThat(parser.nextToken(new Token()), matches(TOKEN, "3"));
97-
assertThat(parser.nextToken(new Token()), matches(EORECORD, "")); // 1
98-
assertThat(parser.nextToken(new Token()), matches(TOKEN, "a"));
99-
assertThat(parser.nextToken(new Token()), matches(TOKEN, "b x"));
100-
assertThat(parser.nextToken(new Token()), matches(EORECORD, "c#no-comment")); // 2
101-
assertThat(parser.nextToken(new Token()), matches(COMMENT, "foo")); // 3
102-
// 4 empty line, ignored // 4
103-
assertThat(parser.nextToken(new Token()), matches(TOKEN, "d"));
104-
assertThat(parser.nextToken(new Token()), matches(TOKEN, "e"));
105-
assertThat(parser.nextToken(new Token()), matches(EORECORD, "#no-comment")); // 5
106-
assertThat(parser.nextToken(new Token()), matches(COMMENT, "penultimate comment")); // 6
107-
assertThat(parser.nextToken(new Token()), matches(COMMENT, "Final comment")); // 7
92+
assertThat(parser.nextToken(new Token()), matches(TOKEN, "first"));
93+
assertThat(parser.nextToken(new Token()), matches(TOKEN, "line"));
94+
assertThat(parser.nextToken(new Token()), matches(EORECORD, ""));
95+
assertThat(parser.nextToken(new Token()), matches(TOKEN, "second"));
96+
assertThat(parser.nextToken(new Token()), matches(EORECORD, "line"));
97+
assertThat(parser.nextToken(new Token()), matches(EORECORD, "third line "));
98+
assertThat(parser.nextToken(new Token()), matches(TOKEN, "last"));
99+
assertThat(parser.nextToken(new Token()), matches(EORECORD, " line "));
108100
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
109101
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
102+
}
110103

104+
@Test
105+
public void testComments() throws IOException {
106+
final String code =
107+
"first,line,\n"+
108+
"second,line,tokenWith#no-comment\n"+
109+
"# comment line \n"+
110+
"third,line,#no-comment\n"+
111+
"# penultimate comment\n"+
112+
"# Final comment\n";
113+
final CSVFormat format = CSVFormat.newBuilder().withCommentStart('#').build();
114+
final Lexer parser = getLexer(code, format);
115+
116+
assertThat(parser.nextToken(new Token()), matches(TOKEN, "first"));
117+
assertThat(parser.nextToken(new Token()), matches(TOKEN, "line"));
118+
assertThat(parser.nextToken(new Token()), matches(EORECORD, ""));
119+
assertThat(parser.nextToken(new Token()), matches(TOKEN, "second"));
120+
assertThat(parser.nextToken(new Token()), matches(TOKEN, "line"));
121+
assertThat(parser.nextToken(new Token()), matches(EORECORD, "tokenWith#no-comment"));
122+
assertThat(parser.nextToken(new Token()), matches(COMMENT, "comment line"));
123+
assertThat(parser.nextToken(new Token()), matches(TOKEN, "third"));
124+
assertThat(parser.nextToken(new Token()), matches(TOKEN, "line"));
125+
assertThat(parser.nextToken(new Token()), matches(EORECORD, "#no-comment"));
126+
assertThat(parser.nextToken(new Token()), matches(COMMENT, "penultimate comment"));
127+
assertThat(parser.nextToken(new Token()), matches(COMMENT, "Final comment"));
128+
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
129+
assertThat(parser.nextToken(new Token()), matches(EOF, ""));
111130
}
112131

113132
// multiline including comments (and empty lines)

0 commit comments

Comments
 (0)