mocks.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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, (Diagnostic diagnostic), (override));
  12. };
  13. MATCHER_P(IsDiagnosticMessage, matcher, "") {
  14. const Diagnostic& diag = arg;
  15. return testing::ExplainMatchResult(
  16. matcher, diag.message.format_fn(diag.message), result_listener);
  17. }
  18. inline auto IsDiagnostic(testing::Matcher<DiagnosticKind> kind,
  19. testing::Matcher<DiagnosticLevel> level,
  20. testing::Matcher<int> line_number,
  21. testing::Matcher<int> column_number,
  22. testing::Matcher<std::string> message) {
  23. return testing::AllOf(
  24. testing::Field("level", &Diagnostic::level, level),
  25. testing::Field(
  26. "message", &Diagnostic::message,
  27. testing::AllOf(
  28. testing::Field("kind", &DiagnosticMessage::kind, kind),
  29. testing::Field(
  30. &DiagnosticMessage::location,
  31. testing::AllOf(
  32. testing::Field("line_number",
  33. &DiagnosticLocation::line_number,
  34. line_number),
  35. testing::Field("column_number",
  36. &DiagnosticLocation::column_number,
  37. column_number))))),
  38. IsDiagnosticMessage(message));
  39. }
  40. } // namespace Carbon::Testing
  41. namespace Carbon {
  42. // Printing helpers for tests.
  43. void PrintTo(const Diagnostic& diagnostic, std::ostream* os);
  44. void PrintTo(DiagnosticLevel level, std::ostream* os);
  45. } // namespace Carbon
  46. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_MOCKS_H_