mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 07:24:42 +01:00
Switch FileTest to use StringRefs instead of files. (#2885)
In explorer, we already support parsing a string_view, so use that. In toolchain, we need to build support, probably using vfs, so that's a todo. bazel test //explorer:file_test --runs_per_test=5 - branch: Stats over 250 runs: max = 18.3s, min = 5.2s, avg = 11.2s, dev = 2.9s - trunk: Stats over 250 runs: max = 22.3s, min = 5.9s, avg = 12.1s, dev = 2.8s Not a dramatic improvement, but maybe more effective long-term, and this'd been requested on #2876
This commit is contained in:
@@ -16,19 +16,30 @@
|
||||
namespace Carbon::Testing {
|
||||
namespace {
|
||||
|
||||
using ::testing::AllOf;
|
||||
using ::testing::ElementsAre;
|
||||
using ::testing::Eq;
|
||||
using ::testing::Field;
|
||||
|
||||
class FileTestBaseTest : public FileTestBase {
|
||||
public:
|
||||
explicit FileTestBaseTest(const std::filesystem::path& path)
|
||||
: FileTestBase(path) {}
|
||||
|
||||
auto RunWithFiles(const llvm::SmallVector<std::string>& test_files,
|
||||
static auto HasFilename(std::string filename) -> testing::Matcher<TestFile> {
|
||||
return Field("filename", &TestFile::filename, Eq(filename));
|
||||
}
|
||||
|
||||
static auto HasContent(std::string content) -> testing::Matcher<TestFile> {
|
||||
return Field("content", &TestFile::content, Eq(content));
|
||||
}
|
||||
|
||||
auto RunWithFiles(const llvm::SmallVector<TestFile>& test_files,
|
||||
llvm::raw_ostream& stdout, llvm::raw_ostream& stderr)
|
||||
-> bool override {
|
||||
auto filename = path().filename();
|
||||
if (filename == "example.carbon") {
|
||||
EXPECT_THAT(test_files, ElementsAre("example.carbon"));
|
||||
EXPECT_THAT(test_files, ElementsAre(HasFilename("example.carbon")));
|
||||
stdout << "something\n"
|
||||
"\n"
|
||||
"8: Line delta\n"
|
||||
@@ -37,29 +48,22 @@ class FileTestBaseTest : public FileTestBase {
|
||||
"Foo baz\n";
|
||||
return true;
|
||||
} else if (filename == "fail_example.carbon") {
|
||||
EXPECT_THAT(test_files, ElementsAre("fail_example.carbon"));
|
||||
EXPECT_THAT(test_files, ElementsAre(HasFilename("fail_example.carbon")));
|
||||
stderr << "Oops\n";
|
||||
return false;
|
||||
} else if (filename == "two_files.carbon") {
|
||||
int i = 0;
|
||||
for (const auto& file : test_files) {
|
||||
// Prints line numbers to validate per-file.
|
||||
stdout << file << ": " << ++i << "\n";
|
||||
|
||||
// Make sure the split files have appropriate content.
|
||||
std::ifstream file_in(file);
|
||||
std::stringstream content;
|
||||
content << file_in.rdbuf();
|
||||
if (file == "a.carbon") {
|
||||
EXPECT_THAT(content.str(),
|
||||
testing::Eq("// CHECK:STDOUT: a.carbon: [[@LINE+0]]\n\n"))
|
||||
<< "Checking " << file;
|
||||
} else {
|
||||
EXPECT_THAT(content.str(),
|
||||
testing::Eq("// CHECK:STDOUT: b.carbon: [[@LINE+1]]\n"))
|
||||
<< "Checking " << file;
|
||||
}
|
||||
stdout << file.filename << ": " << ++i << "\n";
|
||||
}
|
||||
EXPECT_THAT(
|
||||
test_files,
|
||||
ElementsAre(
|
||||
AllOf(HasFilename("a.carbon"),
|
||||
HasContent("// CHECK:STDOUT: a.carbon: [[@LINE+0]]\n\n")),
|
||||
AllOf(HasFilename("b.carbon"),
|
||||
HasContent("// CHECK:STDOUT: b.carbon: [[@LINE+1]]\n"))));
|
||||
return true;
|
||||
} else {
|
||||
ADD_FAILURE() << "Unexpected file: " << filename;
|
||||
|
||||
Reference in New Issue
Block a user