mocks.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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_TOOLCHAIN_DIAGNOSTICS_MOCKS_H_
  5. #define CARBON_TOOLCHAIN_DIAGNOSTICS_MOCKS_H_
  6. #include <gmock/gmock.h>
  7. #include "toolchain/diagnostics/diagnostic_emitter.h"
  8. namespace Carbon::Testing {
  9. class MockDiagnosticConsumer : public DiagnosticConsumer {
  10. public:
  11. MOCK_METHOD(void, HandleDiagnostic, (const Diagnostic& diagnostic),
  12. (override));
  13. };
  14. MATCHER_P(IsDiagnosticMessage, matcher, "") {
  15. const Diagnostic& diag = arg;
  16. return testing::ExplainMatchResult(matcher, diag.format_fn(diag),
  17. result_listener);
  18. }
  19. inline auto IsDiagnostic(testing::Matcher<DiagnosticKind> kind,
  20. testing::Matcher<DiagnosticLevel> level,
  21. testing::Matcher<int> line_number,
  22. testing::Matcher<int> column_number,
  23. testing::Matcher<std::string> message) {
  24. return testing::AllOf(
  25. testing::Field("kind", &Diagnostic::kind, kind),
  26. testing::Field("level", &Diagnostic::level, level),
  27. testing::Field(
  28. &Diagnostic::location,
  29. testing::AllOf(
  30. testing::Field("line_number", &DiagnosticLocation::line_number,
  31. line_number),
  32. testing::Field("column_number",
  33. &DiagnosticLocation::column_number,
  34. column_number))),
  35. IsDiagnosticMessage(message));
  36. }
  37. } // namespace Carbon::Testing
  38. namespace Carbon {
  39. // Printing helpers for tests.
  40. void PrintTo(const Diagnostic& diagnostic, std::ostream* os);
  41. void PrintTo(DiagnosticLevel level, std::ostream* os);
  42. } // namespace Carbon
  43. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_MOCKS_H_