@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); }
@Before public void initTest() { List<Token> tokenList = new ArrayList<>(); tokenList.add(new Token("a", 1, 0)); tokenList.add(new Token("bc", 1, 2)); tokenList.add(new Token("def", 1, 5)); tokenQueue = new TokenQueue(tokenList); }
public TokenQueue chunk(Reader reader) { CodeReader code = new CodeReader(reader); TokenQueue queue = new TokenQueue(); try { channelDispatcher.consume(code, queue); return queue; } catch (Exception e) { throw new DuplicationsException("Unable to lex source code at line : " + code.getLinePosition() + " and column : " + code.getColumnPosition(), e); } }
@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 shouldNotMatchWhenNoRight() { Token t1 = new Token("(", 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, times(1)).isNextTokenValue("("); verify(tokenQueue, times(1)).poll(); verify(tokenQueue, times(2)).peek(); verifyNoMoreInteractions(tokenQueue); verify(output).add(t1); 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 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); UptoTokenMatcher matcher = new UptoTokenMatcher(new String[] { ";" }); assertThat(matcher.matchToken(tokenQueue, output), is(false)); verify(tokenQueue, times(2)).poll(); verify(tokenQueue, times(2)).peek(); verifyNoMoreInteractions(tokenQueue); verify(output).add(t1); verify(output).add(t2); 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 shouldMatch() { 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); AnyTokenMatcher matcher = new AnyTokenMatcher(); assertThat(matcher.matchToken(tokenQueue, output), is(true)); verify(tokenQueue).poll(); verifyNoMoreInteractions(tokenQueue); verify(output).add(t1); verifyNoMoreInteractions(output); }
@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); }
@Test public void shouldMatch() { Token t1 = new Token("a", 1, 1); Token t2 = new Token(";", 2, 1); // should stop on this token Token t3 = new Token(";", 3, 1); TokenQueue tokenQueue = spy(new TokenQueue(Arrays.asList(t1, t2, t3))); List<Token> output = mock(List.class); UptoTokenMatcher matcher = new UptoTokenMatcher(new String[] { ";" }); assertThat(matcher.matchToken(tokenQueue, output), is(true)); verify(tokenQueue, times(2)).poll(); verify(tokenQueue).peek(); verifyNoMoreInteractions(tokenQueue); verify(output).add(t1); verify(output).add(t2); verifyNoMoreInteractions(output); }
@Test public void shouldMatch() { 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("a"); assertThat(matcher.matchToken(tokenQueue, output), is(true)); verify(tokenQueue).isNextTokenValue("a"); verify(tokenQueue).poll(); verifyNoMoreInteractions(tokenQueue); verify(output).add(t1); 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); }
@Test public void shouldMatchAnyOfProvidedTokens() { Token t1 = new Token("a", 1, 1); Token t2 = new Token("{", 2, 1); Token t3 = new Token("b", 3, 1); Token t4 = new Token("}", 4, 1); TokenQueue tokenQueue = spy(new TokenQueue(Arrays.asList(t1, t2, t3, t4))); List<Token> output = mock(List.class); UptoTokenMatcher matcher = new UptoTokenMatcher(new String[] { "{", "}" }); assertThat(matcher.matchToken(tokenQueue, output), is(true)); assertThat(matcher.matchToken(tokenQueue, output), is(true)); verify(tokenQueue, times(4)).poll(); verify(tokenQueue, times(2)).peek(); verifyNoMoreInteractions(tokenQueue); verify(output).add(t1); verify(output).add(t2); verify(output).add(t3); verify(output).add(t4); verifyNoMoreInteractions(output); }
@Test public void shouldMatch() { Token t1 = new Token("(", 1, 1); Token t2 = new Token("a", 2, 1); Token t3 = new Token("(", 3, 1); Token t4 = new Token("b", 4, 1); Token t5 = new Token(")", 5, 1); Token t6 = new Token("c", 6, 1); Token t7 = new Token(")", 7, 1); TokenQueue tokenQueue = spy(new TokenQueue(Arrays.asList(t1, t2, t3, t4, t5, t6, t7))); List<Token> output = mock(List.class); BridgeTokenMatcher matcher = new BridgeTokenMatcher("(", ")"); assertThat(matcher.matchToken(tokenQueue, output), is(true)); verify(tokenQueue, times(1)).isNextTokenValue("("); verify(tokenQueue, times(7)).poll(); verify(tokenQueue, times(7)).peek(); verifyNoMoreInteractions(tokenQueue); verify(output).add(t1); verify(output).add(t2); verify(output).add(t3); verify(output).add(t4); verify(output).add(t5); verify(output).add(t6); verify(output).add(t7); verifyNoMoreInteractions(output); }
public TokenQueue chunk(Reader reader) { CodeReader code = new CodeReader(reader); TokenQueue queue = new TokenQueue(); try { channelDispatcher.consume(code, queue); return queue; } catch (Exception e) { throw new DuplicationsException("Unable to lex source code at line : " + code.getLinePosition() + " and column : " + code.getColumnPosition(), e); } }