gtest-spi.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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 "gtest/gtest.h"
  34. GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \
  35. /* class A needs to have dll-interface to be used by clients of class B */)
  36. namespace testing {
  37. // This helper class can be used to mock out Google Test failure reporting
  38. // so that we can test Google Test or code that builds on Google Test.
  39. //
  40. // An object of this class appends a TestPartResult object to the
  41. // TestPartResultArray object given in the constructor whenever a Google Test
  42. // failure is reported. It can either intercept only failures that are
  43. // generated in the same thread that created this object or it can intercept
  44. // all generated failures. The scope of this mock object can be controlled with
  45. // the second argument to the two arguments constructor.
  46. class GTEST_API_ ScopedFakeTestPartResultReporter
  47. : public TestPartResultReporterInterface {
  48. public:
  49. // The two possible mocking modes of this object.
  50. enum InterceptMode {
  51. INTERCEPT_ONLY_CURRENT_THREAD, // Intercepts only thread local failures.
  52. INTERCEPT_ALL_THREADS // Intercepts all failures.
  53. };
  54. // The c'tor sets this object as the test part result reporter used
  55. // by Google Test. The 'result' parameter specifies where to report the
  56. // results. This reporter will only catch failures generated in the current
  57. // thread. DEPRECATED
  58. explicit ScopedFakeTestPartResultReporter(TestPartResultArray* result);
  59. // Same as above, but you can choose the interception scope of this object.
  60. ScopedFakeTestPartResultReporter(InterceptMode intercept_mode,
  61. TestPartResultArray* result);
  62. // The d'tor restores the previous test part result reporter.
  63. ~ScopedFakeTestPartResultReporter() override;
  64. // Appends the TestPartResult object to the TestPartResultArray
  65. // received in the constructor.
  66. //
  67. // This method is from the TestPartResultReporterInterface
  68. // interface.
  69. void ReportTestPartResult(const TestPartResult& result) override;
  70. private:
  71. void Init();
  72. const InterceptMode intercept_mode_;
  73. TestPartResultReporterInterface* old_reporter_;
  74. TestPartResultArray* const result_;
  75. ScopedFakeTestPartResultReporter(const ScopedFakeTestPartResultReporter&) =
  76. delete;
  77. ScopedFakeTestPartResultReporter& operator=(
  78. const ScopedFakeTestPartResultReporter&) = delete;
  79. };
  80. namespace internal {
  81. // A helper class for implementing EXPECT_FATAL_FAILURE() and
  82. // EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given
  83. // TestPartResultArray contains exactly one failure that has the given
  84. // type and contains the given substring. If that's not the case, a
  85. // non-fatal failure will be generated.
  86. class GTEST_API_ SingleFailureChecker {
  87. public:
  88. // The constructor remembers the arguments.
  89. SingleFailureChecker(const TestPartResultArray* results,
  90. TestPartResult::Type type, const std::string& substr);
  91. ~SingleFailureChecker();
  92. private:
  93. const TestPartResultArray* const results_;
  94. const TestPartResult::Type type_;
  95. const std::string substr_;
  96. SingleFailureChecker(const SingleFailureChecker&) = delete;
  97. SingleFailureChecker& operator=(const SingleFailureChecker&) = delete;
  98. };
  99. } // namespace internal
  100. } // namespace testing
  101. GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251
  102. // A set of macros for testing Google Test assertions or code that's expected
  103. // to generate Google Test fatal failures (e.g. a failure from an ASSERT_EQ, but
  104. // not a non-fatal failure, as from EXPECT_EQ). It verifies that the given
  105. // statement will cause exactly one fatal Google Test failure with 'substr'
  106. // being part of the failure message.
  107. //
  108. // There are two different versions of this macro. EXPECT_FATAL_FAILURE only
  109. // affects and considers failures generated in the current thread and
  110. // EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
  111. //
  112. // The verification of the assertion is done correctly even when the statement
  113. // throws an exception or aborts the current function.
  114. //
  115. // Known restrictions:
  116. // - 'statement' cannot reference local non-static variables or
  117. // non-static members of the current object.
  118. // - 'statement' cannot return a value.
  119. // - You cannot stream a failure message to this macro.
  120. //
  121. // Note that even though the implementations of the following two
  122. // macros are much alike, we cannot refactor them to use a common
  123. // helper macro, due to some peculiarity in how the preprocessor
  124. // works. The AcceptsMacroThatExpandsToUnprotectedComma test in
  125. // gtest_unittest.cc will fail to compile if we do that.
  126. #define EXPECT_FATAL_FAILURE(statement, substr) \
  127. do { \
  128. class GTestExpectFatalFailureHelper { \
  129. public: \
  130. static void Execute() { statement; } \
  131. }; \
  132. ::testing::TestPartResultArray gtest_failures; \
  133. ::testing::internal::SingleFailureChecker gtest_checker( \
  134. &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \
  135. { \
  136. ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \
  137. ::testing::ScopedFakeTestPartResultReporter:: \
  138. INTERCEPT_ONLY_CURRENT_THREAD, \
  139. &gtest_failures); \
  140. GTestExpectFatalFailureHelper::Execute(); \
  141. } \
  142. } while (::testing::internal::AlwaysFalse())
  143. #define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
  144. do { \
  145. class GTestExpectFatalFailureHelper { \
  146. public: \
  147. static void Execute() { statement; } \
  148. }; \
  149. ::testing::TestPartResultArray gtest_failures; \
  150. ::testing::internal::SingleFailureChecker gtest_checker( \
  151. &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr)); \
  152. { \
  153. ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \
  154. ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
  155. &gtest_failures); \
  156. GTestExpectFatalFailureHelper::Execute(); \
  157. } \
  158. } while (::testing::internal::AlwaysFalse())
  159. // A macro for testing Google Test assertions or code that's expected to
  160. // generate Google Test non-fatal failures (e.g. a failure from an EXPECT_EQ,
  161. // but not from an ASSERT_EQ). It asserts that the given statement will cause
  162. // exactly one non-fatal Google Test failure with 'substr' being part of the
  163. // failure message.
  164. //
  165. // There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only
  166. // affects and considers failures generated in the current thread and
  167. // EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
  168. //
  169. // 'statement' is allowed to reference local variables and members of
  170. // the current object.
  171. //
  172. // The verification of the assertion is done correctly even when the statement
  173. // throws an exception or aborts the current function.
  174. //
  175. // Known restrictions:
  176. // - You cannot stream a failure message to this macro.
  177. //
  178. // Note that even though the implementations of the following two
  179. // macros are much alike, we cannot refactor them to use a common
  180. // helper macro, due to some peculiarity in how the preprocessor
  181. // works. If we do that, the code won't compile when the user gives
  182. // EXPECT_NONFATAL_FAILURE() a statement that contains a macro that
  183. // expands to code containing an unprotected comma. The
  184. // AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc
  185. // catches that.
  186. //
  187. // For the same reason, we have to write
  188. // if (::testing::internal::AlwaysTrue()) { statement; }
  189. // instead of
  190. // GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
  191. // to avoid an MSVC warning on unreachable code.
  192. #define EXPECT_NONFATAL_FAILURE(statement, substr) \
  193. do { \
  194. ::testing::TestPartResultArray gtest_failures; \
  195. ::testing::internal::SingleFailureChecker gtest_checker( \
  196. &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
  197. (substr)); \
  198. { \
  199. ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \
  200. ::testing::ScopedFakeTestPartResultReporter:: \
  201. INTERCEPT_ONLY_CURRENT_THREAD, \
  202. &gtest_failures); \
  203. if (::testing::internal::AlwaysTrue()) { \
  204. statement; \
  205. } \
  206. } \
  207. } while (::testing::internal::AlwaysFalse())
  208. #define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
  209. do { \
  210. ::testing::TestPartResultArray gtest_failures; \
  211. ::testing::internal::SingleFailureChecker gtest_checker( \
  212. &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
  213. (substr)); \
  214. { \
  215. ::testing::ScopedFakeTestPartResultReporter gtest_reporter( \
  216. ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
  217. &gtest_failures); \
  218. if (::testing::internal::AlwaysTrue()) { \
  219. statement; \
  220. } \
  221. } \
  222. } while (::testing::internal::AlwaysFalse())
  223. #endif // GOOGLETEST_INCLUDE_GTEST_GTEST_SPI_H_