/** * <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.11">Separators</a> */ @Test public void shouldPreserveSeparators() { assertThat(chunk("(){}[];,.")).containsExactly( new Token("(", 1, 0), new Token(")", 1, 1), new Token("{", 1, 2), new Token("}", 1, 3), new Token("[", 1, 4), new Token("]", 1, 5), new Token(";", 1, 6), new Token(",", 1, 7), new Token(".", 1, 8)); }
/** * <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.12">Operators</a> */ @Test public void shouldPreserveOperators() { assertThat(chunk("+=")).containsExactly(new Token("+", 1, 0), new Token("=", 1, 1)); assertThat(chunk("--")).containsExactly(new Token("-", 1, 0), new Token("-", 1, 1)); }
/** * <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.10.7">The Null Literal</a> */ @Test public void shouldPreserverNullLiteral() { assertThat(chunk("null")).containsExactly(new Token("null", 1, 0)); }
@Test public void shouldNotMatch() { Token t1 = new Token("a", 1, 1); Token t2 = new Token("b", 2, 1); TokenQueue tokenQueue = spy(new TokenQueue(Arrays.asList(t1, t2))); List<Token> output = mock(List.class); ExactTokenMatcher matcher = new ExactTokenMatcher("b"); assertThat(matcher.matchToken(tokenQueue, output), is(false)); verify(tokenQueue).isNextTokenValue("b"); verifyNoMoreInteractions(tokenQueue); verifyNoMoreInteractions(output); }
/** * <a href="http://java.sun.com/docs/books/jls/third_edition/html/lexical.html#3.7">Comments</a> */ @Test public void shouldIgnoreEndOfLineComment() { assertThat(chunk("// This is a comment")).isEmpty(); assertThat(chunk("// This is a comment \n and_this_is_not")).containsExactly(new Token("and_this_is_not", 2, 1)); }
@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(expected = IllegalStateException.class) public void shouldThrowAnException() { TokenMatcher tokenMatcher = mock(TokenMatcher.class); 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); dispatcher.consume(tokenQueue, statements); }
@Test public void shouldNotMatchWhenNoLeft() { Token t1 = new Token("a", 1, 1); TokenQueue tokenQueue = spy(new TokenQueue(Arrays.asList(t1))); List<Token> output = mock(List.class); BridgeTokenMatcher matcher = new BridgeTokenMatcher("(", ")"); assertThat(matcher.matchToken(tokenQueue, output), is(false)); verify(tokenQueue).isNextTokenValue("("); verifyNoMoreInteractions(tokenQueue); verifyNoMoreInteractions(output); }
@Test public void shouldMatch() { TokenQueue tokenQueue = spy(new TokenQueue()); Token token = new Token("a", 0, 0); List<Token> output = new ArrayList<>(Arrays.asList(token)); ForgetLastTokenMatcher matcher = new ForgetLastTokenMatcher(); assertThat(matcher.matchToken(tokenQueue, output), is(true)); assertThat(output.size(), is(0)); verify(tokenQueue).pushForward(eq(Collections.singletonList(token))); }
@Test public void shouldPushTokenAtBegining() { Token pushedToken = new Token("push", 1, 0); List<Token> pushedTokenList = new ArrayList<>(); pushedTokenList.add(pushedToken); tokenQueue.pushForward(pushedTokenList); assertThat(tokenQueue.peek(), is(pushedToken)); assertThat(tokenQueue.size(), is(4)); }
@Test public void shouldConsume() { TokenChannel channel = new TokenChannel("ABC"); TokenQueue output = mock(TokenQueue.class); CodeReader codeReader = new CodeReader("ABCD"); assertThat(channel.consume(codeReader, output), is(true)); ArgumentCaptor<Token> token = ArgumentCaptor.forClass(Token.class); verify(output).add(token.capture()); assertThat(token.getValue(), is(new Token("ABC", 1, 0))); verifyNoMoreInteractions(output); assertThat(codeReader.getLinePosition(), is(1)); assertThat(codeReader.getColumnPosition(), is(3)); }
@Test public void shouldCorrectlyDeterminePositionWhenTokenSpansMultipleLines() { TokenChannel channel = new TokenChannel("AB\nC"); TokenQueue output = mock(TokenQueue.class); CodeReader codeReader = new CodeReader("AB\nCD"); assertThat(channel.consume(codeReader, output), is(true)); ArgumentCaptor<Token> token = ArgumentCaptor.forClass(Token.class); verify(output).add(token.capture()); assertThat(token.getValue(), is(new Token("AB\nC", 1, 0))); verifyNoMoreInteractions(output); assertThat(codeReader.getLinePosition(), is(2)); assertThat(codeReader.getColumnPosition(), is(1)); }
@Test public void shouldPeekToken() { Token token = tokenQueue.peek(); assertThat(token, is(new Token("a", 1, 0))); assertThat(tokenQueue.size(), is(3)); }
@Test public void shouldPollToken() { Token token = tokenQueue.poll(); assertThat(token, is(new Token("a", 1, 0))); assertThat(tokenQueue.size(), is(2)); }