status_matchers.cc (2388B)
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 32 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 39 void StatusIsMatcherCommonImpl::DescribeNegationTo(std::ostream* os) const { 40 *os << "either 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 46 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 ? status.ok() ? "which is OK" 53 : "whose status code is wrong" 54 : "which has a status code " + 55 inner_listener.str()); 56 return false; 57 } 58 59 if (!message_matcher_.Matches(std::string(status.message()))) { 60 *result_listener << "whose error message is wrong"; 61 return false; 62 } 63 64 return true; 65 } 66 67 } // namespace status_internal 68 ABSL_NAMESPACE_END 69 } // namespace absl_testing