PageRenderTime 68ms CodeModel.GetById 15ms app.highlight 46ms RepoModel.GetById 1ms app.codeStats 0ms

/thirdparty/breakpad/third_party/protobuf/protobuf/gtest/test/gtest_output_test_.cc

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