file_test_base.h 6.6 KB

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