file_test_base_test.cpp 3.0 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 "llvm/ADT/StringExtras.h"
  8. namespace Carbon::Testing {
  9. namespace {
  10. using ::testing::AllOf;
  11. using ::testing::ElementsAre;
  12. using ::testing::Eq;
  13. using ::testing::Field;
  14. using ::testing::Matcher;
  15. class FileTestBaseTest : public FileTestBase {
  16. public:
  17. using FileTestBase::FileTestBase;
  18. static auto HasFilename(std::string filename) -> Matcher<TestFile> {
  19. return Field("filename", &TestFile::filename, Eq(filename));
  20. }
  21. static auto HasContent(std::string content) -> Matcher<TestFile> {
  22. return Field("content", &TestFile::content, Eq(content));
  23. }
  24. auto Run(const llvm::SmallVector<llvm::StringRef>& test_args,
  25. const llvm::SmallVector<TestFile>& test_files,
  26. llvm::raw_pwrite_stream& stdout, llvm::raw_pwrite_stream& stderr)
  27. -> ErrorOr<bool> override {
  28. if (!test_args.empty()) {
  29. llvm::ListSeparator sep;
  30. stdout << test_args.size() << " args: ";
  31. for (const auto& arg : test_args) {
  32. stdout << sep << "`" << arg << "`";
  33. }
  34. stdout << "\n";
  35. }
  36. auto filename = path().filename();
  37. if (filename == "args.carbon") {
  38. return true;
  39. } else if (filename == "example.carbon") {
  40. int delta_line = 10;
  41. stdout << "something\n"
  42. << "\n"
  43. << "example.carbon:" << delta_line + 1 << ": Line delta\n"
  44. << "example.carbon:" << delta_line << ": Negative line delta\n"
  45. << "+*[]{}\n"
  46. << "Foo baz\n";
  47. return true;
  48. } else if (filename == "fail_example.carbon") {
  49. stderr << "Oops\n";
  50. return false;
  51. } else if (filename == "two_files.carbon") {
  52. int i = 0;
  53. for (const auto& file : test_files) {
  54. // Prints line numbers to validate per-file.
  55. stdout << file.filename << ":2: " << ++i << "\n";
  56. }
  57. return true;
  58. } else {
  59. return ErrorBuilder() << "Unexpected file: " << filename;
  60. }
  61. }
  62. auto ValidateRun(const llvm::SmallVector<TestFile>& test_files)
  63. -> void override {
  64. auto filename = path().filename();
  65. if (filename == "two_files.carbon") {
  66. EXPECT_THAT(
  67. test_files,
  68. ElementsAre(
  69. AllOf(HasFilename("a.carbon"),
  70. HasContent(
  71. "// CHECK:STDOUT: a.carbon:[[@LINE+1]]: 1\naaa\n\n")),
  72. AllOf(HasFilename("b.carbon"),
  73. HasContent(
  74. "// CHECK:STDOUT: b.carbon:[[@LINE+1]]: 2\nbbb\n"))));
  75. } else {
  76. EXPECT_THAT(test_files, ElementsAre(HasFilename(filename)));
  77. }
  78. }
  79. auto GetDefaultArgs() -> llvm::SmallVector<std::string> override {
  80. return {"default_args", "%s"};
  81. }
  82. };
  83. } // namespace
  84. CARBON_FILE_TEST_FACTORY(FileTestBaseTest);
  85. } // namespace Carbon::Testing