mocks.h 1.8 KB

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