1 // Copyright 2010, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
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
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.
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.
31 // Tests for Google Test itself. Tests in this file throw C++ or SEH
32 // exceptions, and the output is verified by
33 // googletest-catch-exceptions-test.py.
35 #include <stdio.h> // NOLINT
36 #include <stdlib.h> // For exit().
38 #include "gtest/gtest.h"
44 #if GTEST_HAS_EXCEPTIONS
45 # include <exception> // For set_terminate().
53 class SehExceptionInConstructorTest : public Test {
55 SehExceptionInConstructorTest() { RaiseException(42, 0, 0, NULL); }
58 TEST_F(SehExceptionInConstructorTest, ThrowsExceptionInConstructor) {}
60 class SehExceptionInDestructorTest : public Test {
62 ~SehExceptionInDestructorTest() { RaiseException(42, 0, 0, NULL); }
65 TEST_F(SehExceptionInDestructorTest, ThrowsExceptionInDestructor) {}
67 class SehExceptionInSetUpTestSuiteTest : public Test {
69 static void SetUpTestSuite() { RaiseException(42, 0, 0, NULL); }
72 TEST_F(SehExceptionInSetUpTestSuiteTest, ThrowsExceptionInSetUpTestSuite) {}
74 class SehExceptionInTearDownTestSuiteTest : public Test {
76 static void TearDownTestSuite() { RaiseException(42, 0, 0, NULL); }
79 TEST_F(SehExceptionInTearDownTestSuiteTest,
80 ThrowsExceptionInTearDownTestSuite) {}
82 class SehExceptionInSetUpTest : public Test {
84 virtual void SetUp() { RaiseException(42, 0, 0, NULL); }
87 TEST_F(SehExceptionInSetUpTest, ThrowsExceptionInSetUp) {}
89 class SehExceptionInTearDownTest : public Test {
91 virtual void TearDown() { RaiseException(42, 0, 0, NULL); }
94 TEST_F(SehExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
96 TEST(SehExceptionTest, ThrowsSehException) {
97 RaiseException(42, 0, 0, NULL);
100 #endif // GTEST_HAS_SEH
102 #if GTEST_HAS_EXCEPTIONS
104 class CxxExceptionInConstructorTest : public Test {
106 CxxExceptionInConstructorTest() {
107 // Without this macro VC++ complains about unreachable code at the end of
109 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
110 throw std::runtime_error("Standard C++ exception"));
113 static void TearDownTestSuite() {
115 "CxxExceptionInConstructorTest::TearDownTestSuite() "
116 "called as expected.\n");
120 ~CxxExceptionInConstructorTest() override {
121 ADD_FAILURE() << "CxxExceptionInConstructorTest destructor "
122 << "called unexpectedly.";
125 void SetUp() override {
126 ADD_FAILURE() << "CxxExceptionInConstructorTest::SetUp() "
127 << "called unexpectedly.";
130 void TearDown() override {
131 ADD_FAILURE() << "CxxExceptionInConstructorTest::TearDown() "
132 << "called unexpectedly.";
136 TEST_F(CxxExceptionInConstructorTest, ThrowsExceptionInConstructor) {
137 ADD_FAILURE() << "CxxExceptionInConstructorTest test body "
138 << "called unexpectedly.";
141 class CxxExceptionInSetUpTestSuiteTest : public Test {
143 CxxExceptionInSetUpTestSuiteTest() {
145 "CxxExceptionInSetUpTestSuiteTest constructor "
146 "called as expected.\n");
149 static void SetUpTestSuite() {
150 throw std::runtime_error("Standard C++ exception");
153 static void TearDownTestSuite() {
155 "CxxExceptionInSetUpTestSuiteTest::TearDownTestSuite() "
156 "called as expected.\n");
160 ~CxxExceptionInSetUpTestSuiteTest() override {
162 "CxxExceptionInSetUpTestSuiteTest destructor "
163 "called as expected.\n");
166 void SetUp() override {
168 "CxxExceptionInSetUpTestSuiteTest::SetUp() "
169 "called as expected.\n");
172 void TearDown() override {
174 "CxxExceptionInSetUpTestSuiteTest::TearDown() "
175 "called as expected.\n");
179 TEST_F(CxxExceptionInSetUpTestSuiteTest, ThrowsExceptionInSetUpTestSuite) {
181 "CxxExceptionInSetUpTestSuiteTest test body "
182 "called as expected.\n");
185 class CxxExceptionInTearDownTestSuiteTest : public Test {
187 static void TearDownTestSuite() {
188 throw std::runtime_error("Standard C++ exception");
192 TEST_F(CxxExceptionInTearDownTestSuiteTest,
193 ThrowsExceptionInTearDownTestSuite) {}
195 class CxxExceptionInSetUpTest : public Test {
197 static void TearDownTestSuite() {
199 "CxxExceptionInSetUpTest::TearDownTestSuite() "
200 "called as expected.\n");
204 ~CxxExceptionInSetUpTest() override {
206 "CxxExceptionInSetUpTest destructor "
207 "called as expected.\n");
210 void SetUp() override { throw std::runtime_error("Standard C++ exception"); }
212 void TearDown() override {
214 "CxxExceptionInSetUpTest::TearDown() "
215 "called as expected.\n");
219 TEST_F(CxxExceptionInSetUpTest, ThrowsExceptionInSetUp) {
220 ADD_FAILURE() << "CxxExceptionInSetUpTest test body "
221 << "called unexpectedly.";
224 class CxxExceptionInTearDownTest : public Test {
226 static void TearDownTestSuite() {
228 "CxxExceptionInTearDownTest::TearDownTestSuite() "
229 "called as expected.\n");
233 ~CxxExceptionInTearDownTest() override {
235 "CxxExceptionInTearDownTest destructor "
236 "called as expected.\n");
239 void TearDown() override {
240 throw std::runtime_error("Standard C++ exception");
244 TEST_F(CxxExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
246 class CxxExceptionInTestBodyTest : public Test {
248 static void TearDownTestSuite() {
250 "CxxExceptionInTestBodyTest::TearDownTestSuite() "
251 "called as expected.\n");
255 ~CxxExceptionInTestBodyTest() override {
257 "CxxExceptionInTestBodyTest destructor "
258 "called as expected.\n");
261 void TearDown() override {
263 "CxxExceptionInTestBodyTest::TearDown() "
264 "called as expected.\n");
268 TEST_F(CxxExceptionInTestBodyTest, ThrowsStdCxxException) {
269 throw std::runtime_error("Standard C++ exception");
272 TEST(CxxExceptionTest, ThrowsNonStdCxxException) {
276 // This terminate handler aborts the program using exit() rather than abort().
277 // This avoids showing pop-ups on Windows systems and core dumps on Unix-like
279 void TerminateHandler() {
280 fprintf(stderr, "%s\n", "Unhandled C++ exception terminating the program.");
285 #endif // GTEST_HAS_EXCEPTIONS
287 int main(int argc, char** argv) {
288 #if GTEST_HAS_EXCEPTIONS
289 std::set_terminate(&TerminateHandler);
291 testing::InitGoogleTest(&argc, argv);
292 return RUN_ALL_TESTS();