mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 20:11:12 +01:00
Add file_test support for specifying arguments and only checking a subset of output. (#3018)
This is sufficient to convert remaining toolchain tests to file_test. %s and %t are currently used. While #2978 might use %T with lit, I think that's resolving a lit-specific issue that's not necessary when TEST_TMPDIR is readily available for any necessary operations (i.e., setting the working directory). With ARGS, I think it's feasible to switch to file_test without %T. Also adds documentation to file_test_base.h, which was starting to feel like a significant gap. This is not yet handled by autoupdate, but I'm eyeing that next.
This commit is contained in:
@@ -10,7 +10,7 @@
|
||||
#include <fstream>
|
||||
#include <vector>
|
||||
|
||||
#include "llvm/ADT/StringRef.h"
|
||||
#include "llvm/ADT/StringExtras.h"
|
||||
#include "llvm/Support/raw_ostream.h"
|
||||
|
||||
namespace Carbon::Testing {
|
||||
@@ -20,30 +20,44 @@ using ::testing::AllOf;
|
||||
using ::testing::ElementsAre;
|
||||
using ::testing::Eq;
|
||||
using ::testing::Field;
|
||||
using ::testing::Matcher;
|
||||
|
||||
class FileTestBaseTest : public FileTestBase {
|
||||
public:
|
||||
explicit FileTestBaseTest(const std::filesystem::path& path)
|
||||
: FileTestBase(path) {}
|
||||
|
||||
static auto HasFilename(std::string filename) -> testing::Matcher<TestFile> {
|
||||
static auto HasFilename(std::string filename) -> Matcher<TestFile> {
|
||||
return Field("filename", &TestFile::filename, Eq(filename));
|
||||
}
|
||||
|
||||
static auto HasContent(std::string content) -> testing::Matcher<TestFile> {
|
||||
static auto HasContent(std::string content) -> Matcher<TestFile> {
|
||||
return Field("content", &TestFile::content, Eq(content));
|
||||
}
|
||||
|
||||
auto RunWithFiles(const llvm::SmallVector<TestFile>& test_files,
|
||||
auto RunWithFiles(const llvm::SmallVector<llvm::StringRef>& test_args,
|
||||
const llvm::SmallVector<TestFile>& test_files,
|
||||
llvm::raw_pwrite_stream& stdout,
|
||||
llvm::raw_pwrite_stream& stderr) -> bool override {
|
||||
if (!test_args.empty()) {
|
||||
llvm::ListSeparator sep;
|
||||
stdout << test_args.size() << " args: ";
|
||||
for (const auto& arg : test_args) {
|
||||
stdout << sep << "`" << arg << "`";
|
||||
}
|
||||
stdout << "\n";
|
||||
}
|
||||
|
||||
auto filename = path().filename();
|
||||
if (filename == "example.carbon") {
|
||||
if (filename == "args.carbon") {
|
||||
EXPECT_THAT(test_files, ElementsAre(HasFilename("args.carbon")));
|
||||
return true;
|
||||
} else if (filename == "example.carbon") {
|
||||
EXPECT_THAT(test_files, ElementsAre(HasFilename("example.carbon")));
|
||||
stdout << "something\n"
|
||||
"\n"
|
||||
"8: Line delta\n"
|
||||
"7: Negative line delta\n"
|
||||
"9: Line delta\n"
|
||||
"8: Negative line delta\n"
|
||||
"+*[]{}\n"
|
||||
"Foo baz\n";
|
||||
return true;
|
||||
@@ -70,6 +84,10 @@ class FileTestBaseTest : public FileTestBase {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
auto GetDefaultArgs() -> llvm::SmallVector<std::string> override {
|
||||
return {"default_args", "%s"};
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user