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