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(
|
|
|
|
rapidjson::PrettyFormatOptions::kFormatSingleLineArray);
|
|
|
|
writer.SetIndent(' ', 2);
|
|
|
|
|
|
|
|
buffer.Clear();
|
|
|
|
document.Accept(writer);
|
|
|
|
return buffer.GetString();
|
|
|
|
}
|
|
|
|
|
2017-04-03 01:34:15 +00:00
|
|
|
std::vector<std::string> SplitString(const std::string& str, const std::string& delimiter) {
|
2017-02-27 07:28:33 +00:00
|
|
|
// http://stackoverflow.com/a/13172514
|
|
|
|
std::vector<std::string> strings;
|
|
|
|
|
|
|
|
std::string::size_type pos = 0;
|
|
|
|
std::string::size_type prev = 0;
|
|
|
|
while ((pos = str.find(delimiter, prev)) != std::string::npos) {
|
|
|
|
strings.push_back(str.substr(prev, pos - prev));
|
|
|
|
prev = pos + 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// To get the last substring (or only, if delimiter is not found)
|
|
|
|
strings.push_back(str.substr(prev));
|
|
|
|
|
|
|
|
return strings;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-04-03 01:34:15 +00:00
|
|
|
void DiffDocuments(std::string path, rapidjson::Document& expected, rapidjson::Document& actual) {
|
|
|
|
std::string joined_actual_output = ToString(actual);
|
|
|
|
std::vector<std::string> actual_output = SplitString(joined_actual_output, "\n");
|
|
|
|
std::string joined_expected_output = ToString(expected);
|
|
|
|
std::vector<std::string> expected_output = SplitString(joined_expected_output, "\n");
|
2017-02-27 07:28:33 +00:00
|
|
|
|
2017-04-03 01:34:15 +00:00
|
|
|
|
|
|
|
std::cout << "[FAILED] " << path << std::endl;
|
|
|
|
std::cout << "Expected output for " << path << ":" << std::endl;
|
|
|
|
std::cout << joined_expected_output << std::endl;
|
|
|
|
std::cout << "Actual output for " << path << ":" << std::endl;
|
|
|
|
std::cout << joined_actual_output << std::endl;
|
|
|
|
std::cout << std::endl;
|
|
|
|
|
|
|
|
int max_diff = 5;
|
2017-02-27 07:28:33 +00:00
|
|
|
|
|
|
|
int len = std::min(actual_output.size(), expected_output.size());
|
|
|
|
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;
|
|
|
|
for (int i = len; i < actual_output.size(); ++i)
|
|
|
|
std::cout << " " << actual_output[i] << std::endl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (expected_output.size() > len) {
|
|
|
|
std::cout << "Additional output in expected:" << std::endl;
|
|
|
|
for (int i = len; i < expected_output.size(); ++i)
|
|
|
|
std::cout << " " << expected_output[i] << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-07 09:32:29 +00:00
|
|
|
void VerifySerializeToFrom(IndexedFile& file) {
|
|
|
|
std::string expected = file.ToString();
|
2017-04-05 08:30:19 +00:00
|
|
|
std::string actual = Deserialize("--.cc", Serialize(file)).value().ToString();
|
2017-03-01 04:12:57 +00:00
|
|
|
if (expected != actual) {
|
|
|
|
std::cerr << "Serialization failure" << std::endl;;
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 07:06:01 +00:00
|
|
|
void RunTests() {
|
2017-02-27 07:28:33 +00:00
|
|
|
// TODO: Assert that we need to be on clang >= 3.9.1
|
|
|
|
|
|
|
|
/*
|
|
|
|
ParsingDatabase db = Parse("tests/vars/function_local.cc");
|
|
|
|
std::cout << std::endl << "== Database ==" << std::endl;
|
|
|
|
std::cout << db.ToString();
|
|
|
|
std::cin.get();
|
|
|
|
return 0;
|
|
|
|
*/
|
|
|
|
|
2017-03-11 02:24:51 +00:00
|
|
|
for (std::string path : GetFilesInFolder("tests", true /*recursive*/, true /*add_folder_to_path*/)) {
|
2017-03-06 06:23:41 +00:00
|
|
|
//if (path != "tests/templates/specialized_func_definition.cc") continue;
|
2017-03-14 04:31:53 +00:00
|
|
|
//if (path != "tests/templates/namespace_template_class_template_func_usage_folded_into_one.cc") continue;
|
2017-04-03 01:34:15 +00:00
|
|
|
//if (path != "tests/multi_file/header.h") continue;
|
|
|
|
//if (path != "tests/multi_file/impl.cc") continue;
|
2017-04-05 08:06:18 +00:00
|
|
|
//if (path != "tests/constructors/constructor.cc") continue;
|
2017-03-14 04:31:53 +00:00
|
|
|
//if (path != "tests/templates/implicit_variable_instantiation.cc") continue;
|
2017-04-03 01:34:15 +00:00
|
|
|
//if (path != "tests/_empty_test.cc") continue;
|
2017-03-05 22:49:23 +00:00
|
|
|
|
|
|
|
//if (path != "tests/templates/template_class_type_usage_folded_into_one.cc") continue;
|
|
|
|
//path = "C:/Users/jacob/Desktop/superindex/indexer/" + path;
|
2017-02-27 07:28:33 +00:00
|
|
|
|
|
|
|
// Parse expected output from the test, parse it into JSON document.
|
|
|
|
std::string expected_output;
|
|
|
|
ParseTestExpectation(path, &expected_output);
|
|
|
|
rapidjson::Document expected;
|
|
|
|
expected.Parse(expected_output.c_str());
|
|
|
|
|
|
|
|
// Run test.
|
|
|
|
std::cout << "[START] " << path << std::endl;
|
2017-04-03 01:34:15 +00:00
|
|
|
IndexedFile db = Parse(path, {
|
|
|
|
"-xc++",
|
|
|
|
"-std=c++11",
|
|
|
|
"-IC:/Users/jacob/Desktop/superindex/indexer/third_party/",
|
|
|
|
"-IC:/Users/jacob/Desktop/superindex/indexer/third_party/doctest/",
|
|
|
|
"-IC:/Users/jacob/Desktop/superindex/indexer/third_party/rapidjson/include",
|
|
|
|
"-IC:/Users/jacob/Desktop/superindex/indexer/src"
|
|
|
|
}, false /*dump_ast*/);
|
2017-03-07 09:32:29 +00:00
|
|
|
VerifySerializeToFrom(db);
|
2017-02-27 07:28:33 +00:00
|
|
|
std::string actual_output = db.ToString();
|
|
|
|
|
|
|
|
rapidjson::Document actual;
|
|
|
|
actual.Parse(actual_output.c_str());
|
|
|
|
|
|
|
|
if (actual == expected) {
|
|
|
|
std::cout << "[PASSED] " << path << std::endl;
|
|
|
|
}
|
|
|
|
else {
|
2017-04-03 01:34:15 +00:00
|
|
|
DiffDocuments(path, expected, actual);
|
2017-04-05 08:29:15 +00:00
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << std::endl;
|
|
|
|
std::cout << "[Enter to continue next test]";
|
|
|
|
std::cin.get();
|
|
|
|
std::cin.get();
|
|
|
|
//break;
|
2017-02-27 07:28:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::cin.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: ctor/dtor, copy ctor
|