rtc_error_matchers.h (2910B)
1 /* 2 * Copyright 2024 The WebRTC project authors. All Rights Reserved. 3 * 4 * Use of this source code is governed by a BSD-style license 5 * that can be found in the LICENSE file in the root of the source 6 * tree. An additional intellectual property rights grant can be found 7 * in the file PATENTS. All contributing project authors may 8 * be found in the AUTHORS file in the root of the source tree. 9 */ 10 11 #ifndef API_TEST_RTC_ERROR_MATCHERS_H_ 12 #define API_TEST_RTC_ERROR_MATCHERS_H_ 13 14 #include <string> 15 16 #include "absl/strings/str_cat.h" 17 #include "api/rtc_error.h" 18 #include "test/gmock.h" 19 20 namespace webrtc { 21 22 MATCHER(IsRtcOk, "") { 23 if (!arg.ok()) { 24 *result_listener << "Expected OK, got " << absl::StrCat(arg); 25 return false; 26 } 27 return true; 28 } 29 30 MATCHER_P(IsRtcOkAndHolds, 31 matcher, 32 "RtcErrorOr that is holding an OK status and ") { 33 if (!arg.ok()) { 34 *result_listener << "Expected OK, got " << absl::StrCat(arg); 35 return false; 36 } 37 return testing::ExplainMatchResult(matcher, arg.value(), result_listener); 38 } 39 40 MATCHER_P(IsRtcErrorWithType, error_type, ToString(error_type)) { 41 if (arg.ok()) { 42 *result_listener << "Expected " << ToString(error_type) << ", got OK."; 43 return false; 44 } 45 if (arg.type() != error_type) { 46 *result_listener << "Expected " << ToString(error_type) << ", got " 47 << ToString(arg.type()); 48 return false; 49 } 50 return true; 51 } 52 53 MATCHER_P2(IsRtcErrorWithTypeAndMessage, 54 error_type, 55 message, 56 ToString(error_type)) { 57 if (arg.ok()) { 58 *result_listener << "Expected " << ToString(error_type) << ", got OK."; 59 return false; 60 } 61 if (arg.type() != error_type) { 62 *result_listener << "Expected " << ToString(error_type) << ", got " 63 << ToString(arg.type()); 64 return false; 65 } 66 if (std::string(arg.message()) != message) { 67 *result_listener << "Expected message \"" << message << "\", got \"" 68 << arg.message() << "\""; 69 return false; 70 } 71 return true; 72 } 73 74 MATCHER_P2(IsRtcErrorOrWithMessage, 75 error_matcher, 76 message_matcher, 77 "RtcErrorOr that is holding an error that " + 78 testing::DescribeMatcher<RTCError>(error_matcher, negation) + 79 (negation ? " or " : " and ") + " with a message that " + 80 testing::DescribeMatcher<std::string>(message_matcher, 81 negation)) { 82 if (arg.ok()) { 83 *result_listener << "Expected error, got " << absl::StrCat(arg); 84 return false; 85 } 86 return testing::ExplainMatchResult(error_matcher, arg.error(), 87 result_listener) && 88 testing::ExplainMatchResult(message_matcher, arg.error().message(), 89 result_listener); 90 } 91 92 } // namespace webrtc 93 94 #endif // API_TEST_RTC_ERROR_MATCHERS_H_