From 4d4a86dce1b84d9ef26d95168f2fea5477cedaa5 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Wed, 11 Oct 2017 18:08:09 +0300 Subject: [PATCH] Fixed lookahead. --- .../commons/string/tokenizer/Tokenizer.java | 4 ++-- .../string/tokenizer/TokenizerTest.java | 24 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) create mode 100644 src/test/java/eu/svjatoslav/commons/string/tokenizer/TokenizerTest.java diff --git a/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java b/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java index 4855c18..01216d8 100755 --- a/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java +++ b/src/main/java/eu/svjatoslav/commons/string/tokenizer/Tokenizer.java @@ -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 index 0000000..e40b401 --- /dev/null +++ b/src/test/java/eu/svjatoslav/commons/string/tokenizer/TokenizerTest.java @@ -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 -- 2.20.1