Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-02-22 09:54:31

0001 // Copyright 2024 The Abseil Authors.
0002 //
0003 // Licensed under the Apache License, Version 2.0 (the "License");
0004 // you may not use this file except in compliance with the License.
0005 // You may obtain a copy of the License at
0006 //
0007 //      https://www.apache.org/licenses/LICENSE-2.0
0008 //
0009 // Unless required by applicable law or agreed to in writing, software
0010 // distributed under the License is distributed on an "AS IS" BASIS,
0011 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
0012 // See the License for the specific language governing permissions and
0013 // limitations under the License.
0014 //
0015 // -----------------------------------------------------------------------------
0016 // File: status_matchers.h
0017 // -----------------------------------------------------------------------------
0018 //
0019 // Testing utilities for working with `absl::Status` and `absl::StatusOr`.
0020 //
0021 // Defines the following utilities:
0022 //
0023 //   ===============
0024 //   `IsOkAndHolds(m)`
0025 //   ===============
0026 //
0027 //   This gMock matcher matches a StatusOr<T> value whose status is OK
0028 //   and whose inner value matches matcher m.  Example:
0029 //
0030 //   ```
0031 //   using ::testing::MatchesRegex;
0032 //   using ::absl_testing::IsOkAndHolds;
0033 //   ...
0034 //   absl::StatusOr<string> maybe_name = ...;
0035 //   EXPECT_THAT(maybe_name, IsOkAndHolds(MatchesRegex("John .*")));
0036 //   ```
0037 //
0038 //   ===============================
0039 //   `StatusIs(status_code_matcher)`
0040 //   ===============================
0041 //
0042 //   This is a shorthand for
0043 //     `StatusIs(status_code_matcher, ::testing::_)`
0044 //   In other words, it's like the two-argument `StatusIs()`, except that it
0045 //   ignores error message.
0046 //
0047 //   ===============
0048 //   `IsOk()`
0049 //   ===============
0050 //
0051 //   Matches an `absl::Status` or `absl::StatusOr<T>` value whose status value
0052 //   is `absl::StatusCode::kOk.`
0053 //
0054 //   Equivalent to 'StatusIs(absl::StatusCode::kOk)'.
0055 //   Example:
0056 //   ```
0057 //   using ::absl_testing::IsOk;
0058 //   ...
0059 //   absl::StatusOr<string> maybe_name = ...;
0060 //   EXPECT_THAT(maybe_name, IsOk());
0061 //   Status s = ...;
0062 //   EXPECT_THAT(s, IsOk());
0063 //   ```
0064 
0065 #ifndef ABSL_STATUS_STATUS_MATCHERS_H_
0066 #define ABSL_STATUS_STATUS_MATCHERS_H_
0067 
0068 #include <ostream>  // NOLINT
0069 #include <type_traits>
0070 #include <utility>
0071 
0072 #include "gmock/gmock.h"  // gmock_for_status_matchers.h
0073 #include "absl/base/config.h"
0074 #include "absl/status/internal/status_matchers.h"
0075 
0076 namespace absl_testing {
0077 ABSL_NAMESPACE_BEGIN
0078 
0079 // Returns a gMock matcher that matches a StatusOr<> whose status is
0080 // OK and whose value matches the inner matcher.
0081 template <typename InnerMatcherT>
0082 status_internal::IsOkAndHoldsMatcher<typename std::decay<InnerMatcherT>::type>
0083 IsOkAndHolds(InnerMatcherT&& inner_matcher) {
0084   return status_internal::IsOkAndHoldsMatcher<
0085       typename std::decay<InnerMatcherT>::type>(
0086       std::forward<InnerMatcherT>(inner_matcher));
0087 }
0088 
0089 // Returns a gMock matcher that matches a Status or StatusOr<> whose status code
0090 // matches code_matcher and whose error message matches message_matcher.
0091 // Typically, code_matcher will be an absl::StatusCode, e.g.
0092 //
0093 // StatusIs(absl::StatusCode::kInvalidArgument, "...")
0094 template <typename StatusCodeMatcherT, typename StatusMessageMatcherT>
0095 status_internal::StatusIsMatcher StatusIs(
0096     StatusCodeMatcherT&& code_matcher,
0097     StatusMessageMatcherT&& message_matcher) {
0098   return status_internal::StatusIsMatcher(
0099       std::forward<StatusCodeMatcherT>(code_matcher),
0100       std::forward<StatusMessageMatcherT>(message_matcher));
0101 }
0102 
0103 // Returns a gMock matcher that matches a Status or StatusOr<> and whose status
0104 // code matches code_matcher.  See above for details.
0105 template <typename StatusCodeMatcherT>
0106 status_internal::StatusIsMatcher StatusIs(StatusCodeMatcherT&& code_matcher) {
0107   return StatusIs(std::forward<StatusCodeMatcherT>(code_matcher), ::testing::_);
0108 }
0109 
0110 // Returns a gMock matcher that matches a Status or StatusOr<> which is OK.
0111 inline status_internal::IsOkMatcher IsOk() {
0112   return status_internal::IsOkMatcher();
0113 }
0114 
0115 ABSL_NAMESPACE_END
0116 }  // namespace absl_testing
0117 
0118 #endif  // ABSL_STATUS_STATUS_MATCHERS_H_