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
This commit is contained in:
Benedikt Ritter 2013-03-23 13:07:04 +00:00
parent c4014b6b38
commit 2e543f41f3
1 changed files with 30 additions and 0 deletions

View File

@ -17,6 +17,11 @@
package org.apache.commons.csv;
import static org.apache.commons.csv.Constants.BACKSPACE;
import static org.apache.commons.csv.Constants.CR;
import static org.apache.commons.csv.Constants.FF;
import static org.apache.commons.csv.Constants.LF;
import static org.apache.commons.csv.Constants.TAB;
import static org.apache.commons.csv.Token.Type.COMMENT;
import static org.apache.commons.csv.Token.Type.EOF;
import static org.apache.commons.csv.Token.Type.EORECORD;
@ -282,4 +287,29 @@ public class CSVLexerTest {
assertTokenEquals(TOKEN, "five", parser.nextToken(new Token()));
assertTokenEquals(EOF, "six", parser.nextToken(new Token()));
}
@Test
public void testEscaping() throws Exception {
final String code = "plain," +
"CR!" + CR + "Escaped," +
"LF!" + LF +"Escaped," +
"TAB!" + TAB +"Escaped," +
"BACKSPACE!" + BACKSPACE +"Escaped," +
"FF!" + FF +"Escaped";
final Lexer lexer = getLexer(code, CSVFormat.newBuilder().withEscape('!').build());
assertTokenEquals(TOKEN, "plain", lexer.nextToken(new Token()));
assertTokenEquals(TOKEN, "CR" + CR + "Escaped", lexer.nextToken(new Token()));
assertTokenEquals(TOKEN, "LF" + LF + "Escaped", lexer.nextToken(new Token()));
assertTokenEquals(TOKEN, "TAB" + TAB + "Escaped", lexer.nextToken(new Token()));
assertTokenEquals(TOKEN, "BACKSPACE" + BACKSPACE + "Escaped", lexer.nextToken(new Token()));
assertTokenEquals(EOF, "FF" + FF + "Escaped", lexer.nextToken(new Token()));
}
@Test(expected = IOException.class)
public void testEscapingAtEOF() throws Exception {
final String code = "escaping at EOF is evil!";
final Lexer lexer = getLexer(code, CSVFormat.newBuilder().withEscape('!').build());
lexer.nextToken(new Token());
}
}