You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

238 lines
9.9 KiB

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