Skip to content

Commit 2e543f4

Browse files
committed
Add some unit tests for escape handling in preparation for CSV-58
git-svn-id: https://svn.apache.org/repos/asf/commons/proper/csv/trunk@1460138 13f79535-47bb-0310-9956-ffa450edef68
1 parent c4014b6 commit 2e543f4

1 file changed

Lines changed: 30 additions & 0 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@
1717

1818
package org.apache.commons.csv;
1919

20+
import static org.apache.commons.csv.Constants.BACKSPACE;
21+
import static org.apache.commons.csv.Constants.CR;
22+
import static org.apache.commons.csv.Constants.FF;
23+
import static org.apache.commons.csv.Constants.LF;
24+
import static org.apache.commons.csv.Constants.TAB;
2025
import static org.apache.commons.csv.Token.Type.COMMENT;
2126
import static org.apache.commons.csv.Token.Type.EOF;
2227
import static org.apache.commons.csv.Token.Type.EORECORD;
@@ -282,4 +287,29 @@ public void testDelimiterIsWhitespace() throws IOException {
282287
assertTokenEquals(TOKEN, "five", parser.nextToken(new Token()));
283288
assertTokenEquals(EOF, "six", parser.nextToken(new Token()));
284289
}
290+
291+
@Test
292+
public void testEscaping() throws Exception {
293+
final String code = "plain," +
294+
"CR!" + CR + "Escaped," +
295+
"LF!" + LF +"Escaped," +
296+
"TAB!" + TAB +"Escaped," +
297+
"BACKSPACE!" + BACKSPACE +"Escaped," +
298+
"FF!" + FF +"Escaped";
299+
final Lexer lexer = getLexer(code, CSVFormat.newBuilder().withEscape('!').build());
300+
assertTokenEquals(TOKEN, "plain", lexer.nextToken(new Token()));
301+
assertTokenEquals(TOKEN, "CR" + CR + "Escaped", lexer.nextToken(new Token()));
302+
assertTokenEquals(TOKEN, "LF" + LF + "Escaped", lexer.nextToken(new Token()));
303+
assertTokenEquals(TOKEN, "TAB" + TAB + "Escaped", lexer.nextToken(new Token()));
304+
assertTokenEquals(TOKEN, "BACKSPACE" + BACKSPACE + "Escaped", lexer.nextToken(new Token()));
305+
assertTokenEquals(EOF, "FF" + FF + "Escaped", lexer.nextToken(new Token()));
306+
}
307+
308+
@Test(expected = IOException.class)
309+
public void testEscapingAtEOF() throws Exception {
310+
final String code = "escaping at EOF is evil!";
311+
final Lexer lexer = getLexer(code, CSVFormat.newBuilder().withEscape('!').build());
312+
313+
lexer.nextToken(new Token());
314+
}
285315
}

0 commit comments

Comments
 (0)