test_file.h 3.7 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. #ifndef CARBON_TESTING_FILE_TEST_TEST_FILE_H_
  5. #define CARBON_TESTING_FILE_TEST_TEST_FILE_H_
  6. #include <gmock/gmock.h>
  7. #include <chrono>
  8. #include <string>
  9. #include "llvm/ADT/SmallString.h"
  10. #include "llvm/ADT/SmallVector.h"
  11. #include "testing/file_test/file_test_base.h"
  12. #include "testing/file_test/line.h"
  13. namespace Carbon::Testing {
  14. // A small timer for getting elapsed durations.
  15. class Timer {
  16. public:
  17. explicit Timer() : start_(std::chrono::steady_clock::now()) {}
  18. auto elapsed_ms() -> std::chrono::milliseconds {
  19. return std::chrono::duration_cast<std::chrono::milliseconds>(
  20. std::chrono::steady_clock::now() - start_);
  21. }
  22. private:
  23. std::chrono::steady_clock::time_point start_;
  24. };
  25. // Encapsulates test context generated by processing and running.
  26. //
  27. // Note this should remain internal to `FileTestBase`, not exposed to individual
  28. // tests.
  29. struct TestFile {
  30. // Represents a split within the test file.
  31. struct Split {
  32. friend auto PrintTo(const Split& f, std::ostream* os) -> void {
  33. // Print content escaped.
  34. llvm::raw_os_ostream os_wrap(*os);
  35. os_wrap << "Split(" << f.filename << ", \"" << FormatEscaped(f.content)
  36. << "\")";
  37. }
  38. std::string filename;
  39. std::string content;
  40. };
  41. // The input test file content. Other parts may reference this.
  42. std::string input_content;
  43. // Lines which don't contain CHECKs, and thus need to be retained by
  44. // autoupdate. Their file and line numbers are attached.
  45. //
  46. // If there are splits, then the splitting line is in the respective file.
  47. // For N splits, the 0th file is the parts of the input file which are not
  48. // in any split, plus one file per split file.
  49. llvm::SmallVector<FileTestLine> non_check_lines;
  50. // Whether there are splits.
  51. bool has_splits = false;
  52. // Arguments for the test, generated from ARGS.
  53. llvm::SmallVector<std::string> test_args;
  54. // Extra arguments for the test, generated from EXTRA-ARGS. Unlike ARGS,
  55. // setting EXTRA-ARGS does not suppress the default arguments.
  56. llvm::SmallVector<std::string> extra_args;
  57. // Files in the test, generated by content and splits.
  58. llvm::SmallVector<Split> file_splits;
  59. // Files pulled in by `INCLUDE-FILE`. They're combined with `file_splits` for
  60. // execution.
  61. llvm::SmallVector<Split> include_file_splits;
  62. // The location of the autoupdate marker, for autoupdated files.
  63. std::optional<int> autoupdate_line_number;
  64. // Whether there should be an AUTOUPDATE-SPLIT.
  65. bool autoupdate_split = false;
  66. // Whether to capture stderr and stdout that would head to console,
  67. // generated from SET-CAPTURE-CONSOLE-OUTPUT.
  68. bool capture_console_output = false;
  69. // Whether checks are a subset, generated from SET-CHECK-SUBSET.
  70. bool check_subset = false;
  71. // stdout and stderr based on CHECK lines in the file.
  72. llvm::SmallVector<testing::Matcher<std::string>> expected_stdout;
  73. llvm::SmallVector<testing::Matcher<std::string>> expected_stderr;
  74. // stdout and stderr from Run. 16 is arbitrary but a required value.
  75. llvm::SmallString<16> actual_stdout;
  76. llvm::SmallString<16> actual_stderr;
  77. FileTestBase::RunResult run_result = {.success = false};
  78. // Time spent inside FileTestBase::Run.
  79. std::chrono::milliseconds run_elapsed_ms = std::chrono::milliseconds(0);
  80. };
  81. // Processes the test input, producing test files and expected output.
  82. auto ProcessTestFile(llvm::StringRef test_name, bool running_autoupdate)
  83. -> ErrorOr<TestFile>;
  84. } // namespace Carbon::Testing
  85. #endif // CARBON_TESTING_FILE_TEST_TEST_FILE_H_