mocks.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  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. // NOLINTNEXTLINE(modernize-use-trailing-return-type): From the macro.
  14. MATCHER_P(IsDiagnosticMessage, matcher, "") {
  15. const Diagnostic& diag = arg;
  16. return testing::ExplainMatchResult(
  17. matcher, diag.message.format_fn(diag.message), 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("level", &Diagnostic::level, level),
  26. testing::Field(
  27. "message", &Diagnostic::message,
  28. testing::AllOf(
  29. testing::Field("kind", &DiagnosticMessage::kind, kind),
  30. testing::Field(
  31. &DiagnosticMessage::location,
  32. testing::AllOf(
  33. testing::Field("line_number",
  34. &DiagnosticLocation::line_number,
  35. line_number),
  36. testing::Field("column_number",
  37. &DiagnosticLocation::column_number,
  38. column_number))))),
  39. IsDiagnosticMessage(message));
  40. }
  41. } // namespace Carbon::Testing
  42. namespace Carbon {
  43. // Printing helpers for tests.
  44. void PrintTo(const Diagnostic& diagnostic, std::ostream* os);
  45. void PrintTo(DiagnosticLevel level, std::ostream* os);
  46. } // namespace Carbon
  47. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_MOCKS_H_