X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=blobdiff_plain;f=src%2Ftest%2Fjava%2Feu%2Fsvjatoslav%2Fcommons%2Fstring%2Ftokenizer%2FTokenizerTest.java;h=84571e898a5c5807b0f2c2f8a9ca54d5aa568c29;hb=ba10494483d7eaf1e9d58ddb1402806c8fc58178;hp=e40b40167f4308da24b31ce4fb2a6d3c6d2ead4d;hpb=4d4a86dce1b84d9ef26d95168f2fea5477cedaa5;p=svjatoslav_commons.git diff --git a/src/test/java/eu/svjatoslav/commons/string/tokenizer/TokenizerTest.java b/src/test/java/eu/svjatoslav/commons/string/tokenizer/TokenizerTest.java index e40b401..84571e8 100644 --- a/src/test/java/eu/svjatoslav/commons/string/tokenizer/TokenizerTest.java +++ b/src/test/java/eu/svjatoslav/commons/string/tokenizer/TokenizerTest.java @@ -2,15 +2,19 @@ package eu.svjatoslav.commons.string.tokenizer; import org.junit.Test; +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.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNull; 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"); @@ -21,4 +25,45 @@ public class TokenizerTest { 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