1 // Copyright 2024 The Abseil Authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // https://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // -----------------------------------------------------------------------------
16 // File: status_matchers.cc
17 // -----------------------------------------------------------------------------
18
19 #include "absl/status/internal/status_matchers.h"
20
21 #include <ostream>
22 #include <string>
23
24 #include "gmock/gmock.h" // gmock_for_status_matchers.h
25 #include "absl/base/config.h"
26 #include "absl/status/status.h"
27
28 namespace absl_testing {
29 ABSL_NAMESPACE_BEGIN
30 namespace status_internal {
31
DescribeTo(std::ostream * os) const32 void StatusIsMatcherCommonImpl::DescribeTo(std::ostream* os) const {
33 *os << ", has a status code that ";
34 code_matcher_.DescribeTo(os);
35 *os << ", and has an error message that ";
36 message_matcher_.DescribeTo(os);
37 }
38
DescribeNegationTo(std::ostream * os) const39 void StatusIsMatcherCommonImpl::DescribeNegationTo(std::ostream* os) const {
40 *os << ", or has a status code that ";
41 code_matcher_.DescribeNegationTo(os);
42 *os << ", or has an error message that ";
43 message_matcher_.DescribeNegationTo(os);
44 }
45
MatchAndExplain(const::absl::Status & status,::testing::MatchResultListener * result_listener) const46 bool StatusIsMatcherCommonImpl::MatchAndExplain(
47 const ::absl::Status& status,
48 ::testing::MatchResultListener* result_listener) const {
49 ::testing::StringMatchResultListener inner_listener;
50 if (!code_matcher_.MatchAndExplain(status.code(), &inner_listener)) {
51 *result_listener << (inner_listener.str().empty()
52 ? "whose status code is wrong"
53 : "which has a status code " +
54 inner_listener.str());
55 return false;
56 }
57
58 if (!message_matcher_.Matches(std::string(status.message()))) {
59 *result_listener << "whose error message is wrong";
60 return false;
61 }
62
63 return true;
64 }
65
66 } // namespace status_internal
67 ABSL_NAMESPACE_END
68 } // namespace absl_testing
69