Add unit test for conversion from clang-format edits to LSP ranges

This commit is contained in:
Daniel Martín 2018-01-11 14:16:21 +01:00 committed by Jacob Dufault
parent 903d517b0a
commit a7215c233c

View File

@ -2,6 +2,8 @@
#include "platform.h" #include "platform.h"
#include <doctest/doctest.h>
namespace { namespace {
lsRange GetLsRangeForFixIt(const CXSourceRange& range) { lsRange GetLsRangeForFixIt(const CXSourceRange& range) {
@ -126,6 +128,7 @@ std::vector<lsTextEdit> ConvertClangReplacementsIntoTextEdits(
} }
return text_edits_result; return text_edits_result;
} }
#endif #endif
std::string FileName(CXFile file) { std::string FileName(CXFile file) {
@ -146,3 +149,52 @@ std::string ToString(CXString cx_string) {
std::string ToString(CXCursorKind kind) { std::string ToString(CXCursorKind kind) {
return ToString(clang_getCursorKindSpelling(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::tooling::Replacement> 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