gtest_catch_exceptions_test_.cc
Go to the documentation of this file.
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 for Google Test itself. Tests in this file throw C++ or SEH
00033 // exceptions, and the output is verified by gtest_catch_exceptions_test.py.
00034 
00035 #include "gtest/gtest.h"
00036 
00037 #include <stdio.h>  // NOLINT
00038 #include <stdlib.h>  // For exit().
00039 
00040 #if GTEST_HAS_SEH
00041 # include <windows.h>
00042 #endif
00043 
00044 #if GTEST_HAS_EXCEPTIONS
00045 # include <exception>  // For set_terminate().
00046 # include <stdexcept>
00047 #endif
00048 
00049 using testing::Test;
00050 
00051 #if GTEST_HAS_SEH
00052 
00053 class SehExceptionInConstructorTest : public Test {
00054  public:
00055   SehExceptionInConstructorTest() { RaiseException(42, 0, 0, NULL); }
00056 };
00057 
00058 TEST_F(SehExceptionInConstructorTest, ThrowsExceptionInConstructor) {}
00059 
00060 class SehExceptionInDestructorTest : public Test {
00061  public:
00062   ~SehExceptionInDestructorTest() { RaiseException(42, 0, 0, NULL); }
00063 };
00064 
00065 TEST_F(SehExceptionInDestructorTest, ThrowsExceptionInDestructor) {}
00066 
00067 class SehExceptionInSetUpTestCaseTest : public Test {
00068  public:
00069   static void SetUpTestCase() { RaiseException(42, 0, 0, NULL); }
00070 };
00071 
00072 TEST_F(SehExceptionInSetUpTestCaseTest, ThrowsExceptionInSetUpTestCase) {}
00073 
00074 class SehExceptionInTearDownTestCaseTest : public Test {
00075  public:
00076   static void TearDownTestCase() { RaiseException(42, 0, 0, NULL); }
00077 };
00078 
00079 TEST_F(SehExceptionInTearDownTestCaseTest, ThrowsExceptionInTearDownTestCase) {}
00080 
00081 class SehExceptionInSetUpTest : public Test {
00082  protected:
00083   virtual void SetUp() { RaiseException(42, 0, 0, NULL); }
00084 };
00085 
00086 TEST_F(SehExceptionInSetUpTest, ThrowsExceptionInSetUp) {}
00087 
00088 class SehExceptionInTearDownTest : public Test {
00089  protected:
00090   virtual void TearDown() { RaiseException(42, 0, 0, NULL); }
00091 };
00092 
00093 TEST_F(SehExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
00094 
00095 TEST(SehExceptionTest, ThrowsSehException) {
00096   RaiseException(42, 0, 0, NULL);
00097 }
00098 
00099 #endif  // GTEST_HAS_SEH
00100 
00101 #if GTEST_HAS_EXCEPTIONS
00102 
00103 class CxxExceptionInConstructorTest : public Test {
00104  public:
00105   CxxExceptionInConstructorTest() {
00106     // Without this macro VC++ complains about unreachable code at the end of
00107     // the constructor.
00108     GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
00109         throw std::runtime_error("Standard C++ exception"));
00110   }
00111 
00112   static void TearDownTestCase() {
00113     printf("%s",
00114            "CxxExceptionInConstructorTest::TearDownTestCase() "
00115            "called as expected.\n");
00116   }
00117 
00118  protected:
00119   ~CxxExceptionInConstructorTest() {
00120     ADD_FAILURE() << "CxxExceptionInConstructorTest destructor "
00121                   << "called unexpectedly.";
00122   }
00123 
00124   virtual void SetUp() {
00125     ADD_FAILURE() << "CxxExceptionInConstructorTest::SetUp() "
00126                   << "called unexpectedly.";
00127   }
00128 
00129   virtual void TearDown() {
00130     ADD_FAILURE() << "CxxExceptionInConstructorTest::TearDown() "
00131                   << "called unexpectedly.";
00132   }
00133 };
00134 
00135 TEST_F(CxxExceptionInConstructorTest, ThrowsExceptionInConstructor) {
00136   ADD_FAILURE() << "CxxExceptionInConstructorTest test body "
00137                 << "called unexpectedly.";
00138 }
00139 
00140 // Exceptions in destructors are not supported in C++11.
00141 #if !defined(__GXX_EXPERIMENTAL_CXX0X__) &&  __cplusplus < 201103L
00142 class CxxExceptionInDestructorTest : public Test {
00143  public:
00144   static void TearDownTestCase() {
00145     printf("%s",
00146            "CxxExceptionInDestructorTest::TearDownTestCase() "
00147            "called as expected.\n");
00148   }
00149 
00150  protected:
00151   ~CxxExceptionInDestructorTest() {
00152     GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_(
00153         throw std::runtime_error("Standard C++ exception"));
00154   }
00155 };
00156 
00157 TEST_F(CxxExceptionInDestructorTest, ThrowsExceptionInDestructor) {}
00158 #endif  // C++11 mode
00159 
00160 class CxxExceptionInSetUpTestCaseTest : public Test {
00161  public:
00162   CxxExceptionInSetUpTestCaseTest() {
00163     printf("%s",
00164            "CxxExceptionInSetUpTestCaseTest constructor "
00165            "called as expected.\n");
00166   }
00167 
00168   static void SetUpTestCase() {
00169     throw std::runtime_error("Standard C++ exception");
00170   }
00171 
00172   static void TearDownTestCase() {
00173     printf("%s",
00174            "CxxExceptionInSetUpTestCaseTest::TearDownTestCase() "
00175            "called as expected.\n");
00176   }
00177 
00178  protected:
00179   ~CxxExceptionInSetUpTestCaseTest() {
00180     printf("%s",
00181            "CxxExceptionInSetUpTestCaseTest destructor "
00182            "called as expected.\n");
00183   }
00184 
00185   virtual void SetUp() {
00186     printf("%s",
00187            "CxxExceptionInSetUpTestCaseTest::SetUp() "
00188            "called as expected.\n");
00189   }
00190 
00191   virtual void TearDown() {
00192     printf("%s",
00193            "CxxExceptionInSetUpTestCaseTest::TearDown() "
00194            "called as expected.\n");
00195   }
00196 };
00197 
00198 TEST_F(CxxExceptionInSetUpTestCaseTest, ThrowsExceptionInSetUpTestCase) {
00199   printf("%s",
00200          "CxxExceptionInSetUpTestCaseTest test body "
00201          "called as expected.\n");
00202 }
00203 
00204 class CxxExceptionInTearDownTestCaseTest : public Test {
00205  public:
00206   static void TearDownTestCase() {
00207     throw std::runtime_error("Standard C++ exception");
00208   }
00209 };
00210 
00211 TEST_F(CxxExceptionInTearDownTestCaseTest, ThrowsExceptionInTearDownTestCase) {}
00212 
00213 class CxxExceptionInSetUpTest : public Test {
00214  public:
00215   static void TearDownTestCase() {
00216     printf("%s",
00217            "CxxExceptionInSetUpTest::TearDownTestCase() "
00218            "called as expected.\n");
00219   }
00220 
00221  protected:
00222   ~CxxExceptionInSetUpTest() {
00223     printf("%s",
00224            "CxxExceptionInSetUpTest destructor "
00225            "called as expected.\n");
00226   }
00227 
00228   virtual void SetUp() { throw std::runtime_error("Standard C++ exception"); }
00229 
00230   virtual void TearDown() {
00231     printf("%s",
00232            "CxxExceptionInSetUpTest::TearDown() "
00233            "called as expected.\n");
00234   }
00235 };
00236 
00237 TEST_F(CxxExceptionInSetUpTest, ThrowsExceptionInSetUp) {
00238   ADD_FAILURE() << "CxxExceptionInSetUpTest test body "
00239                 << "called unexpectedly.";
00240 }
00241 
00242 class CxxExceptionInTearDownTest : public Test {
00243  public:
00244   static void TearDownTestCase() {
00245     printf("%s",
00246            "CxxExceptionInTearDownTest::TearDownTestCase() "
00247            "called as expected.\n");
00248   }
00249 
00250  protected:
00251   ~CxxExceptionInTearDownTest() {
00252     printf("%s",
00253            "CxxExceptionInTearDownTest destructor "
00254            "called as expected.\n");
00255   }
00256 
00257   virtual void TearDown() {
00258     throw std::runtime_error("Standard C++ exception");
00259   }
00260 };
00261 
00262 TEST_F(CxxExceptionInTearDownTest, ThrowsExceptionInTearDown) {}
00263 
00264 class CxxExceptionInTestBodyTest : public Test {
00265  public:
00266   static void TearDownTestCase() {
00267     printf("%s",
00268            "CxxExceptionInTestBodyTest::TearDownTestCase() "
00269            "called as expected.\n");
00270   }
00271 
00272  protected:
00273   ~CxxExceptionInTestBodyTest() {
00274     printf("%s",
00275            "CxxExceptionInTestBodyTest destructor "
00276            "called as expected.\n");
00277   }
00278 
00279   virtual void TearDown() {
00280     printf("%s",
00281            "CxxExceptionInTestBodyTest::TearDown() "
00282            "called as expected.\n");
00283   }
00284 };
00285 
00286 TEST_F(CxxExceptionInTestBodyTest, ThrowsStdCxxException) {
00287   throw std::runtime_error("Standard C++ exception");
00288 }
00289 
00290 TEST(CxxExceptionTest, ThrowsNonStdCxxException) {
00291   throw "C-string";
00292 }
00293 
00294 // This terminate handler aborts the program using exit() rather than abort().
00295 // This avoids showing pop-ups on Windows systems and core dumps on Unix-like
00296 // ones.
00297 void TerminateHandler() {
00298   fprintf(stderr, "%s\n", "Unhandled C++ exception terminating the program.");
00299   fflush(NULL);
00300   exit(3);
00301 }
00302 
00303 #endif  // GTEST_HAS_EXCEPTIONS
00304 
00305 int main(int argc, char** argv) {
00306 #if GTEST_HAS_EXCEPTIONS
00307   std::set_terminate(&TerminateHandler);
00308 #endif
00309   testing::InitGoogleTest(&argc, argv);
00310   return RUN_ALL_TESTS();
00311 }


rc_visard_driver
Author(s): Heiko Hirschmueller , Christian Emmerich , Felix Ruess
autogenerated on Thu Jun 6 2019 20:43:04