googletest-output-test_.cc (googletest-release-1.11.0) | : | googletest-output-test_.cc (googletest-release-1.12.0) | ||
---|---|---|---|---|
skipping to change at line 36 | skipping to change at line 36 | |||
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |||
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |||
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |||
// | // | |||
// The purpose of this file is to generate Google Test output under | // The purpose of this file is to generate Google Test output under | |||
// various conditions. The output will then be verified by | // various conditions. The output will then be verified by | |||
// googletest-output-test.py to ensure that Google Test generates the | // googletest-output-test.py to ensure that Google Test generates the | |||
// desired messages. Therefore, most tests in this file are MEANT TO | // desired messages. Therefore, most tests in this file are MEANT TO | |||
// FAIL. | // FAIL. | |||
#include <stdlib.h> | ||||
#include "gtest/gtest-spi.h" | #include "gtest/gtest-spi.h" | |||
#include "gtest/gtest.h" | #include "gtest/gtest.h" | |||
#include "src/gtest-internal-inl.h" | #include "src/gtest-internal-inl.h" | |||
#include <stdlib.h> | ||||
#if _MSC_VER | #if _MSC_VER | |||
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */) | GTEST_DISABLE_MSC_WARNINGS_PUSH_(4127 /* conditional expression is constant */) | |||
#endif // _MSC_VER | #endif // _MSC_VER | |||
#if GTEST_IS_THREADSAFE | #if GTEST_IS_THREADSAFE | |||
using testing::ScopedFakeTestPartResultReporter; | using testing::ScopedFakeTestPartResultReporter; | |||
using testing::TestPartResultArray; | using testing::TestPartResultArray; | |||
using testing::internal::Notification; | using testing::internal::Notification; | |||
using testing::internal::ThreadWithParam; | using testing::internal::ThreadWithParam; | |||
#endif | #endif | |||
namespace posix = ::testing::internal::posix; | namespace posix = ::testing::internal::posix; | |||
// Tests catching fatal failures. | // Tests catching fatal failures. | |||
// A subroutine used by the following test. | // A subroutine used by the following test. | |||
void TestEq1(int x) { | void TestEq1(int x) { ASSERT_EQ(1, x); } | |||
ASSERT_EQ(1, x); | ||||
} | ||||
// This function calls a test subroutine, catches the fatal failure it | // This function calls a test subroutine, catches the fatal failure it | |||
// generates, and then returns early. | // generates, and then returns early. | |||
void TryTestSubroutine() { | void TryTestSubroutine() { | |||
// Calls a subrountine that yields a fatal failure. | // Calls a subrountine that yields a fatal failure. | |||
TestEq1(2); | TestEq1(2); | |||
// Catches the fatal failure and aborts the test. | // Catches the fatal failure and aborts the test. | |||
// | // | |||
// The testing::Test:: prefix is necessary when calling | // The testing::Test:: prefix is necessary when calling | |||
// HasFatalFailure() outside of a TEST, TEST_F, or test fixture. | // HasFatalFailure() outside of a TEST, TEST_F, or test fixture. | |||
if (testing::Test::HasFatalFailure()) return; | if (testing::Test::HasFatalFailure()) return; | |||
// If we get here, something is wrong. | // If we get here, something is wrong. | |||
FAIL() << "This should never be reached."; | FAIL() << "This should never be reached."; | |||
} | } | |||
TEST(PassingTest, PassingTest1) { | TEST(PassingTest, PassingTest1) {} | |||
} | ||||
TEST(PassingTest, PassingTest2) { | TEST(PassingTest, PassingTest2) {} | |||
} | ||||
// Tests that parameters of failing parameterized tests are printed in the | // Tests that parameters of failing parameterized tests are printed in the | |||
// failing test summary. | // failing test summary. | |||
class FailingParamTest : public testing::TestWithParam<int> {}; | class FailingParamTest : public testing::TestWithParam<int> {}; | |||
TEST_P(FailingParamTest, Fails) { | TEST_P(FailingParamTest, Fails) { EXPECT_EQ(1, GetParam()); } | |||
EXPECT_EQ(1, GetParam()); | ||||
} | ||||
// This generates a test which will fail. Google Test is expected to print | // This generates a test which will fail. Google Test is expected to print | |||
// its parameter when it outputs the list of all failed tests. | // its parameter when it outputs the list of all failed tests. | |||
INSTANTIATE_TEST_SUITE_P(PrintingFailingParams, | INSTANTIATE_TEST_SUITE_P(PrintingFailingParams, FailingParamTest, | |||
FailingParamTest, | ||||
testing::Values(2)); | testing::Values(2)); | |||
// Tests that an empty value for the test suite basename yields just | // Tests that an empty value for the test suite basename yields just | |||
// the test name without any prior / | // the test name without any prior / | |||
class EmptyBasenameParamInst : public testing::TestWithParam<int> {}; | class EmptyBasenameParamInst : public testing::TestWithParam<int> {}; | |||
TEST_P(EmptyBasenameParamInst, Passes) { EXPECT_EQ(1, GetParam()); } | TEST_P(EmptyBasenameParamInst, Passes) { EXPECT_EQ(1, GetParam()); } | |||
INSTANTIATE_TEST_SUITE_P(, EmptyBasenameParamInst, testing::Values(1)); | INSTANTIATE_TEST_SUITE_P(, EmptyBasenameParamInst, testing::Values(1)); | |||
skipping to change at line 149 | skipping to change at line 142 | |||
// fixture, the testing::Test:: prefix is not needed. | // fixture, the testing::Test:: prefix is not needed. | |||
if (HasFatalFailure()) return; | if (HasFatalFailure()) return; | |||
// If we get here, something is wrong. | // If we get here, something is wrong. | |||
FAIL() << "This should never be reached."; | FAIL() << "This should never be reached."; | |||
} | } | |||
// Tests HasFatalFailure() after a failed EXPECT check. | // Tests HasFatalFailure() after a failed EXPECT check. | |||
TEST(FatalFailureTest, NonfatalFailureInSubroutine) { | TEST(FatalFailureTest, NonfatalFailureInSubroutine) { | |||
printf("(expecting a failure on false)\n"); | printf("(expecting a failure on false)\n"); | |||
EXPECT_TRUE(false); // Generates a nonfatal failure | EXPECT_TRUE(false); // Generates a nonfatal failure | |||
ASSERT_FALSE(HasFatalFailure()); // This should succeed. | ASSERT_FALSE(HasFatalFailure()); // This should succeed. | |||
} | } | |||
// Tests interleaving user logging and Google Test assertions. | // Tests interleaving user logging and Google Test assertions. | |||
TEST(LoggingTest, InterleavingLoggingAndAssertions) { | TEST(LoggingTest, InterleavingLoggingAndAssertions) { | |||
static const int a[4] = { | static const int a[4] = {3, 9, 2, 6}; | |||
3, 9, 2, 6 | ||||
}; | ||||
printf("(expecting 2 failures on (3) >= (a[i]))\n"); | printf("(expecting 2 failures on (3) >= (a[i]))\n"); | |||
for (int i = 0; i < static_cast<int>(sizeof(a)/sizeof(*a)); i++) { | for (int i = 0; i < static_cast<int>(sizeof(a) / sizeof(*a)); i++) { | |||
printf("i == %d\n", i); | printf("i == %d\n", i); | |||
EXPECT_GE(3, a[i]); | EXPECT_GE(3, a[i]); | |||
} | } | |||
} | } | |||
// Tests the SCOPED_TRACE macro. | // Tests the SCOPED_TRACE macro. | |||
// A helper function for testing SCOPED_TRACE. | // A helper function for testing SCOPED_TRACE. | |||
void SubWithoutTrace(int n) { | void SubWithoutTrace(int n) { | |||
EXPECT_EQ(1, n); | EXPECT_EQ(1, n); | |||
skipping to change at line 300 | skipping to change at line 291 | |||
struct CheckPoints { | struct CheckPoints { | |||
Notification n1; | Notification n1; | |||
Notification n2; | Notification n2; | |||
Notification n3; | Notification n3; | |||
}; | }; | |||
static void ThreadWithScopedTrace(CheckPoints* check_points) { | static void ThreadWithScopedTrace(CheckPoints* check_points) { | |||
{ | { | |||
SCOPED_TRACE("Trace B"); | SCOPED_TRACE("Trace B"); | |||
ADD_FAILURE() | ADD_FAILURE() << "Expected failure #1 (in thread B, only trace B alive)."; | |||
<< "Expected failure #1 (in thread B, only trace B alive)."; | ||||
check_points->n1.Notify(); | check_points->n1.Notify(); | |||
check_points->n2.WaitForNotification(); | check_points->n2.WaitForNotification(); | |||
ADD_FAILURE() | ADD_FAILURE() | |||
<< "Expected failure #3 (in thread B, trace A & B both alive)."; | << "Expected failure #3 (in thread B, trace A & B both alive)."; | |||
} // Trace B dies here. | } // Trace B dies here. | |||
ADD_FAILURE() | ADD_FAILURE() << "Expected failure #4 (in thread B, only trace A alive)."; | |||
<< "Expected failure #4 (in thread B, only trace A alive)."; | ||||
check_points->n3.Notify(); | check_points->n3.Notify(); | |||
} | } | |||
TEST(SCOPED_TRACETest, WorksConcurrently) { | TEST(SCOPED_TRACETest, WorksConcurrently) { | |||
printf("(expecting 6 failures)\n"); | printf("(expecting 6 failures)\n"); | |||
CheckPoints check_points; | CheckPoints check_points; | |||
ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace, &check_points, | ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace, &check_points, | |||
nullptr); | nullptr); | |||
check_points.n1.WaitForNotification(); | check_points.n1.WaitForNotification(); | |||
{ | { | |||
SCOPED_TRACE("Trace A"); | SCOPED_TRACE("Trace A"); | |||
ADD_FAILURE() | ADD_FAILURE() | |||
<< "Expected failure #2 (in thread A, trace A & B both alive)."; | << "Expected failure #2 (in thread A, trace A & B both alive)."; | |||
check_points.n2.Notify(); | check_points.n2.Notify(); | |||
check_points.n3.WaitForNotification(); | check_points.n3.WaitForNotification(); | |||
ADD_FAILURE() | ADD_FAILURE() << "Expected failure #5 (in thread A, only trace A alive)."; | |||
<< "Expected failure #5 (in thread A, only trace A alive)."; | ||||
} // Trace A dies here. | } // Trace A dies here. | |||
ADD_FAILURE() | ADD_FAILURE() << "Expected failure #6 (in thread A, no trace alive)."; | |||
<< "Expected failure #6 (in thread A, no trace alive)."; | ||||
thread.Join(); | thread.Join(); | |||
} | } | |||
#endif // GTEST_IS_THREADSAFE | #endif // GTEST_IS_THREADSAFE | |||
// Tests basic functionality of the ScopedTrace utility (most of its features | // Tests basic functionality of the ScopedTrace utility (most of its features | |||
// are already tested in SCOPED_TRACETest). | // are already tested in SCOPED_TRACETest). | |||
TEST(ScopedTraceTest, WithExplicitFileAndLine) { | TEST(ScopedTraceTest, WithExplicitFileAndLine) { | |||
testing::ScopedTrace trace("explicit_file.cc", 123, "expected trace message"); | testing::ScopedTrace trace("explicit_file.cc", 123, "expected trace message"); | |||
ADD_FAILURE() << "Check that the trace is attached to a particular location."; | ADD_FAILURE() << "Check that the trace is attached to a particular location."; | |||
} | } | |||
skipping to change at line 415 | skipping to change at line 402 | |||
<< "had a fatal failure."; | << "had a fatal failure."; | |||
} | } | |||
void TearDown() override { | void TearDown() override { | |||
ADD_FAILURE() << "UNEXPECTED failure in TearDown(). " | ADD_FAILURE() << "UNEXPECTED failure in TearDown(). " | |||
<< "We should never get here, as the test fixture c'tor " | << "We should never get here, as the test fixture c'tor " | |||
<< "had a fatal failure."; | << "had a fatal failure."; | |||
} | } | |||
private: | private: | |||
void Init() { | void Init() { FAIL() << "Expected failure #1, in the test fixture c'tor."; } | |||
FAIL() << "Expected failure #1, in the test fixture c'tor."; | ||||
} | ||||
}; | }; | |||
TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) { | TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) { | |||
ADD_FAILURE() << "UNEXPECTED failure in the test body. " | ADD_FAILURE() << "UNEXPECTED failure in the test body. " | |||
<< "We should never get here, as the test fixture c'tor " | << "We should never get here, as the test fixture c'tor " | |||
<< "had a fatal failure."; | << "had a fatal failure."; | |||
} | } | |||
// Tests non-fatal failures in SetUp(). | // Tests non-fatal failures in SetUp(). | |||
class NonFatalFailureInSetUpTest : public testing::Test { | class NonFatalFailureInSetUpTest : public testing::Test { | |||
skipping to change at line 439 | skipping to change at line 424 | |||
~NonFatalFailureInSetUpTest() override { Deinit(); } | ~NonFatalFailureInSetUpTest() override { Deinit(); } | |||
void SetUp() override { | void SetUp() override { | |||
printf("(expecting 4 failures)\n"); | printf("(expecting 4 failures)\n"); | |||
ADD_FAILURE() << "Expected failure #1, in SetUp()."; | ADD_FAILURE() << "Expected failure #1, in SetUp()."; | |||
} | } | |||
void TearDown() override { FAIL() << "Expected failure #3, in TearDown()."; } | void TearDown() override { FAIL() << "Expected failure #3, in TearDown()."; } | |||
private: | private: | |||
void Deinit() { | void Deinit() { FAIL() << "Expected failure #4, in the test fixture d'tor."; } | |||
FAIL() << "Expected failure #4, in the test fixture d'tor."; | ||||
} | ||||
}; | }; | |||
TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) { | TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) { | |||
FAIL() << "Expected failure #2, in the test function."; | FAIL() << "Expected failure #2, in the test function."; | |||
} | } | |||
// Tests fatal failures in SetUp(). | // Tests fatal failures in SetUp(). | |||
class FatalFailureInSetUpTest : public testing::Test { | class FatalFailureInSetUpTest : public testing::Test { | |||
protected: | protected: | |||
~FatalFailureInSetUpTest() override { Deinit(); } | ~FatalFailureInSetUpTest() override { Deinit(); } | |||
void SetUp() override { | void SetUp() override { | |||
printf("(expecting 3 failures)\n"); | printf("(expecting 3 failures)\n"); | |||
FAIL() << "Expected failure #1, in SetUp()."; | FAIL() << "Expected failure #1, in SetUp()."; | |||
} | } | |||
void TearDown() override { FAIL() << "Expected failure #2, in TearDown()."; } | void TearDown() override { FAIL() << "Expected failure #2, in TearDown()."; } | |||
private: | private: | |||
void Deinit() { | void Deinit() { FAIL() << "Expected failure #3, in the test fixture d'tor."; } | |||
FAIL() << "Expected failure #3, in the test fixture d'tor."; | ||||
} | ||||
}; | }; | |||
TEST_F(FatalFailureInSetUpTest, FailureInSetUp) { | TEST_F(FatalFailureInSetUpTest, FailureInSetUp) { | |||
FAIL() << "UNEXPECTED failure in the test function. " | FAIL() << "UNEXPECTED failure in the test function. " | |||
<< "We should never get here, as SetUp() failed."; | << "We should never get here, as SetUp() failed."; | |||
} | } | |||
TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) { | TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) { | |||
ADD_FAILURE_AT("foo.cc", 42) << "Expected nonfatal failure in foo.cc"; | ADD_FAILURE_AT("foo.cc", 42) << "Expected nonfatal failure in foo.cc"; | |||
} | } | |||
skipping to change at line 491 | skipping to change at line 472 | |||
// the same test case use. It deliberately contains two fixture | // the same test case use. It deliberately contains two fixture | |||
// classes with the same name but defined in different namespaces. | // classes with the same name but defined in different namespaces. | |||
// The MixedUpTestSuiteWithSameTestNameTest test case verifies that | // The MixedUpTestSuiteWithSameTestNameTest test case verifies that | |||
// when the user defines two tests with the same test case name AND | // when the user defines two tests with the same test case name AND | |||
// same test name (but in different namespaces), the second test will | // same test name (but in different namespaces), the second test will | |||
// fail. | // fail. | |||
namespace foo { | namespace foo { | |||
class MixedUpTestSuiteTest : public testing::Test { | class MixedUpTestSuiteTest : public testing::Test {}; | |||
}; | ||||
TEST_F(MixedUpTestSuiteTest, FirstTestFromNamespaceFoo) {} | TEST_F(MixedUpTestSuiteTest, FirstTestFromNamespaceFoo) {} | |||
TEST_F(MixedUpTestSuiteTest, SecondTestFromNamespaceFoo) {} | TEST_F(MixedUpTestSuiteTest, SecondTestFromNamespaceFoo) {} | |||
class MixedUpTestSuiteWithSameTestNameTest : public testing::Test { | class MixedUpTestSuiteWithSameTestNameTest : public testing::Test {}; | |||
}; | ||||
TEST_F(MixedUpTestSuiteWithSameTestNameTest, | TEST_F(MixedUpTestSuiteWithSameTestNameTest, | |||
TheSecondTestWithThisNameShouldFail) {} | TheSecondTestWithThisNameShouldFail) {} | |||
} // namespace foo | } // namespace foo | |||
namespace bar { | namespace bar { | |||
class MixedUpTestSuiteTest : public testing::Test { | class MixedUpTestSuiteTest : public testing::Test {}; | |||
}; | ||||
// The following two tests are expected to fail. We rely on the | // The following two tests are expected to fail. We rely on the | |||
// golden file to check that Google Test generates the right error message. | // golden file to check that Google Test generates the right error message. | |||
TEST_F(MixedUpTestSuiteTest, ThisShouldFail) {} | TEST_F(MixedUpTestSuiteTest, ThisShouldFail) {} | |||
TEST_F(MixedUpTestSuiteTest, ThisShouldFailToo) {} | TEST_F(MixedUpTestSuiteTest, ThisShouldFailToo) {} | |||
class MixedUpTestSuiteWithSameTestNameTest : public testing::Test { | class MixedUpTestSuiteWithSameTestNameTest : public testing::Test {}; | |||
}; | ||||
// Expected to fail. We rely on the golden file to check that Google Test | // Expected to fail. We rely on the golden file to check that Google Test | |||
// generates the right error message. | // generates the right error message. | |||
TEST_F(MixedUpTestSuiteWithSameTestNameTest, | TEST_F(MixedUpTestSuiteWithSameTestNameTest, | |||
TheSecondTestWithThisNameShouldFail) {} | TheSecondTestWithThisNameShouldFail) {} | |||
} // namespace bar | } // namespace bar | |||
// The following two test cases verify that Google Test catches the user | // The following two test cases verify that Google Test catches the user | |||
// error of mixing TEST and TEST_F in the same test case. The first | // error of mixing TEST and TEST_F in the same test case. The first | |||
// test case checks the scenario where TEST_F appears before TEST, and | // test case checks the scenario where TEST_F appears before TEST, and | |||
// the second one checks where TEST appears before TEST_F. | // the second one checks where TEST appears before TEST_F. | |||
class TEST_F_before_TEST_in_same_test_case : public testing::Test { | class TEST_F_before_TEST_in_same_test_case : public testing::Test {}; | |||
}; | ||||
TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {} | TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {} | |||
// Expected to fail. We rely on the golden file to check that Google Test | // Expected to fail. We rely on the golden file to check that Google Test | |||
// generates the right error message. | // generates the right error message. | |||
TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {} | TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {} | |||
class TEST_before_TEST_F_in_same_test_case : public testing::Test { | class TEST_before_TEST_F_in_same_test_case : public testing::Test {}; | |||
}; | ||||
TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {} | TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {} | |||
// Expected to fail. We rely on the golden file to check that Google Test | // Expected to fail. We rely on the golden file to check that Google Test | |||
// generates the right error message. | // generates the right error message. | |||
TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) { | TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) {} | |||
} | ||||
// Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE(). | // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE(). | |||
int global_integer = 0; | int global_integer = 0; | |||
// Tests that EXPECT_NONFATAL_FAILURE() can reference global variables. | // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables. | |||
TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) { | TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) { | |||
global_integer = 0; | global_integer = 0; | |||
EXPECT_NONFATAL_FAILURE({ | EXPECT_NONFATAL_FAILURE( | |||
EXPECT_EQ(1, global_integer) << "Expected non-fatal failure."; | { EXPECT_EQ(1, global_integer) << "Expected non-fatal failure."; }, | |||
}, "Expected non-fatal failure."); | "Expected non-fatal failure."); | |||
} | } | |||
// Tests that EXPECT_NONFATAL_FAILURE() can reference local variables | // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables | |||
// (static or not). | // (static or not). | |||
TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) { | TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) { | |||
int m = 0; | int m = 0; | |||
static int n; | static int n; | |||
n = 1; | n = 1; | |||
EXPECT_NONFATAL_FAILURE({ | EXPECT_NONFATAL_FAILURE({ EXPECT_EQ(m, n) << "Expected non-fatal failure."; }, | |||
EXPECT_EQ(m, n) << "Expected non-fatal failure."; | "Expected non-fatal failure."); | |||
}, "Expected non-fatal failure."); | ||||
} | } | |||
// Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly | // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly | |||
// one non-fatal failure and no fatal failure. | // one non-fatal failure and no fatal failure. | |||
TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) { | TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) { | |||
EXPECT_NONFATAL_FAILURE({ | EXPECT_NONFATAL_FAILURE({ ADD_FAILURE() << "Expected non-fatal failure."; }, | |||
ADD_FAILURE() << "Expected non-fatal failure."; | "Expected non-fatal failure."); | |||
}, "Expected non-fatal failure."); | ||||
} | } | |||
// Tests that EXPECT_NONFATAL_FAILURE() fails when there is no | // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no | |||
// non-fatal failure. | // non-fatal failure. | |||
TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) { | TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) { | |||
printf("(expecting a failure)\n"); | printf("(expecting a failure)\n"); | |||
EXPECT_NONFATAL_FAILURE({ | EXPECT_NONFATAL_FAILURE({}, ""); | |||
}, ""); | ||||
} | } | |||
// Tests that EXPECT_NONFATAL_FAILURE() fails when there are two | // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two | |||
// non-fatal failures. | // non-fatal failures. | |||
TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) { | TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) { | |||
printf("(expecting a failure)\n"); | printf("(expecting a failure)\n"); | |||
EXPECT_NONFATAL_FAILURE({ | EXPECT_NONFATAL_FAILURE( | |||
ADD_FAILURE() << "Expected non-fatal failure 1."; | { | |||
ADD_FAILURE() << "Expected non-fatal failure 2."; | ADD_FAILURE() << "Expected non-fatal failure 1."; | |||
}, ""); | ADD_FAILURE() << "Expected non-fatal failure 2."; | |||
}, | ||||
""); | ||||
} | } | |||
// Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal | // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal | |||
// failure. | // failure. | |||
TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) { | TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) { | |||
printf("(expecting a failure)\n"); | printf("(expecting a failure)\n"); | |||
EXPECT_NONFATAL_FAILURE({ | EXPECT_NONFATAL_FAILURE({ FAIL() << "Expected fatal failure."; }, ""); | |||
FAIL() << "Expected fatal failure."; | ||||
}, ""); | ||||
} | } | |||
// Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being | // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being | |||
// tested returns. | // tested returns. | |||
TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) { | TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) { | |||
printf("(expecting a failure)\n"); | printf("(expecting a failure)\n"); | |||
EXPECT_NONFATAL_FAILURE({ | EXPECT_NONFATAL_FAILURE({ return; }, ""); | |||
return; | ||||
}, ""); | ||||
} | } | |||
#if GTEST_HAS_EXCEPTIONS | #if GTEST_HAS_EXCEPTIONS | |||
// Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being | // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being | |||
// tested throws. | // tested throws. | |||
TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) { | TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) { | |||
printf("(expecting a failure)\n"); | printf("(expecting a failure)\n"); | |||
try { | try { | |||
EXPECT_NONFATAL_FAILURE({ | EXPECT_NONFATAL_FAILURE({ throw 0; }, ""); | |||
throw 0; | } catch (int) { // NOLINT | |||
}, ""); | ||||
} catch(int) { // NOLINT | ||||
} | } | |||
} | } | |||
#endif // GTEST_HAS_EXCEPTIONS | #endif // GTEST_HAS_EXCEPTIONS | |||
// Tests that EXPECT_FATAL_FAILURE() can reference global variables. | // Tests that EXPECT_FATAL_FAILURE() can reference global variables. | |||
TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) { | TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) { | |||
global_integer = 0; | global_integer = 0; | |||
EXPECT_FATAL_FAILURE({ | EXPECT_FATAL_FAILURE( | |||
ASSERT_EQ(1, global_integer) << "Expected fatal failure."; | { ASSERT_EQ(1, global_integer) << "Expected fatal failure."; }, | |||
}, "Expected fatal failure."); | "Expected fatal failure."); | |||
} | } | |||
// Tests that EXPECT_FATAL_FAILURE() can reference local static | // Tests that EXPECT_FATAL_FAILURE() can reference local static | |||
// variables. | // variables. | |||
TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) { | TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) { | |||
static int n; | static int n; | |||
n = 1; | n = 1; | |||
EXPECT_FATAL_FAILURE({ | EXPECT_FATAL_FAILURE({ ASSERT_EQ(0, n) << "Expected fatal failure."; }, | |||
ASSERT_EQ(0, n) << "Expected fatal failure."; | "Expected fatal failure."); | |||
}, "Expected fatal failure."); | ||||
} | } | |||
// Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly | // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly | |||
// one fatal failure and no non-fatal failure. | // one fatal failure and no non-fatal failure. | |||
TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) { | TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) { | |||
EXPECT_FATAL_FAILURE({ | EXPECT_FATAL_FAILURE({ FAIL() << "Expected fatal failure."; }, | |||
FAIL() << "Expected fatal failure."; | "Expected fatal failure."); | |||
}, "Expected fatal failure."); | ||||
} | } | |||
// Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal | // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal | |||
// failure. | // failure. | |||
TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) { | TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) { | |||
printf("(expecting a failure)\n"); | printf("(expecting a failure)\n"); | |||
EXPECT_FATAL_FAILURE({ | EXPECT_FATAL_FAILURE({}, ""); | |||
}, ""); | ||||
} | } | |||
// A helper for generating a fatal failure. | // A helper for generating a fatal failure. | |||
void FatalFailure() { | void FatalFailure() { FAIL() << "Expected fatal failure."; } | |||
FAIL() << "Expected fatal failure."; | ||||
} | ||||
// Tests that EXPECT_FATAL_FAILURE() fails when there are two | // Tests that EXPECT_FATAL_FAILURE() fails when there are two | |||
// fatal failures. | // fatal failures. | |||
TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) { | TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) { | |||
printf("(expecting a failure)\n"); | printf("(expecting a failure)\n"); | |||
EXPECT_FATAL_FAILURE({ | EXPECT_FATAL_FAILURE( | |||
FatalFailure(); | { | |||
FatalFailure(); | FatalFailure(); | |||
}, ""); | FatalFailure(); | |||
}, | ||||
""); | ||||
} | } | |||
// Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal | // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal | |||
// failure. | // failure. | |||
TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) { | TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) { | |||
printf("(expecting a failure)\n"); | printf("(expecting a failure)\n"); | |||
EXPECT_FATAL_FAILURE({ | EXPECT_FATAL_FAILURE({ ADD_FAILURE() << "Expected non-fatal failure."; }, ""); | |||
ADD_FAILURE() << "Expected non-fatal failure."; | ||||
}, ""); | ||||
} | } | |||
// Tests that EXPECT_FATAL_FAILURE() fails when the statement being | // Tests that EXPECT_FATAL_FAILURE() fails when the statement being | |||
// tested returns. | // tested returns. | |||
TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) { | TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) { | |||
printf("(expecting a failure)\n"); | printf("(expecting a failure)\n"); | |||
EXPECT_FATAL_FAILURE({ | EXPECT_FATAL_FAILURE({ return; }, ""); | |||
return; | ||||
}, ""); | ||||
} | } | |||
#if GTEST_HAS_EXCEPTIONS | #if GTEST_HAS_EXCEPTIONS | |||
// Tests that EXPECT_FATAL_FAILURE() fails when the statement being | // Tests that EXPECT_FATAL_FAILURE() fails when the statement being | |||
// tested throws. | // tested throws. | |||
TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) { | TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) { | |||
printf("(expecting a failure)\n"); | printf("(expecting a failure)\n"); | |||
try { | try { | |||
EXPECT_FATAL_FAILURE({ | EXPECT_FATAL_FAILURE({ throw 0; }, ""); | |||
throw 0; | } catch (int) { // NOLINT | |||
}, ""); | ||||
} catch(int) { // NOLINT | ||||
} | } | |||
} | } | |||
#endif // GTEST_HAS_EXCEPTIONS | #endif // GTEST_HAS_EXCEPTIONS | |||
// This #ifdef block tests the output of value-parameterized tests. | // This #ifdef block tests the output of value-parameterized tests. | |||
std::string ParamNameFunc(const testing::TestParamInfo<std::string>& info) { | std::string ParamNameFunc(const testing::TestParamInfo<std::string>& info) { | |||
return info.param; | return info.param; | |||
} | } | |||
class ParamTest : public testing::TestWithParam<std::string> { | class ParamTest : public testing::TestWithParam<std::string> {}; | |||
}; | ||||
TEST_P(ParamTest, Success) { | TEST_P(ParamTest, Success) { EXPECT_EQ("a", GetParam()); } | |||
EXPECT_EQ("a", GetParam()); | ||||
} | ||||
TEST_P(ParamTest, Failure) { | TEST_P(ParamTest, Failure) { EXPECT_EQ("b", GetParam()) << "Expected failure"; } | |||
EXPECT_EQ("b", GetParam()) << "Expected failure"; | ||||
} | ||||
INSTANTIATE_TEST_SUITE_P(PrintingStrings, | INSTANTIATE_TEST_SUITE_P(PrintingStrings, ParamTest, | |||
ParamTest, | testing::Values(std::string("a")), ParamNameFunc); | |||
testing::Values(std::string("a")), | ||||
ParamNameFunc); | ||||
// The case where a suite has INSTANTIATE_TEST_SUITE_P but not TEST_P. | // The case where a suite has INSTANTIATE_TEST_SUITE_P but not TEST_P. | |||
using NoTests = ParamTest; | using NoTests = ParamTest; | |||
INSTANTIATE_TEST_SUITE_P(ThisIsOdd, NoTests, ::testing::Values("Hello")); | INSTANTIATE_TEST_SUITE_P(ThisIsOdd, NoTests, ::testing::Values("Hello")); | |||
// fails under kErrorOnUninstantiatedParameterizedTest=true | // fails under kErrorOnUninstantiatedParameterizedTest=true | |||
class DetectNotInstantiatedTest : public testing::TestWithParam<int> {}; | class DetectNotInstantiatedTest : public testing::TestWithParam<int> {}; | |||
TEST_P(DetectNotInstantiatedTest, Used) { } | TEST_P(DetectNotInstantiatedTest, Used) {} | |||
// This would make the test failure from the above go away. | // This would make the test failure from the above go away. | |||
// INSTANTIATE_TEST_SUITE_P(Fix, DetectNotInstantiatedTest, testing::Values(1)); | // INSTANTIATE_TEST_SUITE_P(Fix, DetectNotInstantiatedTest, testing::Values(1)); | |||
template <typename T> | template <typename T> | |||
class TypedTest : public testing::Test { | class TypedTest : public testing::Test {}; | |||
}; | ||||
TYPED_TEST_SUITE(TypedTest, testing::Types<int>); | TYPED_TEST_SUITE(TypedTest, testing::Types<int>); | |||
TYPED_TEST(TypedTest, Success) { | TYPED_TEST(TypedTest, Success) { EXPECT_EQ(0, TypeParam()); } | |||
EXPECT_EQ(0, TypeParam()); | ||||
} | ||||
TYPED_TEST(TypedTest, Failure) { | TYPED_TEST(TypedTest, Failure) { | |||
EXPECT_EQ(1, TypeParam()) << "Expected failure"; | EXPECT_EQ(1, TypeParam()) << "Expected failure"; | |||
} | } | |||
typedef testing::Types<char, int> TypesForTestWithNames; | typedef testing::Types<char, int> TypesForTestWithNames; | |||
template <typename T> | template <typename T> | |||
class TypedTestWithNames : public testing::Test {}; | class TypedTestWithNames : public testing::Test {}; | |||
skipping to change at line 784 | skipping to change at line 732 | |||
} | } | |||
}; | }; | |||
TYPED_TEST_SUITE(TypedTestWithNames, TypesForTestWithNames, TypedTestNames); | TYPED_TEST_SUITE(TypedTestWithNames, TypesForTestWithNames, TypedTestNames); | |||
TYPED_TEST(TypedTestWithNames, Success) {} | TYPED_TEST(TypedTestWithNames, Success) {} | |||
TYPED_TEST(TypedTestWithNames, Failure) { FAIL(); } | TYPED_TEST(TypedTestWithNames, Failure) { FAIL(); } | |||
template <typename T> | template <typename T> | |||
class TypedTestP : public testing::Test { | class TypedTestP : public testing::Test {}; | |||
}; | ||||
TYPED_TEST_SUITE_P(TypedTestP); | TYPED_TEST_SUITE_P(TypedTestP); | |||
TYPED_TEST_P(TypedTestP, Success) { | TYPED_TEST_P(TypedTestP, Success) { EXPECT_EQ(0U, TypeParam()); } | |||
EXPECT_EQ(0U, TypeParam()); | ||||
} | ||||
TYPED_TEST_P(TypedTestP, Failure) { | TYPED_TEST_P(TypedTestP, Failure) { | |||
EXPECT_EQ(1U, TypeParam()) << "Expected failure"; | EXPECT_EQ(1U, TypeParam()) << "Expected failure"; | |||
} | } | |||
REGISTER_TYPED_TEST_SUITE_P(TypedTestP, Success, Failure); | REGISTER_TYPED_TEST_SUITE_P(TypedTestP, Success, Failure); | |||
typedef testing::Types<unsigned char, unsigned int> UnsignedTypes; | typedef testing::Types<unsigned char, unsigned int> UnsignedTypes; | |||
INSTANTIATE_TYPED_TEST_SUITE_P(Unsigned, TypedTestP, UnsignedTypes); | INSTANTIATE_TYPED_TEST_SUITE_P(Unsigned, TypedTestP, UnsignedTypes); | |||
skipping to change at line 816 | skipping to change at line 761 | |||
if (std::is_same<T, unsigned char>::value) { | if (std::is_same<T, unsigned char>::value) { | |||
return std::string("unsignedChar") + ::testing::PrintToString(i); | return std::string("unsignedChar") + ::testing::PrintToString(i); | |||
} | } | |||
if (std::is_same<T, unsigned int>::value) { | if (std::is_same<T, unsigned int>::value) { | |||
return std::string("unsignedInt") + ::testing::PrintToString(i); | return std::string("unsignedInt") + ::testing::PrintToString(i); | |||
} | } | |||
} | } | |||
}; | }; | |||
INSTANTIATE_TYPED_TEST_SUITE_P(UnsignedCustomName, TypedTestP, UnsignedTypes, | INSTANTIATE_TYPED_TEST_SUITE_P(UnsignedCustomName, TypedTestP, UnsignedTypes, | |||
TypedTestPNames); | TypedTestPNames); | |||
template <typename T> | template <typename T> | |||
class DetectNotInstantiatedTypesTest : public testing::Test {}; | class DetectNotInstantiatedTypesTest : public testing::Test {}; | |||
TYPED_TEST_SUITE_P(DetectNotInstantiatedTypesTest); | TYPED_TEST_SUITE_P(DetectNotInstantiatedTypesTest); | |||
TYPED_TEST_P(DetectNotInstantiatedTypesTest, Used) { | TYPED_TEST_P(DetectNotInstantiatedTypesTest, Used) { | |||
TypeParam instantiate; | TypeParam instantiate; | |||
(void)instantiate; | (void)instantiate; | |||
} | } | |||
REGISTER_TYPED_TEST_SUITE_P(DetectNotInstantiatedTypesTest, Used); | REGISTER_TYPED_TEST_SUITE_P(DetectNotInstantiatedTypesTest, Used); | |||
skipping to change at line 838 | skipping to change at line 783 | |||
// Adding the following would make that test failure go away. | // Adding the following would make that test failure go away. | |||
// | // | |||
// typedef ::testing::Types<char, int, unsigned int> MyTypes; | // typedef ::testing::Types<char, int, unsigned int> MyTypes; | |||
// INSTANTIATE_TYPED_TEST_SUITE_P(All, DetectNotInstantiatedTypesTest, MyTypes); | // INSTANTIATE_TYPED_TEST_SUITE_P(All, DetectNotInstantiatedTypesTest, MyTypes); | |||
#if GTEST_HAS_DEATH_TEST | #if GTEST_HAS_DEATH_TEST | |||
// We rely on the golden file to verify that tests whose test case | // We rely on the golden file to verify that tests whose test case | |||
// name ends with DeathTest are run first. | // name ends with DeathTest are run first. | |||
TEST(ADeathTest, ShouldRunFirst) { | TEST(ADeathTest, ShouldRunFirst) {} | |||
} | ||||
// We rely on the golden file to verify that typed tests whose test | // We rely on the golden file to verify that typed tests whose test | |||
// case name ends with DeathTest are run first. | // case name ends with DeathTest are run first. | |||
template <typename T> | template <typename T> | |||
class ATypedDeathTest : public testing::Test { | class ATypedDeathTest : public testing::Test {}; | |||
}; | ||||
typedef testing::Types<int, double> NumericTypes; | typedef testing::Types<int, double> NumericTypes; | |||
TYPED_TEST_SUITE(ATypedDeathTest, NumericTypes); | TYPED_TEST_SUITE(ATypedDeathTest, NumericTypes); | |||
TYPED_TEST(ATypedDeathTest, ShouldRunFirst) { | TYPED_TEST(ATypedDeathTest, ShouldRunFirst) {} | |||
} | ||||
// We rely on the golden file to verify that type-parameterized tests | // We rely on the golden file to verify that type-parameterized tests | |||
// whose test case name ends with DeathTest are run first. | // whose test case name ends with DeathTest are run first. | |||
template <typename T> | template <typename T> | |||
class ATypeParamDeathTest : public testing::Test { | class ATypeParamDeathTest : public testing::Test {}; | |||
}; | ||||
TYPED_TEST_SUITE_P(ATypeParamDeathTest); | TYPED_TEST_SUITE_P(ATypeParamDeathTest); | |||
TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) { | TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {} | |||
} | ||||
REGISTER_TYPED_TEST_SUITE_P(ATypeParamDeathTest, ShouldRunFirst); | REGISTER_TYPED_TEST_SUITE_P(ATypeParamDeathTest, ShouldRunFirst); | |||
INSTANTIATE_TYPED_TEST_SUITE_P(My, ATypeParamDeathTest, NumericTypes); | INSTANTIATE_TYPED_TEST_SUITE_P(My, ATypeParamDeathTest, NumericTypes); | |||
#endif // GTEST_HAS_DEATH_TEST | #endif // GTEST_HAS_DEATH_TEST | |||
// Tests various failure conditions of | // Tests various failure conditions of | |||
// EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}. | // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}. | |||
class ExpectFailureTest : public testing::Test { | class ExpectFailureTest : public testing::Test { | |||
public: // Must be public and not protected due to a bug in g++ 3.4.2. | public: // Must be public and not protected due to a bug in g++ 3.4.2. | |||
enum FailureMode { | enum FailureMode { FATAL_FAILURE, NONFATAL_FAILURE }; | |||
FATAL_FAILURE, | ||||
NONFATAL_FAILURE | ||||
}; | ||||
static void AddFailure(FailureMode failure) { | static void AddFailure(FailureMode failure) { | |||
if (failure == FATAL_FAILURE) { | if (failure == FATAL_FAILURE) { | |||
FAIL() << "Expected fatal failure."; | FAIL() << "Expected fatal failure."; | |||
} else { | } else { | |||
ADD_FAILURE() << "Expected non-fatal failure."; | ADD_FAILURE() << "Expected non-fatal failure."; | |||
} | } | |||
} | } | |||
}; | }; | |||
TEST_F(ExpectFailureTest, ExpectFatalFailure) { | TEST_F(ExpectFailureTest, ExpectFatalFailure) { | |||
// Expected fatal failure, but succeeds. | // Expected fatal failure, but succeeds. | |||
printf("(expecting 1 failure)\n"); | printf("(expecting 1 failure)\n"); | |||
EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure."); | EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure."); | |||
// Expected fatal failure, but got a non-fatal failure. | // Expected fatal failure, but got a non-fatal failure. | |||
printf("(expecting 1 failure)\n"); | printf("(expecting 1 failure)\n"); | |||
EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Expected non-fatal " | EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), | |||
"Expected non-fatal " | ||||
"failure."); | "failure."); | |||
// Wrong message. | // Wrong message. | |||
printf("(expecting 1 failure)\n"); | printf("(expecting 1 failure)\n"); | |||
EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Some other fatal failure " | EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), | |||
"Some other fatal failure " | ||||
"expected."); | "expected."); | |||
} | } | |||
TEST_F(ExpectFailureTest, ExpectNonFatalFailure) { | TEST_F(ExpectFailureTest, ExpectNonFatalFailure) { | |||
// Expected non-fatal failure, but succeeds. | // Expected non-fatal failure, but succeeds. | |||
printf("(expecting 1 failure)\n"); | printf("(expecting 1 failure)\n"); | |||
EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure."); | EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure."); | |||
// Expected non-fatal failure, but got a fatal failure. | // Expected non-fatal failure, but got a fatal failure. | |||
printf("(expecting 1 failure)\n"); | printf("(expecting 1 failure)\n"); | |||
EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure."); | EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure."); | |||
// Wrong message. | // Wrong message. | |||
printf("(expecting 1 failure)\n"); | printf("(expecting 1 failure)\n"); | |||
EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Some other non-fatal " | EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), | |||
"Some other non-fatal " | ||||
"failure."); | "failure."); | |||
} | } | |||
#if GTEST_IS_THREADSAFE | #if GTEST_IS_THREADSAFE | |||
class ExpectFailureWithThreadsTest : public ExpectFailureTest { | class ExpectFailureWithThreadsTest : public ExpectFailureTest { | |||
protected: | protected: | |||
static void AddFailureInOtherThread(FailureMode failure) { | static void AddFailureInOtherThread(FailureMode failure) { | |||
ThreadWithParam<FailureMode> thread(&AddFailure, failure, nullptr); | ThreadWithParam<FailureMode> thread(&AddFailure, failure, nullptr); | |||
thread.Join(); | thread.Join(); | |||
skipping to change at line 977 | skipping to change at line 917 | |||
"Expected non-fatal failure."); | "Expected non-fatal failure."); | |||
// Wrong message. | // Wrong message. | |||
printf("(expecting 1 failure)\n"); | printf("(expecting 1 failure)\n"); | |||
EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE), | EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE), | |||
"Some other fatal failure expected."); | "Some other fatal failure expected."); | |||
} | } | |||
TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) { | TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) { | |||
// Expected non-fatal failure, but succeeds. | // Expected non-fatal failure, but succeeds. | |||
printf("(expecting 1 failure)\n"); | printf("(expecting 1 failure)\n"); | |||
EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal " | EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), | |||
"Expected non-fatal " | ||||
"failure."); | "failure."); | |||
// Expected non-fatal failure, but got a fatal failure. | // Expected non-fatal failure, but got a fatal failure. | |||
printf("(expecting 1 failure)\n"); | printf("(expecting 1 failure)\n"); | |||
EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE), | EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE), | |||
"Expected fatal failure."); | "Expected fatal failure."); | |||
// Wrong message. | // Wrong message. | |||
printf("(expecting 1 failure)\n"); | printf("(expecting 1 failure)\n"); | |||
EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE), | EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE), | |||
"Some other non-fatal failure."); | "Some other non-fatal failure."); | |||
} | } | |||
skipping to change at line 1062 | skipping to change at line 1003 | |||
class BarEnvironment : public testing::Environment { | class BarEnvironment : public testing::Environment { | |||
public: | public: | |||
void SetUp() override { printf("%s", "BarEnvironment::SetUp() called.\n"); } | void SetUp() override { printf("%s", "BarEnvironment::SetUp() called.\n"); } | |||
void TearDown() override { | void TearDown() override { | |||
printf("%s", "BarEnvironment::TearDown() called.\n"); | printf("%s", "BarEnvironment::TearDown() called.\n"); | |||
ADD_FAILURE() << "Expected non-fatal failure."; | ADD_FAILURE() << "Expected non-fatal failure."; | |||
} | } | |||
}; | }; | |||
class TestSuiteThatFailsToSetUp : public testing::Test { | ||||
public: | ||||
static void SetUpTestSuite() { EXPECT_TRUE(false); } | ||||
}; | ||||
TEST_F(TestSuiteThatFailsToSetUp, ShouldNotRun) { std::abort(); } | ||||
// The main function. | // The main function. | |||
// | // | |||
// The idea is to use Google Test to run all the tests we have defined (some | // The idea is to use Google Test to run all the tests we have defined (some | |||
// of them are intended to fail), and then compare the test results | // of them are intended to fail), and then compare the test results | |||
// with the "golden" file. | // with the "golden" file. | |||
int main(int argc, char **argv) { | int main(int argc, char** argv) { | |||
testing::GTEST_FLAG(print_time) = false; | GTEST_FLAG_SET(print_time, false); | |||
// We just run the tests, knowing some of them are intended to fail. | // We just run the tests, knowing some of them are intended to fail. | |||
// We will use a separate Python script to compare the output of | // We will use a separate Python script to compare the output of | |||
// this program with the golden file. | // this program with the golden file. | |||
// It's hard to test InitGoogleTest() directly, as it has many | // It's hard to test InitGoogleTest() directly, as it has many | |||
// global side effects. The following line serves as a sanity test | // global side effects. The following line serves as a test | |||
// for it. | // for it. | |||
testing::InitGoogleTest(&argc, argv); | testing::InitGoogleTest(&argc, argv); | |||
bool internal_skip_environment_and_ad_hoc_tests = | bool internal_skip_environment_and_ad_hoc_tests = | |||
std::count(argv, argv + argc, | std::count(argv, argv + argc, | |||
std::string("internal_skip_environment_and_ad_hoc_tests")) > 0; | std::string("internal_skip_environment_and_ad_hoc_tests")) > 0; | |||
#if GTEST_HAS_DEATH_TEST | #if GTEST_HAS_DEATH_TEST | |||
if (testing::internal::GTEST_FLAG(internal_run_death_test) != "") { | if (GTEST_FLAG_GET(internal_run_death_test) != "") { | |||
// Skip the usual output capturing if we're running as the child | // Skip the usual output capturing if we're running as the child | |||
// process of an threadsafe-style death test. | // process of an threadsafe-style death test. | |||
# if GTEST_OS_WINDOWS | #if GTEST_OS_WINDOWS | |||
posix::FReopen("nul:", "w", stdout); | posix::FReopen("nul:", "w", stdout); | |||
# else | #else | |||
posix::FReopen("/dev/null", "w", stdout); | posix::FReopen("/dev/null", "w", stdout); | |||
# endif // GTEST_OS_WINDOWS | #endif // GTEST_OS_WINDOWS | |||
return RUN_ALL_TESTS(); | return RUN_ALL_TESTS(); | |||
} | } | |||
#endif // GTEST_HAS_DEATH_TEST | #endif // GTEST_HAS_DEATH_TEST | |||
if (internal_skip_environment_and_ad_hoc_tests) | if (internal_skip_environment_and_ad_hoc_tests) return RUN_ALL_TESTS(); | |||
return RUN_ALL_TESTS(); | ||||
// Registers two global test environments. | // Registers two global test environments. | |||
// The golden file verifies that they are set up in the order they | // The golden file verifies that they are set up in the order they | |||
// are registered, and torn down in the reverse order. | // are registered, and torn down in the reverse order. | |||
testing::AddGlobalTestEnvironment(new FooEnvironment); | testing::AddGlobalTestEnvironment(new FooEnvironment); | |||
testing::AddGlobalTestEnvironment(new BarEnvironment); | testing::AddGlobalTestEnvironment(new BarEnvironment); | |||
#if _MSC_VER | #if _MSC_VER | |||
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4127 | GTEST_DISABLE_MSC_WARNINGS_POP_() // 4127 | |||
#endif // _MSC_VER | #endif // _MSC_VER | |||
return RunAllTests(); | return RunAllTests(); | |||
} | } | |||
End of changes. 70 change blocks. | ||||
156 lines changed or deleted | 102 lines changed or added |