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.

232 lines
9.7 KiB

  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,
  89. const string& substr);
  90. ~SingleFailureChecker();
  91. private:
  92. const TestPartResultArray* const results_;
  93. const TestPartResult::Type type_;
  94. const string substr_;
  95. GTEST_DISALLOW_COPY_AND_ASSIGN_(SingleFailureChecker);
  96. };
  97. } // namespace internal
  98. } // namespace testing
  99. // A set of macros for testing Google Test assertions or code that's expected
  100. // to generate Google Test fatal failures. It verifies that the given
  101. // statement will cause exactly one fatal Google Test failure with 'substr'
  102. // being part of the failure message.
  103. //
  104. // There are two different versions of this macro. EXPECT_FATAL_FAILURE only
  105. // affects and considers failures generated in the current thread and
  106. // EXPECT_FATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
  107. //
  108. // The verification of the assertion is done correctly even when the statement
  109. // throws an exception or aborts the current function.
  110. //
  111. // Known restrictions:
  112. // - 'statement' cannot reference local non-static variables or
  113. // non-static members of the current object.
  114. // - 'statement' cannot return a value.
  115. // - You cannot stream a failure message to this macro.
  116. //
  117. // Note that even though the implementations of the following two
  118. // macros are much alike, we cannot refactor them to use a common
  119. // helper macro, due to some peculiarity in how the preprocessor
  120. // works. The AcceptsMacroThatExpandsToUnprotectedComma test in
  121. // gtest_unittest.cc will fail to compile if we do that.
  122. #define EXPECT_FATAL_FAILURE(statement, substr) \
  123. do { \
  124. class GTestExpectFatalFailureHelper {\
  125. public:\
  126. static void Execute() { statement; }\
  127. };\
  128. ::testing::TestPartResultArray gtest_failures;\
  129. ::testing::internal::SingleFailureChecker gtest_checker(\
  130. &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
  131. {\
  132. ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
  133. ::testing::ScopedFakeTestPartResultReporter:: \
  134. INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\
  135. GTestExpectFatalFailureHelper::Execute();\
  136. }\
  137. } while (::testing::internal::AlwaysFalse())
  138. #define EXPECT_FATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
  139. do { \
  140. class GTestExpectFatalFailureHelper {\
  141. public:\
  142. static void Execute() { statement; }\
  143. };\
  144. ::testing::TestPartResultArray gtest_failures;\
  145. ::testing::internal::SingleFailureChecker gtest_checker(\
  146. &gtest_failures, ::testing::TestPartResult::kFatalFailure, (substr));\
  147. {\
  148. ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
  149. ::testing::ScopedFakeTestPartResultReporter:: \
  150. INTERCEPT_ALL_THREADS, &gtest_failures);\
  151. GTestExpectFatalFailureHelper::Execute();\
  152. }\
  153. } while (::testing::internal::AlwaysFalse())
  154. // A macro for testing Google Test assertions or code that's expected to
  155. // generate Google Test non-fatal failures. It asserts that the given
  156. // statement will cause exactly one non-fatal Google Test failure with 'substr'
  157. // being part of the failure message.
  158. //
  159. // There are two different versions of this macro. EXPECT_NONFATAL_FAILURE only
  160. // affects and considers failures generated in the current thread and
  161. // EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS does the same but for all threads.
  162. //
  163. // 'statement' is allowed to reference local variables and members of
  164. // the current object.
  165. //
  166. // The verification of the assertion is done correctly even when the statement
  167. // throws an exception or aborts the current function.
  168. //
  169. // Known restrictions:
  170. // - You cannot stream a failure message to this macro.
  171. //
  172. // Note that even though the implementations of the following two
  173. // macros are much alike, we cannot refactor them to use a common
  174. // helper macro, due to some peculiarity in how the preprocessor
  175. // works. If we do that, the code won't compile when the user gives
  176. // EXPECT_NONFATAL_FAILURE() a statement that contains a macro that
  177. // expands to code containing an unprotected comma. The
  178. // AcceptsMacroThatExpandsToUnprotectedComma test in gtest_unittest.cc
  179. // catches that.
  180. //
  181. // For the same reason, we have to write
  182. // if (::testing::internal::AlwaysTrue()) { statement; }
  183. // instead of
  184. // GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(statement)
  185. // to avoid an MSVC warning on unreachable code.
  186. #define EXPECT_NONFATAL_FAILURE(statement, substr) \
  187. do {\
  188. ::testing::TestPartResultArray gtest_failures;\
  189. ::testing::internal::SingleFailureChecker gtest_checker(\
  190. &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
  191. (substr));\
  192. {\
  193. ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
  194. ::testing::ScopedFakeTestPartResultReporter:: \
  195. INTERCEPT_ONLY_CURRENT_THREAD, &gtest_failures);\
  196. if (::testing::internal::AlwaysTrue()) { statement; }\
  197. }\
  198. } while (::testing::internal::AlwaysFalse())
  199. #define EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(statement, substr) \
  200. do {\
  201. ::testing::TestPartResultArray gtest_failures;\
  202. ::testing::internal::SingleFailureChecker gtest_checker(\
  203. &gtest_failures, ::testing::TestPartResult::kNonFatalFailure, \
  204. (substr));\
  205. {\
  206. ::testing::ScopedFakeTestPartResultReporter gtest_reporter(\
  207. ::testing::ScopedFakeTestPartResultReporter::INTERCEPT_ALL_THREADS,\
  208. &gtest_failures);\
  209. if (::testing::internal::AlwaysTrue()) { statement; }\
  210. }\
  211. } while (::testing::internal::AlwaysFalse())
  212. #endif // GTEST_INCLUDE_GTEST_GTEST_SPI_H_