gtest_util.h 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. //
  5. // Testing utilities that extend gtest.
  6. #ifndef NET_TEST_GTEST_UTIL_H_
  7. #define NET_TEST_GTEST_UTIL_H_
  8. #include <string>
  9. #include "base/strings/string_piece.h"
  10. #include "base/test/mock_log.h"
  11. #include "net/base/net_errors.h"
  12. #include "net/test/scoped_disable_exit_on_dfatal.h"
  13. #include "testing/gmock/include/gmock/gmock-matchers.h"
  14. #include "testing/gmock/include/gmock/gmock.h"
  15. #include "testing/gtest/include/gtest/gtest.h"
  16. namespace net::test {
  17. // A GMock matcher that checks whether the argument is the expected net::Error.
  18. // On failure, the expected and actual net::Error names will be printed.
  19. // Usage: EXPECT_THAT(foo(), IsError(net::ERR_INVALID_ARGUMENT));
  20. MATCHER_P(IsError,
  21. expected,
  22. std::string(negation ? "not " : "") + net::ErrorToString(expected)) {
  23. if (arg <= 0)
  24. *result_listener << net::ErrorToString(arg);
  25. return arg == expected;
  26. }
  27. // Shorthand for IsError(net::OK).
  28. // Usage: EXPECT_THAT(foo(), IsOk());
  29. MATCHER(IsOk,
  30. std::string(negation ? "not " : "") + net::ErrorToString(net::OK)) {
  31. if (arg <= 0)
  32. *result_listener << net::ErrorToString(arg);
  33. return arg == net::OK;
  34. }
  35. // A gMock matcher for base::StringPiece arguments.
  36. // gMock's built-in HasSubstrMatcher does not work,
  37. // because base::StringPiece cannot be implicitly converted to std::string.
  38. class StringPieceHasSubstrMatcher {
  39. public:
  40. explicit StringPieceHasSubstrMatcher(const std::string& substring)
  41. : substring_(substring) {}
  42. StringPieceHasSubstrMatcher(const StringPieceHasSubstrMatcher&) = default;
  43. StringPieceHasSubstrMatcher& operator=(const StringPieceHasSubstrMatcher&) =
  44. default;
  45. bool MatchAndExplain(base::StringPiece s,
  46. ::testing::MatchResultListener* listener) const {
  47. return s.find(substring_) != std::string::npos;
  48. }
  49. // Describe what this matcher matches.
  50. void DescribeTo(std::ostream* os) const {
  51. *os << "has substring " << substring_;
  52. }
  53. void DescribeNegationTo(std::ostream* os) const {
  54. *os << "has no substring " << substring_;
  55. }
  56. private:
  57. std::string substring_;
  58. };
  59. // Internal implementation for the EXPECT_DFATAL and ASSERT_DFATAL
  60. // macros. Do not use this directly.
  61. #define GTEST_DFATAL_(statement, severity, matcher, fail) \
  62. do { \
  63. ::base::test::MockLog gtest_log; \
  64. ::net::test::ScopedDisableExitOnDFatal gtest_disable_exit; \
  65. using ::testing::_; \
  66. EXPECT_CALL(gtest_log, Log(_, _, _, _, _)) \
  67. .WillRepeatedly(::testing::Return(false)); \
  68. EXPECT_CALL(gtest_log, Log(::logging::LOG_##severity, _, _, _, matcher)) \
  69. .Times(::testing::AtLeast(1)) \
  70. .WillOnce(::testing::Return(false)); \
  71. gtest_log.StartCapturingLogs(); \
  72. { statement; } \
  73. gtest_log.StopCapturingLogs(); \
  74. if (!testing::Mock::VerifyAndClear(&gtest_log)) \
  75. fail(""); \
  76. } while (false)
  77. // The EXPECT_DFATAL and ASSERT_DFATAL macros are lightweight
  78. // alternatives to EXPECT_DEBUG_DEATH and ASSERT_DEBUG_DEATH. They
  79. // are appropriate for testing that your code logs a message at the
  80. // DFATAL level.
  81. //
  82. // Unlike EXPECT_DEBUG_DEATH and ASSERT_DEBUG_DEATH, these macros
  83. // execute the given statement in the current process, not a forked
  84. // one. This works because we disable exiting the program for
  85. // LOG(DFATAL). This makes the tests run more quickly.
  86. //
  87. // The _WITH() variants allow one to specify any matcher for the
  88. // DFATAL log message, whereas the other variants assume a regex.
  89. #define EXPECT_DFATAL_WITH(statement, matcher) \
  90. GTEST_DFATAL_(statement, DFATAL, matcher, GTEST_NONFATAL_FAILURE_)
  91. #define ASSERT_DFATAL_WITH(statement, matcher) \
  92. GTEST_DFATAL_(statement, DFATAL, matcher, GTEST_FATAL_FAILURE_)
  93. #define EXPECT_DFATAL(statement, regex) \
  94. EXPECT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex))
  95. #define ASSERT_DFATAL(statement, regex) \
  96. ASSERT_DFATAL_WITH(statement, ::testing::ContainsRegex(regex))
  97. // The EXPECT_DEBUG_DFATAL and ASSERT_DEBUG_DFATAL macros are similar to
  98. // EXPECT_DFATAL and ASSERT_DFATAL. Use them in conjunction with DLOG(DFATAL)
  99. // or similar macros that produce no-op in opt build and DFATAL in dbg build.
  100. #ifndef NDEBUG
  101. #define EXPECT_DEBUG_DFATAL(statement, regex) \
  102. EXPECT_DFATAL(statement, regex)
  103. #define ASSERT_DEBUG_DFATAL(statement, regex) \
  104. ASSERT_DFATAL(statement, regex)
  105. #else // NDEBUG
  106. #define EXPECT_DEBUG_DFATAL(statement, regex) \
  107. do { \
  108. (void)(regex); \
  109. statement; \
  110. } while (false)
  111. #define ASSERT_DEBUG_DFATAL(statement, regex) \
  112. do { \
  113. (void)(regex); \
  114. statement; \
  115. } while (false)
  116. #endif // NDEBUG
  117. // The EXPECT_DCHECK and ASSERT_DCHECK macros are similar to EXPECT_DFATAL and
  118. // ASSERT_DFATAL. Use them in conjunction with DCHECK that produces no-op in opt
  119. // build and LOG_DCHECK (FATAL) if DCHECK_IS_ON().
  120. #if DCHECK_IS_ON()
  121. #define EXPECT_DCHECK(statement, regex) \
  122. GTEST_DFATAL_(statement, DCHECK, ::testing::ContainsRegex(regex), \
  123. GTEST_NONFATAL_FAILURE_)
  124. #define ASSERT_DCHECK(statement, regex) \
  125. GTEST_DFATAL_(statement, DCHECK, ::testing::ContainsRegex(regex), \
  126. GTEST_FATAL_FAILURE_)
  127. #else // DCHECK_IS_ON()
  128. #define EXPECT_DCHECK(statement, regex) \
  129. do { \
  130. (void)(regex); \
  131. statement; \
  132. } while (false)
  133. #define ASSERT_DCHECK(statement, regex) \
  134. do { \
  135. (void)(regex); \
  136. statement; \
  137. } while (false)
  138. #endif // DCHECK_IS_ON()
  139. } // namespace net::test
  140. #endif // NET_TEST_GTEST_UTIL_H_