public boolean consume(TokenQueue tokenQueue, List<Statement> statements) { Token nextToken = tokenQueue.peek(); while (nextToken != null) { boolean channelConsumed = false; for (StatementChannel channel : channels) { if (channel.consume(tokenQueue, statements)) { channelConsumed = true; break; } } if (!channelConsumed) { throw new IllegalStateException("None of the statement channel has been able to consume token: " + nextToken); } nextToken = tokenQueue.peek(); } return true; }
@Test public void shouldPushForward() { TokenQueue tokenQueue = mock(TokenQueue.class); TokenMatcher matcher = mock(TokenMatcher.class); List<Statement> output = mock(List.class); StatementChannel channel = StatementChannel.create(matcher); assertThat(channel.consume(tokenQueue, output), is(false)); ArgumentCaptor<List> matchedTokenList = ArgumentCaptor.forClass(List.class); verify(matcher).matchToken(eq(tokenQueue), matchedTokenList.capture()); verifyNoMoreInteractions(matcher); verify(tokenQueue).pushForward(matchedTokenList.getValue()); verifyNoMoreInteractions(tokenQueue); verifyNoMoreInteractions(output); }
@Test public void shouldNotCreateStatement() { TokenQueue tokenQueue = spy(new TokenQueue(Arrays.asList(new Token("a", 1, 1)))); TokenMatcher matcher = spy(new AnyTokenMatcher()); StatementChannel channel = StatementChannel.create(matcher); List<Statement> output = mock(List.class); assertThat(channel.consume(tokenQueue, output), is(true)); verify(matcher).matchToken(eq(tokenQueue), anyList()); verifyNoMoreInteractions(matcher); verify(output).add(any()); verifyNoMoreInteractions(output); }
@Test public void shouldCreateStatement() { Token token = new Token("a", 1, 1); TokenQueue tokenQueue = spy(new TokenQueue(Arrays.asList(token))); TokenMatcher matcher = spy(new AnyTokenMatcher()); StatementChannel channel = StatementChannel.create(matcher); List<Statement> output = mock(List.class); assertThat(channel.consume(tokenQueue, output), is(true)); verify(matcher).matchToken(eq(tokenQueue), anyList()); verifyNoMoreInteractions(matcher); ArgumentCaptor<Statement> statement = ArgumentCaptor.forClass(Statement.class); verify(output).add(statement.capture()); assertThat(statement.getValue().getValue(), is("a")); assertThat(statement.getValue().getStartLine(), is(1)); assertThat(statement.getValue().getEndLine(), is(1)); verifyNoMoreInteractions(output); }
public boolean consume(TokenQueue tokenQueue, List<Statement> statements) { Token nextToken = tokenQueue.peek(); while (nextToken != null) { boolean channelConsumed = false; for (StatementChannel channel : channels) { if (channel.consume(tokenQueue, statements)) { channelConsumed = true; break; } } if (!channelConsumed) { throw new IllegalStateException("None of the statement channel has been able to consume token: " + nextToken); } nextToken = tokenQueue.peek(); } return true; }