error_test_helpers.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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_COMMON_ERROR_TEST_HELPERS_H_
  5. #define CARBON_COMMON_ERROR_TEST_HELPERS_H_
  6. #include <gmock/gmock.h>
  7. #include "common/error.h"
  8. namespace Carbon::Testing {
  9. // Matches the message for an error state of `ErrorOr<T>`. For example:
  10. // EXPECT_THAT(my_result, IsError(StrEq("error message")));
  11. class IsError {
  12. public:
  13. // NOLINTNEXTLINE(readability-identifier-naming)
  14. using is_gtest_matcher = void;
  15. explicit IsError(::testing::Matcher<std::string> matcher)
  16. : matcher_(std::move(matcher)) {}
  17. template <typename T, typename ErrorT>
  18. auto MatchAndExplain(const ErrorOr<T, ErrorT>& result,
  19. ::testing::MatchResultListener* listener) const -> bool {
  20. if (result.ok()) {
  21. *listener << "is a success";
  22. return false;
  23. } else {
  24. RawStringOstream os;
  25. os << result.error();
  26. return matcher_.MatchAndExplain(os.TakeStr(), listener);
  27. }
  28. }
  29. auto DescribeTo(std::ostream* os) const -> void {
  30. *os << "is an error and matches ";
  31. matcher_.DescribeTo(os);
  32. }
  33. auto DescribeNegationTo(std::ostream* os) const -> void {
  34. *os << "is a success or does not match ";
  35. matcher_.DescribeTo(os);
  36. }
  37. private:
  38. ::testing::Matcher<std::string> matcher_;
  39. };
  40. // Implementation of a success matcher for a specific `T` and `ErrorT` in an
  41. // `ErrorOr`. Supports a nested matcher for the `T` value.
  42. template <typename T, typename ErrorT>
  43. class IsSuccessMatcherImpl
  44. : public ::testing::MatcherInterface<const ErrorOr<T, ErrorT>&> {
  45. public:
  46. explicit IsSuccessMatcherImpl(const ::testing::Matcher<T>& matcher)
  47. : matcher_(matcher) {}
  48. auto MatchAndExplain(const ErrorOr<T, ErrorT>& result,
  49. ::testing::MatchResultListener* listener) const
  50. -> bool override {
  51. if (result.ok()) {
  52. return matcher_.MatchAndExplain(*result, listener);
  53. } else {
  54. *listener << "is an error with `" << result.error() << "`";
  55. return false;
  56. }
  57. }
  58. auto DescribeTo(std::ostream* os) const -> void override {
  59. *os << "is a success and matches ";
  60. matcher_.DescribeTo(os);
  61. }
  62. auto DescribeNegationTo(std::ostream* os) const -> void override {
  63. *os << "is an error or does not match ";
  64. matcher_.DescribeNegationTo(os);
  65. }
  66. private:
  67. ::testing::Matcher<T> matcher_;
  68. };
  69. // Polymorphic match implementation for GoogleTest.
  70. //
  71. // To support matching arbitrary types that `InnerMatcher` can also match, this
  72. // itself must match arbitrary types. This is accomplished by not being a
  73. // matcher itself, but by being convertible into matchers for any particular
  74. // `ErrorOr`.
  75. template <typename InnerMatcher>
  76. class IsSuccessMatcher {
  77. public:
  78. explicit IsSuccessMatcher(InnerMatcher matcher)
  79. : matcher_(std::move(matcher)) {}
  80. template <typename T, typename ErrorT>
  81. explicit(false)
  82. // NOLINTNEXTLINE(google-explicit-constructor): Required for matcher APIs.
  83. operator ::testing::Matcher<const ErrorOr<T, ErrorT>&>() const {
  84. return ::testing::Matcher<const ErrorOr<T, ErrorT>&>(
  85. new IsSuccessMatcherImpl<T, ErrorT>(
  86. ::testing::SafeMatcherCast<T>(matcher_)));
  87. }
  88. private:
  89. InnerMatcher matcher_;
  90. };
  91. // Returns a matcher the value for a non-error state of `ErrorOr<T>`.
  92. //
  93. // For example:
  94. // EXPECT_THAT(my_result, IsSuccess(Eq(3)));
  95. template <typename InnerMatcher>
  96. auto IsSuccess(InnerMatcher matcher) -> IsSuccessMatcher<InnerMatcher> {
  97. return IsSuccessMatcher<InnerMatcher>(matcher);
  98. }
  99. } // namespace Carbon::Testing
  100. namespace Carbon {
  101. // Supports printing `ErrorOr<T>` to `std::ostream` in tests.
  102. template <typename T, typename ErrorT>
  103. auto operator<<(std::ostream& out, const ErrorOr<T, ErrorT>& error_or)
  104. -> std::ostream& {
  105. if (error_or.ok()) {
  106. // Try and print the value, but only if we can find a viable `<<` overload
  107. // for the value type. This should ensure that the `formatv` below can
  108. // compile cleanly, and avoid erroring when using matchers on `ErrorOr` with
  109. // unprintable value types.
  110. if constexpr (requires(const T& value) { out << value; }) {
  111. out << llvm::formatv("ErrorOr{{.value = `{0}`}}", *error_or);
  112. } else {
  113. out << "ErrorOr{{.value = `<unknown>`}}";
  114. }
  115. } else {
  116. out << llvm::formatv("ErrorOr{{.error = \"{0}\"}}", error_or.error());
  117. }
  118. return out;
  119. }
  120. } // namespace Carbon
  121. #endif // CARBON_COMMON_ERROR_TEST_HELPERS_H_