mocks.h 2.8 KB

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