googletest-catch-exceptions-test_.cc 8.2 KB

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