Fixed lookahead.
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Wed, 11 Oct 2017 15:08:09 +0000 (18:08 +0300)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Wed, 11 Oct 2017 15:08:09 +0000 (18:08 +0300)
src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java
src/test/java/eu/svjatoslav/commons/string/tokenizer/TokenizerTest.java [new file with mode: 0644]

index 4855c18..01216d8 100755 (executable)
@@ -46,7 +46,7 @@ public class Tokenizer {
         return this;
     }
 
-    public void expectNextToken(final String value)
+    public void expectAndConsumeNextToken(final String value)
             throws InvalidSyntaxException {
         final TokenizerMatch match = getNextToken();
         if (!value.equals(match.token))
@@ -111,7 +111,7 @@ public class Tokenizer {
     }
 
     public boolean peekIsOneOf(String ... possibilities){
-        TokenizerMatch nextToken = peekNextToken();
+        String nextToken = peekNextToken().token;
 
         for (String possibility : possibilities)
             if (possibility.equals(nextToken))
diff --git a/src/test/java/eu/svjatoslav/commons/string/tokenizer/TokenizerTest.java b/src/test/java/eu/svjatoslav/commons/string/tokenizer/TokenizerTest.java
new file mode 100644 (file)
index 0000000..e40b401
--- /dev/null
@@ -0,0 +1,24 @@
+package eu.svjatoslav.commons.string.tokenizer;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class TokenizerTest {
+
+
+    @Test
+    public void peekNextToken() throws Exception {
+        Tokenizer tokenizer = new Tokenizer("this is a test")
+                .addTerminator(" ", Terminator.TerminationStrategy.DROP);
+
+        tokenizer.expectAndConsumeNextToken("this");
+
+        assertEquals("is", tokenizer.peekNextToken().token);
+
+        assertEquals("is", tokenizer.peekNextToken().token);
+
+        assertEquals(true, tokenizer.peekIsOneOf("maybe", "is", "that"));
+    }
+
+}
\ No newline at end of file