import org.junit.Test;
-import static org.junit.Assert.assertEquals;
+import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.DROP;
+import static eu.svjatoslav.commons.string.tokenizer.Terminator.TerminationStrategy.PRESERVE;
+import static org.junit.Assert.*;
public class TokenizerTest {
-
@Test
- public void peekNextToken() throws Exception {
- Tokenizer tokenizer = new Tokenizer("this is a test")
- .addTerminator(" ", Terminator.TerminationStrategy.DROP);
+ public void testPeeking() throws Exception {
+ Tokenizer tokenizer = new Tokenizer("this is a N'2015-03-18 09:48:54.360' test")
+ .addTerminator(" ", DROP)
+ .addTerminator("N'", "'", PRESERVE);
tokenizer.expectAndConsumeNextToken("this");
assertEquals(true, tokenizer.peekIsOneOf("maybe", "is", "that"));
}
+ @Test
+ public void testTokenization() throws Exception {
+ Tokenizer tokenizer = new Tokenizer("\"hello\" /** comment **/ (( is a N'2015-03-18 09:48:54.360' test")
+ .addTerminator(" ", DROP)
+ .addTerminator("(", PRESERVE)
+ .addTerminator("\"", "\"", PRESERVE)
+ .addTerminator("N'", "'", PRESERVE)
+ .addTerminator("/*", "*/", DROP);
+
+ assertTokenEquals("\"", "hello", tokenizer);
+ assertTokenEquals("(", null, tokenizer);
+ assertTokenEquals("(", null, tokenizer);
+ assertTokenEquals("is", null, tokenizer);
+ assertTokenEquals("a", null, tokenizer);
+ assertTokenEquals("N'", "2015-03-18 09:48:54.360", tokenizer);
+ assertTokenEquals("test", null, tokenizer);
+
+ assertNull(tokenizer.getNextToken());
+ assertFalse(tokenizer.hasMoreTokens());
+ }
+
+ private void assertTokenEquals(String token, String reminder, Tokenizer tokenizer) throws InvalidSyntaxException {
+ TokenizerMatch nextToken = tokenizer.getNextToken();
+
+ assertEquals(token, nextToken.token);
+
+ if (reminder == null)
+ assertNull(nextToken.reminder);
+ else
+ assertEquals(reminder, nextToken.reminder);
+ }
+
+ private void debugNextToken(Tokenizer tokenizer) throws InvalidSyntaxException {
+ TokenizerMatch nextToken = tokenizer.getNextToken();
+ if (nextToken == null)
+ System.out.println("null");
+ else
+ System.out.println("T: \"" + nextToken.token + "\", R: \"" + nextToken.reminder + "\"");
+ }
+
}
\ No newline at end of file