### Question:
TreeMetaDataProvider { public Optional<Token> previousToken(TextRange textRange) { int textRangeIndex = indexOfFirstElement(sortedTokens, textRange); if (textRangeIndex <= 0) { return Optional.empty(); } else { return Optional.of(sortedTokens.get(textRangeIndex - 1)); } } TreeMetaDataProvider(List<Comment> comments, List<Token> tokens); List<Comment> allComments(); List<Token> allTokens(); int indexOfFirstToken(TextRange textRange); Optional<Token> firstToken(TextRange textRange); Optional<Token> previousToken(TextRange textRange); Optional<Token> previousToken(TextRange textRange, String expectedTokenValue); Optional<Token> previousToken(TextRange textRange, Predicate<Token> expectedConditionToMatch); void updateTokenType(Token token, Token.Type newType); Token keyword(TextRange textRange); TreeMetaData metaData(TextRange textRange); static final Comparator<HasTextRange> COMPARATOR; }### Answer:
@Test public void previous_token() { Token token1 = new TokenImpl(range(1, 1, 1, 3), "ab", Token.Type.KEYWORD); Token token2 = new TokenImpl(range(1, 4, 1, 6), "cd", Token.Type.KEYWORD); TreeMetaDataProvider provider = new TreeMetaDataProvider(emptyList(), Arrays.asList(token1, token2)); assertThat(provider.previousToken(range(1, 0, 1, 1))).isNotPresent(); assertThat(provider.previousToken(range(1, 1, 1, 3))).isNotPresent(); assertThat(provider.previousToken(range(1, 2, 1, 20)).get().text()).isEqualTo("ab"); assertThat(provider.previousToken(range(1, 5, 1, 20))).isNotPresent(); }
@Test public void previous_token_with_expected_value() { Token token1 = new TokenImpl(range(1, 1, 1, 3), "ab", Token.Type.KEYWORD); Token token2 = new TokenImpl(range(1, 4, 1, 6), "cd", Token.Type.KEYWORD); TreeMetaDataProvider provider = new TreeMetaDataProvider(emptyList(), Arrays.asList(token1, token2)); assertThat(provider.previousToken(range(1, 2, 1, 20), "ef")).isNotPresent(); assertThat(provider.previousToken(range(1, 2, 1, 20), "ab")).isPresent(); assertThat(provider.previousToken(range(1, 2, 1, 20), "AB")).isNotPresent(); assertThat(provider.previousToken(range(1, 2, 1, 20), token -> true)).isPresent(); assertThat(provider.previousToken(range(1, 2, 1, 20), token -> false)).isNotPresent(); }