Refactor FileTest construction so that the test class is directly available. (#3035)

This is a simplification of the construction, although somewhat limiting
(it means that the caller can't register the same file multiple times,
though I stopped doing that anyways since it was causing confusion).
What this more importantly _allows_ is logic on the FileTestBase child
itself that's not test-specific -- in particular, autoupdate
functionality which wouldn't use RUN_ALL_TESTS.

---------

Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
This commit is contained in:
Jon Ross-Perkins
2023-08-01 16:00:29 +00:00
committed by GitHub
co-authored by Chandler Carruth
parent ce56226b73
commit eb05f618ce
10 changed files with 46 additions and 99 deletions
+6 -22
View File
@@ -21,29 +21,11 @@ ABSL_FLAG(std::vector<std::string>, file_tests, {},
namespace Carbon::Testing {
// The length of the base directory.
static int base_dir_len = 0;
using ::testing::Eq;
using ::testing::Matcher;
using ::testing::MatchesRegex;
using ::testing::StrEq;
void FileTestBase::RegisterTests(
const char* fixture_label,
const llvm::SmallVector<std::filesystem::path>& paths,
std::function<FileTestBase*(const std::filesystem::path&)> factory) {
// Use RegisterTest instead of INSTANTIATE_TEST_CASE_P because of ordering
// issues between container initialization and test instantiation by
// InitGoogleTest.
for (const auto& path : paths) {
std::string test_name = path.string().substr(base_dir_len);
testing::RegisterTest(fixture_label, test_name.c_str(), nullptr,
test_name.c_str(), __FILE__, __LINE__,
[=]() { return factory(path); });
}
}
// Reads a file to string.
static auto ReadFile(std::filesystem::path path) -> std::string {
std::ifstream proto_file(path);
@@ -373,18 +355,20 @@ auto main(int argc, char** argv) -> int {
CARBON_CHECK(target_dir.consume_front("/"));
target_dir = target_dir.substr(0, target_dir.rfind(":"));
std::string base_dir = working_dir.string() + target_dir.str() + "/";
Carbon::Testing::base_dir_len = base_dir.size();
auto test_factory = Carbon::Testing::GetFileTestFactory();
// Register tests based on their absolute path.
llvm::SmallVector<std::filesystem::path> paths;
for (const auto& file_test : absl::GetFlag(FLAGS_file_tests)) {
auto path = std::filesystem::absolute(file_test, ec);
CARBON_CHECK(!ec) << file_test << ": " << ec.message();
CARBON_CHECK(llvm::StringRef(path.string()).starts_with(base_dir))
<< "\n " << path << "\n should start with\n " << base_dir;
paths.push_back(path);
std::string test_name = path.string().substr(base_dir.size());
testing::RegisterTest(test_factory.name, test_name.c_str(), nullptr,
test_name.c_str(), __FILE__, __LINE__,
[=]() { return test_factory.factory_fn(path); });
}
Carbon::Testing::RegisterFileTests(paths);
return RUN_ALL_TESTS();
}