00001 // Copyright 2009, 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: wan@google.com (Zhanyong Wan) 00031 00032 // Tests Google Test's throw-on-failure mode with exceptions enabled. 00033 00034 #include "gtest/gtest.h" 00035 00036 #include <stdlib.h> 00037 #include <stdio.h> 00038 #include <string.h> 00039 #include <stdexcept> 00040 00041 // Prints the given failure message and exits the program with 00042 // non-zero. We use this instead of a Google Test assertion to 00043 // indicate a failure, as the latter is been tested and cannot be 00044 // relied on. 00045 void Fail(const char* msg) { 00046 printf("FAILURE: %s\n", msg); 00047 fflush(stdout); 00048 exit(1); 00049 } 00050 00051 // Tests that an assertion failure throws a subclass of 00052 // std::runtime_error. 00053 void TestFailureThrowsRuntimeError() { 00054 testing::GTEST_FLAG(throw_on_failure) = true; 00055 00056 // A successful assertion shouldn't throw. 00057 try { 00058 EXPECT_EQ(3, 3); 00059 } catch(...) { 00060 Fail("A successful assertion wrongfully threw."); 00061 } 00062 00063 // A failed assertion should throw a subclass of std::runtime_error. 00064 try { 00065 EXPECT_EQ(2, 3) << "Expected failure"; 00066 } catch(const std::runtime_error& e) { 00067 if (strstr(e.what(), "Expected failure") != NULL) 00068 return; 00069 00070 printf("%s", 00071 "A failed assertion did throw an exception of the right type, " 00072 "but the message is incorrect. Instead of containing \"Expected " 00073 "failure\", it is:\n"); 00074 Fail(e.what()); 00075 } catch(...) { 00076 Fail("A failed assertion threw the wrong type of exception."); 00077 } 00078 Fail("A failed assertion should've thrown but didn't."); 00079 } 00080 00081 int main(int argc, char** argv) { 00082 testing::InitGoogleTest(&argc, argv); 00083 00084 // We want to ensure that people can use Google Test assertions in 00085 // other testing frameworks, as long as they initialize Google Test 00086 // properly and set the thrown-on-failure mode. Therefore, we don't 00087 // use Google Test's constructs for defining and running tests 00088 // (e.g. TEST and RUN_ALL_TESTS) here. 00089 00090 TestFailureThrowsRuntimeError(); 00091 return 0; 00092 }