file_test_base_test.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #include "testing/file_test/file_test_base.h"
  5. #include <gmock/gmock.h>
  6. #include <gtest/gtest.h>
  7. #include <fstream>
  8. #include <vector>
  9. #include "llvm/ADT/StringExtras.h"
  10. #include "llvm/Support/raw_ostream.h"
  11. namespace Carbon::Testing {
  12. namespace {
  13. using ::testing::AllOf;
  14. using ::testing::ElementsAre;
  15. using ::testing::Eq;
  16. using ::testing::Field;
  17. using ::testing::Matcher;
  18. class FileTestBaseTest : public FileTestBase {
  19. public:
  20. explicit FileTestBaseTest(const std::filesystem::path& path)
  21. : FileTestBase(path) {}
  22. static auto HasFilename(std::string filename) -> Matcher<TestFile> {
  23. return Field("filename", &TestFile::filename, Eq(filename));
  24. }
  25. static auto HasContent(std::string content) -> Matcher<TestFile> {
  26. return Field("content", &TestFile::content, Eq(content));
  27. }
  28. auto RunWithFiles(const llvm::SmallVector<llvm::StringRef>& test_args,
  29. const llvm::SmallVector<TestFile>& test_files,
  30. llvm::raw_pwrite_stream& stdout,
  31. llvm::raw_pwrite_stream& stderr) -> bool override {
  32. if (!test_args.empty()) {
  33. llvm::ListSeparator sep;
  34. stdout << test_args.size() << " args: ";
  35. for (const auto& arg : test_args) {
  36. stdout << sep << "`" << arg << "`";
  37. }
  38. stdout << "\n";
  39. }
  40. auto filename = path().filename();
  41. if (filename == "args.carbon") {
  42. EXPECT_THAT(test_files, ElementsAre(HasFilename("args.carbon")));
  43. return true;
  44. } else if (filename == "example.carbon") {
  45. EXPECT_THAT(test_files, ElementsAre(HasFilename("example.carbon")));
  46. stdout << "something\n"
  47. "\n"
  48. "9: Line delta\n"
  49. "8: Negative line delta\n"
  50. "+*[]{}\n"
  51. "Foo baz\n";
  52. return true;
  53. } else if (filename == "fail_example.carbon") {
  54. EXPECT_THAT(test_files, ElementsAre(HasFilename("fail_example.carbon")));
  55. stderr << "Oops\n";
  56. return false;
  57. } else if (filename == "two_files.carbon") {
  58. int i = 0;
  59. for (const auto& file : test_files) {
  60. // Prints line numbers to validate per-file.
  61. stdout << file.filename << ": " << ++i << "\n";
  62. }
  63. EXPECT_THAT(
  64. test_files,
  65. ElementsAre(
  66. AllOf(HasFilename("a.carbon"),
  67. HasContent("// CHECK:STDOUT: a.carbon: [[@LINE+0]]\n\n")),
  68. AllOf(HasFilename("b.carbon"),
  69. HasContent("// CHECK:STDOUT: b.carbon: [[@LINE+1]]\n"))));
  70. return true;
  71. } else {
  72. ADD_FAILURE() << "Unexpected file: " << filename;
  73. return false;
  74. }
  75. }
  76. auto GetDefaultArgs() -> llvm::SmallVector<std::string> override {
  77. return {"default_args", "%s"};
  78. }
  79. };
  80. } // namespace
  81. auto RegisterFileTests(const llvm::SmallVector<std::filesystem::path>& paths)
  82. -> void {
  83. FileTestBaseTest::RegisterTests<FileTestBaseTest>("FileTestBaseTest", paths);
  84. }
  85. } // namespace Carbon::Testing