Remove custom AssertStartsWith and AssertStringContains matchers

in favor of stock StartsWith and HasSubstr matchers provided by gmock.

BUG=None

Change-Id: Ib7e9a0ac73d506c349b8ec102dd4236767077d61
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/334460
Reviewed-by: Mirko Bonadei <mbonadei@webrtc.org>
Commit-Queue: Philipp Hancke <phancke@microsoft.com>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#41531}
This commit is contained in:
Philipp Hancke 2024-01-15 10:07:58 +01:00 committed by WebRTC LUCI CQ
parent 3b7afb284f
commit 5aaa9ed41e
7 changed files with 33 additions and 92 deletions

View file

@ -25,11 +25,9 @@
#include "rtc_base/gunit.h"
#include "rtc_base/synchronization/mutex.h"
#include "rtc_base/time_utils.h"
#include "system_wrappers/include/sleep.h"
#include "test/frame_utils.h"
#include "test/gtest.h"
using webrtc::SleepMs;
using webrtc::VideoCaptureCapability;
using webrtc::VideoCaptureFactory;
using webrtc::VideoCaptureModule;

View file

@ -55,6 +55,7 @@
#include "rtc_base/rtc_certificate_generator.h"
#include "rtc_base/ssl_fingerprint.h"
#include "rtc_base/thread.h"
#include "test/gmock.h"
#include "test/gtest.h"
#include "test/scoped_key_value_config.h"
#ifdef WEBRTC_ANDROID
@ -70,6 +71,7 @@ namespace webrtc {
using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
using ::testing::Combine;
using ::testing::HasSubstr;
using ::testing::Values;
constexpr int kGenerateCertTimeout = 1000;
@ -789,16 +791,13 @@ TEST_P(PeerConnectionCryptoTest, SessionErrorIfFingerprintInvalid) {
// Set the invalid answer and expect a fingerprint error.
std::string error;
ASSERT_FALSE(callee->SetLocalDescription(std::move(invalid_answer), &error));
EXPECT_PRED_FORMAT2(AssertStringContains, error,
"Local fingerprint does not match identity.");
EXPECT_THAT(error, HasSubstr("Local fingerprint does not match identity."));
// Make sure that setting a valid remote offer or local answer also fails now.
ASSERT_FALSE(callee->SetRemoteDescription(caller->CreateOffer(), &error));
EXPECT_PRED_FORMAT2(AssertStringContains, error,
"Session error code: ERROR_CONTENT.");
EXPECT_THAT(error, HasSubstr("Session error code: ERROR_CONTENT."));
ASSERT_FALSE(callee->SetLocalDescription(std::move(valid_answer), &error));
EXPECT_PRED_FORMAT2(AssertStringContains, error,
"Session error code: ERROR_CONTENT.");
EXPECT_THAT(error, HasSubstr("Session error code: ERROR_CONTENT."));
}
INSTANTIATE_TEST_SUITE_P(PeerConnectionCryptoTest,

View file

@ -66,7 +66,6 @@
#ifdef WEBRTC_ANDROID
#include "pc/test/android_test_initializer.h"
#endif
#include "rtc_base/gunit.h"
#include "rtc_base/virtual_socket_server.h"
#include "test/gmock.h"
@ -78,6 +77,7 @@ using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
using ::testing::Bool;
using ::testing::Combine;
using ::testing::ElementsAre;
using ::testing::HasSubstr;
using ::testing::NotNull;
using ::testing::Values;
@ -286,8 +286,8 @@ TEST_P(PeerConnectionMediaTest,
std::string error;
ASSERT_FALSE(callee->SetRemoteDescription(caller->CreateOffer(), &error));
EXPECT_PRED_FORMAT2(AssertStartsWith, error,
"Failed to set remote offer sdp: Failed to create");
EXPECT_THAT(error,
HasSubstr("Failed to set remote offer sdp: Failed to create"));
}
TEST_P(PeerConnectionMediaTest,
@ -297,8 +297,8 @@ TEST_P(PeerConnectionMediaTest,
std::string error;
ASSERT_FALSE(caller->SetLocalDescription(caller->CreateOffer(), &error));
EXPECT_PRED_FORMAT2(AssertStartsWith, error,
"Failed to set local offer sdp: Failed to create");
EXPECT_THAT(error,
HasSubstr("Failed to set local offer sdp: Failed to create"));
}
std::vector<std::string> GetIds(

View file

@ -64,6 +64,7 @@
#include "rtc_base/rtc_certificate_generator.h"
#include "rtc_base/string_encode.h"
#include "rtc_base/thread.h"
#include "test/gmock.h"
#include "test/gtest.h"
#ifdef WEBRTC_ANDROID
#include "pc/test/android_test_initializer.h"
@ -80,6 +81,7 @@ using RTCConfiguration = PeerConnectionInterface::RTCConfiguration;
using RTCOfferAnswerOptions = PeerConnectionInterface::RTCOfferAnswerOptions;
using ::testing::Bool;
using ::testing::Combine;
using ::testing::StartsWith;
using ::testing::Values;
namespace {
@ -343,8 +345,7 @@ TEST_P(PeerConnectionSignalingStateTest, CreateOffer) {
} else {
std::string error;
ASSERT_FALSE(wrapper->CreateOffer(RTCOfferAnswerOptions(), &error));
EXPECT_PRED_FORMAT2(AssertStartsWith, error,
"CreateOffer called when PeerConnection is closed.");
EXPECT_EQ(error, "CreateOffer called when PeerConnection is closed.");
}
}
@ -379,9 +380,9 @@ TEST_P(PeerConnectionSignalingStateTest, SetLocalOffer) {
std::string error;
ASSERT_FALSE(wrapper->SetLocalDescription(std::move(offer), &error));
EXPECT_PRED_FORMAT2(
AssertStartsWith, error,
"Failed to set local offer sdp: Called in wrong state:");
EXPECT_THAT(
error,
StartsWith("Failed to set local offer sdp: Called in wrong state:"));
}
}
@ -398,9 +399,9 @@ TEST_P(PeerConnectionSignalingStateTest, SetLocalPrAnswer) {
} else {
std::string error;
ASSERT_FALSE(wrapper->SetLocalDescription(std::move(pranswer), &error));
EXPECT_PRED_FORMAT2(
AssertStartsWith, error,
"Failed to set local pranswer sdp: Called in wrong state:");
EXPECT_THAT(
error,
StartsWith("Failed to set local pranswer sdp: Called in wrong state:"));
}
}
@ -416,9 +417,9 @@ TEST_P(PeerConnectionSignalingStateTest, SetLocalAnswer) {
} else {
std::string error;
ASSERT_FALSE(wrapper->SetLocalDescription(std::move(answer), &error));
EXPECT_PRED_FORMAT2(
AssertStartsWith, error,
"Failed to set local answer sdp: Called in wrong state:");
EXPECT_THAT(
error,
StartsWith("Failed to set local answer sdp: Called in wrong state:"));
}
}
@ -435,9 +436,9 @@ TEST_P(PeerConnectionSignalingStateTest, SetRemoteOffer) {
} else {
std::string error;
ASSERT_FALSE(wrapper->SetRemoteDescription(std::move(offer), &error));
EXPECT_PRED_FORMAT2(
AssertStartsWith, error,
"Failed to set remote offer sdp: Called in wrong state:");
EXPECT_THAT(
error,
StartsWith("Failed to set remote offer sdp: Called in wrong state:"));
}
}
@ -454,9 +455,10 @@ TEST_P(PeerConnectionSignalingStateTest, SetRemotePrAnswer) {
} else {
std::string error;
ASSERT_FALSE(wrapper->SetRemoteDescription(std::move(pranswer), &error));
EXPECT_PRED_FORMAT2(
AssertStartsWith, error,
"Failed to set remote pranswer sdp: Called in wrong state:");
EXPECT_THAT(
error,
StartsWith(
"Failed to set remote pranswer sdp: Called in wrong state:"));
}
}
@ -472,9 +474,9 @@ TEST_P(PeerConnectionSignalingStateTest, SetRemoteAnswer) {
} else {
std::string error;
ASSERT_FALSE(wrapper->SetRemoteDescription(std::move(answer), &error));
EXPECT_PRED_FORMAT2(
AssertStartsWith, error,
"Failed to set remote answer sdp: Called in wrong state:");
EXPECT_THAT(
error,
StartsWith("Failed to set remote answer sdp: Called in wrong state:"));
}
}

