Files
Language-Server/tests/features/SemanticTokensTests.cpp
T
faumaray 4b534c61ae Tokenize observer-reported type names
The semantic token legend gains a type entry, and the server emits it
for each class name span reported by the observer when it lies in the
open document. The canonical type itself is still not tokenized
because it has no source span. Registered tests cover the legend index,
and a real stdio session decoded the class names as type tokens beside
the function and declaration tokens.
2026-12-05 23:56:39 +03:00

78 lines
3.1 KiB
C++

#include <vls/features/SemanticTokens.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <vector>
namespace {
using namespace vls::features::semantic_tokens;
using Void::Testing::expect;
using Void::Testing::require;
Token token(std::uint32_t line, std::uint32_t character, std::uint32_t length) {
return {line, character, length};
}
} // namespace
VOID_TEST(SemanticTokens, normalizes_order_and_deltas) {
const std::vector<Token> tokens{token(2, 4, 3), token(0, 8, 2), token(0, 2, 4)};
const auto sorted = normalize(tokens);
require(sorted.size() == 3);
expect(sorted[0] == token(0, 2, 4));
expect(sorted[1] == token(0, 8, 2));
expect(sorted[2] == token(2, 4, 3));
expect(encode(sorted) ==
std::vector<std::uint32_t>{0, 2, 4, 0, 0, 0, 6, 2, 0, 0, 2, 4, 3, 0, 0});
}
VOID_TEST(SemanticTokens, drops_empty_duplicate_and_overlapping_tokens) {
const std::vector<Token> tokens{token(1, 0, 0), token(1, 5, 4), token(1, 5, 4), token(1, 7, 2),
token(1, 9, 2)};
const auto result = normalize(tokens);
require(result.size() == 2);
expect(result[0] == token(1, 5, 4));
expect(result[1] == token(1, 9, 2));
}
VOID_TEST(SemanticTokens, the_same_span_on_different_lines_is_distinct) {
const std::vector<Token> tokens{token(0, 3, 2), token(1, 3, 2)};
const auto result = normalize(tokens);
expect(result.size() == 2);
expect(encode(result) == std::vector<std::uint32_t>{0, 3, 2, 0, 0, 1, 3, 2, 0, 0});
}
VOID_TEST(SemanticTokens, bounds_count_and_length) {
const std::vector<Token> tokens{token(0, 0, 1), token(0, 2, 1), token(0, 4, 1)};
expect(normalize(tokens, Limits{.tokens = 2}).size() == 2);
expect(normalize(tokens, Limits{.tokens = 2, .length = 1}).size() == 2);
const std::vector<Token> long_token{token(0, 0, 64)};
expect(normalize(long_token, Limits{.length = 8}).empty());
}
VOID_TEST(SemanticTokens, modifiers_are_preserved_and_encoded) {
const std::vector<Token> tokens{{0, 0, 1, function_type, declaration_modifier},
{0, 2, 1, function_type, 0}};
const auto result = normalize(tokens);
require(result.size() == 2);
expect(result[0].modifiers == declaration_modifier);
expect(result[1].modifiers == 0);
expect(encode(result) == std::vector<std::uint32_t>{0, 0, 1, 0, 1, 0, 2, 1, 0, 0});
}
VOID_TEST(SemanticTokens, an_overlapping_token_never_replaces_the_earlier_one) {
const std::vector<Token> tokens{{1, 4, 3, function_type, 0},
{1, 4, 3, function_type, declaration_modifier}};
const auto result = normalize(tokens);
require(result.size() == 1);
expect(result.front().modifiers == 0);
}
VOID_TEST(SemanticTokens, type_tokens_keep_their_legend_index) {
const std::vector<Token> tokens{{0, 0, 4, function_type, 0}, {0, 6, 7, type_type, 0}};
const auto result = normalize(tokens);
require(result.size() == 2);
expect(result[1].type == type_type);
expect(encode(result) == std::vector<std::uint32_t>{0, 0, 4, 0, 0, 0, 6, 7, 1, 0});
}