11be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Copyright 2005, 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// 30fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// The purpose of this file is to generate Google Test output under 31fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// various conditions. The output will then be verified by 32fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// gtest_output_test.py to ensure that Google Test generates the 33fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// desired messages. Therefore, most tests in this file are MEANT TO 34fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes// FAIL. 351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Author: wan@google.com (Zhanyong Wan) 371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest-spi.h" 3941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#include "gtest/gtest.h" 401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Indicates that this translation unit is part of Google Test's 421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// implementation. It must come before gtest-internal-inl.h is 431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// included, or there will be a compiler error. This trick is to 441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// prevent a user from accidentally including gtest-internal-inl.h in 451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// his code. 461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#define GTEST_IMPLEMENTATION_ 1 471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include "src/gtest-internal-inl.h" 481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#undef GTEST_IMPLEMENTATION_ 491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#include <stdlib.h> 511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_IS_THREADSAFE 531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing testing::ScopedFakeTestPartResultReporter; 541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniausing testing::TestPartResultArray; 551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::Notification; 5741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::ThreadWithParam; 5841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif 5941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 6041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotnamespace posix = ::testing::internal::posix; 6141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotusing testing::internal::scoped_ptr; 621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests catching fatal failures. 641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A subroutine used by the following test. 661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid TestEq1(int x) { 671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_EQ(1, x); 681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This function calls a test subroutine, catches the fatal failure it 711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// generates, and then returns early. 721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid TryTestSubroutine() { 731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Calls a subrountine that yields a fatal failure. 741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestEq1(2); 751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Catches the fatal failure and aborts the test. 771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The testing::Test:: prefix is necessary when calling 791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // HasFatalFailure() outside of a TEST, TEST_F, or test fixture. 801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (testing::Test::HasFatalFailure()) return; 811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // If we get here, something is wrong. 831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FAIL() << "This should never be reached."; 841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 861be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(PassingTest, PassingTest1) { 871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 891be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(PassingTest, PassingTest2) { 901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that parameters of failing parameterized tests are printed in the 9341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// failing test summary. 9441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass FailingParamTest : public testing::TestWithParam<int> {}; 9541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 9641d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST_P(FailingParamTest, Fails) { 9741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_EQ(1, GetParam()); 9841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 9941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 10041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This generates a test which will fail. Google Test is expected to print 10141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// its parameter when it outputs the list of all failed tests. 10241d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotINSTANTIATE_TEST_CASE_P(PrintingFailingParams, 10341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot FailingParamTest, 10441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot testing::Values(2)); 10541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 106fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughesstatic const char kGoldenString[] = "\"Line\0 1\"\nLine 2"; 107fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 108fc2de66453b0669c09eaca643b07d34443858b6fElliott HughesTEST(NonfatalFailureTest, EscapesStringOperands) { 109fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes std::string actual = "actual \"string\""; 110fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EQ(kGoldenString, actual); 111fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 112fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes const char* golden = kGoldenString; 113fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes EXPECT_EQ(golden, actual); 114fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes} 115fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 1161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests catching a fatal failure in a subroutine. 1171be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(FatalFailureTest, FatalFailureInSubroutine) { 1181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting a failure that x should be 1)\n"); 1191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TryTestSubroutine(); 1211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests catching a fatal failure in a nested subroutine. 1241be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(FatalFailureTest, FatalFailureInNestedSubroutine) { 1251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting a failure that x should be 1)\n"); 1261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Calls a subrountine that yields a fatal failure. 1281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TryTestSubroutine(); 1291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Catches the fatal failure and aborts the test. 1311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // 1321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // When calling HasFatalFailure() inside a TEST, TEST_F, or test 1331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // fixture, the testing::Test:: prefix is not needed. 1341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (HasFatalFailure()) return; 1351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // If we get here, something is wrong. 1371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FAIL() << "This should never be reached."; 1381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests HasFatalFailure() after a failed EXPECT check. 1411be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(FatalFailureTest, NonfatalFailureInSubroutine) { 1421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting a failure on false)\n"); 1431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(false); // Generates a nonfatal failure 1441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_FALSE(HasFatalFailure()); // This should succeed. 1451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests interleaving user logging and Google Test assertions. 1481be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(LoggingTest, InterleavingLoggingAndAssertions) { 1491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static const int a[4] = { 1501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3, 9, 2, 6 1511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }; 1521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 2 failures on (3) >= (a[i]))\n"); 1541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania for (int i = 0; i < static_cast<int>(sizeof(a)/sizeof(*a)); i++) { 1551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("i == %d\n", i); 1561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_GE(3, a[i]); 1571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 1581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests the SCOPED_TRACE macro. 1611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A helper function for testing SCOPED_TRACE. 1631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid SubWithoutTrace(int n) { 1641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(1, n); 1651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_EQ(2, n); 1661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Another helper function for testing SCOPED_TRACE. 1691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid SubWithTrace(int n) { 1701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SCOPED_TRACE(testing::Message() << "n = " << n); 1711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SubWithoutTrace(n); 1731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that SCOPED_TRACE() obeys lexical scopes. 1761be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(SCOPED_TRACETest, ObeysScopes) { 1771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expected to fail)\n"); 1781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // There should be no trace before SCOPED_TRACE() is invoked. 1801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "This failure is expected, and shouldn't have a trace."; 1811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania { 1831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SCOPED_TRACE("Expected trace"); 1841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // After SCOPED_TRACE(), a failure in the current scope should contain 1851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the trace. 1861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "This failure is expected, and should have a trace."; 1871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 1881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Once the control leaves the scope of the SCOPED_TRACE(), there 1901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // should be no trace again. 1911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "This failure is expected, and shouldn't have a trace."; 1921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that SCOPED_TRACE works inside a loop. 1951be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(SCOPED_TRACETest, WorksInLoop) { 1961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expected to fail)\n"); 1971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 1981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania for (int i = 1; i <= 2; i++) { 1991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SCOPED_TRACE(testing::Message() << "i = " << i); 2001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SubWithoutTrace(i); 2021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 2031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 2041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that SCOPED_TRACE works in a subroutine. 2061be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(SCOPED_TRACETest, WorksInSubroutine) { 2071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expected to fail)\n"); 2081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SubWithTrace(1); 2101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SubWithTrace(2); 2111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 2121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that SCOPED_TRACE can be nested. 2141be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(SCOPED_TRACETest, CanBeNested) { 2151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expected to fail)\n"); 2161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SCOPED_TRACE(""); // A trace without a message. 2181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SubWithTrace(2); 2201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 2211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that multiple SCOPED_TRACEs can be used in the same scope. 2231be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(SCOPED_TRACETest, CanBeRepeated) { 2241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expected to fail)\n"); 2251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SCOPED_TRACE("A"); 2271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() 2281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "This failure is expected, and should contain trace point A."; 2291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SCOPED_TRACE("B"); 2311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() 2321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "This failure is expected, and should contain trace point A and B."; 2331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania { 2351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SCOPED_TRACE("C"); 236fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes ADD_FAILURE() << "This failure is expected, and should " 237fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes << "contain trace point A, B, and C."; 2381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 2391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 2401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania SCOPED_TRACE("D"); 241fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes ADD_FAILURE() << "This failure is expected, and should " 242fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes << "contain trace point A, B, and D."; 2431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 2441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 24541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_IS_THREADSAFE 24641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests that SCOPED_TRACE()s can be used concurrently from multiple 24741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// threads. Namely, an assertion should be affected by 24841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// SCOPED_TRACE()s in its own thread only. 24941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 25041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Here's the sequence of actions that happen in the test: 25141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// 25241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Thread A (main) | Thread B (spawned) 25341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// ===============================|================================ 25441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// spawns thread B | 25541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// -------------------------------+-------------------------------- 25641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// waits for n1 | SCOPED_TRACE("Trace B"); 25741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// | generates failure #1 25841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// | notifies n1 25941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// -------------------------------+-------------------------------- 26041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// SCOPED_TRACE("Trace A"); | waits for n2 26141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// generates failure #2 | 26241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// notifies n2 | 26341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// -------------------------------|-------------------------------- 26441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// waits for n3 | generates failure #3 26541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// | trace B dies 26641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// | generates failure #4 26741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// | notifies n3 26841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// -------------------------------|-------------------------------- 26941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// generates failure #5 | finishes 27041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// trace A dies | 27141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// generates failure #6 | 27241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// -------------------------------|-------------------------------- 27341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// waits for thread B to finish | 27441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 27541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct CheckPoints { 27641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Notification n1; 27741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Notification n2; 27841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Notification n3; 27941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot}; 28041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 28141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic void ThreadWithScopedTrace(CheckPoints* check_points) { 28241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot { 28341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot SCOPED_TRACE("Trace B"); 28441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ADD_FAILURE() 28541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Expected failure #1 (in thread B, only trace B alive)."; 28641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot check_points->n1.Notify(); 28741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot check_points->n2.WaitForNotification(); 28841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 28941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ADD_FAILURE() 29041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Expected failure #3 (in thread B, trace A & B both alive)."; 29141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } // Trace B dies here. 29241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ADD_FAILURE() 29341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Expected failure #4 (in thread B, only trace A alive)."; 29441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot check_points->n3.Notify(); 29541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 29641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 29741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(SCOPED_TRACETest, WorksConcurrently) { 29841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot printf("(expecting 6 failures)\n"); 29941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 30041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot CheckPoints check_points; 30141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace, 30241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot &check_points, 30341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot NULL); 30441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot check_points.n1.WaitForNotification(); 30541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 30641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot { 30741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot SCOPED_TRACE("Trace A"); 30841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ADD_FAILURE() 30941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Expected failure #2 (in thread A, trace A & B both alive)."; 31041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot check_points.n2.Notify(); 31141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot check_points.n3.WaitForNotification(); 31241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 31341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ADD_FAILURE() 31441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Expected failure #5 (in thread A, only trace A alive)."; 31541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } // Trace A dies here. 31641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ADD_FAILURE() 31741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot << "Expected failure #6 (in thread A, no trace alive)."; 31841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot thread.Join(); 31941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot} 32041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_IS_THREADSAFE 32141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 3221be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(DisabledTestsWarningTest, 3231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) { 3241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // This test body is intentionally empty. Its sole purpose is for 3251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // verifying that the --gtest_also_run_disabled_tests flag 3261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of 3271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // the test output. 3281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests using assertions outside of TEST and TEST_F. 3311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 3321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This function creates two failures intentionally. 3331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid AdHocTest() { 3341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("The non-test part of the code is expected to have 2 failures.\n\n"); 3351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_TRUE(false); 3361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(2, 3); 3371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Runs all TESTs, all TEST_Fs, and the ad hoc test. 3401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint RunAllTests() { 3411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania AdHocTest(); 3421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return RUN_ALL_TESTS(); 3431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests non-fatal failures in the fixture constructor. 3461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass NonFatalFailureInFixtureConstructorTest : public testing::Test { 3471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania protected: 3481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania NonFatalFailureInFixtureConstructorTest() { 3491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 5 failures)\n"); 3501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor."; 3511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 3521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ~NonFatalFailureInFixtureConstructorTest() { 3541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor."; 3551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 3561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void SetUp() { 3581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "Expected failure #2, in SetUp()."; 3591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 3601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void TearDown() { 3621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "Expected failure #4, in TearDown."; 3631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 3641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 3651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3661be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor) { 3671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "Expected failure #3, in the test body."; 3681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 3691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests fatal failures in the fixture constructor. 3711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass FatalFailureInFixtureConstructorTest : public testing::Test { 3721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania protected: 3731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FatalFailureInFixtureConstructorTest() { 3741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 2 failures)\n"); 3751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Init(); 3761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 3771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ~FatalFailureInFixtureConstructorTest() { 3791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor."; 3801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 3811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void SetUp() { 3831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "UNEXPECTED failure in SetUp(). " 3841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "We should never get here, as the test fixture c'tor " 3851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "had a fatal failure."; 3861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 3871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 3881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void TearDown() { 3891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "UNEXPECTED failure in TearDown(). " 3901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "We should never get here, as the test fixture c'tor " 3911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "had a fatal failure."; 3921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 393fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes 3941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 3951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void Init() { 3961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FAIL() << "Expected failure #1, in the test fixture c'tor."; 3971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 3981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 3991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4001be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) { 4011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "UNEXPECTED failure in the test body. " 4021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "We should never get here, as the test fixture c'tor " 4031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "had a fatal failure."; 4041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests non-fatal failures in SetUp(). 4071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass NonFatalFailureInSetUpTest : public testing::Test { 4081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania protected: 4091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual ~NonFatalFailureInSetUpTest() { 4101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Deinit(); 4111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void SetUp() { 4141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 4 failures)\n"); 4151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "Expected failure #1, in SetUp()."; 4161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void TearDown() { 4191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FAIL() << "Expected failure #3, in TearDown()."; 4201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 4221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void Deinit() { 4231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FAIL() << "Expected failure #4, in the test fixture d'tor."; 4241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 4261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4271be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) { 4281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FAIL() << "Expected failure #2, in the test function."; 4291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests fatal failures in SetUp(). 4321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass FatalFailureInSetUpTest : public testing::Test { 4331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania protected: 4341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual ~FatalFailureInSetUpTest() { 4351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania Deinit(); 4361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void SetUp() { 4391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 3 failures)\n"); 4401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FAIL() << "Expected failure #1, in SetUp()."; 4411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void TearDown() { 4441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FAIL() << "Expected failure #2, in TearDown()."; 4451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 4471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania void Deinit() { 4481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FAIL() << "Expected failure #3, in the test fixture d'tor."; 4491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 4501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 4511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4521be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(FatalFailureInSetUpTest, FailureInSetUp) { 4531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FAIL() << "UNEXPECTED failure in the test function. " 4541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania << "We should never get here, as SetUp() failed."; 4551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 45741d0579e8de9ef4ff178fc4991043c61a19943f7Brett ChabotTEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) { 45841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ADD_FAILURE_AT("foo.cc", 42) << "Expected failure in foo.cc"; 4591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_IS_THREADSAFE 4621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// A unary function that may die. 46441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotvoid DieIf(bool should_die) { 46541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_CHECK_(!should_die) << " - death inside DieIf()."; 4661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 46841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Tests running death tests in a multi-threaded context. 4691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 47041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// Used for coordination between the main and the spawn thread. 47141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstruct SpawnThreadNotifications { 47241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot SpawnThreadNotifications() {} 4731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 47441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Notification spawn_thread_started; 47541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot Notification spawn_thread_ok_to_terminate; 4761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 4771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 47841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot GTEST_DISALLOW_COPY_AND_ASSIGN_(SpawnThreadNotifications); 4791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 4801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// The function to be executed in the thread spawn by the 48241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// MultipleThreads test (below). 48341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotstatic void ThreadRoutine(SpawnThreadNotifications* notifications) { 48441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Signals the main thread that this thread has started. 48541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot notifications->spawn_thread_started.Notify(); 4861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 48741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Waits for permission to finish from the main thread. 48841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot notifications->spawn_thread_ok_to_terminate.WaitForNotification(); 4891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 4901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 49141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// This is a death-test test, but it's not named with a DeathTest 49241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// suffix. It starts threads which might interfere with later 49341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot// death tests, so it must run after all other death tests. 49441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotclass DeathTestAndMultiThreadsTest : public testing::Test { 4951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania protected: 49641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Starts a thread and waits for it to begin. 49741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot virtual void SetUp() { 49841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot thread_.reset(new ThreadWithParam<SpawnThreadNotifications*>( 49941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot &ThreadRoutine, ¬ifications_, NULL)); 50041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot notifications_.spawn_thread_started.WaitForNotification(); 50141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot } 50241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Tells the thread to finish, and reaps it. 50341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // Depending on the version of the thread library in use, 50441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // a manager thread might still be left running that will interfere 50541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // with later death tests. This is unfortunate, but this class 50641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // cleans up after itself as best it can. 5071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void TearDown() { 50841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot notifications_.spawn_thread_ok_to_terminate.Notify(); 5091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 51041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 5111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania private: 51241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot SpawnThreadNotifications notifications_; 51341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot scoped_ptr<ThreadWithParam<SpawnThreadNotifications*> > thread_; 5141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 5151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 51641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_IS_THREADSAFE 5171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The MixedUpTestCaseTest test case verifies that Google Test will fail a 5191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test if it uses a different fixture class than what other tests in 5201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the same test case use. It deliberately contains two fixture 5211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// classes with the same name but defined in different namespaces. 5221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The MixedUpTestCaseWithSameTestNameTest test case verifies that 5241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// when the user defines two tests with the same test case name AND 5251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// same test name (but in different namespaces), the second test will 5261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// fail. 5271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace foo { 5291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass MixedUpTestCaseTest : public testing::Test { 5311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 5321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5331be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MixedUpTestCaseTest, FirstTestFromNamespaceFoo) {} 5341be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MixedUpTestCaseTest, SecondTestFromNamespaceFoo) {} 5351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass MixedUpTestCaseWithSameTestNameTest : public testing::Test { 5371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 5381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5391be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MixedUpTestCaseWithSameTestNameTest, 5401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TheSecondTestWithThisNameShouldFail) {} 5411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace foo 5431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catanianamespace bar { 5451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass MixedUpTestCaseTest : public testing::Test { 5471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 5481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The following two tests are expected to fail. We rely on the 5501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// golden file to check that Google Test generates the right error message. 5511be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MixedUpTestCaseTest, ThisShouldFail) {} 5521be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MixedUpTestCaseTest, ThisShouldFailToo) {} 5531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass MixedUpTestCaseWithSameTestNameTest : public testing::Test { 5551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 5561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Expected to fail. We rely on the golden file to check that Google Test 5581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// generates the right error message. 5591be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(MixedUpTestCaseWithSameTestNameTest, 5601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TheSecondTestWithThisNameShouldFail) {} 5611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} // namespace bar 5631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The following two test cases verify that Google Test catches the user 5651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// error of mixing TEST and TEST_F in the same test case. The first 5661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// test case checks the scenario where TEST_F appears before TEST, and 5671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the second one checks where TEST appears before TEST_F. 5681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TEST_F_before_TEST_in_same_test_case : public testing::Test { 5701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 5711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5721be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {} 5731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Expected to fail. We rely on the golden file to check that Google Test 5751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// generates the right error message. 5761be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {} 5771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TEST_before_TEST_F_in_same_test_case : public testing::Test { 5791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 5801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5811be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {} 5821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Expected to fail. We rely on the golden file to check that Google Test 5841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// generates the right error message. 5851be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) { 5861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE(). 5891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint global_integer = 0; 5901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_NONFATAL_FAILURE() can reference global variables. 5921be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) { 5931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania global_integer = 0; 5941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE({ 5951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(1, global_integer) << "Expected non-fatal failure."; 5961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, "Expected non-fatal failure."); 5971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 5981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 5991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_NONFATAL_FAILURE() can reference local variables 6001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// (static or not). 6011be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) { 6021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania int m = 0; 6031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static int n; 6041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania n = 1; 6051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE({ 6061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(m, n) << "Expected non-fatal failure."; 6071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, "Expected non-fatal failure."); 6081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly 6111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// one non-fatal failure and no fatal failure. 6121be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) { 6131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE({ 6141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "Expected non-fatal failure."; 6151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, "Expected non-fatal failure."); 6161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_NONFATAL_FAILURE() fails when there is no 6191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// non-fatal failure. 6201be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) { 6211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting a failure)\n"); 6221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE({ 6231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, ""); 6241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_NONFATAL_FAILURE() fails when there are two 6271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// non-fatal failures. 6281be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) { 6291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting a failure)\n"); 6301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE({ 6311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "Expected non-fatal failure 1."; 6321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "Expected non-fatal failure 2."; 6331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, ""); 6341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal 6371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// failure. 6381be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) { 6391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting a failure)\n"); 6401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE({ 6411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FAIL() << "Expected fatal failure."; 6421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, ""); 6431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being 6461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// tested returns. 6471be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) { 6481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting a failure)\n"); 6491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE({ 6501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return; 6511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, ""); 6521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_EXCEPTIONS 6551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being 6571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// tested throws. 6581be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) { 6591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting a failure)\n"); 6601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania try { 6611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE({ 6621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania throw 0; 6631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, ""); 6641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } catch(int) { // NOLINT 6651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 6661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_EXCEPTIONS 6691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_FATAL_FAILURE() can reference global variables. 6711be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) { 6721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania global_integer = 0; 6731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE({ 6741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_EQ(1, global_integer) << "Expected fatal failure."; 6751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, "Expected fatal failure."); 6761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_FATAL_FAILURE() can reference local static 6791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// variables. 6801be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) { 6811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static int n; 6821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania n = 1; 6831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE({ 6841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ASSERT_EQ(0, n) << "Expected fatal failure."; 6851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, "Expected fatal failure."); 6861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly 6891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// one fatal failure and no non-fatal failure. 6901be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) { 6911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE({ 6921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FAIL() << "Expected fatal failure."; 6931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, "Expected fatal failure."); 6941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 6951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 6961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal 6971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// failure. 6981be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) { 6991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting a failure)\n"); 7001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE({ 7011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, ""); 7021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// A helper for generating a fatal failure. 7051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniavoid FatalFailure() { 7061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FAIL() << "Expected fatal failure."; 7071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_FATAL_FAILURE() fails when there are two 7101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// fatal failures. 7111be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) { 7121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting a failure)\n"); 7131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE({ 7141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FatalFailure(); 7151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FatalFailure(); 7161be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, ""); 7171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal 7201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// failure. 7211be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) { 7221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting a failure)\n"); 7231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE({ 7241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "Expected non-fatal failure."; 7251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, ""); 7261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_FATAL_FAILURE() fails when the statement being 7291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// tested returns. 7301be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectFatalFailureTest, FailsWhenStatementReturns) { 7311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting a failure)\n"); 7321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE({ 7331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return; 7341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, ""); 7351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_EXCEPTIONS 7381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that EXPECT_FATAL_FAILURE() fails when the statement being 7401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// tested throws. 7411be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ExpectFatalFailureTest, FailsWhenStatementThrows) { 7421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting a failure)\n"); 7431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania try { 7441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE({ 7451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania throw 0; 7461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }, ""); 7471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } catch(int) { // NOLINT 7481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 7491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_EXCEPTIONS 7521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This #ifdef block tests the output of typed tests. 7541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_TYPED_TEST 7551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T> 7571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TypedTest : public testing::Test { 7581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 7591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7601be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTYPED_TEST_CASE(TypedTest, testing::Types<int>); 7611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7621be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTYPED_TEST(TypedTest, Success) { 7631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(0, TypeParam()); 7641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7661be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTYPED_TEST(TypedTest, Failure) { 7671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(1, TypeParam()) << "Expected failure"; 7681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_TYPED_TEST 7711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// This #ifdef block tests the output of type-parameterized tests. 7731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_TYPED_TEST_P 7741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T> 7761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass TypedTestP : public testing::Test { 7771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 7781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7791be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTYPED_TEST_CASE_P(TypedTestP); 7801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7811be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTYPED_TEST_P(TypedTestP, Success) { 78241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_EQ(0U, TypeParam()); 7831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7851be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTYPED_TEST_P(TypedTestP, Failure) { 78641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot EXPECT_EQ(1U, TypeParam()) << "Expected failure"; 7871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 7881be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7891be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaREGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure); 7901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef testing::Types<unsigned char, unsigned int> UnsignedTypes; 7921be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaINSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes); 7931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_TYPED_TEST_P 7951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_DEATH_TEST 7971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 7981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// We rely on the golden file to verify that tests whose test case 7991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// name ends with DeathTest are run first. 8001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8011be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST(ADeathTest, ShouldRunFirst) { 8021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 8031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 80441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_HAS_TYPED_TEST 8051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// We rely on the golden file to verify that typed tests whose test 8071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// case name ends with DeathTest are run first. 8081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T> 8101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ATypedDeathTest : public testing::Test { 8111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 8121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef testing::Types<int, double> NumericTypes; 8141be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTYPED_TEST_CASE(ATypedDeathTest, NumericTypes); 8151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8161be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTYPED_TEST(ATypedDeathTest, ShouldRunFirst) { 8171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 8181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 81941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_HAS_TYPED_TEST 8201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 82141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_HAS_TYPED_TEST_P 8221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// We rely on the golden file to verify that type-parameterized tests 8251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// whose test case name ends with DeathTest are run first. 8261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatemplate <typename T> 8281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ATypeParamDeathTest : public testing::Test { 8291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 8301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8311be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTYPED_TEST_CASE_P(ATypeParamDeathTest); 8321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8331be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) { 8341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 8351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8361be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaREGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest, ShouldRunFirst); 8371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8381be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaINSTANTIATE_TYPED_TEST_CASE_P(My, ATypeParamDeathTest, NumericTypes); 8391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 84041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_HAS_TYPED_TEST_P 8411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_DEATH_TEST 8431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests various failure conditions of 8451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}. 8461be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ExpectFailureTest : public testing::Test { 84741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot public: // Must be public and not protected due to a bug in g++ 3.4.2. 8481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania enum FailureMode { 8491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FATAL_FAILURE, 8501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania NONFATAL_FAILURE 8511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania }; 8521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static void AddFailure(FailureMode failure) { 8531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (failure == FATAL_FAILURE) { 8541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FAIL() << "Expected fatal failure."; 8551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } else { 8561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "Expected non-fatal failure."; 8571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 8581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 8591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 8601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8611be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(ExpectFailureTest, ExpectFatalFailure) { 8621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Expected fatal failure, but succeeds. 8631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 1 failure)\n"); 8641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure."); 8651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Expected fatal failure, but got a non-fatal failure. 8661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 1 failure)\n"); 8671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Expected non-fatal " 8681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "failure."); 8691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Wrong message. 8701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 1 failure)\n"); 8711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Some other fatal failure " 8721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "expected."); 8731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 8741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8751be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(ExpectFailureTest, ExpectNonFatalFailure) { 8761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Expected non-fatal failure, but succeeds. 8771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 1 failure)\n"); 8781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure."); 8791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Expected non-fatal failure, but got a fatal failure. 8801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 1 failure)\n"); 8811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure."); 8821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Wrong message. 8831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 1 failure)\n"); 8841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Some other non-fatal " 8851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "failure."); 8861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 8871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 88841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#if GTEST_IS_THREADSAFE 8891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass ExpectFailureWithThreadsTest : public ExpectFailureTest { 8911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania protected: 8921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania static void AddFailureInOtherThread(FailureMode failure) { 89341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL); 89441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot thread.Join(); 8951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 8961be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 8971be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 8981be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) { 8991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We only intercept the current thread. 9001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 2 failures)\n"); 9011be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE), 9021be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "Expected fatal failure."); 9031be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9041be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9051be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) { 9061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We only intercept the current thread. 9071be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 2 failures)\n"); 9081be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE), 9091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "Expected non-fatal failure."); 9101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniatypedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest; 9131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Tests that the ScopedFakeTestPartResultReporter only catches failures from 9151be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD. 9161be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) { 9171be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 2 failures)\n"); 9181be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania TestPartResultArray results; 9191be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania { 9201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ScopedFakeTestPartResultReporter reporter( 9211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD, 9221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania &results); 9231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania AddFailureInOtherThread(FATAL_FAILURE); 9241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania AddFailureInOtherThread(NONFATAL_FAILURE); 9251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 9261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The two failures should not have been intercepted. 9271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_EQ(0, results.size()) << "This shouldn't fail."; 9281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 93041d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot#endif // GTEST_IS_THREADSAFE 9311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9321be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) { 9331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Expected fatal failure, but succeeds. 9341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 1 failure)\n"); 9351be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure."); 9361be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Expected fatal failure, but got a non-fatal failure. 9371be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 1 failure)\n"); 9381be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE), 9391be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "Expected non-fatal failure."); 9401be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Wrong message. 9411be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 1 failure)\n"); 9421be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE), 9431be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "Some other fatal failure expected."); 9441be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9451be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9461be2c9def7187e4e643c00a31dd9986395795d7dNicolas CataniaTEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) { 9471be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Expected non-fatal failure, but succeeds. 9481be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 1 failure)\n"); 9491be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal " 9501be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "failure."); 9511be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Expected non-fatal failure, but got a fatal failure. 9521be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 1 failure)\n"); 9531be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE), 9541be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "Expected fatal failure."); 9551be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Wrong message. 9561be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("(expecting 1 failure)\n"); 9571be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE), 9581be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania "Some other non-fatal failure."); 9591be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 9601be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9611be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9621be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// Two test environments for testing testing::AddGlobalTestEnvironment(). 9631be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9641be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass FooEnvironment : public testing::Environment { 9651be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 9661be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void SetUp() { 9671be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("%s", "FooEnvironment::SetUp() called.\n"); 9681be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 9691be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9701be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void TearDown() { 9711be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("%s", "FooEnvironment::TearDown() called.\n"); 9721be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania FAIL() << "Expected fatal failure."; 9731be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 9741be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 9751be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9761be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaclass BarEnvironment : public testing::Environment { 9771be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania public: 9781be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void SetUp() { 9791be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("%s", "BarEnvironment::SetUp() called.\n"); 9801be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 9811be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9821be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania virtual void TearDown() { 9831be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania printf("%s", "BarEnvironment::TearDown() called.\n"); 9841be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania ADD_FAILURE() << "Expected non-fatal failure."; 9851be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 9861be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania}; 9871be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 98841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabotbool GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = false; 9891be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 9901be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The main function. 9911be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// 9921be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// The idea is to use Google Test to run all the tests we have defined (some 9931be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// of them are intended to fail), and then compare the test results 9941be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania// with the "golden" file. 9951be2c9def7187e4e643c00a31dd9986395795d7dNicolas Cataniaint main(int argc, char **argv) { 99641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot testing::GTEST_FLAG(print_time) = false; 99741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 9981be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We just run the tests, knowing some of them are intended to fail. 9991be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // We will use a separate Python script to compare the output of 10001be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // this program with the golden file. 100141d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot 100241d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // It's hard to test InitGoogleTest() directly, as it has many 100341d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // global side effects. The following line serves as a sanity test 100441d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot // for it. 10051be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::InitGoogleTest(&argc, argv); 10061be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (argc >= 2 && 1007fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes (std::string(argv[1]) == 1008fc2de66453b0669c09eaca643b07d34443858b6fElliott Hughes "--gtest_internal_skip_environment_and_ad_hoc_tests")) 10091be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = true; 10101be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10111be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#if GTEST_HAS_DEATH_TEST 10121be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (testing::internal::GTEST_FLAG(internal_run_death_test) != "") { 10131be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Skip the usual output capturing if we're running as the child 10141be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // process of an threadsafe-style death test. 101541d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# if GTEST_OS_WINDOWS 101641d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot posix::FReopen("nul:", "w", stdout); 101741d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# else 101841d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot posix::FReopen("/dev/null", "w", stdout); 101941d0579e8de9ef4ff178fc4991043c61a19943f7Brett Chabot# endif // GTEST_OS_WINDOWS 10201be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return RUN_ALL_TESTS(); 10211be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania } 10221be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania#endif // GTEST_HAS_DEATH_TEST 10231be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10241be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania if (GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests)) 10251be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return RUN_ALL_TESTS(); 10261be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10271be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // Registers two global test environments. 10281be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // The golden file verifies that they are set up in the order they 10291be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania // are registered, and torn down in the reverse order. 10301be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::AddGlobalTestEnvironment(new FooEnvironment); 10311be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania testing::AddGlobalTestEnvironment(new BarEnvironment); 10321be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania 10331be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania return RunAllTests(); 10341be2c9def7187e4e643c00a31dd9986395795d7dNicolas Catania} 1035