#ifndef NET_TEST_GTEST_UTIL_H_
#define NET_TEST_GTEST_UTIL_H_
#include <string>
#include <string_view>
#include "base/test/mock_log.h"
#include "net/base/net_errors.h"
#include "net/test/scoped_disable_exit_on_dfatal.h"
#include "testing/gmock/include/gmock/gmock-matchers.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net::test {
MATCHER_P(IsError,
expected,
std::string(negation ? "not " : "") + net::ErrorToString(expected)) {
if (arg <= 0)
*result_listener << net::ErrorToString(arg);
return arg == expected;
}
MATCHER(IsOk,
std::string(negation ? "not " : "") + net::ErrorToString(net::OK)) {
if (arg <= 0)
*result_listener << net::ErrorToString(arg);
return arg == net::OK;
}
class StringPieceHasSubstrMatcher {
public:
explicit StringPieceHasSubstrMatcher(const std::string& substring)
: substring_(substring) {}
StringPieceHasSubstrMatcher(const StringPieceHasSubstrMatcher&) = default;
StringPieceHasSubstrMatcher& operator=(const StringPieceHasSubstrMatcher&) =
default;
bool MatchAndExplain(std::string_view s,
::testing::MatchResultListener* listener) const {
return s.find(substring_) != std::string::npos;
}
void DescribeTo(std::ostream* os) const {
*os << "has substring " << substring_;
}
void DescribeNegationTo(std::ostream* os) const {
*os << "has no substring " << substring_;
}
private:
std::string substring_;
};
#define GTEST_DFATAL_(statement, matcher, fail) \
do { \
::base::test::MockLog gtest_log; \
::net::test::ScopedDisableExitOnDFatal gtest_disable_exit; \
using ::testing::_; \
EXPECT_CALL(gtest_log, Log(_, _, _, _, _)) \
.WillRepeatedly(::testing::Return(false)); \
EXPECT_CALL(gtest_log, Log(::logging::LOGGING_DFATAL, _, _, _, matcher)) \
.Times(::testing::AtLeast(1)) \
.WillOnce(::testing::Return(false)); \
gtest_log.StartCapturingLogs(); \
{ statement; } \
gtest_log.StopCapturingLogs(); \
if (!testing::Mock::VerifyAndClear(>est_log)) \
fail(""); \
} while (false)
#define EXPECT_DFATAL_WITH(statement, matcher) \
GTEST_DFATAL_(statement, matcher, GTEST_NONFATAL_FAILURE_)
#define ASSERT_DFATAL_WITH(statement, matcher) \
GTEST_DFATAL_(statement, matcher, GTEST_FATAL_FAILURE_)
#define EXPECT_DFATAL(statement, regex) \
EXPECT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex))
#define ASSERT_DFATAL(statement, regex) \
ASSERT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex))
}
#endif