error_test_helpers.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // Matches the value for a non-error state of `ErrorOr<T>`. For example:
  41. // EXPECT_THAT(my_result, IsSuccess(Eq(3)));
  42. template <typename InnerMatcher>
  43. class IsSuccessMatcher {
  44. public:
  45. // NOLINTNEXTLINE(readability-identifier-naming)
  46. using is_gtest_matcher = void;
  47. explicit IsSuccessMatcher(InnerMatcher matcher)
  48. : matcher_(std::move(matcher)) {}
  49. template <typename T, typename ErrorT>
  50. auto MatchAndExplain(const ErrorOr<T, ErrorT>& result,
  51. ::testing::MatchResultListener* listener) const -> bool {
  52. if (result.ok()) {
  53. return ::testing::Matcher<T>(matcher_).MatchAndExplain(*result, listener);
  54. } else {
  55. *listener << "is an error with `" << result.error() << "`";
  56. return false;
  57. }
  58. }
  59. auto DescribeTo(std::ostream* os) const -> void {
  60. *os << "is a success and matches ";
  61. matcher_.DescribeTo(os);
  62. }
  63. auto DescribeNegationTo(std::ostream* os) const -> void {
  64. *os << "is an error or does not match ";
  65. matcher_.DescribeTo(os);
  66. }
  67. private:
  68. InnerMatcher matcher_;
  69. };
  70. // Wraps `IsSuccessMatcher` for the inner matcher deduction.
  71. template <typename InnerMatcher>
  72. auto IsSuccess(InnerMatcher matcher) -> IsSuccessMatcher<InnerMatcher> {
  73. return IsSuccessMatcher<InnerMatcher>(matcher);
  74. }
  75. } // namespace Carbon::Testing
  76. namespace Carbon {
  77. // Supports printing `ErrorOr<T>` to `std::ostream` in tests.
  78. template <typename T, typename ErrorT>
  79. auto operator<<(std::ostream& out, const ErrorOr<T, ErrorT>& error_or)
  80. -> std::ostream& {
  81. if (error_or.ok()) {
  82. // Try and print the value, but only if we can find a viable `<<` overload
  83. // for the value type. This should ensure that the `formatv` below can
  84. // compile cleanly, and avoid erroring when using matchers on `ErrorOr` with
  85. // unprintable value types.
  86. if constexpr (requires(const T& value) { out << value; }) {
  87. out << llvm::formatv("ErrorOr{{.value = `{0}`}}", *error_or);
  88. } else {
  89. out << "ErrorOr{{.value = `<unknown>`}}";
  90. }
  91. } else {
  92. out << llvm::formatv("ErrorOr{{.error = \"{0}\"}}", error_or.error());
  93. }
  94. return out;
  95. }
  96. } // namespace Carbon
  97. #endif // CARBON_COMMON_ERROR_TEST_HELPERS_H_