From 283a84d92a504bebce3b9181895472fec2e484ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Bostr=C3=B6m?= Date: Tue, 21 Jan 2025 10:39:45 +0100 Subject: [PATCH] Add matchers for RTCError, rename old matcher for RTCErrorOr. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Needed for testing in a follow-up CL. Using ToString rather than absl::StrCat because I want the name of the enum (e.g. "INVALID_MODIFICATION") as opposed to the enum value (int). Bug: none Change-Id: I45a925fad65395d1e6a886a9f787c2f360fb8604 Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/374343 Reviewed-by: Evan Shrubsole Reviewed-by: Mirko Bonadei Commit-Queue: Henrik Boström Cr-Commit-Position: refs/heads/main@{#43777} --- api/test/rtc_error_matchers.h | 36 ++++++++++++++++++++++++++++++++++- test/wait_until_unittest.cc | 4 ++-- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/api/test/rtc_error_matchers.h b/api/test/rtc_error_matchers.h index cf923b28fa..2abc569f8a 100644 --- a/api/test/rtc_error_matchers.h +++ b/api/test/rtc_error_matchers.h @@ -37,7 +37,41 @@ MATCHER_P(IsRtcOkAndHolds, return testing::ExplainMatchResult(matcher, arg.value(), result_listener); } -MATCHER_P2(IsRtcErrorWithMessage, +MATCHER_P(IsRtcErrorWithType, error_type, ToString(error_type)) { + if (arg.ok()) { + *result_listener << "Expected " << ToString(error_type) << ", got OK."; + return false; + } + if (arg.type() != error_type) { + *result_listener << "Expected " << ToString(error_type) << ", got " + << ToString(arg.type()); + return false; + } + return true; +} + +MATCHER_P2(IsRtcErrorWithTypeAndMessage, + error_type, + message, + ToString(error_type)) { + if (arg.ok()) { + *result_listener << "Expected " << ToString(error_type) << ", got OK."; + return false; + } + if (arg.type() != error_type) { + *result_listener << "Expected " << ToString(error_type) << ", got " + << ToString(arg.type()); + return false; + } + if (std::string(arg.message()) != message) { + *result_listener << "Expected message \"" << message << "\", got \"" + << arg.message() << "\""; + return false; + } + return true; +} + +MATCHER_P2(IsRtcErrorOrWithMessage, error_matcher, message_matcher, "RtcErrorOr that is holding an error that " + diff --git a/test/wait_until_unittest.cc b/test/wait_until_unittest.cc index 5f4ecf1696..fffce708c1 100644 --- a/test/wait_until_unittest.cc +++ b/test/wait_until_unittest.cc @@ -54,7 +54,7 @@ TEST(WaitUntilTest, ReturnsErrorWhenTimeoutIsReached) { // flakiness. EXPECT_THAT( result, - IsRtcErrorWithMessage( + IsRtcErrorOrWithMessage( _, MatchesRegex( "Value of: counter\nExpected: is equal to 1\nActual: -\\d+"))); } @@ -71,7 +71,7 @@ TEST(WaitUntilTest, ErrorContainsMatcherExplanation) { // flakiness. EXPECT_THAT( result, - IsRtcErrorWithMessage( + IsRtcErrorOrWithMessage( _, MatchesRegex("Value of: counter\nExpected: \\(is > 0\\) and " "\\(is < 10\\)\nActual: -\\d+, which doesn't match " "\\(is > 0\\)")));