Files
Language-Server/tests/features/CompletionTests.cpp
T
faumaray da8003f6d4 Implement the VLS roadmap on the native VBS build
Native VBS and CMake builds of the server and every registered suite:
protocol and document sync, VBS/compdb providers, stock GCC diagnostics
(including in-process default profile preparation), the stock occurrence
observer and hover/definition, the bounded Scheduler, C++ module graph,
scan and artifact cache with real CMI production, the context-aware
symbol index with schema-2 snapshot migration, navigation and guarded
rename, reflection provenance with splice-origin extraction, index-backed
completion and signature help, live GCC worker control with a registered
DAP suite, and the Neovim and VS Code clients.

VoidLibrary follows main. The private GCC 16.2.0 frontend is reproduced
from the versioned patches and verified stepping reflection/template code
with projected values 17/34 and 41/82.
2026-12-05 14:06:50 +03:00

144 lines
6.1 KiB
C++

#include <vls/features/Completion.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
#include <vector>
namespace {
using namespace vls::features::completion;
using Void::Testing::expect;
using Void::Testing::require;
Candidate make_candidate(std::string label, Kind kind = Kind::function, std::string detail = {}) {
return {std::move(label), kind, std::move(detail), "gcc"};
}
std::vector<std::string> labels(const Result &result) {
std::vector<std::string> names;
for (const auto &item : result.items)
names.push_back(item.label);
return names;
}
} // namespace
VOID_TEST(Completion, prefix_is_the_identifier_before_the_offset) {
expect(prefix_at("value.", 6).empty());
expect(prefix_at("value.fo", 8) == "fo");
expect(prefix_at("fo", 2) == "fo");
expect(prefix_at(" foo_bar", 9) == "foo_bar");
expect(prefix_at("_x", 2) == "_x");
expect(prefix_at("1abc", 4).empty());
expect(prefix_at("abc", 1) == "a");
expect(prefix_at("abc", 4).empty());
}
VOID_TEST(Completion, filters_by_prefix_and_replaces_it) {
const std::vector<Candidate> candidates{make_candidate("value"), make_candidate("validate"),
make_candidate("other")};
const auto result = complete(candidates, "int va = 0;", 6);
expect(labels(result) == std::vector<std::string>{"validate", "value"});
for (const auto &item : result.items) {
expect(item.replacement_start == 4);
expect(item.replacement_end == 6);
}
expect(result.incomplete);
expect(result.backend == "symbol-index");
}
VOID_TEST(Completion, deduplicates_by_label_and_kind) {
const std::vector<Candidate> candidates{make_candidate("f", Kind::function),
make_candidate("f", Kind::function),
make_candidate("f", Kind::variable)};
const auto result = complete(candidates, "f", 1);
require(result.items.size() == 2);
expect(result.items[0].kind == Kind::function);
expect(result.items[1].kind == Kind::variable);
}
VOID_TEST(Completion, ranks_types_before_functions_before_variables) {
const std::vector<Candidate> candidates{make_candidate("zz", Kind::variable),
make_candidate("zz", Kind::function),
make_candidate("zz", Kind::type)};
const auto result = complete(candidates, "z", 1);
require(result.items.size() == 3);
expect(result.items[0].kind == Kind::type);
expect(result.items[1].kind == Kind::function);
expect(result.items[2].kind == Kind::variable);
expect(result.items[0].sort_text == "0zz");
}
VOID_TEST(Completion, empty_prefix_lists_everything_bounded) {
const std::vector<Candidate> candidates{make_candidate("b"), make_candidate("a")};
const auto result = complete(candidates, "", 0, Limits{.candidates = 1});
require(result.items.size() == 1);
expect(result.items.front().label == "a");
const auto unrestricted = complete(candidates, "", 0);
expect(labels(unrestricted) == std::vector<std::string>{"a", "b"});
}
VOID_TEST(Completion, bounds_every_field_and_rejects_oversized_labels) {
const std::vector<Candidate> candidates{
make_candidate("x", Kind::function, std::string(600, 'd')),
make_candidate(std::string(300, 'y'), Kind::function)};
const auto result = complete(candidates, "x", 1);
require(result.items.size() == 1);
expect(result.items.front().detail.size() == 512);
expect(result.items.front().provenance.size() <= 128);
}
namespace {
Identity identity(std::string name = "f", std::string path = "/project/a.cpp",
std::uint32_t line = 3) {
return {std::move(name), std::move(path), line, 4};
}
Detail detail(std::string text = "int f()", std::string provenance = "gcc") {
return {identity(), Kind::function, std::move(text), std::move(provenance)};
}
} // namespace
VOID_TEST(Completion, resolve_returns_details_for_the_same_generation) {
const std::vector<Detail> current{detail()};
const auto resolved = resolve(identity(), 7, 7, current);
expect(resolved.status == Resolved::Status::resolved);
expect(resolved.detail == "int f()");
expect(resolved.documentation.find("int f()") != std::string::npos);
expect(resolved.documentation.find("/project/a.cpp:3:4") != std::string::npos);
expect(resolved.documentation.find("Provenance: gcc") != std::string::npos);
}
VOID_TEST(Completion, resolve_is_stale_after_a_generation_change) {
const std::vector<Detail> current{detail()};
const auto resolved = resolve(identity(), 7, 8, current);
expect(resolved.status == Resolved::Status::stale);
expect(resolved.documentation.empty() && resolved.detail.empty());
}
VOID_TEST(Completion, resolve_is_unknown_for_a_missing_or_different_identity) {
const std::vector<Detail> current{detail()};
const auto missing = resolve(identity("absent"), 7, 7, current);
expect(missing.status == Resolved::Status::unknown);
const auto other_path = resolve(identity("f", "/project/b.cpp"), 7, 7, current);
expect(other_path.status == Resolved::Status::unknown);
const auto other_line = resolve(identity("f", "/project/a.cpp", 9), 7, 7, current);
expect(other_line.status == Resolved::Status::unknown);
}
VOID_TEST(Completion, resolve_fences_hostile_content) {
const std::vector<Detail> current{detail("text with ``` a fence and even ```` four")};
const auto resolved = resolve(identity(), 7, 7, current);
require(resolved.status == Resolved::Status::resolved);
expect(resolved.documentation.starts_with("`````text"));
expect(resolved.documentation.find("\n`````\n") != std::string::npos);
}
VOID_TEST(Completion, resolve_bounds_the_documentation_and_keeps_the_fence_closed) {
const std::vector<Detail> current{detail(std::string(4000, 'x'))};
const auto resolved = resolve(identity(), 7, 7, current, Limits{.detailBytes = 128});
require(resolved.status == Resolved::Status::resolved);
expect(resolved.documentation.size() <= 128);
expect(resolved.documentation.ends_with("\n```\n"));
}