file_test_base.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #ifndef CARBON_TESTING_FILE_TEST_FILE_TEST_BASE_H_
  5. #define CARBON_TESTING_FILE_TEST_FILE_TEST_BASE_H_
  6. #include <gmock/gmock.h>
  7. #include <gtest/gtest.h>
  8. #include <functional>
  9. #include <mutex>
  10. #include "common/error.h"
  11. #include "common/ostream.h"
  12. #include "llvm/ADT/SmallString.h"
  13. #include "llvm/ADT/SmallVector.h"
  14. #include "llvm/ADT/StringMap.h"
  15. #include "llvm/ADT/StringRef.h"
  16. #include "llvm/Support/VirtualFileSystem.h"
  17. #include "testing/file_test/autoupdate.h"
  18. #include "testing/file_test/manifest.h"
  19. namespace Carbon::Testing {
  20. // A framework for testing files. See README.md for documentation.
  21. class FileTestBase {
  22. public:
  23. // Provided for child class convenience.
  24. using LineNumberReplacement = FileTestAutoupdater::LineNumberReplacement;
  25. // The result of Run(), used to detect errors. Failing test files should be
  26. // named with a `fail_` prefix to indicate an expectation of failure.
  27. //
  28. // If per_file_success is empty:
  29. // - The main file has a `fail_` prefix if !success.
  30. // - The prefix of split files is unused.
  31. //
  32. // If per_file_success is non-empty:
  33. // - Each file has a `fail_` prefix if !per_file_success[i].second.
  34. // - Files may be in per_file_success that aren't part of the main test
  35. // file. This allows tracking success in handling files that are
  36. // well-known, such as standard libraries. It is still the responsibility
  37. // of callers to use a `fail_` prefix if !per_file_success[i].second.
  38. // - If any file has a `fail_` prefix, success must be false, and the prefix
  39. // of the main file is unused.
  40. // - If no file has a `fail_` prefix, the main file has a `fail_` prefix if
  41. // !success.
  42. struct RunResult {
  43. bool success;
  44. // Per-file success results. May be empty.
  45. llvm::SmallVector<std::pair<std::string, bool>> per_file_success;
  46. };
  47. explicit FileTestBase(llvm::StringRef test_name) : test_name_(test_name) {}
  48. virtual ~FileTestBase() = default;
  49. // Implemented by children to run the test. The framework will validate the
  50. // content written to `output_stream` and `error_stream`. Children should use
  51. // `fs` for file content, and may add more files.
  52. //
  53. // If there is a split test file named "STDIN", then its contents will be
  54. // provided at `input_stream` instead of `fs`. Otherwise, `input_stream` will
  55. // be null.
  56. //
  57. // This cannot be used for test expectations, such as EXPECT_TRUE.
  58. //
  59. // The return value should be an error if there was an abnormal error, and
  60. // RunResult otherwise.
  61. virtual auto Run(const llvm::SmallVector<llvm::StringRef>& test_args,
  62. llvm::IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem>& fs,
  63. FILE* input_stream, llvm::raw_pwrite_stream& output_stream,
  64. llvm::raw_pwrite_stream& error_stream) const
  65. -> ErrorOr<RunResult> = 0;
  66. // Returns default arguments. Only called when a file doesn't set ARGS.
  67. virtual auto GetDefaultArgs() const -> llvm::SmallVector<std::string> = 0;
  68. // Returns a map of string replacements to implement `%{key}` -> `value` in
  69. // arguments.
  70. virtual auto GetArgReplacements() const -> llvm::StringMap<std::string> {
  71. return {};
  72. }
  73. // Returns a regex to match the default file when a line may not be present.
  74. // May return nullptr if unused. If GetLineNumberReplacements returns an entry
  75. // with has_file=false, this is required.
  76. virtual auto GetDefaultFileRE(llvm::ArrayRef<llvm::StringRef> /*filenames*/)
  77. const -> std::optional<RE2> {
  78. return std::nullopt;
  79. }
  80. // Returns replacement information for line numbers. See LineReplacement for
  81. // construction.
  82. virtual auto GetLineNumberReplacements(
  83. llvm::ArrayRef<llvm::StringRef> filenames) const
  84. -> llvm::SmallVector<LineNumberReplacement>;
  85. // Optionally allows children to provide extra replacements for autoupdate.
  86. virtual auto DoExtraCheckReplacements(std::string& /*check_line*/) const
  87. -> void {}
  88. // Whether to allow running the test in parallel, particularly for autoupdate.
  89. // This can be overridden to force some tests to be run serially. At any given
  90. // time, all parallel tests and a single non-parallel test will be allowed to
  91. // run.
  92. virtual auto AllowParallelRun() const -> bool { return true; }
  93. // Modes for GetBazelCommand.
  94. enum class BazelMode : uint8_t {
  95. Autoupdate,
  96. Dump,
  97. Test,
  98. };
  99. // Returns the requested bazel command string for the given execution mode.
  100. auto GetBazelCommand(BazelMode mode) -> std::string;
  101. auto test_name() const -> llvm::StringRef { return test_name_; }
  102. private:
  103. llvm::StringRef test_name_;
  104. };
  105. // Aggregate a name and factory function for tests using this framework.
  106. struct FileTestFactory {
  107. // The test fixture name.
  108. const char* name;
  109. // A factory function for tests.
  110. std::function<auto(llvm::StringRef exe_path, llvm::StringRef test_name)
  111. ->std::unique_ptr<FileTestBase>>
  112. factory_fn;
  113. };
  114. // Must be implemented by the individual file_test to initialize tests.
  115. //
  116. // We can't use INSTANTIATE_TEST_CASE_P because of ordering issues between
  117. // container initialization and test instantiation by InitGoogleTest, but this
  118. // also allows us more flexibility in execution.
  119. //
  120. // The `CARBON_FILE_TEST_FACTOR` macro below provides a standard, convenient way
  121. // to implement this function.
  122. extern auto GetFileTestFactory() -> FileTestFactory;
  123. // Provides a standard GetFileTestFactory implementation.
  124. #define CARBON_FILE_TEST_FACTORY(Name) \
  125. auto GetFileTestFactory() -> FileTestFactory { \
  126. return {#Name, [](llvm::StringRef exe_path, llvm::StringRef test_name) { \
  127. return std::make_unique<Name>(exe_path, test_name); \
  128. }}; \
  129. }
  130. } // namespace Carbon::Testing
  131. #endif // CARBON_TESTING_FILE_TEST_FILE_TEST_BASE_H_