#include #include #include #include namespace { using namespace vls::features::completion_source; using Void::Testing::expect; using Void::Testing::require; std::string index(std::string_view scopes) { return R"({"schema":1,"scopes":[)" + std::string{scopes} + "]}"; } std::string scope(std::string_view path, int start_line, int start_character, int end_line, int end_character, bool complete, std::string_view items) { return R"({"path":")" + std::string{path} + R"(","startLine":)" + std::to_string(start_line) + R"(,"startCharacter":)" + std::to_string(start_character) + R"(,"endLine":)" + std::to_string(end_line) + R"(,"endCharacter":)" + std::to_string(end_character) + R"(,"complete":)" + (complete ? "true" : "false") + R"(,"items":[)" + std::string{items} + "]}"; } } // namespace VOID_TEST(CompletionSource, parses_scopes_and_items) { const auto bytes = index(scope( "/p/a.cpp", 0, 0, 2, 0, true, R"json({"label":"alpha","kind":12,"detail":"int alpha()","insertText":"alpha()"},{"label":"beta"})json")); const auto parsed = parse(bytes); require(parsed.has_value()); require(parsed->size() == 1); const auto &first = parsed->front(); expect(first.path == "/p/a.cpp"); expect(first.complete); expect(first.start == Position{0, 0}); expect(first.end == Position{2, 0}); require(first.items.size() == 2); expect(first.items[0].label == "alpha"); expect(first.items[0].kind == 12); expect(first.items[0].detail == "int alpha()"); expect(first.items[0].insert_text == "alpha()"); expect(first.items[1].kind == 1); } VOID_TEST(CompletionSource, selects_the_innermost_covering_scope) { const auto bytes = index(scope("/p/a.cpp", 0, 0, 10, 0, false, R"({"label":"outer"})") + "," + scope("/p/a.cpp", 2, 0, 4, 0, true, R"({"label":"inner"})") + "," + scope("/p/b.cpp", 0, 0, 10, 0, true, R"({"label":"other"})")); const auto parsed = parse(bytes); require(parsed.has_value()); const auto *selected = at(*parsed, "/p/a.cpp", {3, 5}); require(selected != nullptr); expect(selected->items.front().label == "inner"); expect(selected->complete); expect(at(*parsed, "/p/a.cpp", {20, 0}) == nullptr); expect(at(*parsed, "/p/missing.cpp", {1, 0}) == nullptr); } VOID_TEST(CompletionSource, rejects_malformed_and_incomplete_scopes) { expect(!parse("not json").has_value()); expect(!parse(R"({"schema":2,"scopes":[]})").has_value()); expect(!parse(R"({"schema":1})").has_value()); expect(!parse(index(scope("/p/a.cpp", 1, 0, 0, 0, true, ""))).has_value()); expect( !parse( index( R"({"path":"/p/a.cpp","startLine":0,"startCharacter":0,"endLine":1,"endCharacter":0,"complete":true,"items":[{"kind":4}]})")) .has_value()); expect( !parse( index( R"({"path":"/p/a.cpp","startLine":0,"startCharacter":0,"endLine":1,"endCharacter":0,"complete":true,"items":[{"label":"x","kind":99}]})")) .has_value()); } VOID_TEST(CompletionSource, is_bounded) { const auto bytes = index(scope("/p/a.cpp", 0, 0, 1, 0, true, R"({"label":"a"},{"label":"b"})")); expect(parse(bytes, Limits{.itemsPerScope = 1}).has_value() == false); expect(parse(bytes, Limits{.scopes = 0}).has_value() == false); expect(parse(bytes, Limits{.fileBytes = 8}).has_value() == false); expect(parse(bytes, Limits{.textBytes = 0}).has_value() == false); }