gtest-spi.h 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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. // Author: wan@google.com (Zhanyong Wan)
  31. //
  32. // Utilities for testing Google Test itself and code that uses Google Test
  33. // (e.g. frameworks built on top of Google Test).
  34. #ifndef GTEST_INCLUDE_GTEST_GTEST_SPI_H_
  35. #define GTEST_INCLUDE_GTEST_GTEST_SPI_H_
  36. #include "gtest/gtest.h"
  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. virtual ~ScopedFakeTestPartResultReporter();
  65. // Appends the TestPartResult object to the TestPartResultArray
  66. // received in the constructor.
  67. //
  68. // This method is from the TestPartResultReporterInterface
  69. // interface.
  70. virtual void ReportTestPartResult(const TestPartResult& result);
  71. private:
  72. void Init();
  73. const InterceptMode intercept_mode_;
  74. TestPartResultReporterInterface* old_reporter_;
  75. TestPartResultArray* const result_;
  76. GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedFakeTestPartResultReporter);
  77. };
  78. namespace internal {
  79. // A helper class for implementing EXPECT_FATAL_FAILURE() and
  80. // EXPECT_NONFATAL_FAILURE(). Its destructor verifies that the given
  81. // TestPartResultArray contains exactly one failure that has the given
  82. // type and contains the given substring. If that's not the case, a
  83. // non-fatal failure will be generated.
  84. class GTEST_API_ SingleFailureChecker {
  85. public:
  86. // The constructor remembers the arguments.
  87. SingleFailureChecker(const TestPartResultArray* results,
  88. TestPartResult::Type type, const std::string& substr);
  89. ~SingleFailureChecker();
  90. private:
  91. const TestPartResultArray* const results_;
  92. const TestPartResult::Type type_;
  93. const std::string substr_;
  94. GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker);
  95. };
  96. } // namespace internal
  97. } // namespace testing
  98. // A set of macros for testing Google Test assertions or code that's expected
  99. // to generate Google Test fatal failures. It verifies that the given
  100. // statement will cause exactly one fatal Google Test failure with 'substr'
  101. // being part of the failure message.
  102. //
  103. // There are two different versions of this macro. EXPECT_FATAL_FAILURE only
  104. // affects and considers failures generated in the current thread and
  105. // EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
  106. //
  107. // The verification of the assertion is done correctly even when the statement
  108. // throws an exception or aborts the current function.
  109. //
  110. // Known restrictions:
  111. // - 'statement' cannot reference local non-static variables or
  112. // non-static members of the current object.
  113. // - 'statement' cannot return a value.
  114. // - You cannot stream a failure message to this macro.
  115. //
  116. // Note that even though the implementations of the following two
  117. // macros are much alike, we cannot refactor them to use a common
  118. // helper macro, due to some peculiarity in how the preprocessor
  119. // works. The AcceptsMacroThatExpandsToUnprotectedComma test in
  120. // gtest_unittest.cc will fail to compile if we do that.
  121. #define EXPECT_FATAL_FAILURE(statement, substr) \
  122. do { \
  123. class GTestExpectFatalFailureHelper {\
  124. public:\
  125. static void Execute() { statement; }\
  126. };\
  127. ::testing::TestPartResultArray gtest_failures;\
  128. ::testing::internal::SingleFailureChecker gtest_checker(\
  129. &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
  130. {\
  131. ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
  132. ::testing::ScopedFakeTestPartResultReporter:: \
  133. INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\
  134. GTestExpectFatalFailureHelper::Execute();\
  135. }\
  136. } while (::testing::internal::AlwaysFalse())
  137. #define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
  138. do { \
  139. class GTestExpectFatalFailureHelper {\
  140. public:\
  141. static void Execute() { statement; }\
  142. };\
  143. ::testing::TestPartResultArray gtest_failures;\
  144. ::testing::internal::SingleFailureChecker gtest_checker(\
  145. &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
  146. {\
  147. ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
  148. ::testing::ScopedFakeTestPartResultReporter:: \
  149. INTERCEPT_ALL_THREADS, &gtest_failures);\
  150. GTestExpectFatalFailureHelper::Execute();\
  151. }\
  152. } while (::testing::internal::AlwaysFalse())
  153. // A macro for testing Google Test assertions or code that's expected to
  154. // generate Google Test non-fatal failures. It asserts that the given
  155. // statement will cause exactly one non-fatal Google Test failure with 'substr'
  156. // being part of the failure message.
  157. //
  158. // There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only
  159. // affects and considers failures generated in the current thread and
  160. // EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
  161. //
  162. // 'statement' is allowed to reference local variables and members of
  163. // the current object.
  164. //
  165. // The verification of the assertion is done correctly even when the statement
  166. // throws an exception or aborts the current function.
  167. //
  168. // Known restrictions:
  169. // - You cannot stream a failure message to this macro.
  170. //
  171. // Note that even though the implementations of the following two
  172. // macros are much alike, we cannot refactor them to use a common
  173. // helper macro, due to some peculiarity in how the preprocessor
  174. // works. If we do that, the code won't compile when the user gives
  175. // EXPECT_NONFATAL_FAILURE() a statement that contains a macro that
  176. // expands to code containing an unprotected comma. The
  177. // AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc
  178. // catches that.
  179. //
  180. // For the same reason, we have to write
  181. // if (::testing::internal::AlwaysTrue()) { statement; }
  182. // instead of
  183. // GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
  184. // to avoid an MSVC warning on unreachable code.
  185. #define EXPECT_NONFATAL_FAILURE(statement, substr) \
  186. do {\
  187. ::testing::TestPartResultArray gtest_failures;\
  188. ::testing::internal::SingleFailureChecker gtest_checker(\
  189. &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
  190. (substr));\
  191. {\
  192. ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
  193. ::testing::ScopedFakeTestPartResultReporter:: \
  194. INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\
  195. if (::testing::internal::AlwaysTrue()) { statement; }\
  196. }\
  197. } while (::testing::internal::AlwaysFalse())
  198. #define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
  199. do {\
  200. ::testing::TestPartResultArray gtest_failures;\
  201. ::testing::internal::SingleFailureChecker gtest_checker(\
  202. &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
  203. (substr));\
  204. {\
  205. ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
  206. ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS, \
  207. &gtest_failures);\
  208. if (::testing::internal::AlwaysTrue()) { statement; }\
  209. }\
  210. } while (::testing::internal::AlwaysFalse())
  211. #endif // GTEST_INCLUDE_GTEST_GTEST_SPI_H_