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 // Author: wan (at) google.com (Zhanyong Wan) 31 // 32 // The Google C++ Testing Framework (Google Test) 33 // 34 // This header file defines the public API for Google Test. It should be 35 // included by any test program that uses Google Test. 36 // 37 // IMPORTANT NOTE: Due to limitation of the C++ language, we have to 38 // leave some internal implementation details in this header file. 39 // They are clearly marked by comments like this: 40 // 41 // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 42 // 43 // Such code is NOT meant to be used by a user directly, and is subject 44 // to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user 45 // program! 46 // 47 // Acknowledgment: Google Test borrowed the idea of automatic test 48 // registration from Barthelemy Dagenais' (barthelemy (at) prologique.com) 49 // easyUnit framework. 50 51 #ifndef GTEST_INCLUDE_GTEST_GTEST_H_ 52 #define GTEST_INCLUDE_GTEST_GTEST_H_ 53 54 // The following platform macros are used throughout Google Test: 55 // _WIN32_WCE Windows CE (set in project files) 56 // __SYMBIAN32__ Symbian (set by Symbian tool chain) 57 // 58 // Note that even though _MSC_VER and _WIN32_WCE really indicate a compiler 59 // and a Win32 implementation, respectively, we use them to indicate the 60 // combination of compiler - Win 32 API - C library, since the code currently 61 // only supports: 62 // Windows proper with Visual C++ and MS C library (_MSC_VER && !_WIN32_WCE) and 63 // Windows Mobile with Visual C++ and no C library (_WIN32_WCE). 64 65 #include <gtest/internal/gtest-internal.h> 66 #include <gtest/internal/gtest-string.h> 67 #include <gtest/gtest-death-test.h> 68 #include <gtest/gtest-message.h> 69 #include <gtest/gtest_prod.h> 70 71 // Depending on the platform, different string classes are available. 72 // On Windows, ::std::string compiles only when exceptions are 73 // enabled. On Linux, in addition to ::std::string, Google also makes 74 // use of class ::string, which has the same interface as 75 // ::std::string, but has a different implementation. 76 // 77 // The user can tell us whether ::std::string is available in his 78 // environment by defining the macro GTEST_HAS_STD_STRING to either 1 79 // or 0 on the compiler command line. He can also define 80 // GTEST_HAS_GLOBAL_STRING to 1 to indicate that ::string is available 81 // AND is a distinct type to ::std::string, or define it to 0 to 82 // indicate otherwise. 83 // 84 // If the user's ::std::string and ::string are the same class due to 85 // aliasing, he should define GTEST_HAS_STD_STRING to 1 and 86 // GTEST_HAS_GLOBAL_STRING to 0. 87 // 88 // If the user doesn't define GTEST_HAS_STD_STRING and/or 89 // GTEST_HAS_GLOBAL_STRING, they are defined heuristically. 90 91 namespace testing { 92 93 // The upper limit for valid stack trace depths. 94 const int kMaxStackTraceDepth = 100; 95 96 // This flag specifies the maximum number of stack frames to be 97 // printed in a failure message. 98 GTEST_DECLARE_int32(stack_trace_depth); 99 100 // This flag controls whether Google Test includes Google Test internal 101 // stack frames in failure stack traces. 102 GTEST_DECLARE_bool(show_internal_stack_frames); 103 104 // The possible outcomes of a test part (i.e. an assertion or an 105 // explicit SUCCEED(), FAIL(), or ADD_FAILURE()). 106 enum TestPartResultType { 107 TPRT_SUCCESS, // Succeeded. 108 TPRT_NONFATAL_FAILURE, // Failed but the test can continue. 109 TPRT_FATAL_FAILURE // Failed and the test should be terminated. 110 }; 111 112 namespace internal { 113 114 class GTestFlagSaver; 115 116 // Converts a streamable value to a String. A NULL pointer is 117 // converted to "(null)". When the input value is a ::string, 118 // ::std::string, ::wstring, or ::std::wstring object, each NUL 119 // character in it is replaced with "\\0". 120 // Declared in gtest-internal.h but defined here, so that it has access 121 // to the definition of the Message class, required by the ARM 122 // compiler. 123 template <typename T> 124 String StreamableToString(const T& streamable) { 125 return (Message() << streamable).GetString(); 126 } 127 128 } // namespace internal 129 130 // A class for indicating whether an assertion was successful. When 131 // the assertion wasn't successful, the AssertionResult object 132 // remembers a non-empty message that described how it failed. 133 // 134 // This class is useful for defining predicate-format functions to be 135 // used with predicate assertions (ASSERT_PRED_FORMAT*, etc). 136 // 137 // The constructor of AssertionResult is private. To create an 138 // instance of this class, use one of the factory functions 139 // (AssertionSuccess() and AssertionFailure()). 140 // 141 // For example, in order to be able to write: 142 // 143 // // Verifies that Foo() returns an even number. 144 // EXPECT_PRED_FORMAT1(IsEven, Foo()); 145 // 146 // you just need to define: 147 // 148 // testing::AssertionResult IsEven(const char* expr, int n) { 149 // if ((n % 2) == 0) return testing::AssertionSuccess(); 150 // 151 // Message msg; 152 // msg << "Expected: " << expr << " is even\n" 153 // << " Actual: it's " << n; 154 // return testing::AssertionFailure(msg); 155 // } 156 // 157 // If Foo() returns 5, you will see the following message: 158 // 159 // Expected: Foo() is even 160 // Actual: it's 5 161 class AssertionResult { 162 public: 163 // Declares factory functions for making successful and failed 164 // assertion results as friends. 165 friend AssertionResult AssertionSuccess(); 166 friend AssertionResult AssertionFailure(const Message&); 167 168 // Returns true iff the assertion succeeded. 169 operator bool() const { return failure_message_.c_str() == NULL; } // NOLINT 170 171 // Returns the assertion's failure message. 172 const char* failure_message() const { return failure_message_.c_str(); } 173 174 private: 175 // The default constructor. It is used when the assertion succeeded. 176 AssertionResult() {} 177 178 // The constructor used when the assertion failed. 179 explicit AssertionResult(const internal::String& failure_message); 180 181 // Stores the assertion's failure message. 182 internal::String failure_message_; 183 }; 184 185 // Makes a successful assertion result. 186 AssertionResult AssertionSuccess(); 187 188 // Makes a failed assertion result with the given failure message. 189 AssertionResult AssertionFailure(const Message& msg); 190 191 // The abstract class that all tests inherit from. 192 // 193 // In Google Test, a unit test program contains one or many TestCases, and 194 // each TestCase contains one or many Tests. 195 // 196 // When you define a test using the TEST macro, you don't need to 197 // explicitly derive from Test - the TEST macro automatically does 198 // this for you. 199 // 200 // The only time you derive from Test is when defining a test fixture 201 // to be used a TEST_F. For example: 202 // 203 // class FooTest : public testing::Test { 204 // protected: 205 // virtual void SetUp() { ... } 206 // virtual void TearDown() { ... } 207 // ... 208 // }; 209 // 210 // TEST_F(FooTest, Bar) { ... } 211 // TEST_F(FooTest, Baz) { ... } 212 // 213 // Test is not copyable. 214 class Test { 215 public: 216 friend class internal::TestInfoImpl; 217 218 // Defines types for pointers to functions that set up and tear down 219 // a test case. 220 typedef void (*SetUpTestCaseFunc)(); 221 typedef void (*TearDownTestCaseFunc)(); 222 223 // The d'tor is virtual as we intend to inherit from Test. 224 virtual ~Test(); 225 226 // Returns true iff the current test has a fatal failure. 227 static bool HasFatalFailure(); 228 229 // Logs a property for the current test. Only the last value for a given 230 // key is remembered. 231 // These are public static so they can be called from utility functions 232 // that are not members of the test fixture. 233 // The arguments are const char* instead strings, as Google Test is used 234 // on platforms where string doesn't compile. 235 // 236 // Note that a driving consideration for these RecordProperty methods 237 // was to produce xml output suited to the Greenspan charting utility, 238 // which at present will only chart values that fit in a 32-bit int. It 239 // is the user's responsibility to restrict their values to 32-bit ints 240 // if they intend them to be used with Greenspan. 241 static void RecordProperty(const char* key, const char* value); 242 static void RecordProperty(const char* key, int value); 243 244 protected: 245 // Creates a Test object. 246 Test(); 247 248 // Sets up the stuff shared by all tests in this test case. 249 // 250 // Google Test will call Foo::SetUpTestCase() before running the first 251 // test in test case Foo. Hence a sub-class can define its own 252 // SetUpTestCase() method to shadow the one defined in the super 253 // class. 254 static void SetUpTestCase() {} 255 256 // Tears down the stuff shared by all tests in this test case. 257 // 258 // Google Test will call Foo::TearDownTestCase() after running the last 259 // test in test case Foo. Hence a sub-class can define its own 260 // TearDownTestCase() method to shadow the one defined in the super 261 // class. 262 static void TearDownTestCase() {} 263 264 // Sets up the test fixture. 265 virtual void SetUp(); 266 267 // Tears down the test fixture. 268 virtual void TearDown(); 269 270 private: 271 // Returns true iff the current test has the same fixture class as 272 // the first test in the current test case. 273 static bool HasSameFixtureClass(); 274 275 // Runs the test after the test fixture has been set up. 276 // 277 // A sub-class must implement this to define the test logic. 278 // 279 // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM. 280 // Instead, use the TEST or TEST_F macro. 281 virtual void TestBody() = 0; 282 283 // Sets up, executes, and tears down the test. 284 void Run(); 285 286 // Uses a GTestFlagSaver to save and restore all Google Test flags. 287 const internal::GTestFlagSaver* const gtest_flag_saver_; 288 289 // Often a user mis-spells SetUp() as Setup() and spends a long time 290 // wondering why it is never called by Google Test. The declaration of 291 // the following method is solely for catching such an error at 292 // compile time: 293 // 294 // - The return type is deliberately chosen to be not void, so it 295 // will be a conflict if a user declares void Setup() in his test 296 // fixture. 297 // 298 // - This method is private, so it will be another compiler error 299 // if a user calls it from his test fixture. 300 // 301 // DO NOT OVERRIDE THIS FUNCTION. 302 // 303 // If you see an error about overriding the following function or 304 // about it being private, you have mis-spelled SetUp() as Setup(). 305 struct Setup_should_be_spelled_SetUp {}; 306 virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; } 307 308 // We disallow copying Tests. 309 GTEST_DISALLOW_COPY_AND_ASSIGN(Test); 310 }; 311 312 313 // Defines the type of a function pointer that creates a Test object 314 // when invoked. 315 typedef Test* (*TestMaker)(); 316 317 318 // A TestInfo object stores the following information about a test: 319 // 320 // Test case name 321 // Test name 322 // Whether the test should be run 323 // A function pointer that creates the test object when invoked 324 // Test result 325 // 326 // The constructor of TestInfo registers itself with the UnitTest 327 // singleton such that the RUN_ALL_TESTS() macro knows which tests to 328 // run. 329 class TestInfo { 330 public: 331 // Destructs a TestInfo object. This function is not virtual, so 332 // don't inherit from TestInfo. 333 ~TestInfo(); 334 335 // Creates a TestInfo object and registers it with the UnitTest 336 // singleton; returns the created object. 337 // 338 // Arguments: 339 // 340 // test_case_name: name of the test case 341 // name: name of the test 342 // fixture_class_id: ID of the test fixture class 343 // set_up_tc: pointer to the function that sets up the test case 344 // tear_down_tc: pointer to the function that tears down the test case 345 // maker: pointer to the function that creates a test object 346 // 347 // This is public only because it's needed by the TEST and TEST_F macros. 348 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 349 static TestInfo* MakeAndRegisterInstance( 350 const char* test_case_name, 351 const char* name, 352 internal::TypeId fixture_class_id, 353 Test::SetUpTestCaseFunc set_up_tc, 354 Test::TearDownTestCaseFunc tear_down_tc, 355 TestMaker maker); 356 357 // Returns the test case name. 358 const char* test_case_name() const; 359 360 // Returns the test name. 361 const char* name() const; 362 363 // Returns true if this test should run. 364 // 365 // Google Test allows the user to filter the tests by their full names. 366 // The full name of a test Bar in test case Foo is defined as 367 // "Foo.Bar". Only the tests that match the filter will run. 368 // 369 // A filter is a colon-separated list of glob (not regex) patterns, 370 // optionally followed by a '-' and a colon-separated list of 371 // negative patterns (tests to exclude). A test is run if it 372 // matches one of the positive patterns and does not match any of 373 // the negative patterns. 374 // 375 // For example, *A*:Foo.* is a filter that matches any string that 376 // contains the character 'A' or starts with "Foo.". 377 bool should_run() const; 378 379 // Returns the result of the test. 380 const internal::TestResult* result() const; 381 private: 382 #ifdef GTEST_HAS_DEATH_TEST 383 friend class internal::DefaultDeathTestFactory; 384 #endif // GTEST_HAS_DEATH_TEST 385 friend class internal::TestInfoImpl; 386 friend class internal::UnitTestImpl; 387 friend class Test; 388 friend class TestCase; 389 390 // Increments the number of death tests encountered in this test so 391 // far. 392 int increment_death_test_count(); 393 394 // Accessors for the implementation object. 395 internal::TestInfoImpl* impl() { return impl_; } 396 const internal::TestInfoImpl* impl() const { return impl_; } 397 398 // Constructs a TestInfo object. 399 TestInfo(const char* test_case_name, const char* name, 400 internal::TypeId fixture_class_id, TestMaker maker); 401 402 // An opaque implementation object. 403 internal::TestInfoImpl* impl_; 404 405 GTEST_DISALLOW_COPY_AND_ASSIGN(TestInfo); 406 }; 407 408 // An Environment object is capable of setting up and tearing down an 409 // environment. The user should subclass this to define his own 410 // environment(s). 411 // 412 // An Environment object does the set-up and tear-down in virtual 413 // methods SetUp() and TearDown() instead of the constructor and the 414 // destructor, as: 415 // 416 // 1. You cannot safely throw from a destructor. This is a problem 417 // as in some cases Google Test is used where exceptions are enabled, and 418 // we may want to implement ASSERT_* using exceptions where they are 419 // available. 420 // 2. You cannot use ASSERT_* directly in a constructor or 421 // destructor. 422 class Environment { 423 public: 424 // The d'tor is virtual as we need to subclass Environment. 425 virtual ~Environment() {} 426 427 // Override this to define how to set up the environment. 428 virtual void SetUp() {} 429 430 // Override this to define how to tear down the environment. 431 virtual void TearDown() {} 432 private: 433 // If you see an error about overriding the following function or 434 // about it being private, you have mis-spelled SetUp() as Setup(). 435 struct Setup_should_be_spelled_SetUp {}; 436 virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; } 437 }; 438 439 // A UnitTest consists of a list of TestCases. 440 // 441 // This is a singleton class. The only instance of UnitTest is 442 // created when UnitTest::GetInstance() is first called. This 443 // instance is never deleted. 444 // 445 // UnitTest is not copyable. 446 // 447 // This class is thread-safe as long as the methods are called 448 // according to their specification. 449 class UnitTest { 450 public: 451 // Gets the singleton UnitTest object. The first time this method 452 // is called, a UnitTest object is constructed and returned. 453 // Consecutive calls will return the same object. 454 static UnitTest* GetInstance(); 455 456 // Registers and returns a global test environment. When a test 457 // program is run, all global test environments will be set-up in 458 // the order they were registered. After all tests in the program 459 // have finished, all global test environments will be torn-down in 460 // the *reverse* order they were registered. 461 // 462 // The UnitTest object takes ownership of the given environment. 463 // 464 // This method can only be called from the main thread. 465 Environment* AddEnvironment(Environment* env); 466 467 // Adds a TestPartResult to the current TestResult object. All 468 // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) 469 // eventually call this to report their results. The user code 470 // should use the assertion macros instead of calling this directly. 471 // 472 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 473 void AddTestPartResult(TestPartResultType result_type, 474 const char* file_name, 475 int line_number, 476 const internal::String& message, 477 const internal::String& os_stack_trace); 478 479 // Adds a TestProperty to the current TestResult object. If the result already 480 // contains a property with the same key, the value will be updated. 481 void RecordPropertyForCurrentTest(const char* key, const char* value); 482 483 // Runs all tests in this UnitTest object and prints the result. 484 // Returns 0 if successful, or 1 otherwise. 485 // 486 // This method can only be called from the main thread. 487 // 488 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 489 int Run() GTEST_MUST_USE_RESULT; 490 491 // Returns the TestCase object for the test that's currently running, 492 // or NULL if no test is running. 493 const TestCase* current_test_case() const; 494 495 // Returns the TestInfo object for the test that's currently running, 496 // or NULL if no test is running. 497 const TestInfo* current_test_info() const; 498 499 // Accessors for the implementation object. 500 internal::UnitTestImpl* impl() { return impl_; } 501 const internal::UnitTestImpl* impl() const { return impl_; } 502 private: 503 // ScopedTrace is a friend as it needs to modify the per-thread 504 // trace stack, which is a private member of UnitTest. 505 friend class internal::ScopedTrace; 506 507 // Creates an empty UnitTest. 508 UnitTest(); 509 510 // D'tor 511 virtual ~UnitTest(); 512 513 // Pushes a trace defined by SCOPED_TRACE() on to the per-thread 514 // Google Test trace stack. 515 void PushGTestTrace(const internal::TraceInfo& trace); 516 517 // Pops a trace from the per-thread Google Test trace stack. 518 void PopGTestTrace(); 519 520 // Protects mutable state in *impl_. This is mutable as some const 521 // methods need to lock it too. 522 mutable internal::Mutex mutex_; 523 524 // Opaque implementation object. This field is never changed once 525 // the object is constructed. We don't mark it as const here, as 526 // doing so will cause a warning in the constructor of UnitTest. 527 // Mutable state in *impl_ is protected by mutex_. 528 internal::UnitTestImpl* impl_; 529 530 // We disallow copying UnitTest. 531 GTEST_DISALLOW_COPY_AND_ASSIGN(UnitTest); 532 }; 533 534 // A convenient wrapper for adding an environment for the test 535 // program. 536 // 537 // You should call this before RUN_ALL_TESTS() is called, probably in 538 // main(). If you use gtest_main, you need to call this before main() 539 // starts for it to take effect. For example, you can define a global 540 // variable like this: 541 // 542 // testing::Environment* const foo_env = 543 // testing::AddGlobalTestEnvironment(new FooEnvironment); 544 // 545 // However, we strongly recommend you to write your own main() and 546 // call AddGlobalTestEnvironment() there, as relying on initialization 547 // of global variables makes the code harder to read and may cause 548 // problems when you register multiple environments from different 549 // translation units and the environments have dependencies among them 550 // (remember that the compiler doesn't guarantee the order in which 551 // global variables from different translation units are initialized). 552 inline Environment* AddGlobalTestEnvironment(Environment* env) { 553 return UnitTest::GetInstance()->AddEnvironment(env); 554 } 555 556 // Initializes Google Test. This must be called before calling 557 // RUN_ALL_TESTS(). In particular, it parses a command line for the 558 // flags that Google Test recognizes. Whenever a Google Test flag is 559 // seen, it is removed from argv, and *argc is decremented. 560 // 561 // No value is returned. Instead, the Google Test flag variables are 562 // updated. 563 void InitGoogleTest(int* argc, char** argv); 564 565 // This overloaded version can be used in Windows programs compiled in 566 // UNICODE mode. 567 #ifdef GTEST_OS_WINDOWS 568 void InitGoogleTest(int* argc, wchar_t** argv); 569 #endif // GTEST_OS_WINDOWS 570 571 namespace internal { 572 573 // These overloaded versions handle ::std::string and ::std::wstring. 574 #if GTEST_HAS_STD_STRING 575 inline String FormatForFailureMessage(const ::std::string& str) { 576 return (Message() << '"' << str << '"').GetString(); 577 } 578 #endif // GTEST_HAS_STD_STRING 579 580 #if GTEST_HAS_STD_WSTRING 581 inline String FormatForFailureMessage(const ::std::wstring& wstr) { 582 return (Message() << "L\"" << wstr << '"').GetString(); 583 } 584 #endif // GTEST_HAS_STD_WSTRING 585 586 // These overloaded versions handle ::string and ::wstring. 587 #if GTEST_HAS_GLOBAL_STRING 588 inline String FormatForFailureMessage(const ::string& str) { 589 return (Message() << '"' << str << '"').GetString(); 590 } 591 #endif // GTEST_HAS_GLOBAL_STRING 592 593 #if GTEST_HAS_GLOBAL_WSTRING 594 inline String FormatForFailureMessage(const ::wstring& wstr) { 595 return (Message() << "L\"" << wstr << '"').GetString(); 596 } 597 #endif // GTEST_HAS_GLOBAL_WSTRING 598 599 // Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc) 600 // operand to be used in a failure message. The type (but not value) 601 // of the other operand may affect the format. This allows us to 602 // print a char* as a raw pointer when it is compared against another 603 // char*, and print it as a C string when it is compared against an 604 // std::string object, for example. 605 // 606 // The default implementation ignores the type of the other operand. 607 // Some specialized versions are used to handle formatting wide or 608 // narrow C strings. 609 // 610 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 611 template <typename T1, typename T2> 612 String FormatForComparisonFailureMessage(const T1& value, 613 const T2& /* other_operand */) { 614 return FormatForFailureMessage(value); 615 } 616 617 // The helper function for {ASSERT|EXPECT}_EQ. 618 template <typename T1, typename T2> 619 AssertionResult CmpHelperEQ(const char* expected_expression, 620 const char* actual_expression, 621 const T1& expected, 622 const T2& actual) { 623 if (expected == actual) { 624 return AssertionSuccess(); 625 } 626 627 return EqFailure(expected_expression, 628 actual_expression, 629 FormatForComparisonFailureMessage(expected, actual), 630 FormatForComparisonFailureMessage(actual, expected), 631 false); 632 } 633 634 // With this overloaded version, we allow anonymous enums to be used 635 // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous enums 636 // can be implicitly cast to BiggestInt. 637 AssertionResult CmpHelperEQ(const char* expected_expression, 638 const char* actual_expression, 639 BiggestInt expected, 640 BiggestInt actual); 641 642 // The helper class for {ASSERT|EXPECT}_EQ. The template argument 643 // lhs_is_null_literal is true iff the first argument to ASSERT_EQ() 644 // is a null pointer literal. The following default implementation is 645 // for lhs_is_null_literal being false. 646 template <bool lhs_is_null_literal> 647 class EqHelper { 648 public: 649 // This templatized version is for the general case. 650 template <typename T1, typename T2> 651 static AssertionResult Compare(const char* expected_expression, 652 const char* actual_expression, 653 const T1& expected, 654 const T2& actual) { 655 return CmpHelperEQ(expected_expression, actual_expression, expected, 656 actual); 657 } 658 659 // With this overloaded version, we allow anonymous enums to be used 660 // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous 661 // enums can be implicitly cast to BiggestInt. 662 // 663 // Even though its body looks the same as the above version, we 664 // cannot merge the two, as it will make anonymous enums unhappy. 665 static AssertionResult Compare(const char* expected_expression, 666 const char* actual_expression, 667 BiggestInt expected, 668 BiggestInt actual) { 669 return CmpHelperEQ(expected_expression, actual_expression, expected, 670 actual); 671 } 672 }; 673 674 // This specialization is used when the first argument to ASSERT_EQ() 675 // is a null pointer literal. 676 template <> 677 class EqHelper<true> { 678 public: 679 // We define two overloaded versions of Compare(). The first 680 // version will be picked when the second argument to ASSERT_EQ() is 681 // NOT a pointer, e.g. ASSERT_EQ(0, AnIntFunction()) or 682 // EXPECT_EQ(false, a_bool). 683 template <typename T1, typename T2> 684 static AssertionResult Compare(const char* expected_expression, 685 const char* actual_expression, 686 const T1& expected, 687 const T2& actual) { 688 return CmpHelperEQ(expected_expression, actual_expression, expected, 689 actual); 690 } 691 692 // This version will be picked when the second argument to 693 // ASSERT_EQ() is a pointer, e.g. ASSERT_EQ(NULL, a_pointer). 694 template <typename T1, typename T2> 695 static AssertionResult Compare(const char* expected_expression, 696 const char* actual_expression, 697 const T1& expected, 698 T2* actual) { 699 // We already know that 'expected' is a null pointer. 700 return CmpHelperEQ(expected_expression, actual_expression, 701 static_cast<T2*>(NULL), actual); 702 } 703 }; 704 705 // A macro for implementing the helper functions needed to implement 706 // ASSERT_?? and EXPECT_??. It is here just to avoid copy-and-paste 707 // of similar code. 708 // 709 // For each templatized helper function, we also define an overloaded 710 // version for BiggestInt in order to reduce code bloat and allow 711 // anonymous enums to be used with {ASSERT|EXPECT}_?? when compiled 712 // with gcc 4. 713 // 714 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 715 #define GTEST_IMPL_CMP_HELPER(op_name, op)\ 716 template <typename T1, typename T2>\ 717 AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ 718 const T1& val1, const T2& val2) {\ 719 if (val1 op val2) {\ 720 return AssertionSuccess();\ 721 } else {\ 722 Message msg;\ 723 msg << "Expected: (" << expr1 << ") " #op " (" << expr2\ 724 << "), actual: " << FormatForComparisonFailureMessage(val1, val2)\ 725 << " vs " << FormatForComparisonFailureMessage(val2, val1);\ 726 return AssertionFailure(msg);\ 727 }\ 728 }\ 729 AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ 730 BiggestInt val1, BiggestInt val2); 731 732 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 733 734 // Implements the helper function for {ASSERT|EXPECT}_NE 735 GTEST_IMPL_CMP_HELPER(NE, !=) 736 // Implements the helper function for {ASSERT|EXPECT}_LE 737 GTEST_IMPL_CMP_HELPER(LE, <=) 738 // Implements the helper function for {ASSERT|EXPECT}_LT 739 GTEST_IMPL_CMP_HELPER(LT, < ) 740 // Implements the helper function for {ASSERT|EXPECT}_GE 741 GTEST_IMPL_CMP_HELPER(GE, >=) 742 // Implements the helper function for {ASSERT|EXPECT}_GT 743 GTEST_IMPL_CMP_HELPER(GT, > ) 744 745 #undef GTEST_IMPL_CMP_HELPER 746 747 // The helper function for {ASSERT|EXPECT}_STREQ. 748 // 749 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 750 AssertionResult CmpHelperSTREQ(const char* expected_expression, 751 const char* actual_expression, 752 const char* expected, 753 const char* actual); 754 755 // The helper function for {ASSERT|EXPECT}_STRCASEEQ. 756 // 757 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 758 AssertionResult CmpHelperSTRCASEEQ(const char* expected_expression, 759 const char* actual_expression, 760 const char* expected, 761 const char* actual); 762 763 // The helper function for {ASSERT|EXPECT}_STRNE. 764 // 765 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 766 AssertionResult CmpHelperSTRNE(const char* s1_expression, 767 const char* s2_expression, 768 const char* s1, 769 const char* s2); 770 771 // The helper function for {ASSERT|EXPECT}_STRCASENE. 772 // 773 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 774 AssertionResult CmpHelperSTRCASENE(const char* s1_expression, 775 const char* s2_expression, 776 const char* s1, 777 const char* s2); 778 779 780 // Helper function for *_STREQ on wide strings. 781 // 782 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 783 AssertionResult CmpHelperSTREQ(const char* expected_expression, 784 const char* actual_expression, 785 const wchar_t* expected, 786 const wchar_t* actual); 787 788 // Helper function for *_STRNE on wide strings. 789 // 790 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 791 AssertionResult CmpHelperSTRNE(const char* s1_expression, 792 const char* s2_expression, 793 const wchar_t* s1, 794 const wchar_t* s2); 795 796 } // namespace internal 797 798 // IsSubstring() and IsNotSubstring() are intended to be used as the 799 // first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by 800 // themselves. They check whether needle is a substring of haystack 801 // (NULL is considered a substring of itself only), and return an 802 // appropriate error message when they fail. 803 // 804 // The {needle,haystack}_expr arguments are the stringified 805 // expressions that generated the two real arguments. 806 AssertionResult IsSubstring( 807 const char* needle_expr, const char* haystack_expr, 808 const char* needle, const char* haystack); 809 AssertionResult IsSubstring( 810 const char* needle_expr, const char* haystack_expr, 811 const wchar_t* needle, const wchar_t* haystack); 812 AssertionResult IsNotSubstring( 813 const char* needle_expr, const char* haystack_expr, 814 const char* needle, const char* haystack); 815 AssertionResult IsNotSubstring( 816 const char* needle_expr, const char* haystack_expr, 817 const wchar_t* needle, const wchar_t* haystack); 818 #if GTEST_HAS_STD_STRING 819 AssertionResult IsSubstring( 820 const char* needle_expr, const char* haystack_expr, 821 const ::std::string& needle, const ::std::string& haystack); 822 AssertionResult IsNotSubstring( 823 const char* needle_expr, const char* haystack_expr, 824 const ::std::string& needle, const ::std::string& haystack); 825 #endif // GTEST_HAS_STD_STRING 826 827 #if GTEST_HAS_STD_WSTRING 828 AssertionResult IsSubstring( 829 const char* needle_expr, const char* haystack_expr, 830 const ::std::wstring& needle, const ::std::wstring& haystack); 831 AssertionResult IsNotSubstring( 832 const char* needle_expr, const char* haystack_expr, 833 const ::std::wstring& needle, const ::std::wstring& haystack); 834 #endif // GTEST_HAS_STD_WSTRING 835 836 namespace internal { 837 838 // Helper template function for comparing floating-points. 839 // 840 // Template parameter: 841 // 842 // RawType: the raw floating-point type (either float or double) 843 // 844 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 845 template <typename RawType> 846 AssertionResult CmpHelperFloatingPointEQ(const char* expected_expression, 847 const char* actual_expression, 848 RawType expected, 849 RawType actual) { 850 const FloatingPoint<RawType> lhs(expected), rhs(actual); 851 852 if (lhs.AlmostEquals(rhs)) { 853 return AssertionSuccess(); 854 } 855 856 StrStream expected_ss; 857 expected_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) 858 << expected; 859 860 StrStream actual_ss; 861 actual_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) 862 << actual; 863 864 return EqFailure(expected_expression, 865 actual_expression, 866 StrStreamToString(&expected_ss), 867 StrStreamToString(&actual_ss), 868 false); 869 } 870 871 // Helper function for implementing ASSERT_NEAR. 872 // 873 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 874 AssertionResult DoubleNearPredFormat(const char* expr1, 875 const char* expr2, 876 const char* abs_error_expr, 877 double val1, 878 double val2, 879 double abs_error); 880 881 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. 882 // A class that enables one to stream messages to assertion macros 883 class AssertHelper { 884 public: 885 // Constructor. 886 AssertHelper(TestPartResultType type, const char* file, int line, 887 const char* message); 888 // Message assignment is a semantic trick to enable assertion 889 // streaming; see the GTEST_MESSAGE macro below. 890 void operator=(const Message& message) const; 891 private: 892 TestPartResultType const type_; 893 const char* const file_; 894 int const line_; 895 String const message_; 896 897 GTEST_DISALLOW_COPY_AND_ASSIGN(AssertHelper); 898 }; 899 900 } // namespace internal 901 902 // Macros for indicating success/failure in test code. 903 904 // ADD_FAILURE unconditionally adds a failure to the current test. 905 // SUCCEED generates a success - it doesn't automatically make the 906 // current test successful, as a test is only successful when it has 907 // no failure. 908 // 909 // EXPECT_* verifies that a certain condition is satisfied. If not, 910 // it behaves like ADD_FAILURE. In particular: 911 // 912 // EXPECT_TRUE verifies that a Boolean condition is true. 913 // EXPECT_FALSE verifies that a Boolean condition is false. 914 // 915 // FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except 916 // that they will also abort the current function on failure. People 917 // usually want the fail-fast behavior of FAIL and ASSERT_*, but those 918 // writing data-driven tests often find themselves using ADD_FAILURE 919 // and EXPECT_* more. 920 // 921 // Examples: 922 // 923 // EXPECT_TRUE(server.StatusIsOK()); 924 // ASSERT_FALSE(server.HasPendingRequest(port)) 925 // << "There are still pending requests " << "on port " << port; 926 927 // Generates a nonfatal failure with a generic message. 928 #define ADD_FAILURE() GTEST_NONFATAL_FAILURE("Failed") 929 930 // Generates a fatal failure with a generic message. 931 #define FAIL() GTEST_FATAL_FAILURE("Failed") 932 933 // Generates a success with a generic message. 934 #define SUCCEED() GTEST_SUCCESS("Succeeded") 935 936 // Boolean assertions. 937 #define EXPECT_TRUE(condition) \ 938 GTEST_TEST_BOOLEAN(condition, #condition, false, true, \ 939 GTEST_NONFATAL_FAILURE) 940 #define EXPECT_FALSE(condition) \ 941 GTEST_TEST_BOOLEAN(!(condition), #condition, true, false, \ 942 GTEST_NONFATAL_FAILURE) 943 #define ASSERT_TRUE(condition) \ 944 GTEST_TEST_BOOLEAN(condition, #condition, false, true, \ 945 GTEST_FATAL_FAILURE) 946 #define ASSERT_FALSE(condition) \ 947 GTEST_TEST_BOOLEAN(!(condition), #condition, true, false, \ 948 GTEST_FATAL_FAILURE) 949 950 // Includes the auto-generated header that implements a family of 951 // generic predicate assertion macros. 952 #include <gtest/gtest_pred_impl.h> 953 954 // Macros for testing equalities and inequalities. 955 // 956 // * {ASSERT|EXPECT}_EQ(expected, actual): Tests that expected == actual 957 // * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2 958 // * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2 959 // * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2 960 // * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2 961 // * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2 962 // 963 // When they are not, Google Test prints both the tested expressions and 964 // their actual values. The values must be compatible built-in types, 965 // or you will get a compiler error. By "compatible" we mean that the 966 // values can be compared by the respective operator. 967 // 968 // Note: 969 // 970 // 1. It is possible to make a user-defined type work with 971 // {ASSERT|EXPECT}_??(), but that requires overloading the 972 // comparison operators and is thus discouraged by the Google C++ 973 // Usage Guide. Therefore, you are advised to use the 974 // {ASSERT|EXPECT}_TRUE() macro to assert that two objects are 975 // equal. 976 // 977 // 2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on 978 // pointers (in particular, C strings). Therefore, if you use it 979 // with two C strings, you are testing how their locations in memory 980 // are related, not how their content is related. To compare two C 981 // strings by content, use {ASSERT|EXPECT}_STR*(). 982 // 983 // 3. {ASSERT|EXPECT}_EQ(expected, actual) is preferred to 984 // {ASSERT|EXPECT}_TRUE(expected == actual), as the former tells you 985 // what the actual value is when it fails, and similarly for the 986 // other comparisons. 987 // 988 // 4. Do not depend on the order in which {ASSERT|EXPECT}_??() 989 // evaluate their arguments, which is undefined. 990 // 991 // 5. These macros evaluate their arguments exactly once. 992 // 993 // Examples: 994 // 995 // EXPECT_NE(5, Foo()); 996 // EXPECT_EQ(NULL, a_pointer); 997 // ASSERT_LT(i, array_size); 998 // ASSERT_GT(records.size(), 0) << "There is no record left."; 999 1000 #define EXPECT_EQ(expected, actual) \ 1001 EXPECT_PRED_FORMAT2(::testing::internal:: \ 1002 EqHelper<GTEST_IS_NULL_LITERAL(expected)>::Compare, \ 1003 expected, actual) 1004 #define EXPECT_NE(expected, actual) \ 1005 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, expected, actual) 1006 #define EXPECT_LE(val1, val2) \ 1007 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) 1008 #define EXPECT_LT(val1, val2) \ 1009 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) 1010 #define EXPECT_GE(val1, val2) \ 1011 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) 1012 #define EXPECT_GT(val1, val2) \ 1013 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) 1014 1015 #define ASSERT_EQ(expected, actual) \ 1016 ASSERT_PRED_FORMAT2(::testing::internal:: \ 1017 EqHelper<GTEST_IS_NULL_LITERAL(expected)>::Compare, \ 1018 expected, actual) 1019 #define ASSERT_NE(val1, val2) \ 1020 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2) 1021 #define ASSERT_LE(val1, val2) \ 1022 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) 1023 #define ASSERT_LT(val1, val2) \ 1024 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) 1025 #define ASSERT_GE(val1, val2) \ 1026 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) 1027 #define ASSERT_GT(val1, val2) \ 1028 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) 1029 1030 // C String Comparisons. All tests treat NULL and any non-NULL string 1031 // as different. Two NULLs are equal. 1032 // 1033 // * {ASSERT|EXPECT}_STREQ(s1, s2): Tests that s1 == s2 1034 // * {ASSERT|EXPECT}_STRNE(s1, s2): Tests that s1 != s2 1035 // * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case 1036 // * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case 1037 // 1038 // For wide or narrow string objects, you can use the 1039 // {ASSERT|EXPECT}_??() macros. 1040 // 1041 // Don't depend on the order in which the arguments are evaluated, 1042 // which is undefined. 1043 // 1044 // These macros evaluate their arguments exactly once. 1045 1046 #define EXPECT_STREQ(expected, actual) \ 1047 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual) 1048 #define EXPECT_STRNE(s1, s2) \ 1049 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) 1050 #define EXPECT_STRCASEEQ(expected, actual) \ 1051 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual) 1052 #define EXPECT_STRCASENE(s1, s2)\ 1053 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) 1054 1055 #define ASSERT_STREQ(expected, actual) \ 1056 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual) 1057 #define ASSERT_STRNE(s1, s2) \ 1058 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) 1059 #define ASSERT_STRCASEEQ(expected, actual) \ 1060 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual) 1061 #define ASSERT_STRCASENE(s1, s2)\ 1062 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) 1063 1064 // Macros for comparing floating-point numbers. 1065 // 1066 // * {ASSERT|EXPECT}_FLOAT_EQ(expected, actual): 1067 // Tests that two float values are almost equal. 1068 // * {ASSERT|EXPECT}_DOUBLE_EQ(expected, actual): 1069 // Tests that two double values are almost equal. 1070 // * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error): 1071 // Tests that v1 and v2 are within the given distance to each other. 1072 // 1073 // Google Test uses ULP-based comparison to automatically pick a default 1074 // error bound that is appropriate for the operands. See the 1075 // FloatingPoint template class in gtest-internal.h if you are 1076 // interested in the implementation details. 1077 1078 #define EXPECT_FLOAT_EQ(expected, actual)\ 1079 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \ 1080 expected, actual) 1081 1082 #define EXPECT_DOUBLE_EQ(expected, actual)\ 1083 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \ 1084 expected, actual) 1085 1086 #define ASSERT_FLOAT_EQ(expected, actual)\ 1087 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \ 1088 expected, actual) 1089 1090 #define ASSERT_DOUBLE_EQ(expected, actual)\ 1091 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \ 1092 expected, actual) 1093 1094 #define EXPECT_NEAR(val1, val2, abs_error)\ 1095 EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \ 1096 val1, val2, abs_error) 1097 1098 #define ASSERT_NEAR(val1, val2, abs_error)\ 1099 ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \ 1100 val1, val2, abs_error) 1101 1102 // These predicate format functions work on floating-point values, and 1103 // can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g. 1104 // 1105 // EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0); 1106 1107 // Asserts that val1 is less than, or almost equal to, val2. Fails 1108 // otherwise. In particular, it fails if either val1 or val2 is NaN. 1109 AssertionResult FloatLE(const char* expr1, const char* expr2, 1110 float val1, float val2); 1111 AssertionResult DoubleLE(const char* expr1, const char* expr2, 1112 double val1, double val2); 1113 1114 1115 #ifdef GTEST_OS_WINDOWS 1116 1117 // Macros that test for HRESULT failure and success, these are only useful 1118 // on Windows, and rely on Windows SDK macros and APIs to compile. 1119 // 1120 // * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr) 1121 // 1122 // When expr unexpectedly fails or succeeds, Google Test prints the expected result 1123 // and the actual result with both a human-readable string representation of 1124 // the error, if available, as well as the hex result code. 1125 #define EXPECT_HRESULT_SUCCEEDED(expr) \ 1126 EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) 1127 1128 #define ASSERT_HRESULT_SUCCEEDED(expr) \ 1129 ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) 1130 1131 #define EXPECT_HRESULT_FAILED(expr) \ 1132 EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) 1133 1134 #define ASSERT_HRESULT_FAILED(expr) \ 1135 ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) 1136 1137 #endif // GTEST_OS_WINDOWS 1138 1139 1140 // Causes a trace (including the source file path, the current line 1141 // number, and the given message) to be included in every test failure 1142 // message generated by code in the current scope. The effect is 1143 // undone when the control leaves the current scope. 1144 // 1145 // The message argument can be anything streamable to std::ostream. 1146 // 1147 // In the implementation, we include the current line number as part 1148 // of the dummy variable name, thus allowing multiple SCOPED_TRACE()s 1149 // to appear in the same block - as long as they are on different 1150 // lines. 1151 #define SCOPED_TRACE(message) \ 1152 ::testing::internal::ScopedTrace GTEST_CONCAT_TOKEN(gtest_trace_, __LINE__)(\ 1153 __FILE__, __LINE__, ::testing::Message() << (message)) 1154 1155 1156 // Defines a test. 1157 // 1158 // The first parameter is the name of the test case, and the second 1159 // parameter is the name of the test within the test case. 1160 // 1161 // The convention is to end the test case name with "Test". For 1162 // example, a test case for the Foo class can be named FooTest. 1163 // 1164 // The user should put his test code between braces after using this 1165 // macro. Example: 1166 // 1167 // TEST(FooTest, InitializesCorrectly) { 1168 // Foo foo; 1169 // EXPECT_TRUE(foo.StatusIsOK()); 1170 // } 1171 1172 #define TEST(test_case_name, test_name)\ 1173 GTEST_TEST(test_case_name, test_name, ::testing::Test) 1174 1175 1176 // Defines a test that uses a test fixture. 1177 // 1178 // The first parameter is the name of the test fixture class, which 1179 // also doubles as the test case name. The second parameter is the 1180 // name of the test within the test case. 1181 // 1182 // A test fixture class must be declared earlier. The user should put 1183 // his test code between braces after using this macro. Example: 1184 // 1185 // class FooTest : public testing::Test { 1186 // protected: 1187 // virtual void SetUp() { b_.AddElement(3); } 1188 // 1189 // Foo a_; 1190 // Foo b_; 1191 // }; 1192 // 1193 // TEST_F(FooTest, InitializesCorrectly) { 1194 // EXPECT_TRUE(a_.StatusIsOK()); 1195 // } 1196 // 1197 // TEST_F(FooTest, ReturnsElementCountCorrectly) { 1198 // EXPECT_EQ(0, a_.size()); 1199 // EXPECT_EQ(1, b_.size()); 1200 // } 1201 1202 #define TEST_F(test_fixture, test_name)\ 1203 GTEST_TEST(test_fixture, test_name, test_fixture) 1204 1205 // Use this macro in main() to run all tests. It returns 0 if all 1206 // tests are successful, or 1 otherwise. 1207 // 1208 // RUN_ALL_TESTS() should be invoked after the command line has been 1209 // parsed by InitGoogleTest(). 1210 1211 #define RUN_ALL_TESTS()\ 1212 (::testing::UnitTest::GetInstance()->Run()) 1213 1214 } // namespace testing 1215 1216 #endif // GTEST_INCLUDE_GTEST_GTEST_H_ 1217