mocks.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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/emitter.h"
  8. namespace Carbon::Testing {
  9. class MockDiagnosticConsumer : public Diagnostics::Consumer {
  10. public:
  11. MOCK_METHOD(void, HandleDiagnostic, (Diagnostics::Diagnostic diagnostic),
  12. (override));
  13. };
  14. // NOLINTNEXTLINE(modernize-use-trailing-return-type): From the macro.
  15. MATCHER_P(IsDiagnosticsMessageString, matcher, "") {
  16. const Diagnostics::Message& message = arg;
  17. return testing::ExplainMatchResult(matcher, message.Format(),
  18. result_listener);
  19. }
  20. inline auto IsDiagnosticMessage(testing::Matcher<Diagnostics::Kind> kind,
  21. testing::Matcher<Diagnostics::Level> level,
  22. testing::Matcher<int> line_number,
  23. testing::Matcher<int> column_number,
  24. testing::Matcher<std::string> message)
  25. -> testing::Matcher<Diagnostics::Message> {
  26. using testing::AllOf;
  27. using testing::Field;
  28. return AllOf(
  29. Field("kind", &Diagnostics::Message::kind, kind),
  30. Field("level", &Diagnostics::Message::level, level),
  31. Field(&Diagnostics::Message::loc,
  32. AllOf(Field("line_number", &Diagnostics::Loc::line_number,
  33. line_number),
  34. Field("column_number", &Diagnostics::Loc::column_number,
  35. column_number))),
  36. IsDiagnosticsMessageString(message));
  37. }
  38. inline auto IsDiagnostic(
  39. testing::Matcher<Diagnostics::Level> level,
  40. testing::Matcher<llvm::SmallVector<Diagnostics::Message>> elements)
  41. -> testing::Matcher<Diagnostics::Diagnostic> {
  42. return testing::AllOf(
  43. testing::Field("level", &Diagnostics::Diagnostic::level, level),
  44. testing::Field("messages", &Diagnostics::Diagnostic::messages, elements));
  45. }
  46. inline auto IsSingleDiagnostic(testing::Matcher<Diagnostics::Kind> kind,
  47. testing::Matcher<Diagnostics::Level> level,
  48. testing::Matcher<int> line_number,
  49. testing::Matcher<int> column_number,
  50. testing::Matcher<std::string> message)
  51. -> testing::Matcher<Diagnostics::Diagnostic> {
  52. return IsDiagnostic(
  53. level, testing::ElementsAre(IsDiagnosticMessage(kind, level, line_number,
  54. column_number, message)));
  55. }
  56. } // namespace Carbon::Testing
  57. namespace Carbon::Diagnostics {
  58. // Printing helpers for tests.
  59. auto PrintTo(const Diagnostic& diagnostic, std::ostream* os) -> void;
  60. auto PrintTo(Level level, std::ostream* os) -> void;
  61. } // namespace Carbon::Diagnostics
  62. #endif // CARBON_TOOLCHAIN_DIAGNOSTICS_MOCKS_H_