media/webrtc/trunk/testing/gtest/test/gtest_output_test_.cc

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 // Copyright 2005, Google Inc.
michael@0 2 // All rights reserved.
michael@0 3 //
michael@0 4 // Redistribution and use in source and binary forms, with or without
michael@0 5 // modification, are permitted provided that the following conditions are
michael@0 6 // met:
michael@0 7 //
michael@0 8 // * Redistributions of source code must retain the above copyright
michael@0 9 // notice, this list of conditions and the following disclaimer.
michael@0 10 // * Redistributions in binary form must reproduce the above
michael@0 11 // copyright notice, this list of conditions and the following disclaimer
michael@0 12 // in the documentation and/or other materials provided with the
michael@0 13 // distribution.
michael@0 14 // * Neither the name of Google Inc. nor the names of its
michael@0 15 // contributors may be used to endorse or promote products derived from
michael@0 16 // this software without specific prior written permission.
michael@0 17 //
michael@0 18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
michael@0 19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
michael@0 20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
michael@0 21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
michael@0 22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
michael@0 23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
michael@0 24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
michael@0 25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
michael@0 26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0 27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0 28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0 29 //
michael@0 30 // The purpose of this file is to generate Google Test output under
michael@0 31 // various conditions. The output will then be verified by
michael@0 32 // gtest_output_test.py to ensure that Google Test generates the
michael@0 33 // desired messages. Therefore, most tests in this file are MEANT TO
michael@0 34 // FAIL.
michael@0 35 //
michael@0 36 // Author: wan@google.com (Zhanyong Wan)
michael@0 37
michael@0 38 #include "gtest/gtest-spi.h"
michael@0 39 #include "gtest/gtest.h"
michael@0 40
michael@0 41 // Indicates that this translation unit is part of Google Test's
michael@0 42 // implementation. It must come before gtest-internal-inl.h is
michael@0 43 // included, or there will be a compiler error. This trick is to
michael@0 44 // prevent a user from accidentally including gtest-internal-inl.h in
michael@0 45 // his code.
michael@0 46 #define GTEST_IMPLEMENTATION_ 1
michael@0 47 #include "src/gtest-internal-inl.h"
michael@0 48 #undef GTEST_IMPLEMENTATION_
michael@0 49
michael@0 50 #include <stdlib.h>
michael@0 51
michael@0 52 #if GTEST_IS_THREADSAFE
michael@0 53 using testing::ScopedFakeTestPartResultReporter;
michael@0 54 using testing::TestPartResultArray;
michael@0 55
michael@0 56 using testing::internal::Notification;
michael@0 57 using testing::internal::ThreadWithParam;
michael@0 58 #endif
michael@0 59
michael@0 60 namespace posix = ::testing::internal::posix;
michael@0 61 using testing::internal::String;
michael@0 62 using testing::internal::scoped_ptr;
michael@0 63
michael@0 64 // Tests catching fatal failures.
michael@0 65
michael@0 66 // A subroutine used by the following test.
michael@0 67 void TestEq1(int x) {
michael@0 68 ASSERT_EQ(1, x);
michael@0 69 }
michael@0 70
michael@0 71 // This function calls a test subroutine, catches the fatal failure it
michael@0 72 // generates, and then returns early.
michael@0 73 void TryTestSubroutine() {
michael@0 74 // Calls a subrountine that yields a fatal failure.
michael@0 75 TestEq1(2);
michael@0 76
michael@0 77 // Catches the fatal failure and aborts the test.
michael@0 78 //
michael@0 79 // The testing::Test:: prefix is necessary when calling
michael@0 80 // HasFatalFailure() outside of a TEST, TEST_F, or test fixture.
michael@0 81 if (testing::Test::HasFatalFailure()) return;
michael@0 82
michael@0 83 // If we get here, something is wrong.
michael@0 84 FAIL() << "This should never be reached.";
michael@0 85 }
michael@0 86
michael@0 87 TEST(PassingTest, PassingTest1) {
michael@0 88 }
michael@0 89
michael@0 90 TEST(PassingTest, PassingTest2) {
michael@0 91 }
michael@0 92
michael@0 93 // Tests that parameters of failing parameterized tests are printed in the
michael@0 94 // failing test summary.
michael@0 95 class FailingParamTest : public testing::TestWithParam<int> {};
michael@0 96
michael@0 97 TEST_P(FailingParamTest, Fails) {
michael@0 98 EXPECT_EQ(1, GetParam());
michael@0 99 }
michael@0 100
michael@0 101 // This generates a test which will fail. Google Test is expected to print
michael@0 102 // its parameter when it outputs the list of all failed tests.
michael@0 103 INSTANTIATE_TEST_CASE_P(PrintingFailingParams,
michael@0 104 FailingParamTest,
michael@0 105 testing::Values(2));
michael@0 106
michael@0 107 static const char kGoldenString[] = "\"Line\0 1\"\nLine 2";
michael@0 108
michael@0 109 TEST(NonfatalFailureTest, EscapesStringOperands) {
michael@0 110 std::string actual = "actual \"string\"";
michael@0 111 EXPECT_EQ(kGoldenString, actual);
michael@0 112
michael@0 113 const char* golden = kGoldenString;
michael@0 114 EXPECT_EQ(golden, actual);
michael@0 115 }
michael@0 116
michael@0 117 // Tests catching a fatal failure in a subroutine.
michael@0 118 TEST(FatalFailureTest, FatalFailureInSubroutine) {
michael@0 119 printf("(expecting a failure that x should be 1)\n");
michael@0 120
michael@0 121 TryTestSubroutine();
michael@0 122 }
michael@0 123
michael@0 124 // Tests catching a fatal failure in a nested subroutine.
michael@0 125 TEST(FatalFailureTest, FatalFailureInNestedSubroutine) {
michael@0 126 printf("(expecting a failure that x should be 1)\n");
michael@0 127
michael@0 128 // Calls a subrountine that yields a fatal failure.
michael@0 129 TryTestSubroutine();
michael@0 130
michael@0 131 // Catches the fatal failure and aborts the test.
michael@0 132 //
michael@0 133 // When calling HasFatalFailure() inside a TEST, TEST_F, or test
michael@0 134 // fixture, the testing::Test:: prefix is not needed.
michael@0 135 if (HasFatalFailure()) return;
michael@0 136
michael@0 137 // If we get here, something is wrong.
michael@0 138 FAIL() << "This should never be reached.";
michael@0 139 }
michael@0 140
michael@0 141 // Tests HasFatalFailure() after a failed EXPECT check.
michael@0 142 TEST(FatalFailureTest, NonfatalFailureInSubroutine) {
michael@0 143 printf("(expecting a failure on false)\n");
michael@0 144 EXPECT_TRUE(false); // Generates a nonfatal failure
michael@0 145 ASSERT_FALSE(HasFatalFailure()); // This should succeed.
michael@0 146 }
michael@0 147
michael@0 148 // Tests interleaving user logging and Google Test assertions.
michael@0 149 TEST(LoggingTest, InterleavingLoggingAndAssertions) {
michael@0 150 static const int a[4] = {
michael@0 151 3, 9, 2, 6
michael@0 152 };
michael@0 153
michael@0 154 printf("(expecting 2 failures on (3) >= (a[i]))\n");
michael@0 155 for (int i = 0; i < static_cast<int>(sizeof(a)/sizeof(*a)); i++) {
michael@0 156 printf("i == %d\n", i);
michael@0 157 EXPECT_GE(3, a[i]);
michael@0 158 }
michael@0 159 }
michael@0 160
michael@0 161 // Tests the SCOPED_TRACE macro.
michael@0 162
michael@0 163 // A helper function for testing SCOPED_TRACE.
michael@0 164 void SubWithoutTrace(int n) {
michael@0 165 EXPECT_EQ(1, n);
michael@0 166 ASSERT_EQ(2, n);
michael@0 167 }
michael@0 168
michael@0 169 // Another helper function for testing SCOPED_TRACE.
michael@0 170 void SubWithTrace(int n) {
michael@0 171 SCOPED_TRACE(testing::Message() << "n = " << n);
michael@0 172
michael@0 173 SubWithoutTrace(n);
michael@0 174 }
michael@0 175
michael@0 176 // Tests that SCOPED_TRACE() obeys lexical scopes.
michael@0 177 TEST(SCOPED_TRACETest, ObeysScopes) {
michael@0 178 printf("(expected to fail)\n");
michael@0 179
michael@0 180 // There should be no trace before SCOPED_TRACE() is invoked.
michael@0 181 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
michael@0 182
michael@0 183 {
michael@0 184 SCOPED_TRACE("Expected trace");
michael@0 185 // After SCOPED_TRACE(), a failure in the current scope should contain
michael@0 186 // the trace.
michael@0 187 ADD_FAILURE() << "This failure is expected, and should have a trace.";
michael@0 188 }
michael@0 189
michael@0 190 // Once the control leaves the scope of the SCOPED_TRACE(), there
michael@0 191 // should be no trace again.
michael@0 192 ADD_FAILURE() << "This failure is expected, and shouldn't have a trace.";
michael@0 193 }
michael@0 194
michael@0 195 // Tests that SCOPED_TRACE works inside a loop.
michael@0 196 TEST(SCOPED_TRACETest, WorksInLoop) {
michael@0 197 printf("(expected to fail)\n");
michael@0 198
michael@0 199 for (int i = 1; i <= 2; i++) {
michael@0 200 SCOPED_TRACE(testing::Message() << "i = " << i);
michael@0 201
michael@0 202 SubWithoutTrace(i);
michael@0 203 }
michael@0 204 }
michael@0 205
michael@0 206 // Tests that SCOPED_TRACE works in a subroutine.
michael@0 207 TEST(SCOPED_TRACETest, WorksInSubroutine) {
michael@0 208 printf("(expected to fail)\n");
michael@0 209
michael@0 210 SubWithTrace(1);
michael@0 211 SubWithTrace(2);
michael@0 212 }
michael@0 213
michael@0 214 // Tests that SCOPED_TRACE can be nested.
michael@0 215 TEST(SCOPED_TRACETest, CanBeNested) {
michael@0 216 printf("(expected to fail)\n");
michael@0 217
michael@0 218 SCOPED_TRACE(""); // A trace without a message.
michael@0 219
michael@0 220 SubWithTrace(2);
michael@0 221 }
michael@0 222
michael@0 223 // Tests that multiple SCOPED_TRACEs can be used in the same scope.
michael@0 224 TEST(SCOPED_TRACETest, CanBeRepeated) {
michael@0 225 printf("(expected to fail)\n");
michael@0 226
michael@0 227 SCOPED_TRACE("A");
michael@0 228 ADD_FAILURE()
michael@0 229 << "This failure is expected, and should contain trace point A.";
michael@0 230
michael@0 231 SCOPED_TRACE("B");
michael@0 232 ADD_FAILURE()
michael@0 233 << "This failure is expected, and should contain trace point A and B.";
michael@0 234
michael@0 235 {
michael@0 236 SCOPED_TRACE("C");
michael@0 237 ADD_FAILURE() << "This failure is expected, and should "
michael@0 238 << "contain trace point A, B, and C.";
michael@0 239 }
michael@0 240
michael@0 241 SCOPED_TRACE("D");
michael@0 242 ADD_FAILURE() << "This failure is expected, and should "
michael@0 243 << "contain trace point A, B, and D.";
michael@0 244 }
michael@0 245
michael@0 246 #if GTEST_IS_THREADSAFE
michael@0 247 // Tests that SCOPED_TRACE()s can be used concurrently from multiple
michael@0 248 // threads. Namely, an assertion should be affected by
michael@0 249 // SCOPED_TRACE()s in its own thread only.
michael@0 250
michael@0 251 // Here's the sequence of actions that happen in the test:
michael@0 252 //
michael@0 253 // Thread A (main) | Thread B (spawned)
michael@0 254 // ===============================|================================
michael@0 255 // spawns thread B |
michael@0 256 // -------------------------------+--------------------------------
michael@0 257 // waits for n1 | SCOPED_TRACE("Trace B");
michael@0 258 // | generates failure #1
michael@0 259 // | notifies n1
michael@0 260 // -------------------------------+--------------------------------
michael@0 261 // SCOPED_TRACE("Trace A"); | waits for n2
michael@0 262 // generates failure #2 |
michael@0 263 // notifies n2 |
michael@0 264 // -------------------------------|--------------------------------
michael@0 265 // waits for n3 | generates failure #3
michael@0 266 // | trace B dies
michael@0 267 // | generates failure #4
michael@0 268 // | notifies n3
michael@0 269 // -------------------------------|--------------------------------
michael@0 270 // generates failure #5 | finishes
michael@0 271 // trace A dies |
michael@0 272 // generates failure #6 |
michael@0 273 // -------------------------------|--------------------------------
michael@0 274 // waits for thread B to finish |
michael@0 275
michael@0 276 struct CheckPoints {
michael@0 277 Notification n1;
michael@0 278 Notification n2;
michael@0 279 Notification n3;
michael@0 280 };
michael@0 281
michael@0 282 static void ThreadWithScopedTrace(CheckPoints* check_points) {
michael@0 283 {
michael@0 284 SCOPED_TRACE("Trace B");
michael@0 285 ADD_FAILURE()
michael@0 286 << "Expected failure #1 (in thread B, only trace B alive).";
michael@0 287 check_points->n1.Notify();
michael@0 288 check_points->n2.WaitForNotification();
michael@0 289
michael@0 290 ADD_FAILURE()
michael@0 291 << "Expected failure #3 (in thread B, trace A & B both alive).";
michael@0 292 } // Trace B dies here.
michael@0 293 ADD_FAILURE()
michael@0 294 << "Expected failure #4 (in thread B, only trace A alive).";
michael@0 295 check_points->n3.Notify();
michael@0 296 }
michael@0 297
michael@0 298 TEST(SCOPED_TRACETest, WorksConcurrently) {
michael@0 299 printf("(expecting 6 failures)\n");
michael@0 300
michael@0 301 CheckPoints check_points;
michael@0 302 ThreadWithParam<CheckPoints*> thread(&ThreadWithScopedTrace,
michael@0 303 &check_points,
michael@0 304 NULL);
michael@0 305 check_points.n1.WaitForNotification();
michael@0 306
michael@0 307 {
michael@0 308 SCOPED_TRACE("Trace A");
michael@0 309 ADD_FAILURE()
michael@0 310 << "Expected failure #2 (in thread A, trace A & B both alive).";
michael@0 311 check_points.n2.Notify();
michael@0 312 check_points.n3.WaitForNotification();
michael@0 313
michael@0 314 ADD_FAILURE()
michael@0 315 << "Expected failure #5 (in thread A, only trace A alive).";
michael@0 316 } // Trace A dies here.
michael@0 317 ADD_FAILURE()
michael@0 318 << "Expected failure #6 (in thread A, no trace alive).";
michael@0 319 thread.Join();
michael@0 320 }
michael@0 321 #endif // GTEST_IS_THREADSAFE
michael@0 322
michael@0 323 TEST(DisabledTestsWarningTest,
michael@0 324 DISABLED_AlsoRunDisabledTestsFlagSuppressesWarning) {
michael@0 325 // This test body is intentionally empty. Its sole purpose is for
michael@0 326 // verifying that the --gtest_also_run_disabled_tests flag
michael@0 327 // suppresses the "YOU HAVE 12 DISABLED TESTS" warning at the end of
michael@0 328 // the test output.
michael@0 329 }
michael@0 330
michael@0 331 // Tests using assertions outside of TEST and TEST_F.
michael@0 332 //
michael@0 333 // This function creates two failures intentionally.
michael@0 334 void AdHocTest() {
michael@0 335 printf("The non-test part of the code is expected to have 2 failures.\n\n");
michael@0 336 EXPECT_TRUE(false);
michael@0 337 EXPECT_EQ(2, 3);
michael@0 338 }
michael@0 339
michael@0 340 // Runs all TESTs, all TEST_Fs, and the ad hoc test.
michael@0 341 int RunAllTests() {
michael@0 342 AdHocTest();
michael@0 343 return RUN_ALL_TESTS();
michael@0 344 }
michael@0 345
michael@0 346 // Tests non-fatal failures in the fixture constructor.
michael@0 347 class NonFatalFailureInFixtureConstructorTest : public testing::Test {
michael@0 348 protected:
michael@0 349 NonFatalFailureInFixtureConstructorTest() {
michael@0 350 printf("(expecting 5 failures)\n");
michael@0 351 ADD_FAILURE() << "Expected failure #1, in the test fixture c'tor.";
michael@0 352 }
michael@0 353
michael@0 354 ~NonFatalFailureInFixtureConstructorTest() {
michael@0 355 ADD_FAILURE() << "Expected failure #5, in the test fixture d'tor.";
michael@0 356 }
michael@0 357
michael@0 358 virtual void SetUp() {
michael@0 359 ADD_FAILURE() << "Expected failure #2, in SetUp().";
michael@0 360 }
michael@0 361
michael@0 362 virtual void TearDown() {
michael@0 363 ADD_FAILURE() << "Expected failure #4, in TearDown.";
michael@0 364 }
michael@0 365 };
michael@0 366
michael@0 367 TEST_F(NonFatalFailureInFixtureConstructorTest, FailureInConstructor) {
michael@0 368 ADD_FAILURE() << "Expected failure #3, in the test body.";
michael@0 369 }
michael@0 370
michael@0 371 // Tests fatal failures in the fixture constructor.
michael@0 372 class FatalFailureInFixtureConstructorTest : public testing::Test {
michael@0 373 protected:
michael@0 374 FatalFailureInFixtureConstructorTest() {
michael@0 375 printf("(expecting 2 failures)\n");
michael@0 376 Init();
michael@0 377 }
michael@0 378
michael@0 379 ~FatalFailureInFixtureConstructorTest() {
michael@0 380 ADD_FAILURE() << "Expected failure #2, in the test fixture d'tor.";
michael@0 381 }
michael@0 382
michael@0 383 virtual void SetUp() {
michael@0 384 ADD_FAILURE() << "UNEXPECTED failure in SetUp(). "
michael@0 385 << "We should never get here, as the test fixture c'tor "
michael@0 386 << "had a fatal failure.";
michael@0 387 }
michael@0 388
michael@0 389 virtual void TearDown() {
michael@0 390 ADD_FAILURE() << "UNEXPECTED failure in TearDown(). "
michael@0 391 << "We should never get here, as the test fixture c'tor "
michael@0 392 << "had a fatal failure.";
michael@0 393 }
michael@0 394
michael@0 395 private:
michael@0 396 void Init() {
michael@0 397 FAIL() << "Expected failure #1, in the test fixture c'tor.";
michael@0 398 }
michael@0 399 };
michael@0 400
michael@0 401 TEST_F(FatalFailureInFixtureConstructorTest, FailureInConstructor) {
michael@0 402 ADD_FAILURE() << "UNEXPECTED failure in the test body. "
michael@0 403 << "We should never get here, as the test fixture c'tor "
michael@0 404 << "had a fatal failure.";
michael@0 405 }
michael@0 406
michael@0 407 // Tests non-fatal failures in SetUp().
michael@0 408 class NonFatalFailureInSetUpTest : public testing::Test {
michael@0 409 protected:
michael@0 410 virtual ~NonFatalFailureInSetUpTest() {
michael@0 411 Deinit();
michael@0 412 }
michael@0 413
michael@0 414 virtual void SetUp() {
michael@0 415 printf("(expecting 4 failures)\n");
michael@0 416 ADD_FAILURE() << "Expected failure #1, in SetUp().";
michael@0 417 }
michael@0 418
michael@0 419 virtual void TearDown() {
michael@0 420 FAIL() << "Expected failure #3, in TearDown().";
michael@0 421 }
michael@0 422 private:
michael@0 423 void Deinit() {
michael@0 424 FAIL() << "Expected failure #4, in the test fixture d'tor.";
michael@0 425 }
michael@0 426 };
michael@0 427
michael@0 428 TEST_F(NonFatalFailureInSetUpTest, FailureInSetUp) {
michael@0 429 FAIL() << "Expected failure #2, in the test function.";
michael@0 430 }
michael@0 431
michael@0 432 // Tests fatal failures in SetUp().
michael@0 433 class FatalFailureInSetUpTest : public testing::Test {
michael@0 434 protected:
michael@0 435 virtual ~FatalFailureInSetUpTest() {
michael@0 436 Deinit();
michael@0 437 }
michael@0 438
michael@0 439 virtual void SetUp() {
michael@0 440 printf("(expecting 3 failures)\n");
michael@0 441 FAIL() << "Expected failure #1, in SetUp().";
michael@0 442 }
michael@0 443
michael@0 444 virtual void TearDown() {
michael@0 445 FAIL() << "Expected failure #2, in TearDown().";
michael@0 446 }
michael@0 447 private:
michael@0 448 void Deinit() {
michael@0 449 FAIL() << "Expected failure #3, in the test fixture d'tor.";
michael@0 450 }
michael@0 451 };
michael@0 452
michael@0 453 TEST_F(FatalFailureInSetUpTest, FailureInSetUp) {
michael@0 454 FAIL() << "UNEXPECTED failure in the test function. "
michael@0 455 << "We should never get here, as SetUp() failed.";
michael@0 456 }
michael@0 457
michael@0 458 TEST(AddFailureAtTest, MessageContainsSpecifiedFileAndLineNumber) {
michael@0 459 ADD_FAILURE_AT("foo.cc", 42) << "Expected failure in foo.cc";
michael@0 460 }
michael@0 461
michael@0 462 #if GTEST_IS_THREADSAFE
michael@0 463
michael@0 464 // A unary function that may die.
michael@0 465 void DieIf(bool should_die) {
michael@0 466 GTEST_CHECK_(!should_die) << " - death inside DieIf().";
michael@0 467 }
michael@0 468
michael@0 469 // Tests running death tests in a multi-threaded context.
michael@0 470
michael@0 471 // Used for coordination between the main and the spawn thread.
michael@0 472 struct SpawnThreadNotifications {
michael@0 473 SpawnThreadNotifications() {}
michael@0 474
michael@0 475 Notification spawn_thread_started;
michael@0 476 Notification spawn_thread_ok_to_terminate;
michael@0 477
michael@0 478 private:
michael@0 479 GTEST_DISALLOW_COPY_AND_ASSIGN_(SpawnThreadNotifications);
michael@0 480 };
michael@0 481
michael@0 482 // The function to be executed in the thread spawn by the
michael@0 483 // MultipleThreads test (below).
michael@0 484 static void ThreadRoutine(SpawnThreadNotifications* notifications) {
michael@0 485 // Signals the main thread that this thread has started.
michael@0 486 notifications->spawn_thread_started.Notify();
michael@0 487
michael@0 488 // Waits for permission to finish from the main thread.
michael@0 489 notifications->spawn_thread_ok_to_terminate.WaitForNotification();
michael@0 490 }
michael@0 491
michael@0 492 // This is a death-test test, but it's not named with a DeathTest
michael@0 493 // suffix. It starts threads which might interfere with later
michael@0 494 // death tests, so it must run after all other death tests.
michael@0 495 class DeathTestAndMultiThreadsTest : public testing::Test {
michael@0 496 protected:
michael@0 497 // Starts a thread and waits for it to begin.
michael@0 498 virtual void SetUp() {
michael@0 499 thread_.reset(new ThreadWithParam<SpawnThreadNotifications*>(
michael@0 500 &ThreadRoutine, &notifications_, NULL));
michael@0 501 notifications_.spawn_thread_started.WaitForNotification();
michael@0 502 }
michael@0 503 // Tells the thread to finish, and reaps it.
michael@0 504 // Depending on the version of the thread library in use,
michael@0 505 // a manager thread might still be left running that will interfere
michael@0 506 // with later death tests. This is unfortunate, but this class
michael@0 507 // cleans up after itself as best it can.
michael@0 508 virtual void TearDown() {
michael@0 509 notifications_.spawn_thread_ok_to_terminate.Notify();
michael@0 510 }
michael@0 511
michael@0 512 private:
michael@0 513 SpawnThreadNotifications notifications_;
michael@0 514 scoped_ptr<ThreadWithParam<SpawnThreadNotifications*> > thread_;
michael@0 515 };
michael@0 516
michael@0 517 #endif // GTEST_IS_THREADSAFE
michael@0 518
michael@0 519 // The MixedUpTestCaseTest test case verifies that Google Test will fail a
michael@0 520 // test if it uses a different fixture class than what other tests in
michael@0 521 // the same test case use. It deliberately contains two fixture
michael@0 522 // classes with the same name but defined in different namespaces.
michael@0 523
michael@0 524 // The MixedUpTestCaseWithSameTestNameTest test case verifies that
michael@0 525 // when the user defines two tests with the same test case name AND
michael@0 526 // same test name (but in different namespaces), the second test will
michael@0 527 // fail.
michael@0 528
michael@0 529 namespace foo {
michael@0 530
michael@0 531 class MixedUpTestCaseTest : public testing::Test {
michael@0 532 };
michael@0 533
michael@0 534 TEST_F(MixedUpTestCaseTest, FirstTestFromNamespaceFoo) {}
michael@0 535 TEST_F(MixedUpTestCaseTest, SecondTestFromNamespaceFoo) {}
michael@0 536
michael@0 537 class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
michael@0 538 };
michael@0 539
michael@0 540 TEST_F(MixedUpTestCaseWithSameTestNameTest,
michael@0 541 TheSecondTestWithThisNameShouldFail) {}
michael@0 542
michael@0 543 } // namespace foo
michael@0 544
michael@0 545 namespace bar {
michael@0 546
michael@0 547 class MixedUpTestCaseTest : public testing::Test {
michael@0 548 };
michael@0 549
michael@0 550 // The following two tests are expected to fail. We rely on the
michael@0 551 // golden file to check that Google Test generates the right error message.
michael@0 552 TEST_F(MixedUpTestCaseTest, ThisShouldFail) {}
michael@0 553 TEST_F(MixedUpTestCaseTest, ThisShouldFailToo) {}
michael@0 554
michael@0 555 class MixedUpTestCaseWithSameTestNameTest : public testing::Test {
michael@0 556 };
michael@0 557
michael@0 558 // Expected to fail. We rely on the golden file to check that Google Test
michael@0 559 // generates the right error message.
michael@0 560 TEST_F(MixedUpTestCaseWithSameTestNameTest,
michael@0 561 TheSecondTestWithThisNameShouldFail) {}
michael@0 562
michael@0 563 } // namespace bar
michael@0 564
michael@0 565 // The following two test cases verify that Google Test catches the user
michael@0 566 // error of mixing TEST and TEST_F in the same test case. The first
michael@0 567 // test case checks the scenario where TEST_F appears before TEST, and
michael@0 568 // the second one checks where TEST appears before TEST_F.
michael@0 569
michael@0 570 class TEST_F_before_TEST_in_same_test_case : public testing::Test {
michael@0 571 };
michael@0 572
michael@0 573 TEST_F(TEST_F_before_TEST_in_same_test_case, DefinedUsingTEST_F) {}
michael@0 574
michael@0 575 // Expected to fail. We rely on the golden file to check that Google Test
michael@0 576 // generates the right error message.
michael@0 577 TEST(TEST_F_before_TEST_in_same_test_case, DefinedUsingTESTAndShouldFail) {}
michael@0 578
michael@0 579 class TEST_before_TEST_F_in_same_test_case : public testing::Test {
michael@0 580 };
michael@0 581
michael@0 582 TEST(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST) {}
michael@0 583
michael@0 584 // Expected to fail. We rely on the golden file to check that Google Test
michael@0 585 // generates the right error message.
michael@0 586 TEST_F(TEST_before_TEST_F_in_same_test_case, DefinedUsingTEST_FAndShouldFail) {
michael@0 587 }
michael@0 588
michael@0 589 // Used for testing EXPECT_NONFATAL_FAILURE() and EXPECT_FATAL_FAILURE().
michael@0 590 int global_integer = 0;
michael@0 591
michael@0 592 // Tests that EXPECT_NONFATAL_FAILURE() can reference global variables.
michael@0 593 TEST(ExpectNonfatalFailureTest, CanReferenceGlobalVariables) {
michael@0 594 global_integer = 0;
michael@0 595 EXPECT_NONFATAL_FAILURE({
michael@0 596 EXPECT_EQ(1, global_integer) << "Expected non-fatal failure.";
michael@0 597 }, "Expected non-fatal failure.");
michael@0 598 }
michael@0 599
michael@0 600 // Tests that EXPECT_NONFATAL_FAILURE() can reference local variables
michael@0 601 // (static or not).
michael@0 602 TEST(ExpectNonfatalFailureTest, CanReferenceLocalVariables) {
michael@0 603 int m = 0;
michael@0 604 static int n;
michael@0 605 n = 1;
michael@0 606 EXPECT_NONFATAL_FAILURE({
michael@0 607 EXPECT_EQ(m, n) << "Expected non-fatal failure.";
michael@0 608 }, "Expected non-fatal failure.");
michael@0 609 }
michael@0 610
michael@0 611 // Tests that EXPECT_NONFATAL_FAILURE() succeeds when there is exactly
michael@0 612 // one non-fatal failure and no fatal failure.
michael@0 613 TEST(ExpectNonfatalFailureTest, SucceedsWhenThereIsOneNonfatalFailure) {
michael@0 614 EXPECT_NONFATAL_FAILURE({
michael@0 615 ADD_FAILURE() << "Expected non-fatal failure.";
michael@0 616 }, "Expected non-fatal failure.");
michael@0 617 }
michael@0 618
michael@0 619 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is no
michael@0 620 // non-fatal failure.
michael@0 621 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsNoNonfatalFailure) {
michael@0 622 printf("(expecting a failure)\n");
michael@0 623 EXPECT_NONFATAL_FAILURE({
michael@0 624 }, "");
michael@0 625 }
michael@0 626
michael@0 627 // Tests that EXPECT_NONFATAL_FAILURE() fails when there are two
michael@0 628 // non-fatal failures.
michael@0 629 TEST(ExpectNonfatalFailureTest, FailsWhenThereAreTwoNonfatalFailures) {
michael@0 630 printf("(expecting a failure)\n");
michael@0 631 EXPECT_NONFATAL_FAILURE({
michael@0 632 ADD_FAILURE() << "Expected non-fatal failure 1.";
michael@0 633 ADD_FAILURE() << "Expected non-fatal failure 2.";
michael@0 634 }, "");
michael@0 635 }
michael@0 636
michael@0 637 // Tests that EXPECT_NONFATAL_FAILURE() fails when there is one fatal
michael@0 638 // failure.
michael@0 639 TEST(ExpectNonfatalFailureTest, FailsWhenThereIsOneFatalFailure) {
michael@0 640 printf("(expecting a failure)\n");
michael@0 641 EXPECT_NONFATAL_FAILURE({
michael@0 642 FAIL() << "Expected fatal failure.";
michael@0 643 }, "");
michael@0 644 }
michael@0 645
michael@0 646 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
michael@0 647 // tested returns.
michael@0 648 TEST(ExpectNonfatalFailureTest, FailsWhenStatementReturns) {
michael@0 649 printf("(expecting a failure)\n");
michael@0 650 EXPECT_NONFATAL_FAILURE({
michael@0 651 return;
michael@0 652 }, "");
michael@0 653 }
michael@0 654
michael@0 655 #if GTEST_HAS_EXCEPTIONS
michael@0 656
michael@0 657 // Tests that EXPECT_NONFATAL_FAILURE() fails when the statement being
michael@0 658 // tested throws.
michael@0 659 TEST(ExpectNonfatalFailureTest, FailsWhenStatementThrows) {
michael@0 660 printf("(expecting a failure)\n");
michael@0 661 try {
michael@0 662 EXPECT_NONFATAL_FAILURE({
michael@0 663 throw 0;
michael@0 664 }, "");
michael@0 665 } catch(int) { // NOLINT
michael@0 666 }
michael@0 667 }
michael@0 668
michael@0 669 #endif // GTEST_HAS_EXCEPTIONS
michael@0 670
michael@0 671 // Tests that EXPECT_FATAL_FAILURE() can reference global variables.
michael@0 672 TEST(ExpectFatalFailureTest, CanReferenceGlobalVariables) {
michael@0 673 global_integer = 0;
michael@0 674 EXPECT_FATAL_FAILURE({
michael@0 675 ASSERT_EQ(1, global_integer) << "Expected fatal failure.";
michael@0 676 }, "Expected fatal failure.");
michael@0 677 }
michael@0 678
michael@0 679 // Tests that EXPECT_FATAL_FAILURE() can reference local static
michael@0 680 // variables.
michael@0 681 TEST(ExpectFatalFailureTest, CanReferenceLocalStaticVariables) {
michael@0 682 static int n;
michael@0 683 n = 1;
michael@0 684 EXPECT_FATAL_FAILURE({
michael@0 685 ASSERT_EQ(0, n) << "Expected fatal failure.";
michael@0 686 }, "Expected fatal failure.");
michael@0 687 }
michael@0 688
michael@0 689 // Tests that EXPECT_FATAL_FAILURE() succeeds when there is exactly
michael@0 690 // one fatal failure and no non-fatal failure.
michael@0 691 TEST(ExpectFatalFailureTest, SucceedsWhenThereIsOneFatalFailure) {
michael@0 692 EXPECT_FATAL_FAILURE({
michael@0 693 FAIL() << "Expected fatal failure.";
michael@0 694 }, "Expected fatal failure.");
michael@0 695 }
michael@0 696
michael@0 697 // Tests that EXPECT_FATAL_FAILURE() fails when there is no fatal
michael@0 698 // failure.
michael@0 699 TEST(ExpectFatalFailureTest, FailsWhenThereIsNoFatalFailure) {
michael@0 700 printf("(expecting a failure)\n");
michael@0 701 EXPECT_FATAL_FAILURE({
michael@0 702 }, "");
michael@0 703 }
michael@0 704
michael@0 705 // A helper for generating a fatal failure.
michael@0 706 void FatalFailure() {
michael@0 707 FAIL() << "Expected fatal failure.";
michael@0 708 }
michael@0 709
michael@0 710 // Tests that EXPECT_FATAL_FAILURE() fails when there are two
michael@0 711 // fatal failures.
michael@0 712 TEST(ExpectFatalFailureTest, FailsWhenThereAreTwoFatalFailures) {
michael@0 713 printf("(expecting a failure)\n");
michael@0 714 EXPECT_FATAL_FAILURE({
michael@0 715 FatalFailure();
michael@0 716 FatalFailure();
michael@0 717 }, "");
michael@0 718 }
michael@0 719
michael@0 720 // Tests that EXPECT_FATAL_FAILURE() fails when there is one non-fatal
michael@0 721 // failure.
michael@0 722 TEST(ExpectFatalFailureTest, FailsWhenThereIsOneNonfatalFailure) {
michael@0 723 printf("(expecting a failure)\n");
michael@0 724 EXPECT_FATAL_FAILURE({
michael@0 725 ADD_FAILURE() << "Expected non-fatal failure.";
michael@0 726 }, "");
michael@0 727 }
michael@0 728
michael@0 729 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
michael@0 730 // tested returns.
michael@0 731 TEST(ExpectFatalFailureTest, FailsWhenStatementReturns) {
michael@0 732 printf("(expecting a failure)\n");
michael@0 733 EXPECT_FATAL_FAILURE({
michael@0 734 return;
michael@0 735 }, "");
michael@0 736 }
michael@0 737
michael@0 738 #if GTEST_HAS_EXCEPTIONS
michael@0 739
michael@0 740 // Tests that EXPECT_FATAL_FAILURE() fails when the statement being
michael@0 741 // tested throws.
michael@0 742 TEST(ExpectFatalFailureTest, FailsWhenStatementThrows) {
michael@0 743 printf("(expecting a failure)\n");
michael@0 744 try {
michael@0 745 EXPECT_FATAL_FAILURE({
michael@0 746 throw 0;
michael@0 747 }, "");
michael@0 748 } catch(int) { // NOLINT
michael@0 749 }
michael@0 750 }
michael@0 751
michael@0 752 #endif // GTEST_HAS_EXCEPTIONS
michael@0 753
michael@0 754 // This #ifdef block tests the output of typed tests.
michael@0 755 #if GTEST_HAS_TYPED_TEST
michael@0 756
michael@0 757 template <typename T>
michael@0 758 class TypedTest : public testing::Test {
michael@0 759 };
michael@0 760
michael@0 761 TYPED_TEST_CASE(TypedTest, testing::Types<int>);
michael@0 762
michael@0 763 TYPED_TEST(TypedTest, Success) {
michael@0 764 EXPECT_EQ(0, TypeParam());
michael@0 765 }
michael@0 766
michael@0 767 TYPED_TEST(TypedTest, Failure) {
michael@0 768 EXPECT_EQ(1, TypeParam()) << "Expected failure";
michael@0 769 }
michael@0 770
michael@0 771 #endif // GTEST_HAS_TYPED_TEST
michael@0 772
michael@0 773 // This #ifdef block tests the output of type-parameterized tests.
michael@0 774 #if GTEST_HAS_TYPED_TEST_P
michael@0 775
michael@0 776 template <typename T>
michael@0 777 class TypedTestP : public testing::Test {
michael@0 778 };
michael@0 779
michael@0 780 TYPED_TEST_CASE_P(TypedTestP);
michael@0 781
michael@0 782 TYPED_TEST_P(TypedTestP, Success) {
michael@0 783 EXPECT_EQ(0U, TypeParam());
michael@0 784 }
michael@0 785
michael@0 786 TYPED_TEST_P(TypedTestP, Failure) {
michael@0 787 EXPECT_EQ(1U, TypeParam()) << "Expected failure";
michael@0 788 }
michael@0 789
michael@0 790 REGISTER_TYPED_TEST_CASE_P(TypedTestP, Success, Failure);
michael@0 791
michael@0 792 typedef testing::Types<unsigned char, unsigned int> UnsignedTypes;
michael@0 793 INSTANTIATE_TYPED_TEST_CASE_P(Unsigned, TypedTestP, UnsignedTypes);
michael@0 794
michael@0 795 #endif // GTEST_HAS_TYPED_TEST_P
michael@0 796
michael@0 797 #if GTEST_HAS_DEATH_TEST
michael@0 798
michael@0 799 // We rely on the golden file to verify that tests whose test case
michael@0 800 // name ends with DeathTest are run first.
michael@0 801
michael@0 802 TEST(ADeathTest, ShouldRunFirst) {
michael@0 803 }
michael@0 804
michael@0 805 # if GTEST_HAS_TYPED_TEST
michael@0 806
michael@0 807 // We rely on the golden file to verify that typed tests whose test
michael@0 808 // case name ends with DeathTest are run first.
michael@0 809
michael@0 810 template <typename T>
michael@0 811 class ATypedDeathTest : public testing::Test {
michael@0 812 };
michael@0 813
michael@0 814 typedef testing::Types<int, double> NumericTypes;
michael@0 815 TYPED_TEST_CASE(ATypedDeathTest, NumericTypes);
michael@0 816
michael@0 817 TYPED_TEST(ATypedDeathTest, ShouldRunFirst) {
michael@0 818 }
michael@0 819
michael@0 820 # endif // GTEST_HAS_TYPED_TEST
michael@0 821
michael@0 822 # if GTEST_HAS_TYPED_TEST_P
michael@0 823
michael@0 824
michael@0 825 // We rely on the golden file to verify that type-parameterized tests
michael@0 826 // whose test case name ends with DeathTest are run first.
michael@0 827
michael@0 828 template <typename T>
michael@0 829 class ATypeParamDeathTest : public testing::Test {
michael@0 830 };
michael@0 831
michael@0 832 TYPED_TEST_CASE_P(ATypeParamDeathTest);
michael@0 833
michael@0 834 TYPED_TEST_P(ATypeParamDeathTest, ShouldRunFirst) {
michael@0 835 }
michael@0 836
michael@0 837 REGISTER_TYPED_TEST_CASE_P(ATypeParamDeathTest, ShouldRunFirst);
michael@0 838
michael@0 839 INSTANTIATE_TYPED_TEST_CASE_P(My, ATypeParamDeathTest, NumericTypes);
michael@0 840
michael@0 841 # endif // GTEST_HAS_TYPED_TEST_P
michael@0 842
michael@0 843 #endif // GTEST_HAS_DEATH_TEST
michael@0 844
michael@0 845 // Tests various failure conditions of
michael@0 846 // EXPECT_{,NON}FATAL_FAILURE{,_ON_ALL_THREADS}.
michael@0 847 class ExpectFailureTest : public testing::Test {
michael@0 848 public: // Must be public and not protected due to a bug in g++ 3.4.2.
michael@0 849 enum FailureMode {
michael@0 850 FATAL_FAILURE,
michael@0 851 NONFATAL_FAILURE
michael@0 852 };
michael@0 853 static void AddFailure(FailureMode failure) {
michael@0 854 if (failure == FATAL_FAILURE) {
michael@0 855 FAIL() << "Expected fatal failure.";
michael@0 856 } else {
michael@0 857 ADD_FAILURE() << "Expected non-fatal failure.";
michael@0 858 }
michael@0 859 }
michael@0 860 };
michael@0 861
michael@0 862 TEST_F(ExpectFailureTest, ExpectFatalFailure) {
michael@0 863 // Expected fatal failure, but succeeds.
michael@0 864 printf("(expecting 1 failure)\n");
michael@0 865 EXPECT_FATAL_FAILURE(SUCCEED(), "Expected fatal failure.");
michael@0 866 // Expected fatal failure, but got a non-fatal failure.
michael@0 867 printf("(expecting 1 failure)\n");
michael@0 868 EXPECT_FATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Expected non-fatal "
michael@0 869 "failure.");
michael@0 870 // Wrong message.
michael@0 871 printf("(expecting 1 failure)\n");
michael@0 872 EXPECT_FATAL_FAILURE(AddFailure(FATAL_FAILURE), "Some other fatal failure "
michael@0 873 "expected.");
michael@0 874 }
michael@0 875
michael@0 876 TEST_F(ExpectFailureTest, ExpectNonFatalFailure) {
michael@0 877 // Expected non-fatal failure, but succeeds.
michael@0 878 printf("(expecting 1 failure)\n");
michael@0 879 EXPECT_NONFATAL_FAILURE(SUCCEED(), "Expected non-fatal failure.");
michael@0 880 // Expected non-fatal failure, but got a fatal failure.
michael@0 881 printf("(expecting 1 failure)\n");
michael@0 882 EXPECT_NONFATAL_FAILURE(AddFailure(FATAL_FAILURE), "Expected fatal failure.");
michael@0 883 // Wrong message.
michael@0 884 printf("(expecting 1 failure)\n");
michael@0 885 EXPECT_NONFATAL_FAILURE(AddFailure(NONFATAL_FAILURE), "Some other non-fatal "
michael@0 886 "failure.");
michael@0 887 }
michael@0 888
michael@0 889 #if GTEST_IS_THREADSAFE
michael@0 890
michael@0 891 class ExpectFailureWithThreadsTest : public ExpectFailureTest {
michael@0 892 protected:
michael@0 893 static void AddFailureInOtherThread(FailureMode failure) {
michael@0 894 ThreadWithParam<FailureMode> thread(&AddFailure, failure, NULL);
michael@0 895 thread.Join();
michael@0 896 }
michael@0 897 };
michael@0 898
michael@0 899 TEST_F(ExpectFailureWithThreadsTest, ExpectFatalFailure) {
michael@0 900 // We only intercept the current thread.
michael@0 901 printf("(expecting 2 failures)\n");
michael@0 902 EXPECT_FATAL_FAILURE(AddFailureInOtherThread(FATAL_FAILURE),
michael@0 903 "Expected fatal failure.");
michael@0 904 }
michael@0 905
michael@0 906 TEST_F(ExpectFailureWithThreadsTest, ExpectNonFatalFailure) {
michael@0 907 // We only intercept the current thread.
michael@0 908 printf("(expecting 2 failures)\n");
michael@0 909 EXPECT_NONFATAL_FAILURE(AddFailureInOtherThread(NONFATAL_FAILURE),
michael@0 910 "Expected non-fatal failure.");
michael@0 911 }
michael@0 912
michael@0 913 typedef ExpectFailureWithThreadsTest ScopedFakeTestPartResultReporterTest;
michael@0 914
michael@0 915 // Tests that the ScopedFakeTestPartResultReporter only catches failures from
michael@0 916 // the current thread if it is instantiated with INTERCEPT_ONLY_CURRENT_THREAD.
michael@0 917 TEST_F(ScopedFakeTestPartResultReporterTest, InterceptOnlyCurrentThread) {
michael@0 918 printf("(expecting 2 failures)\n");
michael@0 919 TestPartResultArray results;
michael@0 920 {
michael@0 921 ScopedFakeTestPartResultReporter reporter(
michael@0 922 ScopedFakeTestPartResultReporter::INTERCEPT_ONLY_CURRENT_THREAD,
michael@0 923 &results);
michael@0 924 AddFailureInOtherThread(FATAL_FAILURE);
michael@0 925 AddFailureInOtherThread(NONFATAL_FAILURE);
michael@0 926 }
michael@0 927 // The two failures should not have been intercepted.
michael@0 928 EXPECT_EQ(0, results.size()) << "This shouldn't fail.";
michael@0 929 }
michael@0 930
michael@0 931 #endif // GTEST_IS_THREADSAFE
michael@0 932
michael@0 933 TEST_F(ExpectFailureTest, ExpectFatalFailureOnAllThreads) {
michael@0 934 // Expected fatal failure, but succeeds.
michael@0 935 printf("(expecting 1 failure)\n");
michael@0 936 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected fatal failure.");
michael@0 937 // Expected fatal failure, but got a non-fatal failure.
michael@0 938 printf("(expecting 1 failure)\n");
michael@0 939 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
michael@0 940 "Expected non-fatal failure.");
michael@0 941 // Wrong message.
michael@0 942 printf("(expecting 1 failure)\n");
michael@0 943 EXPECT_FATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
michael@0 944 "Some other fatal failure expected.");
michael@0 945 }
michael@0 946
michael@0 947 TEST_F(ExpectFailureTest, ExpectNonFatalFailureOnAllThreads) {
michael@0 948 // Expected non-fatal failure, but succeeds.
michael@0 949 printf("(expecting 1 failure)\n");
michael@0 950 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(SUCCEED(), "Expected non-fatal "
michael@0 951 "failure.");
michael@0 952 // Expected non-fatal failure, but got a fatal failure.
michael@0 953 printf("(expecting 1 failure)\n");
michael@0 954 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(FATAL_FAILURE),
michael@0 955 "Expected fatal failure.");
michael@0 956 // Wrong message.
michael@0 957 printf("(expecting 1 failure)\n");
michael@0 958 EXPECT_NONFATAL_FAILURE_ON_ALL_THREADS(AddFailure(NONFATAL_FAILURE),
michael@0 959 "Some other non-fatal failure.");
michael@0 960 }
michael@0 961
michael@0 962
michael@0 963 // Two test environments for testing testing::AddGlobalTestEnvironment().
michael@0 964
michael@0 965 class FooEnvironment : public testing::Environment {
michael@0 966 public:
michael@0 967 virtual void SetUp() {
michael@0 968 printf("%s", "FooEnvironment::SetUp() called.\n");
michael@0 969 }
michael@0 970
michael@0 971 virtual void TearDown() {
michael@0 972 printf("%s", "FooEnvironment::TearDown() called.\n");
michael@0 973 FAIL() << "Expected fatal failure.";
michael@0 974 }
michael@0 975 };
michael@0 976
michael@0 977 class BarEnvironment : public testing::Environment {
michael@0 978 public:
michael@0 979 virtual void SetUp() {
michael@0 980 printf("%s", "BarEnvironment::SetUp() called.\n");
michael@0 981 }
michael@0 982
michael@0 983 virtual void TearDown() {
michael@0 984 printf("%s", "BarEnvironment::TearDown() called.\n");
michael@0 985 ADD_FAILURE() << "Expected non-fatal failure.";
michael@0 986 }
michael@0 987 };
michael@0 988
michael@0 989 bool GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = false;
michael@0 990
michael@0 991 // The main function.
michael@0 992 //
michael@0 993 // The idea is to use Google Test to run all the tests we have defined (some
michael@0 994 // of them are intended to fail), and then compare the test results
michael@0 995 // with the "golden" file.
michael@0 996 int main(int argc, char **argv) {
michael@0 997 testing::GTEST_FLAG(print_time) = false;
michael@0 998
michael@0 999 // We just run the tests, knowing some of them are intended to fail.
michael@0 1000 // We will use a separate Python script to compare the output of
michael@0 1001 // this program with the golden file.
michael@0 1002
michael@0 1003 // It's hard to test InitGoogleTest() directly, as it has many
michael@0 1004 // global side effects. The following line serves as a sanity test
michael@0 1005 // for it.
michael@0 1006 testing::InitGoogleTest(&argc, argv);
michael@0 1007 if (argc >= 2 &&
michael@0 1008 String(argv[1]) == "--gtest_internal_skip_environment_and_ad_hoc_tests")
michael@0 1009 GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests) = true;
michael@0 1010
michael@0 1011 #if GTEST_HAS_DEATH_TEST
michael@0 1012 if (testing::internal::GTEST_FLAG(internal_run_death_test) != "") {
michael@0 1013 // Skip the usual output capturing if we're running as the child
michael@0 1014 // process of an threadsafe-style death test.
michael@0 1015 # if GTEST_OS_WINDOWS
michael@0 1016 posix::FReopen("nul:", "w", stdout);
michael@0 1017 # else
michael@0 1018 posix::FReopen("/dev/null", "w", stdout);
michael@0 1019 # endif // GTEST_OS_WINDOWS
michael@0 1020 return RUN_ALL_TESTS();
michael@0 1021 }
michael@0 1022 #endif // GTEST_HAS_DEATH_TEST
michael@0 1023
michael@0 1024 if (GTEST_FLAG(internal_skip_environment_and_ad_hoc_tests))
michael@0 1025 return RUN_ALL_TESTS();
michael@0 1026
michael@0 1027 // Registers two global test environments.
michael@0 1028 // The golden file verifies that they are set up in the order they
michael@0 1029 // are registered, and torn down in the reverse order.
michael@0 1030 testing::AddGlobalTestEnvironment(new FooEnvironment);
michael@0 1031 testing::AddGlobalTestEnvironment(new BarEnvironment);
michael@0 1032
michael@0 1033 return RunAllTests();
michael@0 1034 }

mercurial