00001 // Copyright 2010, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00030 // Author: vladl@google.com (Vlad Losev) 00031 // 00032 // Tests that verify interaction of exceptions and death tests. 00033 00034 #include "gtest/gtest-death-test.h" 00035 #include "gtest/gtest.h" 00036 00037 #if GTEST_HAS_DEATH_TEST 00038 00039 # if GTEST_HAS_SEH 00040 # include <windows.h> // For RaiseException(). 00041 # endif 00042 00043 # include "gtest/gtest-spi.h" 00044 00045 # if GTEST_HAS_EXCEPTIONS 00046 00047 # include <exception> // For std::exception. 00048 00049 // Tests that death tests report thrown exceptions as failures and that the 00050 // exceptions do not escape death test macros. 00051 TEST(CxxExceptionDeathTest, ExceptionIsFailure) { 00052 try { 00053 EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(throw 1, ""), "threw an exception"); 00054 } catch (...) { // NOLINT 00055 FAIL() << "An exception escaped a death test macro invocation " 00056 << "with catch_exceptions " 00057 << (testing::GTEST_FLAG(catch_exceptions) ? "enabled" : "disabled"); 00058 } 00059 } 00060 00061 class TestException : public std::exception { 00062 public: 00063 virtual const char* what() const throw() { return "exceptional message"; } 00064 }; 00065 00066 TEST(CxxExceptionDeathTest, PrintsMessageForStdExceptions) { 00067 // Verifies that the exception message is quoted in the failure text. 00068 EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(throw TestException(), ""), 00069 "exceptional message"); 00070 // Verifies that the location is mentioned in the failure text. 00071 EXPECT_NONFATAL_FAILURE(EXPECT_DEATH(throw TestException(), ""), 00072 "gtest-death-test_ex_test.cc"); 00073 } 00074 # endif // GTEST_HAS_EXCEPTIONS 00075 00076 # if GTEST_HAS_SEH 00077 // Tests that enabling interception of SEH exceptions with the 00078 // catch_exceptions flag does not interfere with SEH exceptions being 00079 // treated as death by death tests. 00080 TEST(SehExceptionDeasTest, CatchExceptionsDoesNotInterfere) { 00081 EXPECT_DEATH(RaiseException(42, 0x0, 0, NULL), "") 00082 << "with catch_exceptions " 00083 << (testing::GTEST_FLAG(catch_exceptions) ? "enabled" : "disabled"); 00084 } 00085 # endif 00086 00087 #endif // GTEST_HAS_DEATH_TEST 00088 00089 int main(int argc, char** argv) { 00090 testing::InitGoogleTest(&argc, argv); 00091 testing::GTEST_FLAG(catch_exceptions) = GTEST_ENABLE_CATCH_EXCEPTIONS_ != 0; 00092 return RUN_ALL_TESTS(); 00093 }