file_test_base.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 "common/error.h"
  10. #include "common/ostream.h"
  11. #include "llvm/ADT/SmallString.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. #include "llvm/ADT/StringRef.h"
  14. #include "llvm/Support/VirtualFileSystem.h"
  15. #include "testing/file_test/autoupdate.h"
  16. namespace Carbon::Testing {
  17. // A framework for testing files. See README.md for documentation.
  18. class FileTestBase : public testing::Test {
  19. public:
  20. struct TestFile {
  21. friend void PrintTo(const TestFile& f, std::ostream* os) {
  22. // Print content escaped.
  23. llvm::raw_os_ostream os_wrap(*os);
  24. os_wrap << "TestFile(" << f.filename << ", \"";
  25. os_wrap.write_escaped(f.content);
  26. os_wrap << "\")";
  27. }
  28. std::string filename;
  29. std::string content;
  30. };
  31. // Provided for child class convenience.
  32. using LineNumberReplacement = FileTestAutoupdater::LineNumberReplacement;
  33. explicit FileTestBase(llvm::StringRef test_name) : test_name_(test_name) {}
  34. // Implemented by children to run the test. For example, TestBody validates
  35. // stdout and stderr. Children should use fs for file content, and may add
  36. // more files.
  37. //
  38. // Any test expectations should be called from ValidateRun, not Run.
  39. //
  40. // The return value should be an error if there was an abnormal error. It
  41. // should be true if a binary would return EXIT_SUCCESS, and false for
  42. // EXIT_FAILURE (which is a test success for `fail_*` tests).
  43. virtual auto Run(const llvm::SmallVector<llvm::StringRef>& test_args,
  44. llvm::vfs::InMemoryFileSystem& fs,
  45. llvm::raw_pwrite_stream& stdout,
  46. llvm::raw_pwrite_stream& stderr) -> ErrorOr<bool> = 0;
  47. // Implemented by children to do post-Run test expectations. Only called when
  48. // testing. Does not need to be provided if only CHECK test expectations are
  49. // used.
  50. virtual auto ValidateRun() -> void {}
  51. // Returns default arguments. Only called when a file doesn't set ARGS.
  52. virtual auto GetDefaultArgs() -> llvm::SmallVector<std::string> = 0;
  53. // Returns a regex to match the default file when a line may not be present.
  54. // May return nullptr if unused. If GetLineNumberReplacements returns an entry
  55. // with has_file=false, this is required.
  56. virtual auto GetDefaultFileRE(llvm::ArrayRef<llvm::StringRef> /*filenames*/)
  57. -> std::optional<RE2> {
  58. return std::nullopt;
  59. }
  60. // Returns replacement information for line numbers. See LineReplacement for
  61. // construction.
  62. virtual auto GetLineNumberReplacements(
  63. llvm::ArrayRef<llvm::StringRef> filenames)
  64. -> llvm::SmallVector<LineNumberReplacement>;
  65. // Optionally allows children to provide extra replacements for autoupdate.
  66. virtual auto DoExtraCheckReplacements(std::string& /*check_line*/) -> void {}
  67. // Runs a test and compares output. This keeps output split by line so that
  68. // issues are a little easier to identify by the different line.
  69. auto TestBody() -> void final;
  70. // Runs the test and autoupdates checks. Returns true if updated.
  71. auto Autoupdate() -> ErrorOr<bool>;
  72. // Returns the name of the test (relative to the repo root).
  73. auto test_name() const -> llvm::StringRef { return test_name_; }
  74. private:
  75. // Encapsulates test context generated by processing and running.
  76. struct TestContext {
  77. // The input test file content. Other parts may reference this.
  78. std::string input_content;
  79. // Lines which don't contain CHECKs, and thus need to be retained by
  80. // autoupdate. Their file and line numbers are attached.
  81. //
  82. // If there are splits, then the splitting line is in the respective file.
  83. // For N splits, the 0th file is the parts of the input file which are not
  84. // in any split, plus one file per split file.
  85. llvm::SmallVector<FileTestLine> non_check_lines;
  86. // Whether there are splits.
  87. bool has_splits = false;
  88. // Arguments for the test, generated from ARGS.
  89. llvm::SmallVector<std::string> test_args;
  90. // Files in the test, generated by content and splits.
  91. llvm::SmallVector<TestFile> test_files;
  92. // The location of the autoupdate marker, for autoupdated files.
  93. std::optional<int> autoupdate_line_number;
  94. // Whether checks are a subset, generated from SET-CHECK-SUBSET.
  95. bool check_subset = false;
  96. // stdout and stderr based on CHECK lines in the file.
  97. llvm::SmallVector<testing::Matcher<std::string>> expected_stdout;
  98. llvm::SmallVector<testing::Matcher<std::string>> expected_stderr;
  99. // stdout and stderr from Run. 16 is arbitrary but a required value.
  100. llvm::SmallString<16> stdout;
  101. llvm::SmallString<16> stderr;
  102. // Whether Run exited with success.
  103. bool exit_with_success = false;
  104. };
  105. // Processes the test file and runs the test. Returns an error if something
  106. // went wrong.
  107. auto ProcessTestFileAndRun(TestContext& context) -> ErrorOr<Success>;
  108. // Does replacements in ARGS for %s and %t.
  109. auto DoArgReplacements(llvm::SmallVector<std::string>& test_args,
  110. const llvm::SmallVector<TestFile>& test_files)
  111. -> ErrorOr<Success>;
  112. // Processes the test input, producing test files and expected output.
  113. auto ProcessTestFile(TestContext& context) -> ErrorOr<Success>;
  114. // Runs the FileTestAutoupdater, returning the result.
  115. auto RunAutoupdater(const TestContext& context, bool dry_run) -> bool;
  116. llvm::StringRef test_name_;
  117. };
  118. // Aggregate a name and factory function for tests using this framework.
  119. struct FileTestFactory {
  120. // The test fixture name.
  121. const char* name;
  122. // A factory function for tests.
  123. std::function<FileTestBase*(llvm::StringRef path)> factory_fn;
  124. };
  125. // Must be implemented by the individual file_test to initialize tests.
  126. //
  127. // We can't use INSTANTIATE_TEST_CASE_P because of ordering issues between
  128. // container initialization and test instantiation by InitGoogleTest, but this
  129. // also allows us more flexibility in execution.
  130. //
  131. // The `CARBON_FILE_TEST_FACTOR` macro below provides a standard, convenient way
  132. // to implement this function.
  133. extern auto GetFileTestFactory() -> FileTestFactory;
  134. // Provides a standard GetFileTestFactory implementation.
  135. #define CARBON_FILE_TEST_FACTORY(Name) \
  136. auto GetFileTestFactory() -> FileTestFactory { \
  137. return {#Name, [](llvm::StringRef path) { return new Name(path); }}; \
  138. }
  139. } // namespace Carbon::Testing
  140. #endif // CARBON_TESTING_FILE_TEST_FILE_TEST_BASE_H_