Files
carbon-lang/explorer/file_test.cpp
T
Jon Ross-Perkins 6586179c8d Add support for splitting a test file. (#2876)
I'm looking at this as I start thinking about handling `import`. Syntax is based on llvm's `split-file` tool.

The `std::vector` -> `llvm::SmallVector` switch is minor, I'm doing it here because I had to touch everything anyways and I think for tests I'll lean slightly more towards the toolchain's way of doing things versus explorer's.
2023-06-07 08:56:20 -07:00

98 lines
3.3 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "explorer/parse_and_execute/parse_and_execute.h"
#include "testing/file_test/file_test_base.h"
namespace Carbon::Testing {
namespace {
class ParseAndExecuteTestFile : public FileTestBase {
public:
explicit ParseAndExecuteTestFile(const std::filesystem::path& path,
bool trace)
: FileTestBase(path), trace_(trace) {}
auto SetUp() -> void override {
if (trace_) {
std::string path_str = path().string();
llvm::StringRef path_ref = path_str;
if (path_ref.find("/limits/") != llvm::StringRef::npos) {
GTEST_SKIP()
<< "`limits` tests check for various limit conditions (such as an "
"infinite loop). The tests collectively don't test tracing "
"because it creates substantial additional overhead.";
} else if (path_ref.endswith(
"testdata/assoc_const/rewrite_large_type.carbon") ||
path_ref.endswith(
"testdata/linked_list/typed_linked_list.carbon")) {
GTEST_SKIP() << "Expensive test to trace";
}
}
}
auto RunWithFiles(const llvm::SmallVector<std::string>& test_files,
llvm::raw_ostream& stdout, llvm::raw_ostream& stderr)
-> bool override {
if (test_files.size() != 1) {
ADD_FAILURE() << "Only 1 file is supported: " << test_files.size()
<< " provided";
return false;
}
// Capture trace streaming, but only when in debug mode.
TraceStream trace_stream;
std::string trace_stream_str;
llvm::raw_string_ostream trace_stream_ostream(trace_stream_str);
if (trace_) {
trace_stream.set_stream(&trace_stream_ostream);
}
// Set the location of the prelude.
char* test_srcdir = getenv("TEST_SRCDIR");
CARBON_CHECK(test_srcdir != nullptr);
std::string prelude_path(test_srcdir);
prelude_path += "/carbon/explorer/data/prelude.carbon";
// Run the parse. Parser debug output is always off because it's difficult
// to redirect.
auto result =
ParseAndExecuteFile(prelude_path, path().filename().string(),
/*parser_debug=*/false, &trace_stream, &stdout);
// This mirrors printing currently done by main.cpp.
if (result.ok()) {
stdout << "result: " << *result << "\n";
} else {
stderr << result.error() << "\n";
}
if (trace_) {
EXPECT_FALSE(trace_stream_str.empty())
<< "Tracing should always do something";
}
return result.ok();
}
private:
bool trace_;
};
} // namespace
extern auto RegisterFileTests(
const llvm::SmallVector<std::filesystem::path>& paths) -> void {
ParseAndExecuteTestFile::RegisterTests(
"ParseAndExecuteTestFile", paths, [=](const std::filesystem::path& path) {
return new ParseAndExecuteTestFile(path, /*trace=*/false);
});
ParseAndExecuteTestFile::RegisterTests(
"ParseAndExecuteTestFile.trace", paths,
[=](const std::filesystem::path& path) {
return new ParseAndExecuteTestFile(path, /*trace=*/true);
});
}
} // namespace Carbon::Testing