Files
Language-Server/tests/features/IncludeUpdateTests.cpp
T
faumaray b0b48d961c Update includes when files are renamed
The server advertises willRename and answers workspace/willRenameFiles
with a WorkspaceEdit. A bounded core resolves quoted includes against
the document directory and replaces only the spelling (relative stays
relative, absolute stays absolute, angle includes untouched), with
registered tests. EditorDiagnostics converts the byte edits to
negotiated positions for every open overlay; a real stdio session
returned the extra.hpp -> renamed.hpp edit.
2026-12-05 16:19:52 +03:00

80 lines
3.3 KiB
C++

#include <vls/features/IncludeUpdate.hpp>
#include <Void/Testing/Assert.hpp>
#include <Void/Testing/Test.hpp>
#include <string>
namespace {
using namespace vls::features::include_update;
using Void::Testing::expect;
using Void::Testing::require;
std::string replacement(const Edit &edit, std::string_view text) {
return std::string{text.substr(edit.start, edit.end - edit.start)};
}
} // namespace
VOID_TEST(IncludeUpdate, rewrites_a_quoted_include_resolved_from_the_document) {
const std::string text = "#pragma once\n#include \"extra.hpp\"\nint x;\n";
const auto edits =
include_edits(text, "/project/main.cpp", "/project/extra.hpp", "/project/renamed.hpp");
require(edits.size() == 1);
expect(replacement(edits.front(), text) == "extra.hpp");
expect(edits.front().replacement == "renamed.hpp");
}
VOID_TEST(IncludeUpdate, keeps_relative_spelling_and_directory) {
const std::string text = "#include \"sub/extra.hpp\"\n";
const auto edits = include_edits(text, "/project/main.cpp", "/project/sub/extra.hpp",
"/project/sub/renamed.hpp");
require(edits.size() == 1);
expect(edits.front().replacement == "sub/renamed.hpp");
const std::string dotted = "#include \"./extra.hpp\"\n";
const auto prefixed =
include_edits(dotted, "/project/main.cpp", "/project/extra.hpp", "/project/renamed.hpp");
require(prefixed.size() == 1);
expect(prefixed.front().replacement == "./renamed.hpp");
}
VOID_TEST(IncludeUpdate, leaves_unrelated_and_angle_includes_alone) {
const std::string text =
"#include <extra.hpp>\n#include \"other.hpp\"\n#include \"extra.hpp\"\n";
const auto edits =
include_edits(text, "/project/main.cpp", "/project/extra.hpp", "/project/renamed.hpp");
require(edits.size() == 1);
expect(replacement(edits.front(), text) == "extra.hpp");
expect(edits.front().start > text.find("#include <extra.hpp>"));
}
VOID_TEST(IncludeUpdate, absolute_spelling_stays_absolute) {
const std::string text = "#include \"/project/extra.hpp\"\n";
const auto edits =
include_edits(text, "/project/main.cpp", "/project/extra.hpp", "/project/renamed.hpp");
require(edits.size() == 1);
expect(edits.front().replacement == "/project/renamed.hpp");
}
VOID_TEST(IncludeUpdate, unrelated_documents_and_paths_produce_nothing) {
const std::string text = "#include \"extra.hpp\"\n";
expect(include_edits(text, "/other/main.cpp", "/project/extra.hpp", "/project/renamed.hpp")
.empty());
expect(include_edits(text, "/project/main.cpp", "/project/missing.hpp", "/project/renamed.hpp")
.empty());
expect(include_edits(text, "/project/main.cpp", "", "/project/renamed.hpp").empty());
}
VOID_TEST(IncludeUpdate, edits_are_bounded) {
std::string text;
for (int line = 0; line != 4; ++line)
text += "#include \"extra.hpp\"\n";
expect(include_edits(text, "/project/main.cpp", "/project/extra.hpp", "/project/renamed.hpp")
.size() == 4);
expect(include_edits(text, "/project/main.cpp", "/project/extra.hpp", "/project/renamed.hpp",
Limits{.edits = 2})
.size() == 2);
expect(include_edits(text, "/project/main.cpp", "/project/extra.hpp", "/project/renamed.hpp",
Limits{.pathBytes = 4})
.empty());
}