diff --git a/src/clang_utils.cc b/src/clang_utils.cc index 278e6d22..cddd2ffc 100644 --- a/src/clang_utils.cc +++ b/src/clang_utils.cc @@ -2,6 +2,8 @@ #include "platform.h" +#include + namespace { lsRange GetLsRangeForFixIt(const CXSourceRange& range) { @@ -126,6 +128,7 @@ std::vector ConvertClangReplacementsIntoTextEdits( } return text_edits_result; } + #endif std::string FileName(CXFile file) { @@ -146,3 +149,52 @@ std::string ToString(CXString cx_string) { std::string ToString(CXCursorKind kind) { return ToString(clang_getCursorKindSpelling(kind)); } + +#if USE_CLANG_CXX +TEST_SUITE("ClangUtils") { + TEST_CASE("replacements") { + const std::string sample_document = + "int \n" + "main() { int *i = 0; return 0; \n" + "}"; + const std::vector clang_replacements = { + {"foo.cc", 3, 2, " "}, {"foo.cc", 13, 1, "\n "}, + {"foo.cc", 17, 1, ""}, {"foo.cc", 19, 0, " "}, + {"foo.cc", 25, 1, "\n "}, {"foo.cc", 35, 2, "\n"}}; + + // Expected format: + // + // int main() { + // int *i = 0; + // return 0; + // } + + const auto text_edits = ConvertClangReplacementsIntoTextEdits( + sample_document, clang_replacements); + REQUIRE(text_edits.size() == 6); + REQUIRE(text_edits[0].range.start.line == 0); + REQUIRE(text_edits[0].range.start.character == 3); + REQUIRE(text_edits[0].newText == " "); + + REQUIRE(text_edits[1].range.start.line == 1); + REQUIRE(text_edits[1].range.start.character == 8); + REQUIRE(text_edits[1].newText == "\n "); + + REQUIRE(text_edits[2].range.start.line == 1); + REQUIRE(text_edits[2].range.start.character == 12); + REQUIRE(text_edits[2].newText == ""); + + REQUIRE(text_edits[3].range.start.line == 1); + REQUIRE(text_edits[3].range.start.character == 14); + REQUIRE(text_edits[3].newText == " "); + + REQUIRE(text_edits[4].range.start.line == 1); + REQUIRE(text_edits[4].range.start.character == 20); + REQUIRE(text_edits[4].newText == "\n "); + + REQUIRE(text_edits[5].range.start.line == 1); + REQUIRE(text_edits[5].range.start.character == 30); + REQUIRE(text_edits[5].newText == "\n"); + } +} +#endif