Test improvements:

- Update files with \r\n newlines.
- Require clang 4.0.0 for running tests.
This commit is contained in:
Jacob Dufault 2017-12-23 07:51:34 -08:00
parent 6538274e66
commit 4aeb6acf04
5 changed files with 58 additions and 4 deletions

View File

@ -1868,3 +1868,7 @@ void ClangSanityCheck() {
clang_disposeTranslationUnit(tu);
clang_disposeIndex(index);
}
std::string GetClangVersion() {
return ToString(clang_getClangVersion());
}

View File

@ -559,3 +559,5 @@ std::vector<std::unique_ptr<IndexFile>> ParseWithTu(
void IndexInit();
void ClangSanityCheck();
std::string GetClangVersion();

View File

@ -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");
}
}
}

View File

@ -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");
}
}

View File

@ -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);