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