2017-03-10 07:06:01 +00:00
|
|
|
#include "test.h"
|
|
|
|
|
2017-02-27 07:28:33 +00:00
|
|
|
#include "indexer.h"
|
2017-03-25 20:15:00 +00:00
|
|
|
#include "platform.h"
|
2017-03-01 04:12:57 +00:00
|
|
|
#include "serializer.h"
|
2017-03-01 08:36:11 +00:00
|
|
|
#include "utils.h"
|
2017-02-27 07:28:33 +00:00
|
|
|
|
|
|
|
void Write(const std::vector<std::string>& strs) {
|
2017-03-01 04:12:57 +00:00
|
|
|
for (const std::string& str : strs)
|
2017-02-27 07:28:33 +00:00
|
|
|
std::cout << str << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::string ToString(const rapidjson::Document& document) {
|
|
|
|
rapidjson::StringBuffer buffer;
|
|
|
|
rapidjson::PrettyWriter<rapidjson::StringBuffer> writer(buffer);
|
|
|
|
writer.SetFormatOptions(
|
2017-09-22 01:14:57 +00:00
|
|
|
rapidjson::PrettyFormatOptions::kFormatSingleLineArray);
|
2017-02-27 07:28:33 +00:00
|
|
|
writer.SetIndent(' ', 2);
|
|
|
|
|
|
|
|
buffer.Clear();
|
|
|
|
document.Accept(writer);
|
2017-12-23 15:51:34 +00:00
|
|
|
std::string output = buffer.GetString();
|
|
|
|
return UpdateToRnNewlines(output);
|
2017-02-27 07:28:33 +00:00
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
void DiffDocuments(std::string path,
|
|
|
|
std::string path_section,
|
|
|
|
rapidjson::Document& expected,
|
|
|
|
rapidjson::Document& actual) {
|
2017-04-03 01:34:15 +00:00
|
|
|
std::string joined_actual_output = ToString(actual);
|
2017-09-22 01:14:57 +00:00
|
|
|
std::vector<std::string> actual_output =
|
|
|
|
SplitString(joined_actual_output, "\n");
|
2017-04-03 01:34:15 +00:00
|
|
|
std::string joined_expected_output = ToString(expected);
|
2017-09-22 01:14:57 +00:00
|
|
|
std::vector<std::string> expected_output =
|
|
|
|
SplitString(joined_expected_output, "\n");
|
2017-02-27 07:28:33 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
std::cout << "[FAILED] " << path << " (section " << path_section << ")"
|
|
|
|
<< std::endl;
|
|
|
|
std::cout << "Expected output for " << path << " (section " << path_section
|
|
|
|
<< "):" << std::endl;
|
2017-04-03 01:34:15 +00:00
|
|
|
std::cout << joined_expected_output << std::endl;
|
2017-09-22 01:14:57 +00:00
|
|
|
std::cout << "Actual output for " << path << " (section " << path_section
|
|
|
|
<< "):" << std::endl;
|
2017-04-03 01:34:15 +00:00
|
|
|
std::cout << joined_actual_output << std::endl;
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
int max_diff = 5;
|
2017-02-27 07:28:33 +00:00
|
|
|
|
2017-04-08 07:11:57 +00:00
|
|
|
size_t len = std::min(actual_output.size(), expected_output.size());
|
2017-02-27 07:28:33 +00:00
|
|
|
for (int i = 0; i < len; ++i) {
|
|
|
|
if (actual_output[i] != expected_output[i]) {
|
2017-04-03 01:34:15 +00:00
|
|
|
if (--max_diff < 0) {
|
|
|
|
std::cout << "(... more lines may differ ...)" << std::endl;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2017-02-27 07:28:33 +00:00
|
|
|
std::cout << "Line " << i << " differs:" << std::endl;
|
|
|
|
std::cout << " expected: " << expected_output[i] << std::endl;
|
|
|
|
std::cout << " actual: " << actual_output[i] << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (actual_output.size() > len) {
|
|
|
|
std::cout << "Additional output in actual:" << std::endl;
|
2017-04-08 20:00:08 +00:00
|
|
|
for (size_t i = len; i < actual_output.size(); ++i)
|
2017-02-27 07:28:33 +00:00
|
|
|
std::cout << " " << actual_output[i] << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expected_output.size() > len) {
|
|
|
|
std::cout << "Additional output in expected:" << std::endl;
|
2017-04-08 20:00:08 +00:00
|
|
|
for (size_t i = len; i < expected_output.size(); ++i)
|
2017-02-27 07:28:33 +00:00
|
|
|
std::cout << " " << expected_output[i] << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-12 06:08:15 +00:00
|
|
|
void VerifySerializeToFrom(IndexFile* file) {
|
2017-04-08 20:00:08 +00:00
|
|
|
std::string expected = file->ToString();
|
2018-01-07 02:01:32 +00:00
|
|
|
std::unique_ptr<IndexFile> result = Deserialize(
|
|
|
|
SerializeFormat::Json, "--.cc", Serialize(SerializeFormat::Json, *file),
|
|
|
|
nullopt /*expected_version*/);
|
2017-06-14 04:00:51 +00:00
|
|
|
std::string actual = result->ToString();
|
2017-03-01 04:12:57 +00:00
|
|
|
if (expected != actual) {
|
2017-09-22 01:14:57 +00:00
|
|
|
std::cerr << "Serialization failure" << std::endl;
|
2017-03-01 04:12:57 +00:00
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
std::string FindExpectedOutputForFilename(
|
|
|
|
std::string filename,
|
|
|
|
const std::unordered_map<std::string, std::string>& expected) {
|
2017-04-08 22:54:36 +00:00
|
|
|
for (const auto& entry : expected) {
|
|
|
|
if (EndsWith(entry.first, filename))
|
|
|
|
return entry.second;
|
|
|
|
}
|
2017-02-27 07:28:33 +00:00
|
|
|
|
2017-04-08 22:54:36 +00:00
|
|
|
std::cerr << "Couldn't find expected output for " << filename << std::endl;
|
|
|
|
std::cin.get();
|
2017-02-27 07:28:33 +00:00
|
|
|
std::cin.get();
|
2017-04-08 22:54:36 +00:00
|
|
|
return "{}";
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
IndexFile* FindDbForPathEnding(
|
|
|
|
const std::string& path,
|
|
|
|
const std::vector<std::unique_ptr<IndexFile>>& dbs) {
|
2017-04-08 22:54:36 +00:00
|
|
|
for (auto& db : dbs) {
|
|
|
|
if (EndsWith(db->path, path))
|
|
|
|
return db.get();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-01-04 02:43:45 +00:00
|
|
|
bool RunIndexTests(const std::string& filter_path, bool enable_update) {
|
2017-04-20 06:02:24 +00:00
|
|
|
SetTestOutputMode();
|
2017-04-20 04:57:44 +00:00
|
|
|
|
2017-12-23 15:51:34 +00:00
|
|
|
// Index tests change based on the version of clang used.
|
|
|
|
static constexpr const char* kRequiredClangVersion =
|
2017-12-28 03:31:42 +00:00
|
|
|
"clang version 5.0.1 (tags/RELEASE_501/final)";
|
2017-12-23 15:51:34 +00:00
|
|
|
if (GetClangVersion() != kRequiredClangVersion) {
|
|
|
|
std::cerr << "Index tests must be run using clang version \""
|
|
|
|
<< kRequiredClangVersion << "\" (cquery is running with \""
|
|
|
|
<< GetClangVersion() << "\")" << std::endl;
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2018-01-04 02:43:45 +00:00
|
|
|
bool success = true;
|
2017-04-15 04:53:10 +00:00
|
|
|
bool update_all = false;
|
2017-11-11 19:41:09 +00:00
|
|
|
ClangIndex index;
|
2017-02-27 07:28:33 +00:00
|
|
|
|
2017-12-28 17:42:55 +00:00
|
|
|
for (std::string path : GetFilesInFolder("index_tests", true /*recursive*/,
|
2017-09-22 01:14:57 +00:00
|
|
|
true /*add_folder_to_path*/)) {
|
2017-12-22 17:07:21 +00:00
|
|
|
if (!RunObjectiveCIndexTests() && EndsWithAny(path, {".m", ".mm"})) {
|
2017-12-22 17:14:11 +00:00
|
|
|
std::cout << "Skipping \"" << path << "\" since this platform does not "
|
2018-01-04 02:06:32 +00:00
|
|
|
<< "support running Objective-C tests." << std::endl;
|
2017-12-22 17:14:11 +00:00
|
|
|
continue;
|
2017-12-22 17:07:21 +00:00
|
|
|
}
|
|
|
|
|
2017-12-28 17:55:17 +00:00
|
|
|
if (path.find(filter_path) == std::string::npos)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (!filter_path.empty())
|
|
|
|
std::cout << "Running " << path << std::endl;
|
|
|
|
|
|
|
|
// Parse expected output from the test, parse it into JSON document.
|
|
|
|
std::vector<std::string> lines_with_endings = ReadLinesWithEnding(path);
|
|
|
|
TextReplacer text_replacer;
|
|
|
|
std::vector<std::string> flags;
|
|
|
|
std::unordered_map<std::string, std::string> all_expected_output;
|
|
|
|
ParseTestExpectation(path, lines_with_endings, &text_replacer, &flags,
|
|
|
|
&all_expected_output);
|
|
|
|
|
|
|
|
// Build flags.
|
|
|
|
bool had_extra_flags = !flags.empty();
|
|
|
|
if (!AnyStartsWith(flags, "-x"))
|
|
|
|
flags.push_back("-xc++");
|
|
|
|
if (!AnyStartsWith(flags, "-std"))
|
|
|
|
flags.push_back("-std=c++11");
|
2018-01-02 10:49:56 +00:00
|
|
|
flags.push_back("-resource-dir=" + GetDefaultResourceDirectory());
|
2017-12-28 17:55:17 +00:00
|
|
|
if (had_extra_flags) {
|
|
|
|
std::cout << "For " << path << std::endl;
|
|
|
|
std::cout << " flags: " << StringJoin(flags) << std::endl;
|
|
|
|
}
|
2017-11-27 06:17:51 +00:00
|
|
|
flags.push_back(path);
|
2017-12-28 17:55:17 +00:00
|
|
|
|
|
|
|
// Run test.
|
|
|
|
Config config;
|
2017-12-29 16:29:47 +00:00
|
|
|
FileConsumerSharedState file_consumer_shared;
|
2017-12-28 17:55:17 +00:00
|
|
|
PerformanceImportFile perf;
|
|
|
|
std::vector<std::unique_ptr<IndexFile>> dbs =
|
|
|
|
Parse(&config, &file_consumer_shared, path, flags, {}, &perf, &index,
|
|
|
|
false /*dump_ast*/);
|
|
|
|
|
|
|
|
for (const auto& entry : all_expected_output) {
|
|
|
|
const std::string& expected_path = entry.first;
|
|
|
|
std::string expected_output = text_replacer.Apply(entry.second);
|
|
|
|
|
|
|
|
// FIXME: promote to utils, find and remove duplicates (ie,
|
|
|
|
// cquery_call_tree.cc, maybe something in project.cc).
|
|
|
|
auto basename = [](const std::string& path) -> std::string {
|
|
|
|
size_t last_index = path.find_last_of('/');
|
|
|
|
if (last_index == std::string::npos)
|
|
|
|
return path;
|
|
|
|
return path.substr(last_index + 1);
|
|
|
|
};
|
|
|
|
|
|
|
|
auto severity_to_string = [](const lsDiagnosticSeverity& severity) {
|
|
|
|
switch (severity) {
|
|
|
|
case lsDiagnosticSeverity::Error:
|
|
|
|
return "error ";
|
|
|
|
case lsDiagnosticSeverity::Warning:
|
|
|
|
return "warning ";
|
|
|
|
case lsDiagnosticSeverity::Information:
|
|
|
|
return "information ";
|
|
|
|
case lsDiagnosticSeverity::Hint:
|
|
|
|
return "hint ";
|
|
|
|
}
|
|
|
|
assert(false && "not reached");
|
|
|
|
return "";
|
|
|
|
};
|
2017-05-25 02:04:19 +00:00
|
|
|
|
2017-12-28 17:55:17 +00:00
|
|
|
// Get output from index operation.
|
|
|
|
IndexFile* db = FindDbForPathEnding(expected_path, dbs);
|
2018-01-04 02:32:15 +00:00
|
|
|
assert(db);
|
2017-12-28 17:55:17 +00:00
|
|
|
if (!db->diagnostics_.empty()) {
|
|
|
|
std::cout << "For " << path << std::endl;
|
|
|
|
for (const lsDiagnostic& diagnostic : db->diagnostics_) {
|
|
|
|
std::cout << " ";
|
|
|
|
if (diagnostic.severity)
|
|
|
|
std::cout << severity_to_string(*diagnostic.severity);
|
|
|
|
std::cout << basename(db->path) << ":"
|
|
|
|
<< diagnostic.range.start.ToString() << "-"
|
|
|
|
<< diagnostic.range.end.ToString() << ": "
|
|
|
|
<< diagnostic.message << std::endl;
|
2017-12-20 17:10:57 +00:00
|
|
|
}
|
2017-12-28 17:55:17 +00:00
|
|
|
}
|
|
|
|
std::string actual_output = "{}";
|
|
|
|
if (db) {
|
|
|
|
VerifySerializeToFrom(db);
|
|
|
|
actual_output = db->ToString();
|
|
|
|
}
|
|
|
|
actual_output = text_replacer.Apply(actual_output);
|
|
|
|
|
|
|
|
// Compare output via rapidjson::Document to ignore any formatting
|
|
|
|
// differences.
|
|
|
|
rapidjson::Document actual;
|
|
|
|
actual.Parse(actual_output.c_str());
|
|
|
|
rapidjson::Document expected;
|
|
|
|
expected.Parse(expected_output.c_str());
|
|
|
|
|
|
|
|
if (actual == expected) {
|
|
|
|
// std::cout << "[PASSED] " << path << std::endl;
|
|
|
|
} else {
|
2018-01-04 02:43:45 +00:00
|
|
|
success = false;
|
2017-12-28 17:55:17 +00:00
|
|
|
DiffDocuments(path, expected_path, expected, actual);
|
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << std::endl;
|
2018-01-04 02:43:45 +00:00
|
|
|
if (enable_update) {
|
|
|
|
std::cout
|
2017-12-28 17:55:17 +00:00
|
|
|
<< "[Enter to continue - type u to update test, a to update all]";
|
2018-01-04 02:43:45 +00:00
|
|
|
char c = 'u';
|
|
|
|
if (!update_all) {
|
|
|
|
c = (char)std::cin.get();
|
|
|
|
std::cin.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c == 'a')
|
|
|
|
update_all = true;
|
|
|
|
|
|
|
|
if (update_all || c == 'u') {
|
|
|
|
// Note: we use |entry.second| instead of |expected_output| because
|
|
|
|
// |expected_output| has had text replacements applied.
|
|
|
|
UpdateTestExpectation(path, entry.second, ToString(actual) + "\n");
|
|
|
|
}
|
2017-04-15 04:53:10 +00:00
|
|
|
}
|
2017-04-08 22:54:36 +00:00
|
|
|
}
|
2017-02-27 07:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
2018-01-04 02:43:45 +00:00
|
|
|
|
|
|
|
return success;
|
2017-02-27 07:28:33 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: ctor/dtor, copy ctor
|
2017-09-22 01:14:57 +00:00
|
|
|
// TODO: Always pass IndexFile by pointer, ie, search and remove all IndexFile&
|
|
|
|
// refs.
|