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.
30 // Author: vladl@google.com (Vlad Losev)
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.
35 #include "gtest/gtest.h"
37 #include <stdio.h> // NOLINT
38 #include <stdlib.h> // For exit().
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 SehExceptionInSetUpTestCaseTest : public Test {
69 static void SetUpTestCase() { RaiseException(42, 0, 0, NULL); }
72 TEST_F(SehExceptionInSetUpTestCaseTest, ThrowsExceptionInSetUpTestCase) {}
74 class SehExceptionInTearDownTestCaseTest : public Test {
76 static void TearDownTestCase() { RaiseException(42, 0, 0, NULL); }
79 TEST_F(SehExceptionInTearDownTestCaseTest, ThrowsExceptionInTearDownTestCase) {}
81 class SehExceptionInSetUpTest : public Test {
83 virtual void SetUp() { RaiseException(42, 0, 0, NULL); }
86 TEST_F(SehExceptionInSetUpTest, ThrowsExceptionInSetUp) {}
88 class SehExceptionInTearDownTest : public Test {
90 virtual void TearDown() { RaiseException(42, 0, 0, NULL); }
93 TEST_F(SehExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
95 TEST(SehExceptionTest, ThrowsSehException) {
96 RaiseException(42, 0, 0, NULL);
99 #endif // GTEST_HAS_SEH
101 #if GTEST_HAS_EXCEPTIONS
103 class CxxExceptionInConstructorTest : public Test {
105 CxxExceptionInConstructorTest() {
106 // Without this macro VC++ complains about unreachable code at the end of
108 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
109 throw std::runtime_error("Standard C++ exception"));
112 static void TearDownTestCase() {
114 "CxxExceptionInConstructorTest::TearDownTestCase() "
115 "called as expected.\n");
119 ~CxxExceptionInConstructorTest() {
120 ADD_FAILURE() << "CxxExceptionInConstructorTest destructor "
121 << "called unexpectedly.";
124 virtual void SetUp() {
125 ADD_FAILURE() << "CxxExceptionInConstructorTest::SetUp() "
126 << "called unexpectedly.";
129 virtual void TearDown() {
130 ADD_FAILURE() << "CxxExceptionInConstructorTest::TearDown() "
131 << "called unexpectedly.";
135 TEST_F(CxxExceptionInConstructorTest, ThrowsExceptionInConstructor) {
136 ADD_FAILURE() << "CxxExceptionInConstructorTest test body "
137 << "called unexpectedly.";
140 class CxxExceptionInDestructorTest : public Test {
142 static void TearDownTestCase() {
144 "CxxExceptionInDestructorTest::TearDownTestCase() "
145 "called as expected.\n");
149 ~CxxExceptionInDestructorTest() {
150 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
151 throw std::runtime_error("Standard C++ exception"));
155 TEST_F(CxxExceptionInDestructorTest, ThrowsExceptionInDestructor) {}
157 class CxxExceptionInSetUpTestCaseTest : public Test {
159 CxxExceptionInSetUpTestCaseTest() {
161 "CxxExceptionInSetUpTestCaseTest constructor "
162 "called as expected.\n");
165 static void SetUpTestCase() {
166 throw std::runtime_error("Standard C++ exception");
169 static void TearDownTestCase() {
171 "CxxExceptionInSetUpTestCaseTest::TearDownTestCase() "
172 "called as expected.\n");
176 ~CxxExceptionInSetUpTestCaseTest() {
178 "CxxExceptionInSetUpTestCaseTest destructor "
179 "called as expected.\n");
182 virtual void SetUp() {
184 "CxxExceptionInSetUpTestCaseTest::SetUp() "
185 "called as expected.\n");
188 virtual void TearDown() {
190 "CxxExceptionInSetUpTestCaseTest::TearDown() "
191 "called as expected.\n");
195 TEST_F(CxxExceptionInSetUpTestCaseTest, ThrowsExceptionInSetUpTestCase) {
197 "CxxExceptionInSetUpTestCaseTest test body "
198 "called as expected.\n");
201 class CxxExceptionInTearDownTestCaseTest : public Test {
203 static void TearDownTestCase() {
204 throw std::runtime_error("Standard C++ exception");
208 TEST_F(CxxExceptionInTearDownTestCaseTest, ThrowsExceptionInTearDownTestCase) {}
210 class CxxExceptionInSetUpTest : public Test {
212 static void TearDownTestCase() {
214 "CxxExceptionInSetUpTest::TearDownTestCase() "
215 "called as expected.\n");
219 ~CxxExceptionInSetUpTest() {
221 "CxxExceptionInSetUpTest destructor "
222 "called as expected.\n");
225 virtual void SetUp() { throw std::runtime_error("Standard C++ exception"); }
227 virtual void TearDown() {
229 "CxxExceptionInSetUpTest::TearDown() "
230 "called as expected.\n");
234 TEST_F(CxxExceptionInSetUpTest, ThrowsExceptionInSetUp) {
235 ADD_FAILURE() << "CxxExceptionInSetUpTest test body "
236 << "called unexpectedly.";
239 class CxxExceptionInTearDownTest : public Test {
241 static void TearDownTestCase() {
243 "CxxExceptionInTearDownTest::TearDownTestCase() "
244 "called as expected.\n");
248 ~CxxExceptionInTearDownTest() {
250 "CxxExceptionInTearDownTest destructor "
251 "called as expected.\n");
254 virtual void TearDown() {
255 throw std::runtime_error("Standard C++ exception");
259 TEST_F(CxxExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
261 class CxxExceptionInTestBodyTest : public Test {
263 static void TearDownTestCase() {
265 "CxxExceptionInTestBodyTest::TearDownTestCase() "
266 "called as expected.\n");
270 ~CxxExceptionInTestBodyTest() {
272 "CxxExceptionInTestBodyTest destructor "
273 "called as expected.\n");
276 virtual void TearDown() {
278 "CxxExceptionInTestBodyTest::TearDown() "
279 "called as expected.\n");
283 TEST_F(CxxExceptionInTestBodyTest, ThrowsStdCxxException) {
284 throw std::runtime_error("Standard C++ exception");
287 TEST(CxxExceptionTest, ThrowsNonStdCxxException) {
291 // This terminate handler aborts the program using exit() rather than abort().
292 // This avoids showing pop-ups on Windows systems and core dumps on Unix-like
294 void TerminateHandler() {
295 fprintf(stderr, "%s\n", "Unhandled C++ exception terminating the program.");
300 #endif // GTEST_HAS_EXCEPTIONS
302 int main(int argc, char** argv) {
303 #if GTEST_HAS_EXCEPTIONS
304 std::set_terminate(&TerminateHandler);
306 testing::InitGoogleTest(&argc, argv);
307 return RUN_ALL_TESTS();