file_test_base.h 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 <vector>
  11. #include "llvm/ADT/StringRef.h"
  12. #include "llvm/Support/raw_ostream.h"
  13. namespace Carbon::Testing {
  14. // A framework for testing files. Children implement `RegisterTestFiles` with
  15. // calls to `RegisterTests` using a factory that constructs the child.
  16. // `RunOverFile` must also be implemented and will be called as part of
  17. // individual test executions. This framework includes a `main` implementation,
  18. // so users must not provide one.
  19. //
  20. // Tests should have CHECK lines similar to `FileCheck` syntax:
  21. // https://llvm.org/docs/CommandGuide/FileCheck.html
  22. //
  23. // Special nuances are that stdout and stderr will look like `// CHECK:STDOUT:
  24. // ...` and `// CHECK:STDERR: ...` respectively. `[[@LINE+offset]` and
  25. // `{{regex}}` syntaxes should also work.
  26. //
  27. // `lit_autoupdate.py` automatically constructs compatible lines.
  28. class FileTestBase : public testing::Test {
  29. public:
  30. explicit FileTestBase(const std::filesystem::path& path);
  31. ~FileTestBase() override;
  32. // Used by children to register tests with gtest.
  33. static void RegisterTests(
  34. const char* fixture_label,
  35. const std::vector<std::filesystem::path>& paths,
  36. std::function<FileTestBase*(const std::filesystem::path&)> factory);
  37. // Implemented by children to run the test. Called by the TestBody
  38. // implementation, which will validate stdout and stderr. The return value
  39. // should be false when "fail_" is in the filename.
  40. virtual auto RunOverFile(llvm::raw_ostream& stdout, llvm::raw_ostream& stderr)
  41. -> bool = 0;
  42. // Runs a test and compares output. This keeps output split by line so that
  43. // issues are a little easier to identify by the different line.
  44. auto TestBody() -> void final;
  45. // Returns the full path of the file being tested.
  46. auto path() -> const std::filesystem::path& { return *path_; };
  47. private:
  48. // Transforms an expectation on a given line from `FileCheck` syntax into a
  49. // standard regex matcher.
  50. static auto TransformExpectation(int line_index, llvm::StringRef in)
  51. -> testing::Matcher<std::string>;
  52. const std::filesystem::path* path_;
  53. };
  54. // Must be implemented by the individual file_test to initialize tests.
  55. extern auto RegisterFileTests(const std::vector<std::filesystem::path>& paths)
  56. -> void;
  57. } // namespace Carbon::Testing
  58. #endif // CARBON_TESTING_FILE_TEST_FILE_TEST_BASE_H_