file_test_base.h 6.3 KB

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