1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/media/webrtc/trunk/testing/gtest/test/gtest_output_test_.cc Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,1034 @@ 1.4 +// Copyright 2005, Google Inc. 1.5 +// All rights reserved. 1.6 +// 1.7 +// Redistribution and use in source and binary forms, with or without 1.8 +// modification, are permitted provided that the following conditions are 1.9 +// met: 1.10 +// 1.11 +// * Redistributions of source code must retain the above copyright 1.12 +// notice, this list of conditions and the following disclaimer. 1.13 +// * Redistributions in binary form must reproduce the above 1.14 +// copyright notice, this list of conditions and the following disclaimer 1.15 +// in the documentation and/or other materials provided with the 1.16 +// distribution. 1.17 +// * Neither the name of Google Inc. nor the names of its 1.18 +// contributors may be used to endorse or promote products derived from 1.19 +// this software without specific prior written permission. 1.20 +// 1.21 +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1.22 +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1.23 +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1.24 +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 1.25 +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 1.26 +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 1.27 +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 1.28 +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 1.29 +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.30 +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.31 +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.32 +// 1.33 +// The purpose of this file is to generate Google Test output under 1.34 +// various conditions. The output will then be verified by 1.35 +// gtest_output_test.py to ensure that Google Test generates the 1.36 +// desired messages. Therefore, most tests in this file are MEANT TO 1.37 +// FAIL. 1.38 +// 1.39 +// Author: wan@google.com (Zhanyong Wan) 1.40 + 1.41 +#include "gtest/gtest-spi.h" 1.42 +#include "gtest/gtest.h" 1.43 + 1.44 +// Indicates that this translation unit is part of Google Test's 1.45 +// implementation. It must come before gtest-internal-inl.h is 1.46 +// included, or there will be a compiler error. This trick is to 1.47 +// prevent a user from accidentally including gtest-internal-inl.h in 1.48 +// his code. 1.49 +#define GTEST_IMPLEMENTATION_ 1 1.50 +#include "src/gtest-internal-inl.h" 1.51 +#undef GTEST_IMPLEMENTATION_ 1.52 + 1.53 +#include <stdlib.h> 1.54 + 1.55 +#if GTEST_IS_THREADSAFE 1.56 +using testing::ScopedFakeTestPartResultReporter; 1.57 +using testing::TestPartResultArray; 1.58 + 1.59 +using testing::internal::Notification; 1.60 +using testing::internal::ThreadWithParam; 1.61 +#endif 1.62 + 1.63 +namespace posix = ::testing::internal::posix; 1.64 +using testing::internal::String; 1.65 +using testing::internal::scoped_ptr; 1.66 + 1.67 +// Tests catching fatal failures. 1.68 + 1.69 +// A subroutine used by the following test. 1.70 +void TestEq1(int x) { 1.71 + ASSERT_EQ(1, x); 1.72 +} 1.73 + 1.74 +// This function calls a test subroutine, catches the fatal failure it 1.75 +// generates, and then returns early. 1.76 +void TryTestSubroutine() { 1.77 + // Calls a subrountine that yields a fatal failure. 1.78 + TestEq1(2); 1.79 + 1.80 + // Catches the fatal failure and aborts the test. 1.81 + // 1.82 + // The testing::Test:: prefix is necessary when calling 1.83 + // HasFatalFailure() outside of a TEST, TEST_F, or test fixture. 1.84 + if (testing::Test::HasFatalFailure()) return; 1.85 + 1.86 + // If we get here, something is wrong. 1.87 + FAIL() << "This should never be reached."; 1.88 +} 1.89 + 1.90 +TEST(PassingTest, PassingTest1) { 1.91 +} 1.92 + 1.93 +TEST(PassingTest, PassingTest2) { 1.94 +} 1.95 + 1.96 +// Tests that parameters of failing parameterized tests are printed in the 1.97 +// failing test summary. 1.98 +class FailingParamTest : public testing::TestWithParam<int> {}; 1.99 + 1.100 +TEST_P(FailingParamTest, Fails) { 1.101 + EXPECT_EQ(1, GetParam()); 1.102 +} 1.103 + 1.104 +// This generates a test which will fail. Google Test is expected to print 1.105 +// its parameter when it outputs the list of all failed tests. 1.106 +INSTANTIATE_TEST_CASE_P(PrintingFailingParams, 1.107 + FailingParamTest, 1.108 + testing::Values(2)); 1.109 + 1.110 +static const char kGoldenString[] = "\"Line\0 1\"\nLine 2"; 1.111 + 1.112 +TEST(NonfatalFailureTest, EscapesStringOperands) { 1.113 + std::string actual = "actual \"string\""; 1.114 + EXPECT_EQ(kGoldenString, actual); 1.115 + 1.116 + const char* golden = kGoldenString; 1.117 + EXPECT_EQ(golden, actual); 1.118 +} 1.119 + 1.120 +// Tests catching a fatal failure in a subroutine. 1.121 +TEST(FatalFailureTest, FatalFailureInSubroutine) { 1.122 + printf("(expecting a failure that x should be 1)\n"); 1.123 + 1.124 + TryTestSubroutine(); 1.125 +} 1.126 + 1.127 +// Tests catching a fatal failure in a nested subroutine. 1.128 +TEST(FatalFailureTest, FatalFailureInNestedSubroutine) { 1.129 + printf("(expecting a failure that x should be 1)\n"); 1.130 + 1.131 + // Calls a subrountine that yields a fatal failure. 1.132 + TryTestSubroutine(); 1.133 + 1.134 + // Catches the fatal failure and aborts the test. 1.135 + // 1.136 + // When calling HasFatalFailure() inside a TEST, TEST_F, or test 1.137 + // fixture, the testing::Test:: prefix is not needed. 1.138 + if (HasFatalFailure()) return; 1.139 + 1.140 + // If we get here, something is wrong. 1.141 + FAIL() << "This should never be reached."; 1.142 +} 1.143 + 1.144 +// Tests HasFatalFailure() after a failed EXPECT check. 1.145 +TEST(FatalFailureTest, NonfatalFailureInSubroutine) { 1.146 + printf("(expecting a failure on false)\n"); 1.147 + EXPECT_TRUE(false); // Generates a nonfatal failure 1.148 + ASSERT_FALSE(HasFatalFailure()); // This should succeed. 1.149 +} 1.150 + 1.151 +// Tests interleaving user logging and Google Test assertions. 1.152 +TEST(LoggingTest, InterleavingLoggingAndAssertions) { 1.153 + static const int a[4] = { 1.154 + 3, 9, 2, 6 1.155 + }; 1.156 + 1.157 + printf("(expecting 2 failures on (3) >= (a[i]))\n"); 1.158 + for (int i = 0; i < static_cast<int>(sizeof(a)/sizeof(*a)); i++) { 1.159 + printf("i == %d\n", i); 1.160 + EXPECT_GE(3, a[i]); 1.161 + } 1.162 +} 1.163 + 1.164 +// Tests the SCOPED_TRACE macro. 1.165 + 1.166 +// A helper function for testing SCOPED_TRACE. 1.167 +void SubWithoutTrace(int n) { 1.168 + EXPECT_EQ(1, n); 1.169 + ASSERT_EQ(2, n); 1.170 +} 1.171 + 1.172 +// Another helper function for testing SCOPED_TRACE. 1.173 +void SubWithTrace(int n) { 1.174 + SCOPED_TRACE(testing::Message() << "n = " << n); 1.175 + 1.176 + SubWithoutTrace(n); 1.177 +} 1.178 + 1.179 +// Tests that SCOPED_TRACE() obeys lexical scopes. 1.180 +TEST(SCOPED_TRACETest, ObeysScopes) { 1.181 + printf("(expected to fail)\n"); 1.182 + 1.183 + // There should be no trace before SCOPED_TRACE() is invoked. 1.184 + ADD_FAILURE() << "This failure is expected, and shouldn't have a trace."; 1.185 + 1.186 + { 1.187 + SCOPED_TRACE("Expected trace"); 1.188 + // After SCOPED_TRACE(), a failure in the current scope should contain 1.189 + // the trace. 1.190 + ADD_FAILURE() << "This failure is expected, and should have a trace."; 1.191 + } 1.192 + 1.193 + // Once the control leaves the scope of the SCOPED_TRACE(), there 1.194 + // should be no trace again. 1.195 + ADD_FAILURE() << "This failure is expected, and shouldn't have a trace."; 1.196 +} 1.197 + 1.198 +// Tests that SCOPED_TRACE works inside a loop. 1.199 +TEST(SCOPED_TRACETest, WorksInLoop) { 1.200 + printf("(expected to fail)\n"); 1.201 + 1.202 + for (int i = 1; i <= 2; i++) { 1.203 + SCOPED_TRACE(testing::Message() << "i = " << i); 1.204 + 1.205 + SubWithoutTrace(i); 1.206 + } 1.207 +} 1.208 + 1.209 +// Tests that SCOPED_TRACE works in a subroutine. 1.210 +TEST(SCOPED_TRACETest, WorksInSubroutine) { 1.211 + printf("(expected to fail)\n"); 1.212 + 1.213 + SubWithTrace(1); 1.214 + SubWithTrace(2); 1.215 +} 1.216 + 1.217 +// Tests that SCOPED_TRACE can be nested. 1.218 +TEST(SCOPED_TRACETest, CanBeNested) { 1.219 + printf("(expected to fail)\n"); 1.220 + 1.221 + SCOPED_TRACE(""); // A trace without a message. 1.222 + 1.223 + SubWithTrace(2); 1.224 +} 1.225 + 1.226 +// Tests that multiple SCOPED_TRACEs can be used in the same scope. 1.227 +TEST(SCOPED_TRACETest, CanBeRepeated) { 1.228 + printf("(expected to fail)\n"); 1.229 + 1.230 + SCOPED_TRACE("A"); 1.231 + ADD_FAILURE() 1.232 + << "This failure is expected, and should contain trace point A."; 1.233 + 1.234 + SCOPED_TRACE("B"); 1.235 + ADD_FAILURE() 1.236 + << "This failure is expected, and should contain trace point A and B."; 1.237 + 1.238 + { 1.239 + SCOPED_TRACE("C"); 1.240 + ADD_FAILURE() << "This failure is expected, and should " 1.241 + << "contain trace point A, B, and C."; 1.242 + } 1.243 + 1.244 + SCOPED_TRACE("D"); 1.245 + ADD_FAILURE() << "This failure is expected, and should " 1.246 + << "contain trace point A, B, and D."; 1.247 +} 1.248 + 1.249 +#if GTEST_IS_THREADSAFE 1.250 +// Tests that SCOPED_TRACE()s can be used concurrently from multiple 1.251 +// threads. Namely, an assertion should be affected by 1.252 +// SCOPED_TRACE()s in its own thread only. 1.253 + 1.254 +// Here's the sequence of actions that happen in the test: 1.255 +// 1.256 +// Thread A (main) | Thread B (spawned) 1.257 +// ===============================|================================ 1.258 +// spawns thread B | 1.259 +// -------------------------------+-------------------------------- 1.260 +// waits for n1 | SCOPED_TRACE("Trace B"); 1.261 +// | generates failure #1 1.262 +// | notifies n1 1.263 +// -------------------------------+-------------------------------- 1.264 +// SCOPED_TRACE("Trace A"); | waits for n2 1.265 +// generates failure #2 | 1.266 +// notifies n2 | 1.267 +// -------------------------------|-------------------------------- 1.268 +// waits for n3 | generates failure #3 1.269 +// | trace B dies 1.270 +// | generates failure #4 1.271 +// | notifies n3 1.272 +// -------------------------------|-------------------------------- 1.273 +// generates failure #5 | finishes 1.274 +// trace A dies | 1.275 +// generates failure #6 | 1.276 +// -------------------------------|-------------------------------- 1.277 +// waits for thread B to finish | 1.278 + 1.279 +struct CheckPoints { 1.280 + Notification n1; 1.281 + Notification n2; 1.282 + Notification n3; 1.283 +}; 1.284 + 1.285 +static void ThreadWithScopedTrace(CheckPoints* check_points) { 1.286 + { 1.287 + SCOPED_TRACE("Trace B"); 1.288 + ADD_FAILURE() 1.289 + << "Expected failure #1 (in thread B, only trace B alive)."; 1.290 + check_points->n1.Notify(); 1.291 + check_points->n2.WaitForNotification(); 1.292 + 1.293 + ADD_FAILURE() 1.294 + << "Expected failure #3 (in thread B, trace A & B both alive)."; 1.295 + } // Trace B dies here. 1.296 + ADD_FAILURE() 1.297 + << "Expected failure #4 (in thread B, only trace A alive)."; 1.298 + check_points->n3.Notify(); 1.299 +} 1.300 + 1.301 +TEST(SCOPED_TRACETest, WorksConcurrently) { 1.302 + printf("(expecting 6 failures)\n"); 1.303 + 1.304 + CheckPoints check_points; 1.305 + ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace, 1.306 + &check_points, 1.307 + NULL); 1.308 + check_points.n1.WaitForNotification(); 1.309 + 1.310 + { 1.311 + SCOPED_TRACE("Trace A"); 1.312 + ADD_FAILURE() 1.313 + << "Expected failure #2 (in thread A, trace A & B both alive)."; 1.314 + check_points.n2.Notify(); 1.315 + check_points.n3.WaitForNotification(); 1.316 + 1.317 + ADD_FAILURE() 1.318 + << "Expected failure #5 (in thread A, only trace A alive)."; 1.319 + } // Trace A dies here. 1.320 + ADD_FAILURE() 1.321 + << "Expected failure #6 (in thread A, no trace alive)."; 1.322 + thread.Join(); 1.323 +} 1.324 +#endif // GTEST_IS_THREADSAFE 1.325 + 1.326 +TEST(DisabledTestsWarningTest, 1.327 + DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) { 1.328 + // This test body is intentionally empty. Its sole purpose is for 1.329 + // verifying that the --gtest_also_run_disabled_tests flag 1.330 + // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of 1.331 + // the test output. 1.332 +} 1.333 + 1.334 +// Tests using assertions outside of TEST and TEST_F. 1.335 +// 1.336 +// This function creates two failures intentionally. 1.337 +void AdHocTest() { 1.338 + printf("The non-test part of the code is expected to have 2 failures.\n\n"); 1.339 + EXPECT_TRUE(false); 1.340 + EXPECT_EQ(2, 3); 1.341 +} 1.342 + 1.343 +// Runs all TESTs, all TEST_Fs, and the ad hoc test. 1.344 +int RunAllTests() { 1.345 + AdHocTest(); 1.346 + return RUN_ALL_TESTS(); 1.347 +} 1.348 + 1.349 +// Tests non-fatal failures in the fixture constructor. 1.350 +class NonFatalFailureInFixtureConstructorTest : public testing::Test { 1.351 + protected: 1.352 + NonFatalFailureInFixtureConstructorTest() { 1.353 + printf("(expecting 5 failures)\n"); 1.354 + ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor."; 1.355 + } 1.356 + 1.357 + ~NonFatalFailureInFixtureConstructorTest() { 1.358 + ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor."; 1.359 + } 1.360 + 1.361 + virtual void SetUp() { 1.362 + ADD_FAILURE() << "Expected failure #2, in SetUp()."; 1.363 + } 1.364 + 1.365 + virtual void TearDown() { 1.366 + ADD_FAILURE() << "Expected failure #4, in TearDown."; 1.367 + } 1.368 +}; 1.369 + 1.370 +TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor) { 1.371 + ADD_FAILURE() << "Expected failure #3, in the test body."; 1.372 +} 1.373 + 1.374 +// Tests fatal failures in the fixture constructor. 1.375 +class FatalFailureInFixtureConstructorTest : public testing::Test { 1.376 + protected: 1.377 + FatalFailureInFixtureConstructorTest() { 1.378 + printf("(expecting 2 failures)\n"); 1.379 + Init(); 1.380 + } 1.381 + 1.382 + ~FatalFailureInFixtureConstructorTest() { 1.383 + ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor."; 1.384 + } 1.385 + 1.386 + virtual void SetUp() { 1.387 + ADD_FAILURE() << "UNEXPECTED failure in SetUp(). " 1.388 + << "We should never get here, as the test fixture c'tor " 1.389 + << "had a fatal failure."; 1.390 + } 1.391 + 1.392 + virtual void TearDown() { 1.393 + ADD_FAILURE() << "UNEXPECTED failure in TearDown(). " 1.394 + << "We should never get here, as the test fixture c'tor " 1.395 + << "had a fatal failure."; 1.396 + } 1.397 + 1.398 + private: 1.399 + void Init() { 1.400 + FAIL() << "Expected failure #1, in the test fixture c'tor."; 1.401 + } 1.402 +}; 1.403 + 1.404 +TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) { 1.405 + ADD_FAILURE() << "UNEXPECTED failure in the test body. " 1.406 + << "We should never get here, as the test fixture c'tor " 1.407 + << "had a fatal failure."; 1.408 +} 1.409 + 1.410 +// Tests non-fatal failures in SetUp(). 1.411 +class NonFatalFailureInSetUpTest : public testing::Test { 1.412 + protected: 1.413 + virtual ~NonFatalFailureInSetUpTest() { 1.414 + Deinit(); 1.415 + } 1.416 + 1.417 + virtual void SetUp() { 1.418 + printf("(expecting 4 failures)\n"); 1.419 + ADD_FAILURE() << "Expected failure #1, in SetUp()."; 1.420 + } 1.421 + 1.422 + virtual void TearDown() { 1.423 + FAIL() << "Expected failure #3, in TearDown()."; 1.424 + } 1.425 + private: 1.426 + void Deinit() { 1.427 + FAIL() << "Expected failure #4, in the test fixture d'tor."; 1.428 + } 1.429 +}; 1.430 + 1.431 +TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) { 1.432 + FAIL() << "Expected failure #2, in the test function."; 1.433 +} 1.434 + 1.435 +// Tests fatal failures in SetUp(). 1.436 +class FatalFailureInSetUpTest : public testing::Test { 1.437 + protected: 1.438 + virtual ~FatalFailureInSetUpTest() { 1.439 + Deinit(); 1.440 + } 1.441 + 1.442 + virtual void SetUp() { 1.443 + printf("(expecting 3 failures)\n"); 1.444 + FAIL() << "Expected failure #1, in SetUp()."; 1.445 + } 1.446 + 1.447 + virtual void TearDown() { 1.448 + FAIL() << "Expected failure #2, in TearDown()."; 1.449 + } 1.450 + private: 1.451 + void Deinit() { 1.452 + FAIL() << "Expected failure #3, in the test fixture d'tor."; 1.453 + } 1.454 +}; 1.455 + 1.456 +TEST_F(FatalFailureInSetUpTest, FailureInSetUp) { 1.457 + FAIL() << "UNEXPECTED failure in the test function. " 1.458 + << "We should never get here, as SetUp() failed."; 1.459 +} 1.460 + 1.461 +TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) { 1.462 + ADD_FAILURE_AT("foo.cc", 42) << "Expected failure in foo.cc"; 1.463 +} 1.464 + 1.465 +#if GTEST_IS_THREADSAFE 1.466 + 1.467 +// A unary function that may die. 1.468 +void DieIf(bool should_die) { 1.469 + GTEST_CHECK_(!should_die) << " - death inside DieIf()."; 1.470 +} 1.471 + 1.472 +// Tests running death tests in a multi-threaded context. 1.473 + 1.474 +// Used for coordination between the main and the spawn thread. 1.475 +struct SpawnThreadNotifications { 1.476 + SpawnThreadNotifications() {} 1.477 + 1.478 + Notification spawn_thread_started; 1.479 + Notification spawn_thread_ok_to_terminate; 1.480 + 1.481 + private: 1.482 + GTEST_DISALLOW_COPY_AND_ASSIGN_(SpawnThreadNotifications); 1.483 +}; 1.484 + 1.485 +// The function to be executed in the thread spawn by the 1.486 +// MultipleThreads test (below). 1.487 +static void ThreadRoutine(SpawnThreadNotifications* notifications) { 1.488 + // Signals the main thread that this thread has started. 1.489 + notifications->spawn_thread_started.Notify(); 1.490 + 1.491 + // Waits for permission to finish from the main thread. 1.492 + notifications->spawn_thread_ok_to_terminate.WaitForNotification(); 1.493 +} 1.494 + 1.495 +// This is a death-test test, but it's not named with a DeathTest 1.496 +// suffix. It starts threads which might interfere with later 1.497 +// death tests, so it must run after all other death tests. 1.498 +class DeathTestAndMultiThreadsTest : public testing::Test { 1.499 + protected: 1.500 + // Starts a thread and waits for it to begin. 1.501 + virtual void SetUp() { 1.502 + thread_.reset(new ThreadWithParam<SpawnThreadNotifications*>( 1.503 + &ThreadRoutine, ¬ifications_, NULL)); 1.504 + notifications_.spawn_thread_started.WaitForNotification(); 1.505 + } 1.506 + // Tells the thread to finish, and reaps it. 1.507 + // Depending on the version of the thread library in use, 1.508 + // a manager thread might still be left running that will interfere 1.509 + // with later death tests. This is unfortunate, but this class 1.510 + // cleans up after itself as best it can. 1.511 + virtual void TearDown() { 1.512 + notifications_.spawn_thread_ok_to_terminate.Notify(); 1.513 + } 1.514 + 1.515 + private: 1.516 + SpawnThreadNotifications notifications_; 1.517 + scoped_ptr<ThreadWithParam<SpawnThreadNotifications*> > thread_; 1.518 +}; 1.519 + 1.520 +#endif // GTEST_IS_THREADSAFE 1.521 + 1.522 +// The MixedUpTestCaseTest test case verifies that Google Test will fail a 1.523 +// test if it uses a different fixture class than what other tests in 1.524 +// the same test case use. It deliberately contains two fixture 1.525 +// classes with the same name but defined in different namespaces. 1.526 + 1.527 +// The MixedUpTestCaseWithSameTestNameTest test case verifies that 1.528 +// when the user defines two tests with the same test case name AND 1.529 +// same test name (but in different namespaces), the second test will 1.530 +// fail. 1.531 + 1.532 +namespace foo { 1.533 + 1.534 +class MixedUpTestCaseTest : public testing::Test { 1.535 +}; 1.536 + 1.537 +TEST_F(MixedUpTestCaseTest, FirstTestFromNamespaceFoo) {} 1.538 +TEST_F(MixedUpTestCaseTest, SecondTestFromNamespaceFoo) {} 1.539 + 1.540 +class MixedUpTestCaseWithSameTestNameTest : public testing::Test { 1.541 +}; 1.542 + 1.543 +TEST_F(MixedUpTestCaseWithSameTestNameTest, 1.544 + TheSecondTestWithThisNameShouldFail) {} 1.545 + 1.546 +} // namespace foo 1.547 + 1.548 +namespace bar { 1.549 + 1.550 +class MixedUpTestCaseTest : public testing::Test { 1.551 +}; 1.552 + 1.553 +// The following two tests are expected to fail. We rely on the 1.554 +// golden file to check that Google Test generates the right error message. 1.555 +TEST_F(MixedUpTestCaseTest, ThisShouldFail) {} 1.556 +TEST_F(MixedUpTestCaseTest, ThisShouldFailToo) {} 1.557 + 1.558 +class MixedUpTestCaseWithSameTestNameTest : public testing::Test { 1.559 +}; 1.560 + 1.561 +// Expected to fail. We rely on the golden file to check that Google Test 1.562 +// generates the right error message. 1.563 +TEST_F(MixedUpTestCaseWithSameTestNameTest, 1.564 + TheSecondTestWithThisNameShouldFail) {} 1.565 + 1.566 +} // namespace bar 1.567 + 1.568 +// The following two test cases verify that Google Test catches the user 1.569 +// error of mixing TEST and TEST_F in the same test case. The first 1.570 +// test case checks the scenario where TEST_F appears before TEST, and 1.571 +// the second one checks where TEST appears before TEST_F. 1.572 + 1.573 +class TEST_F_before_TEST_in_same_test_case : public testing::Test { 1.574 +}; 1.575 + 1.576 +TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {} 1.577 + 1.578 +// Expected to fail. We rely on the golden file to check that Google Test 1.579 +// generates the right error message. 1.580 +TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {} 1.581 + 1.582 +class TEST_before_TEST_F_in_same_test_case : public testing::Test { 1.583 +}; 1.584 + 1.585 +TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {} 1.586 + 1.587 +// Expected to fail. We rely on the golden file to check that Google Test 1.588 +// generates the right error message. 1.589 +TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) { 1.590 +} 1.591 + 1.592 +// Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE(). 1.593 +int global_integer = 0; 1.594 + 1.595 +// Tests that EXPECT_NONFATAL_FAILURE() can reference global variables. 1.596 +TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) { 1.597 + global_integer = 0; 1.598 + EXPECT_NONFATAL_FAILURE({ 1.599 + EXPECT_EQ(1, global_integer) << "Expected non-fatal failure."; 1.600 + }, "Expected non-fatal failure."); 1.601 +} 1.602 + 1.603 +// Tests that EXPECT_NONFATAL_FAILURE() can reference local variables 1.604 +// (static or not). 1.605 +TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) { 1.606 + int m = 0; 1.607 + static int n; 1.608 + n = 1; 1.609 + EXPECT_NONFATAL_FAILURE({ 1.610 + EXPECT_EQ(m, n) << "Expected non-fatal failure."; 1.611 + }, "Expected non-fatal failure."); 1.612 +} 1.613 + 1.614 +// Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly 1.615 +// one non-fatal failure and no fatal failure. 1.616 +TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) { 1.617 + EXPECT_NONFATAL_FAILURE({ 1.618 + ADD_FAILURE() << "Expected non-fatal failure."; 1.619 + }, "Expected non-fatal failure."); 1.620 +} 1.621 + 1.622 +// Tests that EXPECT_NONFATAL_FAILURE() fails when there is no 1.623 +// non-fatal failure. 1.624 +TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) { 1.625 + printf("(expecting a failure)\n"); 1.626 + EXPECT_NONFATAL_FAILURE({ 1.627 + }, ""); 1.628 +} 1.629 + 1.630 +// Tests that EXPECT_NONFATAL_FAILURE() fails when there are two 1.631 +// non-fatal failures. 1.632 +TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) { 1.633 + printf("(expecting a failure)\n"); 1.634 + EXPECT_NONFATAL_FAILURE({ 1.635 + ADD_FAILURE() << "Expected non-fatal failure 1."; 1.636 + ADD_FAILURE() << "Expected non-fatal failure 2."; 1.637 + }, ""); 1.638 +} 1.639 + 1.640 +// Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal 1.641 +// failure. 1.642 +TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) { 1.643 + printf("(expecting a failure)\n"); 1.644 + EXPECT_NONFATAL_FAILURE({ 1.645 + FAIL() << "Expected fatal failure."; 1.646 + }, ""); 1.647 +} 1.648 + 1.649 +// Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being 1.650 +// tested returns. 1.651 +TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) { 1.652 + printf("(expecting a failure)\n"); 1.653 + EXPECT_NONFATAL_FAILURE({ 1.654 + return; 1.655 + }, ""); 1.656 +} 1.657 + 1.658 +#if GTEST_HAS_EXCEPTIONS 1.659 + 1.660 +// Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being 1.661 +// tested throws. 1.662 +TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) { 1.663 + printf("(expecting a failure)\n"); 1.664 + try { 1.665 + EXPECT_NONFATAL_FAILURE({ 1.666 + throw 0; 1.667 + }, ""); 1.668 + } catch(int) { // NOLINT 1.669 + } 1.670 +} 1.671 + 1.672 +#endif // GTEST_HAS_EXCEPTIONS 1.673 + 1.674 +// Tests that EXPECT_FATAL_FAILURE() can reference global variables. 1.675 +TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) { 1.676 + global_integer = 0; 1.677 + EXPECT_FATAL_FAILURE({ 1.678 + ASSERT_EQ(1, global_integer) << "Expected fatal failure."; 1.679 + }, "Expected fatal failure."); 1.680 +} 1.681 + 1.682 +// Tests that EXPECT_FATAL_FAILURE() can reference local static 1.683 +// variables. 1.684 +TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) { 1.685 + static int n; 1.686 + n = 1; 1.687 + EXPECT_FATAL_FAILURE({ 1.688 + ASSERT_EQ(0, n) << "Expected fatal failure."; 1.689 + }, "Expected fatal failure."); 1.690 +} 1.691 + 1.692 +// Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly 1.693 +// one fatal failure and no non-fatal failure. 1.694 +TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) { 1.695 + EXPECT_FATAL_FAILURE({ 1.696 + FAIL() << "Expected fatal failure."; 1.697 + }, "Expected fatal failure."); 1.698 +} 1.699 + 1.700 +// Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal 1.701 +// failure. 1.702 +TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) { 1.703 + printf("(expecting a failure)\n"); 1.704 + EXPECT_FATAL_FAILURE({ 1.705 + }, ""); 1.706 +} 1.707 + 1.708 +// A helper for generating a fatal failure. 1.709 +void FatalFailure() { 1.710 + FAIL() << "Expected fatal failure."; 1.711 +} 1.712 + 1.713 +// Tests that EXPECT_FATAL_FAILURE() fails when there are two 1.714 +// fatal failures. 1.715 +TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) { 1.716 + printf("(expecting a failure)\n"); 1.717 + EXPECT_FATAL_FAILURE({ 1.718 + FatalFailure(); 1.719 + FatalFailure(); 1.720 + }, ""); 1.721 +} 1.722 + 1.723 +// Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal 1.724 +// failure. 1.725 +TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) { 1.726 + printf("(expecting a failure)\n"); 1.727 + EXPECT_FATAL_FAILURE({ 1.728 + ADD_FAILURE() << "Expected non-fatal failure."; 1.729 + }, ""); 1.730 +} 1.731 + 1.732 +// Tests that EXPECT_FATAL_FAILURE() fails when the statement being 1.733 +// tested returns. 1.734 +TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) { 1.735 + printf("(expecting a failure)\n"); 1.736 + EXPECT_FATAL_FAILURE({ 1.737 + return; 1.738 + }, ""); 1.739 +} 1.740 + 1.741 +#if GTEST_HAS_EXCEPTIONS 1.742 + 1.743 +// Tests that EXPECT_FATAL_FAILURE() fails when the statement being 1.744 +// tested throws. 1.745 +TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) { 1.746 + printf("(expecting a failure)\n"); 1.747 + try { 1.748 + EXPECT_FATAL_FAILURE({ 1.749 + throw 0; 1.750 + }, ""); 1.751 + } catch(int) { // NOLINT 1.752 + } 1.753 +} 1.754 + 1.755 +#endif // GTEST_HAS_EXCEPTIONS 1.756 + 1.757 +// This #ifdef block tests the output of typed tests. 1.758 +#if GTEST_HAS_TYPED_TEST 1.759 + 1.760 +template <typename T> 1.761 +class TypedTest : public testing::Test { 1.762 +}; 1.763 + 1.764 +TYPED_TEST_CASE(TypedTest, testing::Types<int>); 1.765 + 1.766 +TYPED_TEST(TypedTest, Success) { 1.767 + EXPECT_EQ(0, TypeParam()); 1.768 +} 1.769 + 1.770 +TYPED_TEST(TypedTest, Failure) { 1.771 + EXPECT_EQ(1, TypeParam()) << "Expected failure"; 1.772 +} 1.773 + 1.774 +#endif // GTEST_HAS_TYPED_TEST 1.775 + 1.776 +// This #ifdef block tests the output of type-parameterized tests. 1.777 +#if GTEST_HAS_TYPED_TEST_P 1.778 + 1.779 +template <typename T> 1.780 +class TypedTestP : public testing::Test { 1.781 +}; 1.782 + 1.783 +TYPED_TEST_CASE_P(TypedTestP); 1.784 + 1.785 +TYPED_TEST_P(TypedTestP, Success) { 1.786 + EXPECT_EQ(0U, TypeParam()); 1.787 +} 1.788 + 1.789 +TYPED_TEST_P(TypedTestP, Failure) { 1.790 + EXPECT_EQ(1U, TypeParam()) << "Expected failure"; 1.791 +} 1.792 + 1.793 +REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure); 1.794 + 1.795 +typedef testing::Types<unsigned char, unsigned int> UnsignedTypes; 1.796 +INSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes); 1.797 + 1.798 +#endif // GTEST_HAS_TYPED_TEST_P 1.799 + 1.800 +#if GTEST_HAS_DEATH_TEST 1.801 + 1.802 +// We rely on the golden file to verify that tests whose test case 1.803 +// name ends with DeathTest are run first. 1.804 + 1.805 +TEST(ADeathTest, ShouldRunFirst) { 1.806 +} 1.807 + 1.808 +# if GTEST_HAS_TYPED_TEST 1.809 + 1.810 +// We rely on the golden file to verify that typed tests whose test 1.811 +// case name ends with DeathTest are run first. 1.812 + 1.813 +template <typename T> 1.814 +class ATypedDeathTest : public testing::Test { 1.815 +}; 1.816 + 1.817 +typedef testing::Types<int, double> NumericTypes; 1.818 +TYPED_TEST_CASE(ATypedDeathTest, NumericTypes); 1.819 + 1.820 +TYPED_TEST(ATypedDeathTest, ShouldRunFirst) { 1.821 +} 1.822 + 1.823 +# endif // GTEST_HAS_TYPED_TEST 1.824 + 1.825 +# if GTEST_HAS_TYPED_TEST_P 1.826 + 1.827 + 1.828 +// We rely on the golden file to verify that type-parameterized tests 1.829 +// whose test case name ends with DeathTest are run first. 1.830 + 1.831 +template <typename T> 1.832 +class ATypeParamDeathTest : public testing::Test { 1.833 +}; 1.834 + 1.835 +TYPED_TEST_CASE_P(ATypeParamDeathTest); 1.836 + 1.837 +TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) { 1.838 +} 1.839 + 1.840 +REGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest, ShouldRunFirst); 1.841 + 1.842 +INSTANTIATE_TYPED_TEST_CASE_P(My, ATypeParamDeathTest, NumericTypes); 1.843 + 1.844 +# endif // GTEST_HAS_TYPED_TEST_P 1.845 + 1.846 +#endif // GTEST_HAS_DEATH_TEST 1.847 + 1.848 +// Tests various failure conditions of 1.849 +// EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}. 1.850 +class ExpectFailureTest : public testing::Test { 1.851 + public: // Must be public and not protected due to a bug in g++ 3.4.2. 1.852 + enum FailureMode { 1.853 + FATAL_FAILURE, 1.854 + NONFATAL_FAILURE 1.855 + }; 1.856 + static void AddFailure(FailureMode failure) { 1.857 + if (failure == FATAL_FAILURE) { 1.858 + FAIL() << "Expected fatal failure."; 1.859 + } else { 1.860 + ADD_FAILURE() << "Expected non-fatal failure."; 1.861 + } 1.862 + } 1.863 +}; 1.864 + 1.865 +TEST_F(ExpectFailureTest, ExpectFatalFailure) { 1.866 + // Expected fatal failure, but succeeds. 1.867 + printf("(expecting 1 failure)\n"); 1.868 + EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure."); 1.869 + // Expected fatal failure, but got a non-fatal failure. 1.870 + printf("(expecting 1 failure)\n"); 1.871 + EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Expected non-fatal " 1.872 + "failure."); 1.873 + // Wrong message. 1.874 + printf("(expecting 1 failure)\n"); 1.875 + EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Some other fatal failure " 1.876 + "expected."); 1.877 +} 1.878 + 1.879 +TEST_F(ExpectFailureTest, ExpectNonFatalFailure) { 1.880 + // Expected non-fatal failure, but succeeds. 1.881 + printf("(expecting 1 failure)\n"); 1.882 + EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure."); 1.883 + // Expected non-fatal failure, but got a fatal failure. 1.884 + printf("(expecting 1 failure)\n"); 1.885 + EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure."); 1.886 + // Wrong message. 1.887 + printf("(expecting 1 failure)\n"); 1.888 + EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Some other non-fatal " 1.889 + "failure."); 1.890 +} 1.891 + 1.892 +#if GTEST_IS_THREADSAFE 1.893 + 1.894 +class ExpectFailureWithThreadsTest : public ExpectFailureTest { 1.895 + protected: 1.896 + static void AddFailureInOtherThread(FailureMode failure) { 1.897 + ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL); 1.898 + thread.Join(); 1.899 + } 1.900 +}; 1.901 + 1.902 +TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) { 1.903 + // We only intercept the current thread. 1.904 + printf("(expecting 2 failures)\n"); 1.905 + EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE), 1.906 + "Expected fatal failure."); 1.907 +} 1.908 + 1.909 +TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) { 1.910 + // We only intercept the current thread. 1.911 + printf("(expecting 2 failures)\n"); 1.912 + EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE), 1.913 + "Expected non-fatal failure."); 1.914 +} 1.915 + 1.916 +typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest; 1.917 + 1.918 +// Tests that the ScopedFakeTestPartResultReporter only catches failures from 1.919 +// the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD. 1.920 +TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) { 1.921 + printf("(expecting 2 failures)\n"); 1.922 + TestPartResultArray results; 1.923 + { 1.924 + ScopedFakeTestPartResultReporter reporter( 1.925 + ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD, 1.926 + &results); 1.927 + AddFailureInOtherThread(FATAL_FAILURE); 1.928 + AddFailureInOtherThread(NONFATAL_FAILURE); 1.929 + } 1.930 + // The two failures should not have been intercepted. 1.931 + EXPECT_EQ(0, results.size()) << "This shouldn't fail."; 1.932 +} 1.933 + 1.934 +#endif // GTEST_IS_THREADSAFE 1.935 + 1.936 +TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) { 1.937 + // Expected fatal failure, but succeeds. 1.938 + printf("(expecting 1 failure)\n"); 1.939 + EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure."); 1.940 + // Expected fatal failure, but got a non-fatal failure. 1.941 + printf("(expecting 1 failure)\n"); 1.942 + EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE), 1.943 + "Expected non-fatal failure."); 1.944 + // Wrong message. 1.945 + printf("(expecting 1 failure)\n"); 1.946 + EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE), 1.947 + "Some other fatal failure expected."); 1.948 +} 1.949 + 1.950 +TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) { 1.951 + // Expected non-fatal failure, but succeeds. 1.952 + printf("(expecting 1 failure)\n"); 1.953 + EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal " 1.954 + "failure."); 1.955 + // Expected non-fatal failure, but got a fatal failure. 1.956 + printf("(expecting 1 failure)\n"); 1.957 + EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE), 1.958 + "Expected fatal failure."); 1.959 + // Wrong message. 1.960 + printf("(expecting 1 failure)\n"); 1.961 + EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE), 1.962 + "Some other non-fatal failure."); 1.963 +} 1.964 + 1.965 + 1.966 +// Two test environments for testing testing::AddGlobalTestEnvironment(). 1.967 + 1.968 +class FooEnvironment : public testing::Environment { 1.969 + public: 1.970 + virtual void SetUp() { 1.971 + printf("%s", "FooEnvironment::SetUp() called.\n"); 1.972 + } 1.973 + 1.974 + virtual void TearDown() { 1.975 + printf("%s", "FooEnvironment::TearDown() called.\n"); 1.976 + FAIL() << "Expected fatal failure."; 1.977 + } 1.978 +}; 1.979 + 1.980 +class BarEnvironment : public testing::Environment { 1.981 + public: 1.982 + virtual void SetUp() { 1.983 + printf("%s", "BarEnvironment::SetUp() called.\n"); 1.984 + } 1.985 + 1.986 + virtual void TearDown() { 1.987 + printf("%s", "BarEnvironment::TearDown() called.\n"); 1.988 + ADD_FAILURE() << "Expected non-fatal failure."; 1.989 + } 1.990 +}; 1.991 + 1.992 +bool GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = false; 1.993 + 1.994 +// The main function. 1.995 +// 1.996 +// The idea is to use Google Test to run all the tests we have defined (some 1.997 +// of them are intended to fail), and then compare the test results 1.998 +// with the "golden" file. 1.999 +int main(int argc, char **argv) { 1.1000 + testing::GTEST_FLAG(print_time) = false; 1.1001 + 1.1002 + // We just run the tests, knowing some of them are intended to fail. 1.1003 + // We will use a separate Python script to compare the output of 1.1004 + // this program with the golden file. 1.1005 + 1.1006 + // It's hard to test InitGoogleTest() directly, as it has many 1.1007 + // global side effects. The following line serves as a sanity test 1.1008 + // for it. 1.1009 + testing::InitGoogleTest(&argc, argv); 1.1010 + if (argc >= 2 && 1.1011 + String(argv[1]) == "--gtest_internal_skip_environment_and_ad_hoc_tests") 1.1012 + GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = true; 1.1013 + 1.1014 +#if GTEST_HAS_DEATH_TEST 1.1015 + if (testing::internal::GTEST_FLAG(internal_run_death_test) != "") { 1.1016 + // Skip the usual output capturing if we're running as the child 1.1017 + // process of an threadsafe-style death test. 1.1018 +# if GTEST_OS_WINDOWS 1.1019 + posix::FReopen("nul:", "w", stdout); 1.1020 +# else 1.1021 + posix::FReopen("/dev/null", "w", stdout); 1.1022 +# endif // GTEST_OS_WINDOWS 1.1023 + return RUN_ALL_TESTS(); 1.1024 + } 1.1025 +#endif // GTEST_HAS_DEATH_TEST 1.1026 + 1.1027 + if (GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests)) 1.1028 + return RUN_ALL_TESTS(); 1.1029 + 1.1030 + // Registers two global test environments. 1.1031 + // The golden file verifies that they are set up in the order they 1.1032 + // are registered, and torn down in the reverse order. 1.1033 + testing::AddGlobalTestEnvironment(new FooEnvironment); 1.1034 + testing::AddGlobalTestEnvironment(new BarEnvironment); 1.1035 + 1.1036 + return RunAllTests(); 1.1037 +}