#include #include #include #include #include #include #include namespace { namespace D = Void::LanguageServer::Documents; using namespace vls::semantic; using Void::Testing::expect; using Void::Testing::require; constexpr std::string_view nonce = "0123456789abcdef0123456789abcdef"; constexpr std::string_view path = "/workspace/unit.cpp"; std::shared_ptr snapshot(std::string text, D::PositionEncoding encoding = D::PositionEncoding::utf16) { auto value = std::make_shared(); value->text = std::move(text); value->line_index = D::LineIndex(value->text); value->encoding = encoding; return value; } std::vector capture(std::string text, D::PositionEncoding encoding = D::PositionEncoding::utf16) { std::vector entries; entries.push_back({path, snapshot(std::move(text), encoding), SourceNode::regular, {}}); return entries; } struct Span { std::uint32_t line, start, end; }; Span span(std::string_view text, std::string_view name, std::size_t ordinal = 0) { std::size_t offset = 0; for (std::size_t found = 0; found <= ordinal; ++found) { offset = text.find(name, offset); require(offset != std::string_view::npos); if (found != ordinal) offset += name.size(); } std::uint32_t line = 1; std::size_t line_start = 0; for (std::size_t i = 0; i != offset; ++i) if (text[i] == '\n') { ++line; line_start = i + 1; } return {line, static_cast(offset - line_start + 1), static_cast(offset - line_start + 1 + name.size())}; } std::string span_json(std::string_view where, const Span &value) { return R"({"path":")" + std::string{where} + R"(","line":)" + std::to_string(value.line) + R"(,"startByte":)" + std::to_string(value.start) + R"(,"endByte":)" + std::to_string(value.end) + "}"; } std::string occurrence(std::string_view name, std::string_view type, const std::string &use, const std::string &definition) { return R"({"name":")" + std::string{name} + R"(","type":")" + std::string{type} + R"(","canonicalType":")" + std::string{type} + R"sig(","signature":"int f(int)","use":)sig" + use + R"(,"definition":)" + definition + "}"; } std::string report(std::string_view occurrences, bool complete = true, bool partial = false, std::string_view reason = "", std::string_view selected = nonce) { return R"({"schema":1,"nonce":")" + std::string{selected} + R"(","complete":)" + (complete ? "true" : "false") + R"(,"partial":)" + (partial ? "true" : "false") + R"(,"reason":")" + std::string{reason} + R"(","occurrences":[)" + std::string{occurrences} + "]}"; } auto decode(std::string_view text, std::span entries) { return decode_occurrences(text, nonce, entries); } } // namespace constexpr std::string_view source = "int f(int v) { return v; }\nint q() { return f(1); }\n"; VOID_TEST(Occurrences, binds_use_and_definition) { auto entries = capture(std::string{source}); const auto call = span(source, "f", 1), declaration = span(source, "f", 0); auto decoded = decode(report(occurrence("f", "int", span_json(path, call), span_json(path, declaration))), entries); require(decoded.has_value()); require(decoded->complete && !decoded->partial); require(decoded->occurrences.size() == 1); const auto &fact = decoded->occurrences.front(); expect(fact.name == "f" && fact.type == "int" && fact.canonical_type == "int"); expect(fact.signature == "int f(int)"); require(fact.has_definition()); const auto offset = entries.front().snapshot->position_to_offset(fact.use.start); require(offset.has_value()); expect(entries.front().snapshot->text.substr(*offset, 1) == "f"); } VOID_TEST(Occurrences, rejects_wrong_nonce) { auto entries = capture(std::string{source}); const auto call = span(source, "f", 1); auto decoded = decode(report(occurrence("f", "int", span_json(path, call), "null"), true, false, "", "ffffffffffffffffffffffffffffffff"), entries); require(!decoded.has_value()); expect(decoded.error() == OccurrenceError::wrong_nonce); } VOID_TEST(Occurrences, rejects_unknown_path) { auto entries = capture(std::string{source}); const auto call = span(source, "f", 1); auto decoded = decode( report(occurrence("f", "int", span_json("/workspace/other.cpp", call), "null")), entries); require(!decoded.has_value()); expect(decoded.error() == OccurrenceError::invalid_report); } VOID_TEST(Occurrences, rejects_mismatched_bytes) { auto entries = capture(std::string{source}); auto call = span(source, "f", 1); ++call.start; auto decoded = decode(report(occurrence("f", "int", span_json(path, call), "null")), entries); require(!decoded.has_value()); expect(decoded.error() == OccurrenceError::invalid_report); } VOID_TEST(Occurrences, incomplete_publishes_no_facts) { auto entries = capture(std::string{source}); const auto call = span(source, "f", 1); auto decoded = decode( report(occurrence("f", "int", span_json(path, call), "null"), false, false, "capacity"), entries); require(decoded.has_value()); expect(!decoded->complete && decoded->reason == "capacity" && decoded->occurrences.empty()); } VOID_TEST(Occurrences, converts_through_the_negotiated_encoding) { const std::string text = "int q(){ /*\xF0\x9F\x99\x82*/ return f(1); }\n"; for (const auto encoding : {D::PositionEncoding::utf16, D::PositionEncoding::utf8}) { auto entries = capture(text, encoding); const auto call = span(text, "f"); auto decoded = decode(report(occurrence("f", "int", span_json(path, call), "null")), entries); require(decoded.has_value()); const auto &fact = decoded->occurrences.front(); const auto prefix = text.substr(0, text.find('f')); const auto expected = encoding == D::PositionEncoding::utf16 ? prefix.size() - 4 + 2 // emoji: 4 bytes, 2 UTF-16 units : prefix.size(); expect(fact.use.start.character == expected); } } VOID_TEST(Occurrences, rejects_duplicate_uses) { auto entries = capture(std::string{source}); const auto call = span(source, "f", 1); const auto item = occurrence("f", "int", span_json(path, call), "null"); auto decoded = decode(report(item + "," + item), entries); require(!decoded.has_value()); expect(decoded.error() == OccurrenceError::invalid_report); } VOID_TEST(Occurrences, withholds_a_definition_outside_the_capture) { auto entries = capture(std::string{source}); const auto call = span(source, "f", 1); auto decoded = decode(report(occurrence("f", "int", span_json(path, call), span_json("/workspace/header.hpp", call))), entries); require(decoded.has_value()); require(decoded->occurrences.size() == 1); expect(!decoded->occurrences.front().has_definition()); } VOID_TEST(Occurrences, rejects_a_malformed_envelope) { auto entries = capture(std::string{source}); const auto call = span(source, "f", 1); auto extra = report(occurrence("f", "int", span_json(path, call), "null")); extra.insert(extra.size() - 1, R"(,"extra":1)"); require(!decode(extra, entries).has_value()); require(!decode("{\"schema\":1}", entries).has_value()); } VOID_TEST(Occurrences, binds_a_multiline_definition_extent) { auto entries = capture(std::string{source}); const auto call = span(source, "f", 1), declaration = span(source, "f", 0); const auto definition = span_json(path, declaration); const auto with_extent = definition.substr(0, definition.size() - 1) + R"(,"extent":{"startLine":1,"startByte":5,"endLine":1,"endByte":21}})"; auto decoded = decode(report(occurrence("f", "int", span_json(path, call), with_extent)), entries); require(decoded.has_value()); require(decoded->occurrences.size() == 1); const auto &fact = decoded->occurrences.front(); require(fact.extent.has_value()); expect(fact.extent->start.line == 0 && fact.extent->start.character == 4); expect(fact.extent->end.line == 0 && fact.extent->end.character == 20); // A malformed extent rejects the report rather than being dropped. const auto bad = definition.substr(0, definition.size() - 1) + R"(,"extent":{"startLine":2,"startByte":5,"endLine":1,"endByte":21}})"; expect( !decode(report(occurrence("f", "int", span_json(path, call), bad)), entries).has_value()); }