1 // Copyright 2010, Google Inc. 2 // All rights reserved. 3 // 4 // Redistribution and use in source and binary forms, with or without 5 // modification, are permitted provided that the following conditions are 6 // met: 7 // 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 13 // distribution. 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. 17 // 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. 29 // 30 // Author: vladl (at) google.com (Vlad Losev) 31 // 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. 34 35 #include "gtest/gtest.h" 36 37 #include <stdio.h> // NOLINT 38 #include <stdlib.h> // For exit(). 39 40 #if GTEST_HAS_SEH 41 # include <windows.h> 42 #endif 43 44 #if GTEST_HAS_EXCEPTIONS 45 # include <exception> // For set_terminate(). 46 # include <stdexcept> 47 #endif 48 49 using testing::Test; 50 51 #if GTEST_HAS_SEH 52 53 class SehExceptionInConstructorTest : public Test { 54 public: 55 SehExceptionInConstructorTest() { RaiseException(42, 0, 0, NULL); } 56 }; 57 58 TEST_F(SehExceptionInConstructorTest, ThrowsExceptionInConstructor) {} 59 60 class SehExceptionInDestructorTest : public Test { 61 public: 62 ~SehExceptionInDestructorTest() { RaiseException(42, 0, 0, NULL); } 63 }; 64 65 TEST_F(SehExceptionInDestructorTest, ThrowsExceptionInDestructor) {} 66 67 class SehExceptionInSetUpTestCaseTest : public Test { 68 public: 69 static void SetUpTestCase() { RaiseException(42, 0, 0, NULL); } 70 }; 71 72 TEST_F(SehExceptionInSetUpTestCaseTest, ThrowsExceptionInSetUpTestCase) {} 73 74 class SehExceptionInTearDownTestCaseTest : public Test { 75 public: 76 static void TearDownTestCase() { RaiseException(42, 0, 0, NULL); } 77 }; 78 79 TEST_F(SehExceptionInTearDownTestCaseTest, ThrowsExceptionInTearDownTestCase) {} 80 81 class SehExceptionInSetUpTest : public Test { 82 protected: 83 virtual void SetUp() { RaiseException(42, 0, 0, NULL); } 84 }; 85 86 TEST_F(SehExceptionInSetUpTest, ThrowsExceptionInSetUp) {} 87 88 class SehExceptionInTearDownTest : public Test { 89 protected: 90 virtual void TearDown() { RaiseException(42, 0, 0, NULL); } 91 }; 92 93 TEST_F(SehExceptionInTearDownTest, ThrowsExceptionInTearDown) {} 94 95 TEST(SehExceptionTest, ThrowsSehException) { 96 RaiseException(42, 0, 0, NULL); 97 } 98 99 #endif // GTEST_HAS_SEH 100 101 #if GTEST_HAS_EXCEPTIONS 102 103 class CxxExceptionInConstructorTest : public Test { 104 public: 105 CxxExceptionInConstructorTest() { 106 // Without this macro VC++ complains about unreachable code at the end of 107 // the constructor. 108 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_( 109 throw std::runtime_error("Standard C++ exception")); 110 } 111 112 static void TearDownTestCase() { 113 printf("%s", 114 "CxxExceptionInConstructorTest::TearDownTestCase() " 115 "called as expected.\n"); 116 } 117 118 protected: 119 ~CxxExceptionInConstructorTest() { 120 ADD_FAILURE() << "CxxExceptionInConstructorTest destructor " 121 << "called unexpectedly."; 122 } 123 124 virtual void SetUp() { 125 ADD_FAILURE() << "CxxExceptionInConstructorTest::SetUp() " 126 << "called unexpectedly."; 127 } 128 129 virtual void TearDown() { 130 ADD_FAILURE() << "CxxExceptionInConstructorTest::TearDown() " 131 << "called unexpectedly."; 132 } 133 }; 134 135 TEST_F(CxxExceptionInConstructorTest, ThrowsExceptionInConstructor) { 136 ADD_FAILURE() << "CxxExceptionInConstructorTest test body " 137 << "called unexpectedly."; 138 } 139 140 class CxxExceptionInDestructorTest : public Test { 141 public: 142 static void TearDownTestCase() { 143 printf("%s", 144 "CxxExceptionInDestructorTest::TearDownTestCase() " 145 "called as expected.\n"); 146 } 147 148 protected: 149 ~CxxExceptionInDestructorTest() { 150 GTEST_SUPPRESS_UNREACHABLE_CODE_WARNING_BELOW_( 151 throw std::runtime_error("Standard C++ exception")); 152 } 153 }; 154 155 TEST_F(CxxExceptionInDestructorTest, ThrowsExceptionInDestructor) {} 156 157 class CxxExceptionInSetUpTestCaseTest : public Test { 158 public: 159 CxxExceptionInSetUpTestCaseTest() { 160 printf("%s", 161 "CxxExceptionInSetUpTestCaseTest constructor " 162 "called as expected.\n"); 163 } 164 165 static void SetUpTestCase() { 166 throw std::runtime_error("Standard C++ exception"); 167 } 168 169 static void TearDownTestCase() { 170 printf("%s", 171 "CxxExceptionInSetUpTestCaseTest::TearDownTestCase() " 172 "called as expected.\n"); 173 } 174 175 protected: 176 ~CxxExceptionInSetUpTestCaseTest() { 177 printf("%s", 178 "CxxExceptionInSetUpTestCaseTest destructor " 179 "called as expected.\n"); 180 } 181 182 virtual void SetUp() { 183 printf("%s", 184 "CxxExceptionInSetUpTestCaseTest::SetUp() " 185 "called as expected.\n"); 186 } 187 188 virtual void TearDown() { 189 printf("%s", 190 "CxxExceptionInSetUpTestCaseTest::TearDown() " 191 "called as expected.\n"); 192 } 193 }; 194 195 TEST_F(CxxExceptionInSetUpTestCaseTest, ThrowsExceptionInSetUpTestCase) { 196 printf("%s", 197 "CxxExceptionInSetUpTestCaseTest test body " 198 "called as expected.\n"); 199 } 200 201 class CxxExceptionInTearDownTestCaseTest : public Test { 202 public: 203 static void TearDownTestCase() { 204 throw std::runtime_error("Standard C++ exception"); 205 } 206 }; 207 208 TEST_F(CxxExceptionInTearDownTestCaseTest, ThrowsExceptionInTearDownTestCase) {} 209 210 class CxxExceptionInSetUpTest : public Test { 211 public: 212 static void TearDownTestCase() { 213 printf("%s", 214 "CxxExceptionInSetUpTest::TearDownTestCase() " 215 "called as expected.\n"); 216 } 217 218 protected: 219 ~CxxExceptionInSetUpTest() { 220 printf("%s", 221 "CxxExceptionInSetUpTest destructor " 222 "called as expected.\n"); 223 } 224 225 virtual void SetUp() { throw std::runtime_error("Standard C++ exception"); } 226 227 virtual void TearDown() { 228 printf("%s", 229 "CxxExceptionInSetUpTest::TearDown() " 230 "called as expected.\n"); 231 } 232 }; 233 234 TEST_F(CxxExceptionInSetUpTest, ThrowsExceptionInSetUp) { 235 ADD_FAILURE() << "CxxExceptionInSetUpTest test body " 236 << "called unexpectedly."; 237 } 238 239 class CxxExceptionInTearDownTest : public Test { 240 public: 241 static void TearDownTestCase() { 242 printf("%s", 243 "CxxExceptionInTearDownTest::TearDownTestCase() " 244 "called as expected.\n"); 245 } 246 247 protected: 248 ~CxxExceptionInTearDownTest() { 249 printf("%s", 250 "CxxExceptionInTearDownTest destructor " 251 "called as expected.\n"); 252 } 253 254 virtual void TearDown() { 255 throw std::runtime_error("Standard C++ exception"); 256 } 257 }; 258 259 TEST_F(CxxExceptionInTearDownTest, ThrowsExceptionInTearDown) {} 260 261 class CxxExceptionInTestBodyTest : public Test { 262 public: 263 static void TearDownTestCase() { 264 printf("%s", 265 "CxxExceptionInTestBodyTest::TearDownTestCase() " 266 "called as expected.\n"); 267 } 268 269 protected: 270 ~CxxExceptionInTestBodyTest() { 271 printf("%s", 272 "CxxExceptionInTestBodyTest destructor " 273 "called as expected.\n"); 274 } 275 276 virtual void TearDown() { 277 printf("%s", 278 "CxxExceptionInTestBodyTest::TearDown() " 279 "called as expected.\n"); 280 } 281 }; 282 283 TEST_F(CxxExceptionInTestBodyTest, ThrowsStdCxxException) { 284 throw std::runtime_error("Standard C++ exception"); 285 } 286 287 TEST(CxxExceptionTest, ThrowsNonStdCxxException) { 288 throw "C-string"; 289 } 290 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 293 // ones. 294 void TerminateHandler() { 295 fprintf(stderr, "%s\n", "Unhandled C++ exception terminating the program."); 296 fflush(NULL); 297 exit(3); 298 } 299 300 #endif // GTEST_HAS_EXCEPTIONS 301 302 int main(int argc, char** argv) { 303 #if GTEST_HAS_EXCEPTIONS 304 std::set_terminate(&TerminateHandler); 305 #endif 306 testing::InitGoogleTest(&argc, argv); 307 return RUN_ALL_TESTS(); 308 } 309