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