mocks.h 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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(IsDiagnosticMessageString, matcher, "") {
  15. const DiagnosticMessage& message = arg;
  16. return testing::ExplainMatchResult(matcher, message.format_fn(message),
  17. result_listener);
  18. }
  19. inline auto IsDiagnosticMessage(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. -> testing::Matcher<DiagnosticMessage> {
  25. using testing::AllOf;
  26. using testing::Field;
  27. return AllOf(Field("kind", &DiagnosticMessage::kind, kind),
  28. Field("level", &DiagnosticMessage::level, level),
  29. Field(&DiagnosticMessage::loc,
  30. AllOf(Field("line_number", &DiagnosticLoc::line_number,
  31. line_number),
  32. Field("column_number", &DiagnosticLoc::column_number,
  33. column_number))),
  34. IsDiagnosticMessageString(message));
  35. }
  36. inline auto IsDiagnostic(
  37. testing::Matcher<DiagnosticLevel> level,
  38. testing::Matcher<llvm::SmallVector<DiagnosticMessage>> elements)
  39. -> testing::Matcher<Diagnostic> {
  40. return testing::AllOf(
  41. testing::Field("level", &Diagnostic::level, level),
  42. testing::Field("messages", &Diagnostic::messages, elements));
  43. }
  44. inline auto IsSingleDiagnostic(testing::Matcher<DiagnosticKind> kind,
  45. testing::Matcher<DiagnosticLevel> level,
  46. testing::Matcher<int> line_number,
  47. testing::Matcher<int> column_number,
  48. testing::Matcher<std::string> message)
  49. -> testing::Matcher<Diagnostic> {
  50. return IsDiagnostic(
  51. level, testing::ElementsAre(IsDiagnosticMessage(kind, level, line_number,
  52. column_number, message)));
  53. }
  54. } // namespace Carbon::Testing
  55. namespace Carbon {
  56. // Printing helpers for tests.
  57. void PrintTo(const Diagnostic& diagnostic, std::ostream* os);
  58. void PrintTo(DiagnosticLevel level, std::ostream* os);
  59. } // namespace Carbon
  60. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_MOCKS_H_