file_test_base_test.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "common/ostream.h"
  8. #include "llvm/ADT/StringExtras.h"
  9. namespace Carbon::Testing {
  10. namespace {
  11. using ::testing::Eq;
  12. // Helper to validate file content.
  13. static auto CheckFileContent(llvm::vfs::InMemoryFileSystem& fs,
  14. llvm::StringRef filename,
  15. llvm::StringRef expected_content)
  16. -> ErrorOr<Success> {
  17. auto file = fs.getBufferForFile(filename, /*FileSize=*/-1,
  18. /*RequiresNullTerminator=*/false);
  19. if (file.getError()) {
  20. return ErrorBuilder() << "Missing " << filename;
  21. }
  22. if (file->get()->getBuffer() != expected_content) {
  23. return ErrorBuilder() << "Unexpected file content for " << filename
  24. << ".\n--- Actual:\n"
  25. << file->get()->getBuffer() << "\n--- Expected:\n"
  26. << expected_content << "\n---";
  27. }
  28. return Success();
  29. }
  30. class FileTestBaseTest : public FileTestBase {
  31. public:
  32. using FileTestBase::FileTestBase;
  33. auto Run(const llvm::SmallVector<llvm::StringRef>& test_args,
  34. llvm::vfs::InMemoryFileSystem& fs, llvm::raw_pwrite_stream& stdout,
  35. llvm::raw_pwrite_stream& stderr) -> ErrorOr<bool> override {
  36. if (!test_args.empty()) {
  37. llvm::ListSeparator sep;
  38. stdout << test_args.size() << " args: ";
  39. for (const auto& arg : test_args) {
  40. stdout << sep << "`" << arg << "`";
  41. }
  42. stdout << "\n";
  43. }
  44. auto filename = path().filename().string();
  45. if (filename == "two_files.carbon") {
  46. // Verify the split.
  47. CARBON_RETURN_IF_ERROR(CheckFileContent(
  48. fs, "a.carbon", "aaa\n// CHECK:STDOUT: a.carbon:[[@LINE-1]]: 1\n\n"));
  49. CARBON_RETURN_IF_ERROR(CheckFileContent(
  50. fs, "b.carbon", "bbb\n// CHECK:STDOUT: b.carbon:[[@LINE-1]]: 2\n"));
  51. } else {
  52. // Other files should be copied directly, so aren't as interesting.
  53. if (!fs.exists(filename)) {
  54. return ErrorBuilder() << "Missing file: " << filename;
  55. }
  56. }
  57. if (filename == "args.carbon") {
  58. return true;
  59. } else if (filename == "example.carbon") {
  60. int delta_line = 10;
  61. stdout << "something\n"
  62. << "\n"
  63. << "example.carbon:" << delta_line + 1 << ": Line delta\n"
  64. << "example.carbon:" << delta_line << ": Negative line delta\n"
  65. << "+*[]{}\n"
  66. << "Foo baz\n";
  67. return true;
  68. } else if (filename == "fail_example.carbon") {
  69. stderr << "Oops\n";
  70. return false;
  71. } else if (filename == "two_files.carbon") {
  72. // Prints line numbers to validate per-file.
  73. stdout << "a.carbon:1: 1\nb.carbon:1: 2\n";
  74. return true;
  75. } else {
  76. return ErrorBuilder() << "Unexpected file: " << 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