gtest-spi.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. // Copyright 2007, Google Inc.
  2. // All rights reserved.
  3. //
  4. // Redistribution and use in source and binary forms, with or without
  5. // modification, are permitted provided that the following conditions are
  6. // met:
  7. //
  8. // * Redistributions of source code must retain the above copyright
  9. // notice, this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above
  11. // copyright notice, this list of conditions and the following disclaimer
  12. // in the documentation and/or other materials provided with the
  13. // distribution.
  14. // * Neither the name of Google Inc. nor the names of its
  15. // contributors may be used to endorse or promote products derived from
  16. // this software without specific prior written permission.
  17. //
  18. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  22. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  23. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  24. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  25. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  26. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  28. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. // Utilities for testing Google Test itself and code that uses Google Test
  30. // (e.g. frameworks built on top of Google Test).
  31. #ifndef GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_
  32. #define GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_
  33. #include <string>
  34. #include "gtest/gtest.h"
  35. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
  36. /* class A needs to have dll-interface to be used by clients of class B */)
  37. namespace testing {
  38. // This helper class can be used to mock out Google Test failure reporting
  39. // so that we can test Google Test or code that builds on Google Test.
  40. //
  41. // An object of this class appends a TestPartResult object to the
  42. // TestPartResultArray object given in the constructor whenever a Google Test
  43. // failure is reported. It can either intercept only failures that are
  44. // generated in the same thread that created this object or it can intercept
  45. // all generated failures. The scope of this mock object can be controlled with
  46. // the second argument to the two arguments constructor.
  47. class GTEST_API_ ScopedFakeTestPartResultReporter
  48. : public TestPartResultReporterInterface {
  49. public:
  50. // The two possible mocking modes of this object.
  51. enum InterceptMode {
  52. INTERCEPT_ONLY_CURRENT_THREAD, // Intercepts only thread local failures.
  53. INTERCEPT_ALL_THREADS // Intercepts all failures.
  54. };
  55. // The c'tor sets this object as the test part result reporter used
  56. // by Google Test. The 'result' parameter specifies where to report the
  57. // results. This reporter will only catch failures generated in the current
  58. // thread. DEPRECATED
  59. explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result);
  60. // Same as above, but you can choose the interception scope of this object.
  61. ScopedFakeTestPartResultReporter(InterceptMode intercept_mode,
  62. TestPartResultArray* result);
  63. // The d'tor restores the previous test part result reporter.
  64. ~ScopedFakeTestPartResultReporter() override;
  65. // Appends the TestPartResult object to the TestPartResultArray
  66. // received in the constructor.
  67. //
  68. // This method is from the TestPartResultReporterInterface
  69. // interface.
  70. void ReportTestPartResult(const TestPartResult& result) override;
  71. private:
  72. void Init();
  73. const InterceptMode intercept_mode_;
  74. TestPartResultReporterInterface* old_reporter_;
  75. TestPartResultArray* const result_;
  76. ScopedFakeTestPartResultReporter(const ScopedFakeTestPartResultReporter&) =
  77. delete;
  78. ScopedFakeTestPartResultReporter& operator=(
  79. const ScopedFakeTestPartResultReporter&) = delete;
  80. };
  81. namespace internal {
  82. // A helper class for implementing EXPECT_FATAL_FAILURE() and
  83. // EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given
  84. // TestPartResultArray contains exactly one failure that has the given
  85. // type and contains the given substring. If that's not the case, a
  86. // non-fatal failure will be generated.
  87. class GTEST_API_ SingleFailureChecker {
  88. public:
  89. // The constructor remembers the arguments.
  90. SingleFailureChecker(const TestPartResultArray* results,
  91. TestPartResult::Type type, const std::string& substr);
  92. ~SingleFailureChecker();
  93. private:
  94. const TestPartResultArray* const results_;
  95. const TestPartResult::Type type_;
  96. const std::string substr_;
  97. SingleFailureChecker(const SingleFailureChecker&) = delete;
  98. SingleFailureChecker& operator=(const SingleFailureChecker&) = delete;
  99. };
  100. } // namespace internal
  101. } // namespace testing
  102. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
  103. // A set of macros for testing Google Test assertions or code that's expected
  104. // to generate Google Test fatal failures (e.g. a failure from an ASSERT_EQ, but
  105. // not a non-fatal failure, as from EXPECT_EQ). It verifies that the given
  106. // statement will cause exactly one fatal Google Test failure with 'substr'
  107. // being part of the failure message.
  108. //
  109. // There are two different versions of this macro. EXPECT_FATAL_FAILURE only
  110. // affects and considers failures generated in the current thread and
  111. // EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
  112. //
  113. // The verification of the assertion is done correctly even when the statement
  114. // throws an exception or aborts the current function.
  115. //
  116. // Known restrictions:
  117. // - 'statement' cannot reference local non-static variables or
  118. // non-static members of the current object.
  119. // - 'statement' cannot return a value.
  120. // - You cannot stream a failure message to this macro.
  121. //
  122. // Note that even though the implementations of the following two
  123. // macros are much alike, we cannot refactor them to use a common
  124. // helper macro, due to some peculiarity in how the preprocessor
  125. // works. The AcceptsMacroThatExpandsToUnprotectedComma test in
  126. // gtest_unittest.cc will fail to compile if we do that.
  127. #define EXPECT_FATAL_FAILURE(statement, substr) \
  128. do { \
  129. class GTestExpectFatalFailureHelper { \
  130. public: \
  131. static void Execute() { statement; } \
  132. }; \
  133. ::testing::TestPartResultArray gtest_failures; \
  134. ::testing::internal::SingleFailureChecker gtest_checker( \
  135. &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \
  136. { \
  137. ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \
  138. ::testing::ScopedFakeTestPartResultReporter:: \
  139. INTERCEPT_ONLY_CURRENT_THREAD, \
  140. &gtest_failures); \
  141. GTestExpectFatalFailureHelper::Execute(); \
  142. } \
  143. } while (::testing::internal::AlwaysFalse())
  144. #define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
  145. do { \
  146. class GTestExpectFatalFailureHelper { \
  147. public: \
  148. static void Execute() { statement; } \
  149. }; \
  150. ::testing::TestPartResultArray gtest_failures; \
  151. ::testing::internal::SingleFailureChecker gtest_checker( \
  152. &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \
  153. { \
  154. ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \
  155. ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
  156. &gtest_failures); \
  157. GTestExpectFatalFailureHelper::Execute(); \
  158. } \
  159. } while (::testing::internal::AlwaysFalse())
  160. // A macro for testing Google Test assertions or code that's expected to
  161. // generate Google Test non-fatal failures (e.g. a failure from an EXPECT_EQ,
  162. // but not from an ASSERT_EQ). It asserts that the given statement will cause
  163. // exactly one non-fatal Google Test failure with 'substr' being part of the
  164. // failure message.
  165. //
  166. // There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only
  167. // affects and considers failures generated in the current thread and
  168. // EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
  169. //
  170. // 'statement' is allowed to reference local variables and members of
  171. // the current object.
  172. //
  173. // The verification of the assertion is done correctly even when the statement
  174. // throws an exception or aborts the current function.
  175. //
  176. // Known restrictions:
  177. // - You cannot stream a failure message to this macro.
  178. //
  179. // Note that even though the implementations of the following two
  180. // macros are much alike, we cannot refactor them to use a common
  181. // helper macro, due to some peculiarity in how the preprocessor
  182. // works. If we do that, the code won't compile when the user gives
  183. // EXPECT_NONFATAL_FAILURE() a statement that contains a macro that
  184. // expands to code containing an unprotected comma. The
  185. // AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc
  186. // catches that.
  187. //
  188. // For the same reason, we have to write
  189. // if (::testing::internal::AlwaysTrue()) { statement; }
  190. // instead of
  191. // GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
  192. // to avoid an MSVC warning on unreachable code.
  193. #define EXPECT_NONFATAL_FAILURE(statement, substr) \
  194. do { \
  195. ::testing::TestPartResultArray gtest_failures; \
  196. ::testing::internal::SingleFailureChecker gtest_checker( \
  197. &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
  198. (substr)); \
  199. { \
  200. ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \
  201. ::testing::ScopedFakeTestPartResultReporter:: \
  202. INTERCEPT_ONLY_CURRENT_THREAD, \
  203. &gtest_failures); \
  204. if (::testing::internal::AlwaysTrue()) { \
  205. statement; \
  206. } \
  207. } \
  208. } while (::testing::internal::AlwaysFalse())
  209. #define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
  210. do { \
  211. ::testing::TestPartResultArray gtest_failures; \
  212. ::testing::internal::SingleFailureChecker gtest_checker( \
  213. &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
  214. (substr)); \
  215. { \
  216. ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \
  217. ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
  218. &gtest_failures); \
  219. if (::testing::internal::AlwaysTrue()) { \
  220. statement; \
  221. } \
  222. } \
  223. } while (::testing::internal::AlwaysFalse())
  224. #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_