mocks.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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 TOOLCHAIN_DIAGNOSTICS_MOCKS_H_
  5. #define TOOLCHAIN_DIAGNOSTICS_MOCKS_H_
  6. #include "gmock/gmock.h"
  7. #include "toolchain/diagnostics/diagnostic_emitter.h"
  8. namespace Carbon {
  9. namespace Testing {
  10. class MockDiagnosticConsumer : public DiagnosticConsumer {
  11. public:
  12. // TODO: Use `MOCK_METHOD` once it's available.
  13. MOCK_METHOD1(HandleDiagnostic, void(const Diagnostic& diagnostic));
  14. };
  15. // Matcher `DiagnosticAt` matches the location of a diagnostic.
  16. MATCHER_P2(DiagnosticAt, line, column, "") {
  17. const Diagnostic& diag = arg;
  18. const Diagnostic::Location& loc = diag.location;
  19. if (loc.line_number != line) {
  20. *result_listener << "\nExpected diagnostic on line " << line
  21. << " but diagnostic is on line " << loc.line_number << ".";
  22. return false;
  23. }
  24. if (loc.column_number != column) {
  25. *result_listener << "\nExpected diagnostic on column " << column
  26. << " but diagnostic is on column " << loc.column_number
  27. << ".";
  28. return false;
  29. }
  30. return true;
  31. }
  32. auto DiagnosticLevel(Diagnostic::Level level) -> auto {
  33. return testing::Field(&Diagnostic::level, level);
  34. }
  35. template <typename Matcher>
  36. auto DiagnosticMessage(Matcher&& inner_matcher) -> auto {
  37. return testing::Field(&Diagnostic::message,
  38. std::forward<Matcher&&>(inner_matcher));
  39. }
  40. template <typename Matcher>
  41. auto DiagnosticShortName(Matcher&& inner_matcher) -> auto {
  42. return testing::Field(&Diagnostic::short_name,
  43. std::forward<Matcher&&>(inner_matcher));
  44. }
  45. } // namespace Testing
  46. } // namespace Carbon
  47. #endif // TOOLCHAIN_DIAGNOSTICS_MOCKS_H_