TestUtils.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. /*
  2. * Copyright 2015-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #pragma once
  17. /*
  18. * This file contains additional gtest-style check macros to use in unit tests.
  19. *
  20. * - SKIP(), SKIP_IF(EXPR)
  21. * - EXPECT_THROW_RE(), ASSERT_THROW_RE()
  22. * - EXPECT_THROW_ERRNO(), ASSERT_THROW_ERRNO()
  23. * - AreWithinSecs()
  24. *
  25. * Additionally, it includes a PrintTo() function for StringPiece.
  26. * Including this file in your tests will ensure that StringPiece is printed
  27. * nicely when used in EXPECT_EQ() or EXPECT_NE() checks.
  28. */
  29. #include <chrono>
  30. #include <regex>
  31. #include <system_error>
  32. #include <type_traits>
  33. #include <folly/Conv.h>
  34. #include <folly/ExceptionString.h>
  35. #include <folly/Range.h>
  36. #include <folly/portability/GTest.h>
  37. // SKIP() is used to mark a test skipped if we could not successfully execute
  38. // the test due to runtime issues or behavior that do not necessarily indicate
  39. // a problem with the code.
  40. //
  41. // googletest does not have a built-in mechanism to report tests as skipped a
  42. // run time. We either report the test as successful or failure based on the
  43. // FOLLY_SKIP_AS_FAILURE configuration setting. The default is to report the
  44. // test as successful. Enabling FOLLY_SKIP_AS_FAILURE can be useful with a
  45. // test harness that can identify the "Test skipped by client" in the failure
  46. // message and convert this into a skipped test result.
  47. #if FOLLY_SKIP_AS_FAILURE
  48. #define SKIP() GTEST_FATAL_FAILURE_("Test skipped by client")
  49. #else
  50. #define SKIP() return GTEST_SUCCESS_("Test skipped by client")
  51. #endif
  52. // Encapsulate conditional-skip, since it's nontrivial to get right.
  53. #define SKIP_IF(expr) \
  54. GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  55. if (!(expr)) { \
  56. } else \
  57. SKIP()
  58. #define TEST_THROW_ERRNO_(statement, errnoValue, fail) \
  59. GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  60. if (::folly::test::detail::CheckResult gtest_result = \
  61. ::folly::test::detail::checkThrowErrno( \
  62. [&] { statement; }, errnoValue, #statement)) { \
  63. } else \
  64. fail(gtest_result.what())
  65. /**
  66. * Check that a statement throws a std::system_error with the expected errno
  67. * value. This is useful for checking code that uses the functions in
  68. * folly/Exception.h to throw exceptions.
  69. *
  70. * Like other EXPECT_* and ASSERT_* macros, additional message information
  71. * can be included using the << stream operator.
  72. *
  73. * Example usage:
  74. *
  75. * EXPECT_THROW_ERRNO(readFile("notpresent.txt"), ENOENT)
  76. * << "notpresent.txt should not exist";
  77. */
  78. #define EXPECT_THROW_ERRNO(statement, errnoValue) \
  79. TEST_THROW_ERRNO_(statement, errnoValue, GTEST_NONFATAL_FAILURE_)
  80. #define ASSERT_THROW_ERRNO(statement, errnoValue) \
  81. TEST_THROW_ERRNO_(statement, errnoValue, GTEST_FATAL_FAILURE_)
  82. #define TEST_THROW_RE_(statement, exceptionType, pattern, fail) \
  83. GTEST_AMBIGUOUS_ELSE_BLOCKER_ \
  84. if (::folly::test::detail::CheckResult gtest_result = \
  85. ::folly::test::detail::checkThrowRegex<exceptionType>( \
  86. [&] { statement; }, pattern, #statement, #exceptionType)) { \
  87. } else \
  88. fail(gtest_result.what())
  89. /**
  90. * Check that a statement throws the expected exception type, and that the
  91. * exception message matches the specified regular expression.
  92. *
  93. * Partial matches (against just a portion of the error message) are accepted
  94. * if the regular expression does not explicitly start with "^" and end with
  95. * "$". (The matching is performed using std::regex_search() rather than
  96. * std::regex_match().)
  97. *
  98. * This uses ECMA-262 style regular expressions (the default behavior of
  99. * std::regex).
  100. *
  101. * Like other EXPECT_* and ASSERT_* macros, additional message information
  102. * can be included using the << stream operator.
  103. *
  104. * Example usage:
  105. *
  106. * EXPECT_THROW_RE(badFunction(), std::runtime_error, "oh noes")
  107. * << "function did not throw the expected exception";
  108. */
  109. #define EXPECT_THROW_RE(statement, exceptionType, pattern) \
  110. TEST_THROW_RE_(statement, exceptionType, pattern, GTEST_NONFATAL_FAILURE_)
  111. #define ASSERT_THROW_RE(statement, exceptionType, pattern) \
  112. TEST_THROW_RE_(statement, exceptionType, pattern, GTEST_FATAL_FAILURE_)
  113. namespace folly {
  114. namespace test {
  115. template <typename T1, typename T2>
  116. ::testing::AssertionResult
  117. AreWithinSecs(T1 val1, T2 val2, std::chrono::seconds acceptableDeltaSecs) {
  118. auto deltaSecs =
  119. std::chrono::duration_cast<std::chrono::seconds>(val1 - val2);
  120. if (deltaSecs <= acceptableDeltaSecs &&
  121. deltaSecs >= -1 * acceptableDeltaSecs) {
  122. return ::testing::AssertionSuccess();
  123. } else {
  124. return ::testing::AssertionFailure()
  125. << val1.count() << " and " << val2.count() << " are not within "
  126. << acceptableDeltaSecs.count() << " secs of each other";
  127. }
  128. }
  129. namespace detail {
  130. /**
  131. * Helper class for implementing test macros
  132. */
  133. class CheckResult {
  134. public:
  135. explicit CheckResult(bool s) noexcept : success_(s) {}
  136. explicit operator bool() const noexcept {
  137. return success_;
  138. }
  139. const char* what() const noexcept {
  140. return message_.c_str();
  141. }
  142. /**
  143. * Support the << operator for building up the error message.
  144. *
  145. * The arguments are treated as with folly::to<string>(), and we do not
  146. * support iomanip parameters. The main reason we use the << operator
  147. * as opposed to a variadic function like folly::to is that clang-format
  148. * formats long statements using << much nicer than function call arguments.
  149. */
  150. template <typename T>
  151. CheckResult& operator<<(T&& t) {
  152. toAppend(std::forward<T>(t), &message_);
  153. return *this;
  154. }
  155. private:
  156. bool success_;
  157. std::string message_;
  158. };
  159. /**
  160. * Helper function for implementing EXPECT_THROW
  161. */
  162. template <typename Fn>
  163. CheckResult checkThrowErrno(Fn&& fn, int errnoValue, const char* statementStr) {
  164. try {
  165. fn();
  166. } catch (const std::system_error& ex) {
  167. // TODO: POSIX errno values should use std::generic_category(), but
  168. // folly/Exception.h incorrectly throws them using std::system_category()
  169. // at the moment.
  170. // For now we also accept std::system_category so that we will also handle
  171. // exceptions from folly/Exception.h correctly.
  172. if (ex.code().category() != std::generic_category() &&
  173. ex.code().category() != std::system_category()) {
  174. return CheckResult(false)
  175. << "Expected: " << statementStr << " throws an exception with errno "
  176. << errnoValue << " (" << std::generic_category().message(errnoValue)
  177. << ")\nActual: it throws a system_error with category "
  178. << ex.code().category().name() << ": " << ex.what();
  179. }
  180. if (ex.code().value() != errnoValue) {
  181. return CheckResult(false)
  182. << "Expected: " << statementStr << " throws an exception with errno "
  183. << errnoValue << " (" << std::generic_category().message(errnoValue)
  184. << ")\nActual: it throws errno " << ex.code().value() << ": "
  185. << ex.what();
  186. }
  187. return CheckResult(true);
  188. } catch (const std::exception& ex) {
  189. return CheckResult(false)
  190. << "Expected: " << statementStr << " throws an exception with errno "
  191. << errnoValue << " (" << std::generic_category().message(errnoValue)
  192. << ")\nActual: it throws a different exception: " << exceptionStr(ex);
  193. } catch (...) {
  194. return CheckResult(false)
  195. << "Expected: " << statementStr << " throws an exception with errno "
  196. << errnoValue << " (" << std::generic_category().message(errnoValue)
  197. << ")\nActual: it throws a non-exception type";
  198. }
  199. return CheckResult(false)
  200. << "Expected: " << statementStr << " throws an exception with errno "
  201. << errnoValue << " (" << std::generic_category().message(errnoValue)
  202. << ")\nActual: it throws nothing";
  203. }
  204. /**
  205. * Helper function for implementing EXPECT_THROW_RE
  206. */
  207. template <typename ExType, typename Fn>
  208. CheckResult checkThrowRegex(
  209. Fn&& fn,
  210. const char* pattern,
  211. const char* statementStr,
  212. const char* excTypeStr) {
  213. static_assert(
  214. std::is_base_of<std::exception, ExType>::value,
  215. "EXPECT_THROW_RE() exception type must derive from std::exception");
  216. try {
  217. fn();
  218. } catch (const std::exception& ex) {
  219. const auto* derived = dynamic_cast<const ExType*>(&ex);
  220. if (!derived) {
  221. return CheckResult(false)
  222. << "Expected: " << statementStr << "throws a " << excTypeStr
  223. << ")\nActual: it throws a different exception type: "
  224. << exceptionStr(ex);
  225. }
  226. std::regex re(pattern);
  227. if (!std::regex_search(derived->what(), re)) {
  228. return CheckResult(false)
  229. << "Expected: " << statementStr << " throws a " << excTypeStr
  230. << " with message matching \"" << pattern
  231. << "\"\nActual: message is: " << derived->what();
  232. }
  233. return CheckResult(true);
  234. } catch (...) {
  235. return CheckResult(false)
  236. << "Expected: " << statementStr << " throws a " << excTypeStr
  237. << ")\nActual: it throws a non-exception type";
  238. }
  239. return CheckResult(false) << "Expected: " << statementStr << " throws a "
  240. << excTypeStr << ")\nActual: it throws nothing";
  241. }
  242. } // namespace detail
  243. } // namespace test
  244. // Define a PrintTo() function for StringPiece, so that gtest checks
  245. // will print it as a string. Without this gtest identifies StringPiece as a
  246. // container type, and therefore tries printing its elements individually,
  247. // despite the fact that there is an ostream operator<<() defined for
  248. // StringPiece.
  249. inline void PrintTo(StringPiece sp, ::std::ostream* os) {
  250. // gtest's PrintToString() function will quote the string and escape internal
  251. // quotes and non-printable characters, the same way gtest does for the
  252. // standard string types.
  253. *os << ::testing::PrintToString(sp.str());
  254. }
  255. } // namespace folly