file_test_base_test.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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/StringRef.h"
  10. #include "llvm/Support/raw_ostream.h"
  11. namespace Carbon::Testing {
  12. namespace {
  13. using ::testing::ElementsAre;
  14. class FileTestBaseTest : public FileTestBase {
  15. public:
  16. explicit FileTestBaseTest(const std::filesystem::path& path)
  17. : FileTestBase(path) {}
  18. auto RunWithFiles(const llvm::SmallVector<std::string>& test_files,
  19. llvm::raw_ostream& stdout, llvm::raw_ostream& stderr)
  20. -> bool override {
  21. auto filename = path().filename();
  22. if (filename == "example.carbon") {
  23. EXPECT_THAT(test_files, ElementsAre("example.carbon"));
  24. stdout << "something\n"
  25. "\n"
  26. "8: Line delta\n"
  27. "7: Negative line delta\n"
  28. "+*[]{}\n"
  29. "Foo baz\n";
  30. return true;
  31. } else if (filename == "fail_example.carbon") {
  32. EXPECT_THAT(test_files, ElementsAre("fail_example.carbon"));
  33. stderr << "Oops\n";
  34. return false;
  35. } else if (filename == "two_files.carbon") {
  36. int i = 0;
  37. for (const auto& file : test_files) {
  38. // Prints line numbers to validate per-file.
  39. stdout << file << ": " << ++i << "\n";
  40. // Make sure the split files have appropriate content.
  41. std::ifstream file_in(file);
  42. std::stringstream content;
  43. content << file_in.rdbuf();
  44. if (file == "a.carbon") {
  45. EXPECT_THAT(content.str(),
  46. testing::Eq("// CHECK:STDOUT: a.carbon: [[@LINE+0]]\n\n"))
  47. << "Checking " << file;
  48. } else {
  49. EXPECT_THAT(content.str(),
  50. testing::Eq("// CHECK:STDOUT: b.carbon: [[@LINE+1]]\n"))
  51. << "Checking " << file;
  52. }
  53. }
  54. return true;
  55. } else {
  56. ADD_FAILURE() << "Unexpected file: " << filename;
  57. return false;
  58. }
  59. }
  60. };
  61. } // namespace
  62. auto RegisterFileTests(const llvm::SmallVector<std::filesystem::path>& paths)
  63. -> void {
  64. FileTestBaseTest::RegisterTests("FileTestBaseTest", paths,
  65. [](const std::filesystem::path& path) {
  66. return new FileTestBaseTest(path);
  67. });
  68. }
  69. } // namespace Carbon::Testing