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 // Exceptions in destructors are not supported in C++11.
141 #if !defined(__GXX_EXPERIMENTAL_CXX0X__) && __cplusplus < 201103L
142 class CxxExceptionInDestructorTest
: public Test
{
144 static void TearDownTestCase() {
146 "CxxExceptionInDestructorTest::TearDownTestCase() "
147 "called as expected.\n");
151 ~CxxExceptionInDestructorTest() {
152 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
153 throw std::runtime_error("Standard C++ exception"));
157 TEST_F(CxxExceptionInDestructorTest
, ThrowsExceptionInDestructor
) {}
160 class CxxExceptionInSetUpTestCaseTest
: public Test
{
162 CxxExceptionInSetUpTestCaseTest() {
164 "CxxExceptionInSetUpTestCaseTest constructor "
165 "called as expected.\n");
168 static void SetUpTestCase() {
169 throw std::runtime_error("Standard C++ exception");
172 static void TearDownTestCase() {
174 "CxxExceptionInSetUpTestCaseTest::TearDownTestCase() "
175 "called as expected.\n");
179 ~CxxExceptionInSetUpTestCaseTest() {
181 "CxxExceptionInSetUpTestCaseTest destructor "
182 "called as expected.\n");
185 virtual void SetUp() {
187 "CxxExceptionInSetUpTestCaseTest::SetUp() "
188 "called as expected.\n");
191 virtual void TearDown() {
193 "CxxExceptionInSetUpTestCaseTest::TearDown() "
194 "called as expected.\n");
198 TEST_F(CxxExceptionInSetUpTestCaseTest
, ThrowsExceptionInSetUpTestCase
) {
200 "CxxExceptionInSetUpTestCaseTest test body "
201 "called as expected.\n");
204 class CxxExceptionInTearDownTestCaseTest
: public Test
{
206 static void TearDownTestCase() {
207 throw std::runtime_error("Standard C++ exception");
211 TEST_F(CxxExceptionInTearDownTestCaseTest
, ThrowsExceptionInTearDownTestCase
) {}
213 class CxxExceptionInSetUpTest
: public Test
{
215 static void TearDownTestCase() {
217 "CxxExceptionInSetUpTest::TearDownTestCase() "
218 "called as expected.\n");
222 ~CxxExceptionInSetUpTest() {
224 "CxxExceptionInSetUpTest destructor "
225 "called as expected.\n");
228 virtual void SetUp() { throw std::runtime_error("Standard C++ exception"); }
230 virtual void TearDown() {
232 "CxxExceptionInSetUpTest::TearDown() "
233 "called as expected.\n");
237 TEST_F(CxxExceptionInSetUpTest
, ThrowsExceptionInSetUp
) {
238 ADD_FAILURE() << "CxxExceptionInSetUpTest test body "
239 << "called unexpectedly.";
242 class CxxExceptionInTearDownTest
: public Test
{
244 static void TearDownTestCase() {
246 "CxxExceptionInTearDownTest::TearDownTestCase() "
247 "called as expected.\n");
251 ~CxxExceptionInTearDownTest() {
253 "CxxExceptionInTearDownTest destructor "
254 "called as expected.\n");
257 virtual void TearDown() {
258 throw std::runtime_error("Standard C++ exception");
262 TEST_F(CxxExceptionInTearDownTest
, ThrowsExceptionInTearDown
) {}
264 class CxxExceptionInTestBodyTest
: public Test
{
266 static void TearDownTestCase() {
268 "CxxExceptionInTestBodyTest::TearDownTestCase() "
269 "called as expected.\n");
273 ~CxxExceptionInTestBodyTest() {
275 "CxxExceptionInTestBodyTest destructor "
276 "called as expected.\n");
279 virtual void TearDown() {
281 "CxxExceptionInTestBodyTest::TearDown() "
282 "called as expected.\n");
286 TEST_F(CxxExceptionInTestBodyTest
, ThrowsStdCxxException
) {
287 throw std::runtime_error("Standard C++ exception");
290 TEST(CxxExceptionTest
, ThrowsNonStdCxxException
) {
294 // This terminate handler aborts the program using exit() rather than abort().
295 // This avoids showing pop-ups on Windows systems and core dumps on Unix-like
297 void TerminateHandler() {
298 fprintf(stderr
, "%s\n", "Unhandled C++ exception terminating the program.");
303 #endif // GTEST_HAS_EXCEPTIONS
305 int main(int argc
, char** argv
) {
306 #if GTEST_HAS_EXCEPTIONS
307 std::set_terminate(&TerminateHandler
);
309 testing::InitGoogleTest(&argc
, argv
);
310 return RUN_ALL_TESTS();