11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Copyright 2009, Google Inc. 21be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// All rights reserved. 31be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 41be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Redistribution and use in source and binary forms, with or without 51be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// modification, are permitted provided that the following conditions are 61be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// met: 71be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 81be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Redistributions of source code must retain the above copyright 91be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// notice, this list of conditions and the following disclaimer. 101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Redistributions in binary form must reproduce the above 111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// copyright notice, this list of conditions and the following disclaimer 121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// in the documentation and/or other materials provided with the 131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// distribution. 141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// * Neither the name of Google Inc. nor the names of its 151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// contributors may be used to endorse or promote products derived from 161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// this software without specific prior written permission. 171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Author: wan@google.com (Zhanyong Wan) 311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests Google Test's throw-on-failure mode with exceptions enabled. 331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest.h" 351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <stdlib.h> 371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <stdio.h> 381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <string.h> 391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <stdexcept> 401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Prints the given failure message and exits the program with 421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// non-zero. We use this instead of a Google Test assertion to 431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// indicate a failure, as the latter is been tested and cannot be 441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// relied on. 451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid Fail(const char* msg) { 461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("FAILURE: %s\n", msg); 471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania fflush(stdout); 481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania exit(1); 491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that an assertion failure throws a subclass of 521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// std::runtime_error. 531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid TestFailureThrowsRuntimeError() { 541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::GTEST_FLAG(throw_on_failure) = true; 551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // A successful assertion shouldn't throw. 571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania try { 581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(3, 3); 591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } catch(...) { 601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Fail("A successful assertion wrongfully threw."); 611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // A failed assertion should throw a subclass of std::runtime_error. 641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania try { 651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(2, 3) << "Expected failure"; 661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } catch(const std::runtime_error& e) { 671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (strstr(e.what(), "Expected failure") != NULL) 681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return; 691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("%s", 711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "A failed assertion did throw an exception of the right type, " 721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "but the message is incorrect. Instead of containing \"Expected " 731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "failure\", it is:\n"); 741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Fail(e.what()); 751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } catch(...) { 761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Fail("A failed assertion threw the wrong type of exception."); 771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Fail("A failed assertion should've thrown but didn't."); 791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint main(int argc, char** argv) { 821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::InitGoogleTest(&argc, argv); 831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We want to ensure that people can use Google Test assertions in 851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // other testing frameworks, as long as they initialize Google Test 861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // properly and set the thrown-on-failure mode. Therefore, we don't 871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // use Google Test's constructs for defining and running tests 881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // (e.g. TEST and RUN_ALL_TESTS) here. 891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestFailureThrowsRuntimeError(); 911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return 0; 921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 93