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.

308 lines
8.6 KiB

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