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) { RaiseException(42, 0, 0, NULL); }
98 #endif // GTEST_HAS_SEH
100 #if GTEST_HAS_EXCEPTIONS
102 class CxxExceptionInConstructorTest : public Test {
104 CxxExceptionInConstructorTest() {
105 // Without this macro VC++ complains about unreachable code at the end of
107 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
108 throw std::runtime_error("Standard C++ exception"));
111 static void TearDownTestSuite() {
113 "CxxExceptionInConstructorTest::TearDownTestSuite() "
114 "called as expected.\n");
118 ~CxxExceptionInConstructorTest() override {
119 ADD_FAILURE() << "CxxExceptionInConstructorTest destructor "
120 << "called unexpectedly.";
123 void SetUp() override {
124 ADD_FAILURE() << "CxxExceptionInConstructorTest::SetUp() "
125 << "called unexpectedly.";
128 void TearDown() override {
129 ADD_FAILURE() << "CxxExceptionInConstructorTest::TearDown() "
130 << "called unexpectedly.";
134 TEST_F(CxxExceptionInConstructorTest, ThrowsExceptionInConstructor) {
135 ADD_FAILURE() << "CxxExceptionInConstructorTest test body "
136 << "called unexpectedly.";
139 class CxxExceptionInSetUpTestSuiteTest : public Test {
141 CxxExceptionInSetUpTestSuiteTest() {
143 "CxxExceptionInSetUpTestSuiteTest constructor "
144 "called as expected.\n");
147 static void SetUpTestSuite() {
148 throw std::runtime_error("Standard C++ exception");
151 static void TearDownTestSuite() {
153 "CxxExceptionInSetUpTestSuiteTest::TearDownTestSuite() "
154 "called as expected.\n");
158 ~CxxExceptionInSetUpTestSuiteTest() override {
160 "CxxExceptionInSetUpTestSuiteTest destructor "
161 "called as expected.\n");
164 void SetUp() override {
166 "CxxExceptionInSetUpTestSuiteTest::SetUp() "
167 "called as expected.\n");
170 void TearDown() override {
172 "CxxExceptionInSetUpTestSuiteTest::TearDown() "
173 "called as expected.\n");
177 TEST_F(CxxExceptionInSetUpTestSuiteTest, ThrowsExceptionInSetUpTestSuite) {
179 "CxxExceptionInSetUpTestSuiteTest test body "
180 "called as expected.\n");
183 class CxxExceptionInTearDownTestSuiteTest : public Test {
185 static void TearDownTestSuite() {
186 throw std::runtime_error("Standard C++ exception");
190 TEST_F(CxxExceptionInTearDownTestSuiteTest,
191 ThrowsExceptionInTearDownTestSuite) {}
193 class CxxExceptionInSetUpTest : public Test {
195 static void TearDownTestSuite() {
197 "CxxExceptionInSetUpTest::TearDownTestSuite() "
198 "called as expected.\n");
202 ~CxxExceptionInSetUpTest() override {
204 "CxxExceptionInSetUpTest destructor "
205 "called as expected.\n");
208 void SetUp() override { throw std::runtime_error("Standard C++ exception"); }
210 void TearDown() override {
212 "CxxExceptionInSetUpTest::TearDown() "
213 "called as expected.\n");
217 TEST_F(CxxExceptionInSetUpTest, ThrowsExceptionInSetUp) {
218 ADD_FAILURE() << "CxxExceptionInSetUpTest test body "
219 << "called unexpectedly.";
222 class CxxExceptionInTearDownTest : public Test {
224 static void TearDownTestSuite() {
226 "CxxExceptionInTearDownTest::TearDownTestSuite() "
227 "called as expected.\n");
231 ~CxxExceptionInTearDownTest() override {
233 "CxxExceptionInTearDownTest destructor "
234 "called as expected.\n");
237 void TearDown() override {
238 throw std::runtime_error("Standard C++ exception");
242 TEST_F(CxxExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
244 class CxxExceptionInTestBodyTest : public Test {
246 static void TearDownTestSuite() {
248 "CxxExceptionInTestBodyTest::TearDownTestSuite() "
249 "called as expected.\n");
253 ~CxxExceptionInTestBodyTest() override {
255 "CxxExceptionInTestBodyTest destructor "
256 "called as expected.\n");
259 void TearDown() override {
261 "CxxExceptionInTestBodyTest::TearDown() "
262 "called as expected.\n");
266 TEST_F(CxxExceptionInTestBodyTest, ThrowsStdCxxException) {
267 throw std::runtime_error("Standard C++ exception");
270 TEST(CxxExceptionTest, ThrowsNonStdCxxException) { throw "C-string"; }
272 // This terminate handler aborts the program using exit() rather than abort().
273 // This avoids showing pop-ups on Windows systems and core dumps on Unix-like
275 void TerminateHandler() {
276 fprintf(stderr, "%s\n", "Unhandled C++ exception terminating the program.");
281 #endif // GTEST_HAS_EXCEPTIONS
283 int main(int argc, char** argv) {
284 #if GTEST_HAS_EXCEPTIONS
285 std::set_terminate(&TerminateHandler);
287 testing::InitGoogleTest(&argc, argv);
288 return RUN_ALL_TESTS();