#include #include #include #include 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 \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 ")); } 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()); }