xref: /aosp_15_r20/external/abseil-cpp/absl/status/status_matchers.h (revision 9356374a3709195abf420251b3e825997ff56c0f)
1*9356374aSAndroid Build Coastguard Worker // Copyright 2024 The Abseil Authors.
2*9356374aSAndroid Build Coastguard Worker //
3*9356374aSAndroid Build Coastguard Worker // Licensed under the Apache License, Version 2.0 (the "License");
4*9356374aSAndroid Build Coastguard Worker // you may not use this file except in compliance with the License.
5*9356374aSAndroid Build Coastguard Worker // You may obtain a copy of the License at
6*9356374aSAndroid Build Coastguard Worker //
7*9356374aSAndroid Build Coastguard Worker //      https://www.apache.org/licenses/LICENSE-2.0
8*9356374aSAndroid Build Coastguard Worker //
9*9356374aSAndroid Build Coastguard Worker // Unless required by applicable law or agreed to in writing, software
10*9356374aSAndroid Build Coastguard Worker // distributed under the License is distributed on an "AS IS" BASIS,
11*9356374aSAndroid Build Coastguard Worker // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*9356374aSAndroid Build Coastguard Worker // See the License for the specific language governing permissions and
13*9356374aSAndroid Build Coastguard Worker // limitations under the License.
14*9356374aSAndroid Build Coastguard Worker //
15*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
16*9356374aSAndroid Build Coastguard Worker // File: status_matchers.h
17*9356374aSAndroid Build Coastguard Worker // -----------------------------------------------------------------------------
18*9356374aSAndroid Build Coastguard Worker //
19*9356374aSAndroid Build Coastguard Worker // Testing utilities for working with `absl::Status` and `absl::StatusOr`.
20*9356374aSAndroid Build Coastguard Worker //
21*9356374aSAndroid Build Coastguard Worker // Defines the following utilities:
22*9356374aSAndroid Build Coastguard Worker //
23*9356374aSAndroid Build Coastguard Worker //   ===============
24*9356374aSAndroid Build Coastguard Worker //   `IsOkAndHolds(m)`
25*9356374aSAndroid Build Coastguard Worker //   ===============
26*9356374aSAndroid Build Coastguard Worker //
27*9356374aSAndroid Build Coastguard Worker //   This gMock matcher matches a StatusOr<T> value whose status is OK
28*9356374aSAndroid Build Coastguard Worker //   and whose inner value matches matcher m.  Example:
29*9356374aSAndroid Build Coastguard Worker //
30*9356374aSAndroid Build Coastguard Worker //   ```
31*9356374aSAndroid Build Coastguard Worker //   using ::testing::MatchesRegex;
32*9356374aSAndroid Build Coastguard Worker //   using ::absl_testing::IsOkAndHolds;
33*9356374aSAndroid Build Coastguard Worker //   ...
34*9356374aSAndroid Build Coastguard Worker //   absl::StatusOr<string> maybe_name = ...;
35*9356374aSAndroid Build Coastguard Worker //   EXPECT_THAT(maybe_name, IsOkAndHolds(MatchesRegex("John .*")));
36*9356374aSAndroid Build Coastguard Worker //   ```
37*9356374aSAndroid Build Coastguard Worker //
38*9356374aSAndroid Build Coastguard Worker //   ===============================
39*9356374aSAndroid Build Coastguard Worker //   `StatusIs(status_code_matcher)`
40*9356374aSAndroid Build Coastguard Worker //   ===============================
41*9356374aSAndroid Build Coastguard Worker //
42*9356374aSAndroid Build Coastguard Worker //   This is a shorthand for
43*9356374aSAndroid Build Coastguard Worker //     `StatusIs(status_code_matcher, ::testing::_)`
44*9356374aSAndroid Build Coastguard Worker //   In other words, it's like the two-argument `StatusIs()`, except that it
45*9356374aSAndroid Build Coastguard Worker //   ignores error message.
46*9356374aSAndroid Build Coastguard Worker //
47*9356374aSAndroid Build Coastguard Worker //   ===============
48*9356374aSAndroid Build Coastguard Worker //   `IsOk()`
49*9356374aSAndroid Build Coastguard Worker //   ===============
50*9356374aSAndroid Build Coastguard Worker //
51*9356374aSAndroid Build Coastguard Worker //   Matches an `absl::Status` or `absl::StatusOr<T>` value whose status value
52*9356374aSAndroid Build Coastguard Worker //   is `absl::StatusCode::kOk.`
53*9356374aSAndroid Build Coastguard Worker //
54*9356374aSAndroid Build Coastguard Worker //   Equivalent to 'StatusIs(absl::StatusCode::kOk)'.
55*9356374aSAndroid Build Coastguard Worker //   Example:
56*9356374aSAndroid Build Coastguard Worker //   ```
57*9356374aSAndroid Build Coastguard Worker //   using ::absl_testing::IsOk;
58*9356374aSAndroid Build Coastguard Worker //   ...
59*9356374aSAndroid Build Coastguard Worker //   absl::StatusOr<string> maybe_name = ...;
60*9356374aSAndroid Build Coastguard Worker //   EXPECT_THAT(maybe_name, IsOk());
61*9356374aSAndroid Build Coastguard Worker //   Status s = ...;
62*9356374aSAndroid Build Coastguard Worker //   EXPECT_THAT(s, IsOk());
63*9356374aSAndroid Build Coastguard Worker //   ```
64*9356374aSAndroid Build Coastguard Worker 
65*9356374aSAndroid Build Coastguard Worker #ifndef ABSL_STATUS_STATUS_MATCHERS_H_
66*9356374aSAndroid Build Coastguard Worker #define ABSL_STATUS_STATUS_MATCHERS_H_
67*9356374aSAndroid Build Coastguard Worker 
68*9356374aSAndroid Build Coastguard Worker #include <ostream>  // NOLINT
69*9356374aSAndroid Build Coastguard Worker #include <type_traits>
70*9356374aSAndroid Build Coastguard Worker #include <utility>
71*9356374aSAndroid Build Coastguard Worker 
72*9356374aSAndroid Build Coastguard Worker #include "gmock/gmock.h"  // gmock_for_status_matchers.h
73*9356374aSAndroid Build Coastguard Worker #include "absl/base/config.h"
74*9356374aSAndroid Build Coastguard Worker #include "absl/status/internal/status_matchers.h"
75*9356374aSAndroid Build Coastguard Worker 
76*9356374aSAndroid Build Coastguard Worker namespace absl_testing {
77*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_BEGIN
78*9356374aSAndroid Build Coastguard Worker 
79*9356374aSAndroid Build Coastguard Worker // Returns a gMock matcher that matches a StatusOr<> whose status is
80*9356374aSAndroid Build Coastguard Worker // OK and whose value matches the inner matcher.
81*9356374aSAndroid Build Coastguard Worker template <typename InnerMatcherT>
82*9356374aSAndroid Build Coastguard Worker status_internal::IsOkAndHoldsMatcher<typename std::decay<InnerMatcherT>::type>
IsOkAndHolds(InnerMatcherT && inner_matcher)83*9356374aSAndroid Build Coastguard Worker IsOkAndHolds(InnerMatcherT&& inner_matcher) {
84*9356374aSAndroid Build Coastguard Worker   return status_internal::IsOkAndHoldsMatcher<
85*9356374aSAndroid Build Coastguard Worker       typename std::decay<InnerMatcherT>::type>(
86*9356374aSAndroid Build Coastguard Worker       std::forward<InnerMatcherT>(inner_matcher));
87*9356374aSAndroid Build Coastguard Worker }
88*9356374aSAndroid Build Coastguard Worker 
89*9356374aSAndroid Build Coastguard Worker // Returns a gMock matcher that matches a Status or StatusOr<> whose status code
90*9356374aSAndroid Build Coastguard Worker // matches code_matcher and whose error message matches message_matcher.
91*9356374aSAndroid Build Coastguard Worker // Typically, code_matcher will be an absl::StatusCode, e.g.
92*9356374aSAndroid Build Coastguard Worker //
93*9356374aSAndroid Build Coastguard Worker // StatusIs(absl::StatusCode::kInvalidArgument, "...")
94*9356374aSAndroid Build Coastguard Worker template <typename StatusCodeMatcherT, typename StatusMessageMatcherT>
StatusIs(StatusCodeMatcherT && code_matcher,StatusMessageMatcherT && message_matcher)95*9356374aSAndroid Build Coastguard Worker status_internal::StatusIsMatcher StatusIs(
96*9356374aSAndroid Build Coastguard Worker     StatusCodeMatcherT&& code_matcher,
97*9356374aSAndroid Build Coastguard Worker     StatusMessageMatcherT&& message_matcher) {
98*9356374aSAndroid Build Coastguard Worker   return status_internal::StatusIsMatcher(
99*9356374aSAndroid Build Coastguard Worker       std::forward<StatusCodeMatcherT>(code_matcher),
100*9356374aSAndroid Build Coastguard Worker       std::forward<StatusMessageMatcherT>(message_matcher));
101*9356374aSAndroid Build Coastguard Worker }
102*9356374aSAndroid Build Coastguard Worker 
103*9356374aSAndroid Build Coastguard Worker // Returns a gMock matcher that matches a Status or StatusOr<> and whose status
104*9356374aSAndroid Build Coastguard Worker // code matches code_matcher.  See above for details.
105*9356374aSAndroid Build Coastguard Worker template <typename StatusCodeMatcherT>
StatusIs(StatusCodeMatcherT && code_matcher)106*9356374aSAndroid Build Coastguard Worker status_internal::StatusIsMatcher StatusIs(StatusCodeMatcherT&& code_matcher) {
107*9356374aSAndroid Build Coastguard Worker   return StatusIs(std::forward<StatusCodeMatcherT>(code_matcher), ::testing::_);
108*9356374aSAndroid Build Coastguard Worker }
109*9356374aSAndroid Build Coastguard Worker 
110*9356374aSAndroid Build Coastguard Worker // Returns a gMock matcher that matches a Status or StatusOr<> which is OK.
IsOk()111*9356374aSAndroid Build Coastguard Worker inline status_internal::IsOkMatcher IsOk() {
112*9356374aSAndroid Build Coastguard Worker   return status_internal::IsOkMatcher();
113*9356374aSAndroid Build Coastguard Worker }
114*9356374aSAndroid Build Coastguard Worker 
115*9356374aSAndroid Build Coastguard Worker ABSL_NAMESPACE_END
116*9356374aSAndroid Build Coastguard Worker }  // namespace absl_testing
117*9356374aSAndroid Build Coastguard Worker 
118*9356374aSAndroid Build Coastguard Worker #endif  // ABSL_STATUS_STATUS_MATCHERS_H_
119