From 4aeb6acf045c62925616becc20e12c55d90078c9 Mon Sep 17 00:00:00 2001 From: Jacob Dufault Date: Sat, 23 Dec 2017 07:51:34 -0800 Subject: [PATCH] Test improvements: - Update files with \r\n newlines. - Require clang 4.0.0 for running tests. --- src/indexer.cc | 4 ++++ src/indexer.h | 2 ++ src/test.cc | 19 +++++++++++++++---- src/utils.cc | 34 ++++++++++++++++++++++++++++++++++ src/utils.h | 3 +++ 5 files changed, 58 insertions(+), 4 deletions(-) diff --git a/src/indexer.cc b/src/indexer.cc index 12bbd2a7..da178532 100644 --- a/src/indexer.cc +++ b/src/indexer.cc @@ -1868,3 +1868,7 @@ void ClangSanityCheck() { clang_disposeTranslationUnit(tu); clang_disposeIndex(index); } + +std::string GetClangVersion() { + return ToString(clang_getClangVersion()); +} \ No newline at end of file diff --git a/src/indexer.h b/src/indexer.h index 6ba4dbf0..bcd2b94c 100644 --- a/src/indexer.h +++ b/src/indexer.h @@ -559,3 +559,5 @@ std::vector> ParseWithTu( void IndexInit(); void ClangSanityCheck(); + +std::string GetClangVersion(); \ No newline at end of file diff --git a/src/test.cc b/src/test.cc index 54a5a04c..fba2a5e4 100644 --- a/src/test.cc +++ b/src/test.cc @@ -19,7 +19,8 @@ std::string ToString(const rapidjson::Document& document) { buffer.Clear(); document.Accept(writer); - return buffer.GetString(); + std::string output = buffer.GetString(); + return UpdateToRnNewlines(output); } void DiffDocuments(std::string path, @@ -110,7 +111,16 @@ IndexFile* FindDbForPathEnding( void RunIndexTests(const std::string& filter_path) { SetTestOutputMode(); - // TODO: Assert that we need to be on clang >= 3.9.1 + // Index tests change based on the version of clang used. + static constexpr const char* kRequiredClangVersion = + "clang version 4.0.0 (tags/RELEASE_400/final)"; + if (GetClangVersion() != kRequiredClangVersion) { + std::cerr << "Index tests must be run using clang version \"" + << kRequiredClangVersion << "\" (cquery is running with \"" + << GetClangVersion() << "\")" << std::endl; + exit(1); + } + bool update_all = false; ClangIndex index; @@ -230,8 +240,9 @@ void RunIndexTests(const std::string& filter_path) { update_all = true; if (update_all || c == 'u') { - UpdateTestExpectation(path, expected_output, - ToString(actual) + "\n"); + // Note: we use |entry.second| instead of |expected_output| because + // |expected_output| has had text replacements applied. + UpdateTestExpectation(path, entry.second, ToString(actual) + "\n"); } } } diff --git a/src/utils.cc b/src/utils.cc index 879132fc..66c19ca3 100644 --- a/src/utils.cc +++ b/src/utils.cc @@ -502,6 +502,28 @@ std::string GetDefaultResourceDirectory() { return NormalizePath(result); } +std::string UpdateToRnNewlines(std::string output) { + size_t idx = 0; + while (true) { + idx = output.find('\n', idx); + + // No more matches. + if (idx == std::string::npos) + break; + + // Skip an existing "\r\n" match. + if (idx > 0 && output[idx - 1] == '\r') { + ++idx; + continue; + } + + // Replace "\n" with "\r|n". + output.replace(output.begin() + idx, output.begin() + idx + 1, "\r\n"); + } + + return output; +}; + TEST_SUITE("ParseTestExpectation") { TEST_CASE("Parse TEXT_REPLACE") { // clang-format off @@ -551,4 +573,16 @@ TEST_SUITE("ParseTestExpectation") { // Multiple replacements. REQUIRE(replacer.Apply("foofoobar0123") == "barbarbar22456"); } +} + +TEST_SUITE("Update \\n to \\r\\n") { + TEST_CASE("all") { + REQUIRE(UpdateToRnNewlines("\n") == "\r\n"); + REQUIRE(UpdateToRnNewlines("\n\n") == "\r\n\r\n"); + REQUIRE(UpdateToRnNewlines("\r\n\n") == "\r\n\r\n"); + REQUIRE(UpdateToRnNewlines("\n\r\n") == "\r\n\r\n"); + REQUIRE(UpdateToRnNewlines("\r\n\r\n") == "\r\n\r\n"); + REQUIRE(UpdateToRnNewlines("f1\nfo2\nfoo3") == "f1\r\nfo2\r\nfoo3"); + REQUIRE(UpdateToRnNewlines("f1\r\nfo2\r\nfoo3") == "f1\r\nfo2\r\nfoo3"); + } } \ No newline at end of file diff --git a/src/utils.h b/src/utils.h index f8b6dc1e..60f170b8 100644 --- a/src/utils.h +++ b/src/utils.h @@ -181,3 +181,6 @@ float GetProcessMemoryUsedInMb(); std::string FormatMicroseconds(long long microseconds); std::string GetDefaultResourceDirectory(); + +// Makes sure all newlines in |output| are in \r\n format. +std::string UpdateToRnNewlines(std::string output);