googletest-catch-exceptions-test_.cc 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 SehExceptionInSetUpTestSuiteTest : public Test {
  56. public:
  57. static void SetUpTestSuite() { RaiseException(42, 0, 0, NULL); }
  58. };
  59. TEST_F(SehExceptionInSetUpTestSuiteTest, ThrowsExceptionInSetUpTestSuite) {}
  60. class SehExceptionInTearDownTestSuiteTest : public Test {
  61. public:
  62. static void TearDownTestSuite() { RaiseException(42, 0, 0, NULL); }
  63. };
  64. TEST_F(SehExceptionInTearDownTestSuiteTest,
  65. ThrowsExceptionInTearDownTestSuite) {}
  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) { RaiseException(42, 0, 0, NULL); }
  77. #endif // GTEST_HAS_SEH
  78. #if GTEST_HAS_EXCEPTIONS
  79. class CxxExceptionInConstructorTest : public Test {
  80. public:
  81. CxxExceptionInConstructorTest() {
  82. // Without this macro VC++ complains about unreachable code at the end of
  83. // the constructor.
  84. GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
  85. throw std::runtime_error("Standard C++ exception"));
  86. }
  87. static void TearDownTestSuite() {
  88. printf("%s",
  89. "CxxExceptionInConstructorTest::TearDownTestSuite() "
  90. "called as expected.\n");
  91. }
  92. protected:
  93. ~CxxExceptionInConstructorTest() override {
  94. ADD_FAILURE() << "CxxExceptionInConstructorTest destructor "
  95. << "called unexpectedly.";
  96. }
  97. void SetUp() override {
  98. ADD_FAILURE() << "CxxExceptionInConstructorTest::SetUp() "
  99. << "called unexpectedly.";
  100. }
  101. void TearDown() override {
  102. ADD_FAILURE() << "CxxExceptionInConstructorTest::TearDown() "
  103. << "called unexpectedly.";
  104. }
  105. };
  106. TEST_F(CxxExceptionInConstructorTest, ThrowsExceptionInConstructor) {
  107. ADD_FAILURE() << "CxxExceptionInConstructorTest test body "
  108. << "called unexpectedly.";
  109. }
  110. class CxxExceptionInSetUpTestSuiteTest : public Test {
  111. public:
  112. CxxExceptionInSetUpTestSuiteTest() {
  113. printf("%s",
  114. "CxxExceptionInSetUpTestSuiteTest constructor "
  115. "called as expected.\n");
  116. }
  117. static void SetUpTestSuite() {
  118. throw std::runtime_error("Standard C++ exception");
  119. }
  120. static void TearDownTestSuite() {
  121. printf("%s",
  122. "CxxExceptionInSetUpTestSuiteTest::TearDownTestSuite() "
  123. "called as expected.\n");
  124. }
  125. protected:
  126. ~CxxExceptionInSetUpTestSuiteTest() override {
  127. printf("%s",
  128. "CxxExceptionInSetUpTestSuiteTest destructor "
  129. "called as expected.\n");
  130. }
  131. void SetUp() override {
  132. printf("%s",
  133. "CxxExceptionInSetUpTestSuiteTest::SetUp() "
  134. "called as expected.\n");
  135. }
  136. void TearDown() override {
  137. printf("%s",
  138. "CxxExceptionInSetUpTestSuiteTest::TearDown() "
  139. "called as expected.\n");
  140. }
  141. };
  142. TEST_F(CxxExceptionInSetUpTestSuiteTest, ThrowsExceptionInSetUpTestSuite) {
  143. printf("%s",
  144. "CxxExceptionInSetUpTestSuiteTest test body "
  145. "called as expected.\n");
  146. }
  147. class CxxExceptionInTearDownTestSuiteTest : public Test {
  148. public:
  149. static void TearDownTestSuite() {
  150. throw std::runtime_error("Standard C++ exception");
  151. }
  152. };
  153. TEST_F(CxxExceptionInTearDownTestSuiteTest,
  154. ThrowsExceptionInTearDownTestSuite) {}
  155. class CxxExceptionInSetUpTest : public Test {
  156. public:
  157. static void TearDownTestSuite() {
  158. printf("%s",
  159. "CxxExceptionInSetUpTest::TearDownTestSuite() "
  160. "called as expected.\n");
  161. }
  162. protected:
  163. ~CxxExceptionInSetUpTest() override {
  164. printf("%s",
  165. "CxxExceptionInSetUpTest destructor "
  166. "called as expected.\n");
  167. }
  168. void SetUp() override { throw std::runtime_error("Standard C++ exception"); }
  169. void TearDown() override {
  170. printf("%s",
  171. "CxxExceptionInSetUpTest::TearDown() "
  172. "called as expected.\n");
  173. }
  174. };
  175. TEST_F(CxxExceptionInSetUpTest, ThrowsExceptionInSetUp) {
  176. ADD_FAILURE() << "CxxExceptionInSetUpTest test body "
  177. << "called unexpectedly.";
  178. }
  179. class CxxExceptionInTearDownTest : public Test {
  180. public:
  181. static void TearDownTestSuite() {
  182. printf("%s",
  183. "CxxExceptionInTearDownTest::TearDownTestSuite() "
  184. "called as expected.\n");
  185. }
  186. protected:
  187. ~CxxExceptionInTearDownTest() override {
  188. printf("%s",
  189. "CxxExceptionInTearDownTest destructor "
  190. "called as expected.\n");
  191. }
  192. void TearDown() override {
  193. throw std::runtime_error("Standard C++ exception");
  194. }
  195. };
  196. TEST_F(CxxExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
  197. class CxxExceptionInTestBodyTest : public Test {
  198. public:
  199. static void TearDownTestSuite() {
  200. printf("%s",
  201. "CxxExceptionInTestBodyTest::TearDownTestSuite() "
  202. "called as expected.\n");
  203. }
  204. protected:
  205. ~CxxExceptionInTestBodyTest() override {
  206. printf("%s",
  207. "CxxExceptionInTestBodyTest destructor "
  208. "called as expected.\n");
  209. }
  210. void TearDown() override {
  211. printf("%s",
  212. "CxxExceptionInTestBodyTest::TearDown() "
  213. "called as expected.\n");
  214. }
  215. };
  216. TEST_F(CxxExceptionInTestBodyTest, ThrowsStdCxxException) {
  217. throw std::runtime_error("Standard C++ exception");
  218. }
  219. TEST(CxxExceptionTest, ThrowsNonStdCxxException) { throw "C-string"; }
  220. // This terminate handler aborts the program using exit() rather than abort().
  221. // This avoids showing pop-ups on Windows systems and core dumps on Unix-like
  222. // ones.
  223. void TerminateHandler() {
  224. fprintf(stderr, "%s\n", "Unhandled C++ exception terminating the program.");
  225. fflush(nullptr);
  226. exit(3);
  227. }
  228. #endif // GTEST_HAS_EXCEPTIONS
  229. int main(int argc, char** argv) {
  230. #if GTEST_HAS_EXCEPTIONS
  231. std::set_terminate(&TerminateHandler);
  232. #endif
  233. testing::InitGoogleTest(&argc, argv);
  234. return RUN_ALL_TESTS();
  235. }