Skip to content

Commit d49e991

Browse files
authored
Improve lexer and token coverage (apache#67)
* Improve Lexer and Token coverage * fix the imports
1 parent 8ae700e commit d49e991

2 files changed

Lines changed: 49 additions & 1 deletion

File tree

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

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import static org.junit.jupiter.api.Assertions.assertFalse;
3333
import static org.junit.jupiter.api.Assertions.assertThrows;
3434
import static org.junit.jupiter.api.Assertions.assertTrue;
35-
35+
import static org.junit.jupiter.api.Assertions.assertEquals;
3636
import java.io.IOException;
3737
import java.io.StringReader;
3838

@@ -389,4 +389,45 @@ public void testEscapingAtEOF() throws Exception {
389389
assertThrows(IOException.class, () -> lexer.nextToken(new Token()));
390390
}
391391
}
392+
393+
@Test
394+
public void testTrimTrailingSpacesZeroLength() throws Exception {
395+
final StringBuilder buffer = new StringBuilder("");
396+
final Lexer lexer = createLexer(buffer.toString(), CSVFormat.DEFAULT);
397+
lexer.trimTrailingSpaces(buffer);
398+
assertThat(lexer.nextToken(new Token()), matches(EOF, ""));
399+
}
400+
401+
@Test
402+
public void testReadEscapeTab() throws IOException {
403+
try (final Lexer lexer = createLexer("t", CSVFormat.DEFAULT.withEscape('\t'))) {
404+
final int ch = lexer.readEscape();
405+
assertThat(lexer.nextToken(new Token()), matches(EOF, ""));
406+
assertEquals(TAB, ch);
407+
}
408+
}
409+
410+
@Test
411+
public void testReadEscapeBackspace() throws IOException {
412+
try (final Lexer lexer = createLexer("b", CSVFormat.DEFAULT.withEscape('\b'))) {
413+
final int ch = lexer.readEscape();
414+
assertEquals(BACKSPACE, ch);
415+
}
416+
}
417+
418+
@Test
419+
public void testReadEscapeFF() throws IOException {
420+
try (final Lexer lexer = createLexer("f", CSVFormat.DEFAULT.withEscape('\f'))) {
421+
final int ch = lexer.readEscape();
422+
assertEquals(FF, ch);
423+
}
424+
}
425+
426+
@Test
427+
public void testIsMetaCharCommentStart() throws IOException {
428+
try (final Lexer lexer = createLexer("#", CSVFormat.DEFAULT.withCommentMarker('#'))) {
429+
final int ch = lexer.readEscape();
430+
assertEquals('#', ch);
431+
}
432+
}
392433
}

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import static org.apache.commons.csv.TokenMatchers.matches;
2323
import static org.junit.jupiter.api.Assertions.assertFalse;
2424
import static org.junit.jupiter.api.Assertions.assertTrue;
25+
import static org.junit.jupiter.api.Assertions.assertEquals;
2526

2627
import org.junit.jupiter.api.BeforeEach;
2728
import org.junit.jupiter.api.Test;
@@ -67,4 +68,10 @@ public void testMatches() {
6768
assertFalse(matches(Token.Type.EORECORD, "not the content").matches(token));
6869
}
6970

71+
@Test
72+
public void testToString() {
73+
assertTrue(matches(Token.Type.TOKEN, "content").matches(token));
74+
assertEquals("TOKEN", token.type.name());
75+
assertEquals("TOKEN [content]", token.toString());
76+
}
7077
}

0 commit comments

Comments
 (0)