/** * @return always true */ @Override public boolean matchToken(TokenQueue tokenQueue, List<Token> matchedTokenList) { matcher.matchToken(tokenQueue, matchedTokenList); return true; }
public boolean consume(TokenQueue tokenQueue, List<Statement> output) { List<Token> matchedTokenList = new ArrayList<>(); for (TokenMatcher tokenMatcher : tokenMatchers) { if (!tokenMatcher.matchToken(tokenQueue, matchedTokenList)) { tokenQueue.pushForward(matchedTokenList); return false; } } // all matchers were successful, so now build the statement // matchedTokenList.size() check is for case with ForgiveLastTokenMatcher if (!blackHole && !matchedTokenList.isEmpty()) { output.add(new Statement(matchedTokenList)); } return true; }
@Test public void shouldMatch() { TokenQueue tokenQueue = spy(new TokenQueue()); TokenMatcher delegate = mock(TokenMatcher.class); OptTokenMatcher matcher = new OptTokenMatcher(delegate); List<Token> output = mock(List.class); assertThat(matcher.matchToken(tokenQueue, output), is(true)); verify(delegate).matchToken(tokenQueue, output); verifyNoMoreInteractions(delegate); verifyNoMoreInteractions(tokenQueue); verifyNoMoreInteractions(output); }
@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 shouldConsume() { TokenMatcher tokenMatcher = mock(TokenMatcher.class); when(tokenMatcher.matchToken(any(TokenQueue.class), anyList())).thenReturn(true); StatementChannel channel = StatementChannel.create(tokenMatcher); StatementChannelDisptacher dispatcher = new StatementChannelDisptacher(asList(channel)); TokenQueue tokenQueue = mock(TokenQueue.class); when(tokenQueue.peek()).thenReturn(new Token("a", 1, 0)).thenReturn(null); List<Statement> statements = mock(List.class); assertThat(dispatcher.consume(tokenQueue, statements), is(true)); verify(tokenQueue, times(2)).peek(); verifyNoMoreInteractions(tokenQueue); verifyNoMoreInteractions(statements); }
@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); }
/** * @return always true */ @Override public boolean matchToken(TokenQueue tokenQueue, List<Token> matchedTokenList) { matcher.matchToken(tokenQueue, matchedTokenList); return true; }
public boolean consume(TokenQueue tokenQueue, List<Statement> output) { List<Token> matchedTokenList = new ArrayList<Token>(); for (TokenMatcher tokenMatcher : tokenMatchers) { if (!tokenMatcher.matchToken(tokenQueue, matchedTokenList)) { tokenQueue.pushForward(matchedTokenList); return false; } } // all matchers were successful, so now build the statement // matchedTokenList.size() check is for case with ForgiveLastTokenMatcher if (!blackHole && !matchedTokenList.isEmpty()) { output.add(new Statement(matchedTokenList)); } return true; }