|
| 1 | +/* |
| 2 | + * Copyright 2021 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +#ifndef THIRD_PARTY_CEL_CPP_BASE_TESTING_H_ |
| 18 | +#define THIRD_PARTY_CEL_CPP_BASE_TESTING_H_ |
| 19 | + |
| 20 | +#include <ostream> |
| 21 | +#include <string> |
| 22 | +#include <type_traits> |
| 23 | + |
| 24 | +#include "gmock/gmock.h" // IWYU pragma: export |
| 25 | +#include "gtest/gtest.h" |
| 26 | +#include "absl/status/status.h" |
| 27 | +#include "absl/status/statusor.h" |
| 28 | + |
| 29 | +namespace cel_base { |
| 30 | +namespace testing { |
| 31 | + |
| 32 | +inline const absl::Status& GetStatus(const absl::Status& status) { |
| 33 | + return status; |
| 34 | +} |
| 35 | + |
| 36 | +template <typename T> |
| 37 | +inline const absl::Status& GetStatus(const absl::StatusOr<T>& status) { |
| 38 | + return status.status(); |
| 39 | +} |
| 40 | + |
| 41 | +// StatusIs() is a polymorphic matcher. This class is the common |
| 42 | +// implementation of it shared by all types T where StatusIs() can be |
| 43 | +// used as a Matcher<T>. |
| 44 | +class StatusIsMatcherCommonImpl { |
| 45 | + public: |
| 46 | + StatusIsMatcherCommonImpl( |
| 47 | + ::testing::Matcher<absl::StatusCode> code_matcher, |
| 48 | + ::testing::Matcher<const std::string&> message_matcher) |
| 49 | + : code_matcher_(std::move(code_matcher)), |
| 50 | + message_matcher_(std::move(message_matcher)) {} |
| 51 | + |
| 52 | + void DescribeTo(std::ostream* os) const; |
| 53 | + |
| 54 | + void DescribeNegationTo(std::ostream* os) const; |
| 55 | + |
| 56 | + bool MatchAndExplain(const absl::Status& status, |
| 57 | + ::testing::MatchResultListener* result_listener) const; |
| 58 | + |
| 59 | + private: |
| 60 | + const ::testing::Matcher<absl::StatusCode> code_matcher_; |
| 61 | + const ::testing::Matcher<const std::string&> message_matcher_; |
| 62 | +}; |
| 63 | + |
| 64 | +// Monomorphic implementation of matcher StatusIs() for a given type |
| 65 | +// T. T can be Status, StatusOr<>, or a reference to either of them. |
| 66 | +template <typename T> |
| 67 | +class MonoStatusIsMatcherImpl : public ::testing::MatcherInterface<T> { |
| 68 | + public: |
| 69 | + explicit MonoStatusIsMatcherImpl(StatusIsMatcherCommonImpl common_impl) |
| 70 | + : common_impl_(std::move(common_impl)) {} |
| 71 | + |
| 72 | + void DescribeTo(std::ostream* os) const override { |
| 73 | + common_impl_.DescribeTo(os); |
| 74 | + } |
| 75 | + |
| 76 | + void DescribeNegationTo(std::ostream* os) const override { |
| 77 | + common_impl_.DescribeNegationTo(os); |
| 78 | + } |
| 79 | + |
| 80 | + bool MatchAndExplain( |
| 81 | + T actual_value, |
| 82 | + ::testing::MatchResultListener* result_listener) const override { |
| 83 | + return common_impl_.MatchAndExplain(GetStatus(actual_value), |
| 84 | + result_listener); |
| 85 | + } |
| 86 | + |
| 87 | + private: |
| 88 | + StatusIsMatcherCommonImpl common_impl_; |
| 89 | +}; |
| 90 | + |
| 91 | +// Implements StatusIs() as a polymorphic matcher. |
| 92 | +class StatusIsMatcher { |
| 93 | + public: |
| 94 | + StatusIsMatcher(::testing::Matcher<absl::StatusCode> code_matcher, |
| 95 | + ::testing::Matcher<const std::string&> message_matcher) |
| 96 | + : common_impl_(std::move(code_matcher), std::move(message_matcher)) {} |
| 97 | + |
| 98 | + // Converts this polymorphic matcher to a monomorphic matcher of the given |
| 99 | + // type. T can be StatusOr<>, Status, or a reference to either of them. |
| 100 | + template <typename T> |
| 101 | + operator ::testing::Matcher<T>() const { // NOLINT |
| 102 | + return ::testing::MakeMatcher(new MonoStatusIsMatcherImpl<T>(common_impl_)); |
| 103 | + } |
| 104 | + |
| 105 | + private: |
| 106 | + const StatusIsMatcherCommonImpl common_impl_; |
| 107 | +}; |
| 108 | + |
| 109 | +// Monomorphic implementation of matcher IsOk() for a given type T. |
| 110 | +// T can be Status, StatusOr<>, or a reference to either of them. |
| 111 | +template <typename T> |
| 112 | +class MonoIsOkMatcherImpl : public ::testing::MatcherInterface<T> { |
| 113 | + public: |
| 114 | + void DescribeTo(std::ostream* os) const override { *os << "is OK"; } |
| 115 | + void DescribeNegationTo(std::ostream* os) const override { |
| 116 | + *os << "is not OK"; |
| 117 | + } |
| 118 | + bool MatchAndExplain(T actual_value, |
| 119 | + ::testing::MatchResultListener*) const override { |
| 120 | + return GetStatus(actual_value).ok(); |
| 121 | + } |
| 122 | +}; |
| 123 | + |
| 124 | +// Implements IsOk() as a polymorphic matcher. |
| 125 | +class IsOkMatcher { |
| 126 | + public: |
| 127 | + template <typename T> |
| 128 | + operator ::testing::Matcher<T>() const { // NOLINT |
| 129 | + return ::testing::MakeMatcher(new MonoIsOkMatcherImpl<T>()); |
| 130 | + } |
| 131 | +}; |
| 132 | + |
| 133 | +// Returns a gMock matcher that matches a Status or StatusOr<> whose status code |
| 134 | +// matches code_matcher, and whose error message matches message_matcher. |
| 135 | +template <typename StatusCodeMatcher> |
| 136 | +StatusIsMatcher StatusIs( |
| 137 | + StatusCodeMatcher&& code_matcher, |
| 138 | + ::testing::Matcher<const std::string&> message_matcher) { |
| 139 | + return StatusIsMatcher(std::forward<StatusCodeMatcher>(code_matcher), |
| 140 | + std::move(message_matcher)); |
| 141 | +} |
| 142 | + |
| 143 | +// Returns a gMock matcher that matches a Status or StatusOr<> whose status code |
| 144 | +// matches code_matcher. |
| 145 | +template <typename StatusCodeMatcher> |
| 146 | +StatusIsMatcher StatusIs(StatusCodeMatcher&& code_matcher) { |
| 147 | + return StatusIs(std::forward<StatusCodeMatcher>(code_matcher), ::testing::_); |
| 148 | +} |
| 149 | + |
| 150 | +// Returns a gMock matcher that matches a Status or StatusOr<> which is OK. |
| 151 | +inline IsOkMatcher IsOk() { return IsOkMatcher(); } |
| 152 | + |
| 153 | +} // namespace testing |
| 154 | +} // namespace cel_base |
| 155 | + |
| 156 | +#endif // THIRD_PARTY_CEL_CPP_BASE_TESTING_H_ |
0 commit comments