file_test_base_test.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. class FileTestBaseTest : public FileTestBase {
  13. public:
  14. using FileTestBase::FileTestBase;
  15. auto Run(const llvm::SmallVector<llvm::StringRef>& test_args,
  16. llvm::vfs::InMemoryFileSystem& fs, llvm::raw_pwrite_stream& stdout,
  17. llvm::raw_pwrite_stream& stderr) -> ErrorOr<bool> override {
  18. llvm::ArrayRef<llvm::StringRef> args = test_args;
  19. llvm::ListSeparator sep;
  20. stdout << args.size() << " args: ";
  21. for (auto arg : args) {
  22. stdout << sep << "`" << arg << "`";
  23. }
  24. stdout << "\n";
  25. auto filename = std::filesystem::path(test_name().str()).filename();
  26. if (filename == "args.carbon") {
  27. // 'args.carbon' has custom arguments, so don't do regular argument
  28. // validation for it.
  29. return true;
  30. }
  31. if (args.empty() || args.front() != "default_args") {
  32. return ErrorBuilder() << "missing `default_args` argument";
  33. }
  34. args = args.drop_front();
  35. for (auto arg : args) {
  36. if (!fs.exists(arg)) {
  37. return ErrorBuilder() << "Missing file: " << arg;
  38. }
  39. }
  40. if (filename == "example.carbon") {
  41. int delta_line = 10;
  42. stdout << "something\n"
  43. << "\n"
  44. << "example.carbon:" << delta_line + 1 << ": Line delta\n"
  45. << "example.carbon:" << delta_line << ": Negative line delta\n"
  46. << "+*[]{}\n"
  47. << "Foo baz\n";
  48. return true;
  49. } else if (filename == "fail_example.carbon") {
  50. stderr << "Oops\n";
  51. return false;
  52. } else if (filename == "two_files.carbon" ||
  53. filename == "not_split.carbon") {
  54. for (auto arg : args) {
  55. // Describe file contents to stdout to validate splitting.
  56. auto file = fs.getBufferForFile(arg, /*FileSize=*/-1,
  57. /*RequiresNullTerminator=*/false);
  58. if (file.getError()) {
  59. return Error(file.getError().message());
  60. }
  61. llvm::StringRef content = file.get()->getBuffer();
  62. stdout << arg << ":1: starts with \"";
  63. stdout.write_escaped(content.take_front(40));
  64. stdout << "\", length " << content.count('\n') << " lines\n";
  65. }
  66. return true;
  67. } else if (filename == "alternating_files.carbon") {
  68. stdout << "unattached message 1\n"
  69. << "a.carbon:2: message 2\n"
  70. << "b.carbon:5: message 3\n"
  71. << "a.carbon:2: message 4\n"
  72. << "b.carbon:5: message 5\n"
  73. << "unattached message 6\n";
  74. stderr << "unattached message 1\n"
  75. << "a.carbon:2: message 2\n"
  76. << "b.carbon:5: message 3\n"
  77. << "a.carbon:2: message 4\n"
  78. << "b.carbon:5: message 5\n"
  79. << "unattached message 6\n";
  80. return true;
  81. } else if (filename == "unattached_multi_file.carbon") {
  82. stdout << "unattached message 1\n"
  83. << "unattached message 2\n";
  84. stderr << "unattached message 3\n"
  85. << "unattached message 4\n";
  86. return true;
  87. } else {
  88. return ErrorBuilder() << "Unexpected file: " << filename;
  89. }
  90. }
  91. auto GetDefaultArgs() -> llvm::SmallVector<std::string> override {
  92. return {"default_args", "%s"};
  93. }
  94. };
  95. } // namespace
  96. CARBON_FILE_TEST_FACTORY(FileTestBaseTest);
  97. } // namespace Carbon::Testing