gtest_catch_exceptions_test_.cc 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311
  1. // Copyright 2010, 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: vladl@google.com (Vlad Losev)
  31. //
  32. // Tests for Google Test itself. Tests in this file throw C++ or SEH
  33. // exceptions, and the output is verified by gtest_catch_exceptions_test.py.
  34. #include "gtest/gtest.h"
  35. #include <stdio.h> // NOLINT
  36. #include <stdlib.h> // For exit().
  37. #if GTEST_HAS_SEH
  38. # include <windows.h>
  39. #endif
  40. #if GTEST_HAS_EXCEPTIONS
  41. # include <exception> // For set_terminate().
  42. # include <stdexcept>
  43. #endif
  44. using testing::Test;
  45. #if GTEST_HAS_SEH
  46. class SehExceptionInConstructorTest : public Test {
  47. public:
  48. SehExceptionInConstructorTest() { RaiseException(42, 0, 0, NULL); }
  49. };
  50. TEST_F(SehExceptionInConstructorTest, ThrowsExceptionInConstructor) {}
  51. class SehExceptionInDestructorTest : public Test {
  52. public:
  53. ~SehExceptionInDestructorTest() { RaiseException(42, 0, 0, NULL); }
  54. };
  55. TEST_F(SehExceptionInDestructorTest, ThrowsExceptionInDestructor) {}
  56. class SehExceptionInSetUpTestCaseTest : public Test {
  57. public:
  58. static void SetUpTestCase() { RaiseException(42, 0, 0, NULL); }
  59. };
  60. TEST_F(SehExceptionInSetUpTestCaseTest, ThrowsExceptionInSetUpTestCase) {}
  61. class SehExceptionInTearDownTestCaseTest : public Test {
  62. public:
  63. static void TearDownTestCase() { RaiseException(42, 0, 0, NULL); }
  64. };
  65. TEST_F(SehExceptionInTearDownTestCaseTest, ThrowsExceptionInTearDownTestCase) {}
  66. class SehExceptionInSetUpTest : public Test {
  67. protected:
  68. virtual void SetUp() { RaiseException(42, 0, 0, NULL); }
  69. };
  70. TEST_F(SehExceptionInSetUpTest, ThrowsExceptionInSetUp) {}
  71. class SehExceptionInTearDownTest : public Test {
  72. protected:
  73. virtual void TearDown() { RaiseException(42, 0, 0, NULL); }
  74. };
  75. TEST_F(SehExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
  76. TEST(SehExceptionTest, ThrowsSehException) {
  77. RaiseException(42, 0, 0, NULL);
  78. }
  79. #endif // GTEST_HAS_SEH
  80. #if GTEST_HAS_EXCEPTIONS
  81. class CxxExceptionInConstructorTest : public Test {
  82. public:
  83. CxxExceptionInConstructorTest() {
  84. // Without this macro VC++ complains about unreachable code at the end of
  85. // the constructor.
  86. GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
  87. throw std::runtime_error("Standard C++ exception"));
  88. }
  89. static void TearDownTestCase() {
  90. printf("%s",
  91. "CxxExceptionInConstructorTest::TearDownTestCase() "
  92. "called as expected.\n");
  93. }
  94. protected:
  95. ~CxxExceptionInConstructorTest() {
  96. ADD_FAILURE() << "CxxExceptionInConstructorTest destructor "
  97. << "called unexpectedly.";
  98. }
  99. virtual void SetUp() {
  100. ADD_FAILURE() << "CxxExceptionInConstructorTest::SetUp() "
  101. << "called unexpectedly.";
  102. }
  103. virtual void TearDown() {
  104. ADD_FAILURE() << "CxxExceptionInConstructorTest::TearDown() "
  105. << "called unexpectedly.";
  106. }
  107. };
  108. TEST_F(CxxExceptionInConstructorTest, ThrowsExceptionInConstructor) {
  109. ADD_FAILURE() << "CxxExceptionInConstructorTest test body "
  110. << "called unexpectedly.";
  111. }
  112. // Exceptions in destructors are not supported in C++11.
  113. #if !defined(__GXX_EXPERIMENTAL_CXX0X__) && __cplusplus < 201103L
  114. class CxxExceptionInDestructorTest : public Test {
  115. public:
  116. static void TearDownTestCase() {
  117. printf("%s",
  118. "CxxExceptionInDestructorTest::TearDownTestCase() "
  119. "called as expected.\n");
  120. }
  121. protected:
  122. ~CxxExceptionInDestructorTest() {
  123. GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
  124. throw std::runtime_error("Standard C++ exception"));
  125. }
  126. };
  127. TEST_F(CxxExceptionInDestructorTest, ThrowsExceptionInDestructor) {}
  128. #endif // C++11 mode
  129. class CxxExceptionInSetUpTestCaseTest : public Test {
  130. public:
  131. CxxExceptionInSetUpTestCaseTest() {
  132. printf("%s",
  133. "CxxExceptionInSetUpTestCaseTest constructor "
  134. "called as expected.\n");
  135. }
  136. static void SetUpTestCase() {
  137. throw std::runtime_error("Standard C++ exception");
  138. }
  139. static void TearDownTestCase() {
  140. printf("%s",
  141. "CxxExceptionInSetUpTestCaseTest::TearDownTestCase() "
  142. "called as expected.\n");
  143. }
  144. protected:
  145. ~CxxExceptionInSetUpTestCaseTest() {
  146. printf("%s",
  147. "CxxExceptionInSetUpTestCaseTest destructor "
  148. "called as expected.\n");
  149. }
  150. virtual void SetUp() {
  151. printf("%s",
  152. "CxxExceptionInSetUpTestCaseTest::SetUp() "
  153. "called as expected.\n");
  154. }
  155. virtual void TearDown() {
  156. printf("%s",
  157. "CxxExceptionInSetUpTestCaseTest::TearDown() "
  158. "called as expected.\n");
  159. }
  160. };
  161. TEST_F(CxxExceptionInSetUpTestCaseTest, ThrowsExceptionInSetUpTestCase) {
  162. printf("%s",
  163. "CxxExceptionInSetUpTestCaseTest test body "
  164. "called as expected.\n");
  165. }
  166. class CxxExceptionInTearDownTestCaseTest : public Test {
  167. public:
  168. static void TearDownTestCase() {
  169. throw std::runtime_error("Standard C++ exception");
  170. }
  171. };
  172. TEST_F(CxxExceptionInTearDownTestCaseTest, ThrowsExceptionInTearDownTestCase) {}
  173. class CxxExceptionInSetUpTest : public Test {
  174. public:
  175. static void TearDownTestCase() {
  176. printf("%s",
  177. "CxxExceptionInSetUpTest::TearDownTestCase() "
  178. "called as expected.\n");
  179. }
  180. protected:
  181. ~CxxExceptionInSetUpTest() {
  182. printf("%s",
  183. "CxxExceptionInSetUpTest destructor "
  184. "called as expected.\n");
  185. }
  186. virtual void SetUp() { throw std::runtime_error("Standard C++ exception"); }
  187. virtual void TearDown() {
  188. printf("%s",
  189. "CxxExceptionInSetUpTest::TearDown() "
  190. "called as expected.\n");
  191. }
  192. };
  193. TEST_F(CxxExceptionInSetUpTest, ThrowsExceptionInSetUp) {
  194. ADD_FAILURE() << "CxxExceptionInSetUpTest test body "
  195. << "called unexpectedly.";
  196. }
  197. class CxxExceptionInTearDownTest : public Test {
  198. public:
  199. static void TearDownTestCase() {
  200. printf("%s",
  201. "CxxExceptionInTearDownTest::TearDownTestCase() "
  202. "called as expected.\n");
  203. }
  204. protected:
  205. ~CxxExceptionInTearDownTest() {
  206. printf("%s",
  207. "CxxExceptionInTearDownTest destructor "
  208. "called as expected.\n");
  209. }
  210. virtual void TearDown() {
  211. throw std::runtime_error("Standard C++ exception");
  212. }
  213. };
  214. TEST_F(CxxExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
  215. class CxxExceptionInTestBodyTest : public Test {
  216. public:
  217. static void TearDownTestCase() {
  218. printf("%s",
  219. "CxxExceptionInTestBodyTest::TearDownTestCase() "
  220. "called as expected.\n");
  221. }
  222. protected:
  223. ~CxxExceptionInTestBodyTest() {
  224. printf("%s",
  225. "CxxExceptionInTestBodyTest destructor "
  226. "called as expected.\n");
  227. }
  228. virtual void TearDown() {
  229. printf("%s",
  230. "CxxExceptionInTestBodyTest::TearDown() "
  231. "called as expected.\n");
  232. }
  233. };
  234. TEST_F(CxxExceptionInTestBodyTest, ThrowsStdCxxException) {
  235. throw std::runtime_error("Standard C++ exception");
  236. }
  237. TEST(CxxExceptionTest, ThrowsNonStdCxxException) {
  238. throw "C-string";
  239. }
  240. // This terminate handler aborts the program using exit() rather than abort().
  241. // This avoids showing pop-ups on Windows systems and core dumps on Unix-like
  242. // ones.
  243. void TerminateHandler() {
  244. fprintf(stderr, "%s\n", "Unhandled C++ exception terminating the program.");
  245. fflush(NULL);
  246. exit(3);
  247. }
  248. #endif // GTEST_HAS_EXCEPTIONS
  249. int main(int argc, char** argv) {
  250. #if GTEST_HAS_EXCEPTIONS
  251. std::set_terminate(&TerminateHandler);
  252. #endif
  253. testing::InitGoogleTest(&argc, argv);
  254. return RUN_ALL_TESTS();
  255. }