#ifndef NET_TEST_GTEST_UTIL_H_
#define NET_TEST_GTEST_UTIL_H_
#include <string>
#include "base/strings/string_piece.h"
#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(base::StringPiece 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, severity, 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::LOG_##severity, _, _, _, 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, DFATAL, matcher, GTEST_NONFATAL_FAILURE_)
#define ASSERT_DFATAL_WITH(statement, matcher) \
GTEST_DFATAL_(statement, DFATAL, 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))
#ifndef NDEBUG
#define EXPECT_DEBUG_DFATAL(statement, regex) \
EXPECT_DFATAL(statement, regex)
#define ASSERT_DEBUG_DFATAL(statement, regex) \
ASSERT_DFATAL(statement, regex)
#else
#define EXPECT_DEBUG_DFATAL(statement, regex) \
do { \
(void)(regex); \
statement; \
} while (false)
#define ASSERT_DEBUG_DFATAL(statement, regex) \
do { \
(void)(regex); \
statement; \
} while (false)
#endif
#if DCHECK_IS_ON()
#define EXPECT_DCHECK(statement, regex) \
GTEST_DFATAL_(statement, DCHECK, ::testing::ContainsRegex(regex), \
GTEST_NONFATAL_FAILURE_)
#define ASSERT_DCHECK(statement, regex) \
GTEST_DFATAL_(statement, DCHECK, ::testing::ContainsRegex(regex), \
GTEST_FATAL_FAILURE_)
#else
#define EXPECT_DCHECK(statement, regex) \
do { \
(void)(regex); \
statement; \
} while (false)
#define ASSERT_DCHECK(statement, regex) \
do { \
(void)(regex); \
statement; \
} while (false)
#endif
}
#endif