test_file.h 3.2 KB

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