View file

@ -1632,10 +1632,7 @@ rtc_source_set("gtest_prod") {
rtc_library("gunit_helpers") {
testonly = true
sources = [
"gunit.cc",
"gunit.h",
]
sources = [ "gunit.h" ]
deps = [
":logging",
":rtc_base_tests_utils",

View file

@ -1,43 +0,0 @@
/*
* Copyright 2018 The WebRTC Project Authors. All rights reserved.
*
* Use of this source code is governed by a BSD-style license
* that can be found in the LICENSE file in the root of the source
* tree. An additional intellectual property rights grant can be found
* in the file PATENTS. All contributing project authors may
* be found in the AUTHORS file in the root of the source tree.
*/
#include "rtc_base/gunit.h"
#include <string>
#include "absl/strings/match.h"
#include "absl/strings/string_view.h"
::testing::AssertionResult AssertStartsWith(const char* text_expr,
const char* prefix_expr,
absl::string_view text,
absl::string_view prefix) {
if (absl::StartsWith(text, prefix)) {
return ::testing::AssertionSuccess();
} else {
return ::testing::AssertionFailure()
<< text_expr << "\nwhich is\n\"" << text
<< "\"\ndoes not start with\n"
<< prefix_expr << "\nwhich is\n\"" << prefix << "\"";
}
}
::testing::AssertionResult AssertStringContains(const char* str_expr,
const char* substr_expr,
absl::string_view str,
absl::string_view substr) {
if (str.find(substr) != absl::string_view::npos) {
return ::testing::AssertionSuccess();
} else {
return ::testing::AssertionFailure()
<< str_expr << "\nwhich is\n\"" << str << "\"\ndoes not contain\n"
<< substr_expr << "\nwhich is\n\"" << substr << "\"";
}
}

View file

@ -154,16 +154,4 @@
} else \
GTEST_CONCAT_TOKEN_(gunit_label_, __LINE__) : ASSERT_EQ(v1, v2)
// Usage: EXPECT_PRED_FORMAT2(AssertStartsWith, text, "prefix");
testing::AssertionResult AssertStartsWith(const char* text_expr,
const char* prefix_expr,
absl::string_view text,
absl::string_view prefix);
// Usage: EXPECT_PRED_FORMAT2(AssertStringContains, str, "substring");
testing::AssertionResult AssertStringContains(const char* str_expr,
const char* substr_expr,
absl::string_view str,
absl::string_view substr);
#endif // RTC_BASE_GUNIT_H_