file_test_base_test.cpp 2.5 KB

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