00001 // Copyright 2005, Google Inc. 00002 // All rights reserved. 00003 // 00004 // Redistribution and use in source and binary forms, with or without 00005 // modification, are permitted provided that the following conditions are 00006 // met: 00007 // 00008 // * Redistributions of source code must retain the above copyright 00009 // notice, this list of conditions and the following disclaimer. 00010 // * Redistributions in binary form must reproduce the above 00011 // copyright notice, this list of conditions and the following disclaimer 00012 // in the documentation and/or other materials provided with the 00013 // distribution. 00014 // * Neither the name of Google Inc. nor the names of its 00015 // contributors may be used to endorse or promote products derived from 00016 // this software without specific prior written permission. 00017 // 00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00029 // 00030 // Author: wan@google.com (Zhanyong Wan) 00031 // 00032 // The Google C++ Testing Framework (Google Test) 00033 // 00034 // This header file defines the public API for Google Test. It should be 00035 // included by any test program that uses Google Test. 00036 // 00037 // IMPORTANT NOTE: Due to limitation of the C++ language, we have to 00038 // leave some internal implementation details in this header file. 00039 // They are clearly marked by comments like this: 00040 // 00041 // // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 00042 // 00043 // Such code is NOT meant to be used by a user directly, and is subject 00044 // to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user 00045 // program! 00046 // 00047 // Acknowledgment: Google Test borrowed the idea of automatic test 00048 // registration from Barthelemy Dagenais' (barthelemy@prologique.com) 00049 // easyUnit framework. 00050 00051 #ifndef GTEST_INCLUDE_GTEST_GTEST_H_ 00052 #define GTEST_INCLUDE_GTEST_GTEST_H_ 00053 00054 #include <limits> 00055 #include <ostream> 00056 #include <vector> 00057 00058 #include "gtest/internal/gtest-internal.h" 00059 #include "gtest/internal/gtest-string.h" 00060 #include "gtest/gtest-death-test.h" 00061 #include "gtest/gtest-message.h" 00062 #include "gtest/gtest-param-test.h" 00063 #include "gtest/gtest-printers.h" 00064 #include "gtest/gtest_prod.h" 00065 #include "gtest/gtest-test-part.h" 00066 #include "gtest/gtest-typed-test.h" 00067 00068 // Depending on the platform, different string classes are available. 00069 // On Linux, in addition to ::std::string, Google also makes use of 00070 // class ::string, which has the same interface as ::std::string, but 00071 // has a different implementation. 00072 // 00073 // You can define GTEST_HAS_GLOBAL_STRING to 1 to indicate that 00074 // ::string is available AND is a distinct type to ::std::string, or 00075 // define it to 0 to indicate otherwise. 00076 // 00077 // If ::std::string and ::string are the same class on your platform 00078 // due to aliasing, you should define GTEST_HAS_GLOBAL_STRING to 0. 00079 // 00080 // If you do not define GTEST_HAS_GLOBAL_STRING, it is defined 00081 // heuristically. 00082 00083 namespace testing { 00084 00085 // Declares the flags. 00086 00087 // This flag temporary enables the disabled tests. 00088 GTEST_DECLARE_bool_(also_run_disabled_tests); 00089 00090 // This flag brings the debugger on an assertion failure. 00091 GTEST_DECLARE_bool_(break_on_failure); 00092 00093 // This flag controls whether Google Test catches all test-thrown exceptions 00094 // and logs them as failures. 00095 GTEST_DECLARE_bool_(catch_exceptions); 00096 00097 // This flag enables using colors in terminal output. Available values are 00098 // "yes" to enable colors, "no" (disable colors), or "auto" (the default) 00099 // to let Google Test decide. 00100 GTEST_DECLARE_string_(color); 00101 00102 // This flag sets up the filter to select by name using a glob pattern 00103 // the tests to run. If the filter is not given all tests are executed. 00104 GTEST_DECLARE_string_(filter); 00105 00106 // This flag causes the Google Test to list tests. None of the tests listed 00107 // are actually run if the flag is provided. 00108 GTEST_DECLARE_bool_(list_tests); 00109 00110 // This flag controls whether Google Test emits a detailed XML report to a file 00111 // in addition to its normal textual output. 00112 GTEST_DECLARE_string_(output); 00113 00114 // This flags control whether Google Test prints the elapsed time for each 00115 // test. 00116 GTEST_DECLARE_bool_(print_time); 00117 00118 // This flag specifies the random number seed. 00119 GTEST_DECLARE_int32_(random_seed); 00120 00121 // This flag sets how many times the tests are repeated. The default value 00122 // is 1. If the value is -1 the tests are repeating forever. 00123 GTEST_DECLARE_int32_(repeat); 00124 00125 // This flag controls whether Google Test includes Google Test internal 00126 // stack frames in failure stack traces. 00127 GTEST_DECLARE_bool_(show_internal_stack_frames); 00128 00129 // When this flag is specified, tests' order is randomized on every iteration. 00130 GTEST_DECLARE_bool_(shuffle); 00131 00132 // This flag specifies the maximum number of stack frames to be 00133 // printed in a failure message. 00134 GTEST_DECLARE_int32_(stack_trace_depth); 00135 00136 // When this flag is specified, a failed assertion will throw an 00137 // exception if exceptions are enabled, or exit the program with a 00138 // non-zero code otherwise. 00139 GTEST_DECLARE_bool_(throw_on_failure); 00140 00141 // When this flag is set with a "host:port" string, on supported 00142 // platforms test results are streamed to the specified port on 00143 // the specified host machine. 00144 GTEST_DECLARE_string_(stream_result_to); 00145 00146 // The upper limit for valid stack trace depths. 00147 const int kMaxStackTraceDepth = 100; 00148 00149 namespace internal { 00150 00151 class AssertHelper; 00152 class DefaultGlobalTestPartResultReporter; 00153 class ExecDeathTest; 00154 class NoExecDeathTest; 00155 class FinalSuccessChecker; 00156 class GTestFlagSaver; 00157 class StreamingListenerTest; 00158 class TestResultAccessor; 00159 class TestEventListenersAccessor; 00160 class TestEventRepeater; 00161 class UnitTestRecordPropertyTestHelper; 00162 class WindowsDeathTest; 00163 class UnitTestImpl* GetUnitTestImpl(); 00164 void ReportFailureInUnknownLocation(TestPartResult::Type result_type, 00165 const std::string& message); 00166 00167 } // namespace internal 00168 00169 // The friend relationship of some of these classes is cyclic. 00170 // If we don't forward declare them the compiler might confuse the classes 00171 // in friendship clauses with same named classes on the scope. 00172 class Test; 00173 class TestCase; 00174 class TestInfo; 00175 class UnitTest; 00176 00177 // A class for indicating whether an assertion was successful. When 00178 // the assertion wasn't successful, the AssertionResult object 00179 // remembers a non-empty message that describes how it failed. 00180 // 00181 // To create an instance of this class, use one of the factory functions 00182 // (AssertionSuccess() and AssertionFailure()). 00183 // 00184 // This class is useful for two purposes: 00185 // 1. Defining predicate functions to be used with Boolean test assertions 00186 // EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts 00187 // 2. Defining predicate-format functions to be 00188 // used with predicate assertions (ASSERT_PRED_FORMAT*, etc). 00189 // 00190 // For example, if you define IsEven predicate: 00191 // 00192 // testing::AssertionResult IsEven(int n) { 00193 // if ((n % 2) == 0) 00194 // return testing::AssertionSuccess(); 00195 // else 00196 // return testing::AssertionFailure() << n << " is odd"; 00197 // } 00198 // 00199 // Then the failed expectation EXPECT_TRUE(IsEven(Fib(5))) 00200 // will print the message 00201 // 00202 // Value of: IsEven(Fib(5)) 00203 // Actual: false (5 is odd) 00204 // Expected: true 00205 // 00206 // instead of a more opaque 00207 // 00208 // Value of: IsEven(Fib(5)) 00209 // Actual: false 00210 // Expected: true 00211 // 00212 // in case IsEven is a simple Boolean predicate. 00213 // 00214 // If you expect your predicate to be reused and want to support informative 00215 // messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up 00216 // about half as often as positive ones in our tests), supply messages for 00217 // both success and failure cases: 00218 // 00219 // testing::AssertionResult IsEven(int n) { 00220 // if ((n % 2) == 0) 00221 // return testing::AssertionSuccess() << n << " is even"; 00222 // else 00223 // return testing::AssertionFailure() << n << " is odd"; 00224 // } 00225 // 00226 // Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print 00227 // 00228 // Value of: IsEven(Fib(6)) 00229 // Actual: true (8 is even) 00230 // Expected: false 00231 // 00232 // NB: Predicates that support negative Boolean assertions have reduced 00233 // performance in positive ones so be careful not to use them in tests 00234 // that have lots (tens of thousands) of positive Boolean assertions. 00235 // 00236 // To use this class with EXPECT_PRED_FORMAT assertions such as: 00237 // 00238 // // Verifies that Foo() returns an even number. 00239 // EXPECT_PRED_FORMAT1(IsEven, Foo()); 00240 // 00241 // you need to define: 00242 // 00243 // testing::AssertionResult IsEven(const char* expr, int n) { 00244 // if ((n % 2) == 0) 00245 // return testing::AssertionSuccess(); 00246 // else 00247 // return testing::AssertionFailure() 00248 // << "Expected: " << expr << " is even\n Actual: it's " << n; 00249 // } 00250 // 00251 // If Foo() returns 5, you will see the following message: 00252 // 00253 // Expected: Foo() is even 00254 // Actual: it's 5 00255 // 00256 class GTEST_API_ AssertionResult { 00257 public: 00258 // Copy constructor. 00259 // Used in EXPECT_TRUE/FALSE(assertion_result). 00260 AssertionResult(const AssertionResult& other); 00261 00262 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 /* forcing value to bool */) 00263 00264 // Used in the EXPECT_TRUE/FALSE(bool_expression). 00265 // 00266 // T must be contextually convertible to bool. 00267 // 00268 // The second parameter prevents this overload from being considered if 00269 // the argument is implicitly convertible to AssertionResult. In that case 00270 // we want AssertionResult's copy constructor to be used. 00271 template <typename T> 00272 explicit AssertionResult( 00273 const T& success, 00274 typename internal::EnableIf< 00275 !internal::ImplicitlyConvertible<T, AssertionResult>::value>::type* 00276 /*enabler*/ = NULL) 00277 : success_(success) {} 00278 00279 GTEST_DISABLE_MSC_WARNINGS_POP_() 00280 00281 // Assignment operator. 00282 AssertionResult& operator=(AssertionResult other) { 00283 swap(other); 00284 return *this; 00285 } 00286 00287 // Returns true iff the assertion succeeded. 00288 operator bool() const { return success_; } // NOLINT 00289 00290 // Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE. 00291 AssertionResult operator!() const; 00292 00293 // Returns the text streamed into this AssertionResult. Test assertions 00294 // use it when they fail (i.e., the predicate's outcome doesn't match the 00295 // assertion's expectation). When nothing has been streamed into the 00296 // object, returns an empty string. 00297 const char* message() const { 00298 return message_.get() != NULL ? message_->c_str() : ""; 00299 } 00300 // TODO(vladl@google.com): Remove this after making sure no clients use it. 00301 // Deprecated; please use message() instead. 00302 const char* failure_message() const { return message(); } 00303 00304 // Streams a custom failure message into this object. 00305 template <typename T> AssertionResult& operator<<(const T& value) { 00306 AppendMessage(Message() << value); 00307 return *this; 00308 } 00309 00310 // Allows streaming basic output manipulators such as endl or flush into 00311 // this object. 00312 AssertionResult& operator<<( 00313 ::std::ostream& (*basic_manipulator)(::std::ostream& stream)) { 00314 AppendMessage(Message() << basic_manipulator); 00315 return *this; 00316 } 00317 00318 private: 00319 // Appends the contents of message to message_. 00320 void AppendMessage(const Message& a_message) { 00321 if (message_.get() == NULL) 00322 message_.reset(new ::std::string); 00323 message_->append(a_message.GetString().c_str()); 00324 } 00325 00326 // Swap the contents of this AssertionResult with other. 00327 void swap(AssertionResult& other); 00328 00329 // Stores result of the assertion predicate. 00330 bool success_; 00331 // Stores the message describing the condition in case the expectation 00332 // construct is not satisfied with the predicate's outcome. 00333 // Referenced via a pointer to avoid taking too much stack frame space 00334 // with test assertions. 00335 internal::scoped_ptr< ::std::string> message_; 00336 }; 00337 00338 // Makes a successful assertion result. 00339 GTEST_API_ AssertionResult AssertionSuccess(); 00340 00341 // Makes a failed assertion result. 00342 GTEST_API_ AssertionResult AssertionFailure(); 00343 00344 // Makes a failed assertion result with the given failure message. 00345 // Deprecated; use AssertionFailure() << msg. 00346 GTEST_API_ AssertionResult AssertionFailure(const Message& msg); 00347 00348 // The abstract class that all tests inherit from. 00349 // 00350 // In Google Test, a unit test program contains one or many TestCases, and 00351 // each TestCase contains one or many Tests. 00352 // 00353 // When you define a test using the TEST macro, you don't need to 00354 // explicitly derive from Test - the TEST macro automatically does 00355 // this for you. 00356 // 00357 // The only time you derive from Test is when defining a test fixture 00358 // to be used a TEST_F. For example: 00359 // 00360 // class FooTest : public testing::Test { 00361 // protected: 00362 // void SetUp() override { ... } 00363 // void TearDown() override { ... } 00364 // ... 00365 // }; 00366 // 00367 // TEST_F(FooTest, Bar) { ... } 00368 // TEST_F(FooTest, Baz) { ... } 00369 // 00370 // Test is not copyable. 00371 class GTEST_API_ Test { 00372 public: 00373 friend class TestInfo; 00374 00375 // Defines types for pointers to functions that set up and tear down 00376 // a test case. 00377 typedef internal::SetUpTestCaseFunc SetUpTestCaseFunc; 00378 typedef internal::TearDownTestCaseFunc TearDownTestCaseFunc; 00379 00380 // The d'tor is virtual as we intend to inherit from Test. 00381 virtual ~Test(); 00382 00383 // Sets up the stuff shared by all tests in this test case. 00384 // 00385 // Google Test will call Foo::SetUpTestCase() before running the first 00386 // test in test case Foo. Hence a sub-class can define its own 00387 // SetUpTestCase() method to shadow the one defined in the super 00388 // class. 00389 static void SetUpTestCase() {} 00390 00391 // Tears down the stuff shared by all tests in this test case. 00392 // 00393 // Google Test will call Foo::TearDownTestCase() after running the last 00394 // test in test case Foo. Hence a sub-class can define its own 00395 // TearDownTestCase() method to shadow the one defined in the super 00396 // class. 00397 static void TearDownTestCase() {} 00398 00399 // Returns true iff the current test has a fatal failure. 00400 static bool HasFatalFailure(); 00401 00402 // Returns true iff the current test has a non-fatal failure. 00403 static bool HasNonfatalFailure(); 00404 00405 // Returns true iff the current test has a (either fatal or 00406 // non-fatal) failure. 00407 static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); } 00408 00409 // Logs a property for the current test, test case, or for the entire 00410 // invocation of the test program when used outside of the context of a 00411 // test case. Only the last value for a given key is remembered. These 00412 // are public static so they can be called from utility functions that are 00413 // not members of the test fixture. Calls to RecordProperty made during 00414 // lifespan of the test (from the moment its constructor starts to the 00415 // moment its destructor finishes) will be output in XML as attributes of 00416 // the <testcase> element. Properties recorded from fixture's 00417 // SetUpTestCase or TearDownTestCase are logged as attributes of the 00418 // corresponding <testsuite> element. Calls to RecordProperty made in the 00419 // global context (before or after invocation of RUN_ALL_TESTS and from 00420 // SetUp/TearDown method of Environment objects registered with Google 00421 // Test) will be output as attributes of the <testsuites> element. 00422 static void RecordProperty(const std::string& key, const std::string& value); 00423 static void RecordProperty(const std::string& key, int value); 00424 00425 protected: 00426 // Creates a Test object. 00427 Test(); 00428 00429 // Sets up the test fixture. 00430 virtual void SetUp(); 00431 00432 // Tears down the test fixture. 00433 virtual void TearDown(); 00434 00435 private: 00436 // Returns true iff the current test has the same fixture class as 00437 // the first test in the current test case. 00438 static bool HasSameFixtureClass(); 00439 00440 // Runs the test after the test fixture has been set up. 00441 // 00442 // A sub-class must implement this to define the test logic. 00443 // 00444 // DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM. 00445 // Instead, use the TEST or TEST_F macro. 00446 virtual void TestBody() = 0; 00447 00448 // Sets up, executes, and tears down the test. 00449 void Run(); 00450 00451 // Deletes self. We deliberately pick an unusual name for this 00452 // internal method to avoid clashing with names used in user TESTs. 00453 void DeleteSelf_() { delete this; } 00454 00455 // Uses a GTestFlagSaver to save and restore all Google Test flags. 00456 const internal::GTestFlagSaver* const gtest_flag_saver_; 00457 00458 // Often a user misspells SetUp() as Setup() and spends a long time 00459 // wondering why it is never called by Google Test. The declaration of 00460 // the following method is solely for catching such an error at 00461 // compile time: 00462 // 00463 // - The return type is deliberately chosen to be not void, so it 00464 // will be a conflict if void Setup() is declared in the user's 00465 // test fixture. 00466 // 00467 // - This method is private, so it will be another compiler error 00468 // if the method is called from the user's test fixture. 00469 // 00470 // DO NOT OVERRIDE THIS FUNCTION. 00471 // 00472 // If you see an error about overriding the following function or 00473 // about it being private, you have mis-spelled SetUp() as Setup(). 00474 struct Setup_should_be_spelled_SetUp {}; 00475 virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; } 00476 00477 // We disallow copying Tests. 00478 GTEST_DISALLOW_COPY_AND_ASSIGN_(Test); 00479 }; 00480 00481 typedef internal::TimeInMillis TimeInMillis; 00482 00483 // A copyable object representing a user specified test property which can be 00484 // output as a key/value string pair. 00485 // 00486 // Don't inherit from TestProperty as its destructor is not virtual. 00487 class TestProperty { 00488 public: 00489 // C'tor. TestProperty does NOT have a default constructor. 00490 // Always use this constructor (with parameters) to create a 00491 // TestProperty object. 00492 TestProperty(const std::string& a_key, const std::string& a_value) : 00493 key_(a_key), value_(a_value) { 00494 } 00495 00496 // Gets the user supplied key. 00497 const char* key() const { 00498 return key_.c_str(); 00499 } 00500 00501 // Gets the user supplied value. 00502 const char* value() const { 00503 return value_.c_str(); 00504 } 00505 00506 // Sets a new value, overriding the one supplied in the constructor. 00507 void SetValue(const std::string& new_value) { 00508 value_ = new_value; 00509 } 00510 00511 private: 00512 // The key supplied by the user. 00513 std::string key_; 00514 // The value supplied by the user. 00515 std::string value_; 00516 }; 00517 00518 // The result of a single Test. This includes a list of 00519 // TestPartResults, a list of TestProperties, a count of how many 00520 // death tests there are in the Test, and how much time it took to run 00521 // the Test. 00522 // 00523 // TestResult is not copyable. 00524 class GTEST_API_ TestResult { 00525 public: 00526 // Creates an empty TestResult. 00527 TestResult(); 00528 00529 // D'tor. Do not inherit from TestResult. 00530 ~TestResult(); 00531 00532 // Gets the number of all test parts. This is the sum of the number 00533 // of successful test parts and the number of failed test parts. 00534 int total_part_count() const; 00535 00536 // Returns the number of the test properties. 00537 int test_property_count() const; 00538 00539 // Returns true iff the test passed (i.e. no test part failed). 00540 bool Passed() const { return !Failed(); } 00541 00542 // Returns true iff the test failed. 00543 bool Failed() const; 00544 00545 // Returns true iff the test fatally failed. 00546 bool HasFatalFailure() const; 00547 00548 // Returns true iff the test has a non-fatal failure. 00549 bool HasNonfatalFailure() const; 00550 00551 // Returns the elapsed time, in milliseconds. 00552 TimeInMillis elapsed_time() const { return elapsed_time_; } 00553 00554 // Returns the i-th test part result among all the results. i can range 00555 // from 0 to test_property_count() - 1. If i is not in that range, aborts 00556 // the program. 00557 const TestPartResult& GetTestPartResult(int i) const; 00558 00559 // Returns the i-th test property. i can range from 0 to 00560 // test_property_count() - 1. If i is not in that range, aborts the 00561 // program. 00562 const TestProperty& GetTestProperty(int i) const; 00563 00564 private: 00565 friend class TestInfo; 00566 friend class TestCase; 00567 friend class UnitTest; 00568 friend class internal::DefaultGlobalTestPartResultReporter; 00569 friend class internal::ExecDeathTest; 00570 friend class internal::TestResultAccessor; 00571 friend class internal::UnitTestImpl; 00572 friend class internal::WindowsDeathTest; 00573 00574 // Gets the vector of TestPartResults. 00575 const std::vector<TestPartResult>& test_part_results() const { 00576 return test_part_results_; 00577 } 00578 00579 // Gets the vector of TestProperties. 00580 const std::vector<TestProperty>& test_properties() const { 00581 return test_properties_; 00582 } 00583 00584 // Sets the elapsed time. 00585 void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; } 00586 00587 // Adds a test property to the list. The property is validated and may add 00588 // a non-fatal failure if invalid (e.g., if it conflicts with reserved 00589 // key names). If a property is already recorded for the same key, the 00590 // value will be updated, rather than storing multiple values for the same 00591 // key. xml_element specifies the element for which the property is being 00592 // recorded and is used for validation. 00593 void RecordProperty(const std::string& xml_element, 00594 const TestProperty& test_property); 00595 00596 // Adds a failure if the key is a reserved attribute of Google Test 00597 // testcase tags. Returns true if the property is valid. 00598 // TODO(russr): Validate attribute names are legal and human readable. 00599 static bool ValidateTestProperty(const std::string& xml_element, 00600 const TestProperty& test_property); 00601 00602 // Adds a test part result to the list. 00603 void AddTestPartResult(const TestPartResult& test_part_result); 00604 00605 // Returns the death test count. 00606 int death_test_count() const { return death_test_count_; } 00607 00608 // Increments the death test count, returning the new count. 00609 int increment_death_test_count() { return ++death_test_count_; } 00610 00611 // Clears the test part results. 00612 void ClearTestPartResults(); 00613 00614 // Clears the object. 00615 void Clear(); 00616 00617 // Protects mutable state of the property vector and of owned 00618 // properties, whose values may be updated. 00619 internal::Mutex test_properites_mutex_; 00620 00621 // The vector of TestPartResults 00622 std::vector<TestPartResult> test_part_results_; 00623 // The vector of TestProperties 00624 std::vector<TestProperty> test_properties_; 00625 // Running count of death tests. 00626 int death_test_count_; 00627 // The elapsed time, in milliseconds. 00628 TimeInMillis elapsed_time_; 00629 00630 // We disallow copying TestResult. 00631 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestResult); 00632 }; // class TestResult 00633 00634 // A TestInfo object stores the following information about a test: 00635 // 00636 // Test case name 00637 // Test name 00638 // Whether the test should be run 00639 // A function pointer that creates the test object when invoked 00640 // Test result 00641 // 00642 // The constructor of TestInfo registers itself with the UnitTest 00643 // singleton such that the RUN_ALL_TESTS() macro knows which tests to 00644 // run. 00645 class GTEST_API_ TestInfo { 00646 public: 00647 // Destructs a TestInfo object. This function is not virtual, so 00648 // don't inherit from TestInfo. 00649 ~TestInfo(); 00650 00651 // Returns the test case name. 00652 const char* test_case_name() const { return test_case_name_.c_str(); } 00653 00654 // Returns the test name. 00655 const char* name() const { return name_.c_str(); } 00656 00657 // Returns the name of the parameter type, or NULL if this is not a typed 00658 // or a type-parameterized test. 00659 const char* type_param() const { 00660 if (type_param_.get() != NULL) 00661 return type_param_->c_str(); 00662 return NULL; 00663 } 00664 00665 // Returns the text representation of the value parameter, or NULL if this 00666 // is not a value-parameterized test. 00667 const char* value_param() const { 00668 if (value_param_.get() != NULL) 00669 return value_param_->c_str(); 00670 return NULL; 00671 } 00672 00673 // Returns true if this test should run, that is if the test is not 00674 // disabled (or it is disabled but the also_run_disabled_tests flag has 00675 // been specified) and its full name matches the user-specified filter. 00676 // 00677 // Google Test allows the user to filter the tests by their full names. 00678 // The full name of a test Bar in test case Foo is defined as 00679 // "Foo.Bar". Only the tests that match the filter will run. 00680 // 00681 // A filter is a colon-separated list of glob (not regex) patterns, 00682 // optionally followed by a '-' and a colon-separated list of 00683 // negative patterns (tests to exclude). A test is run if it 00684 // matches one of the positive patterns and does not match any of 00685 // the negative patterns. 00686 // 00687 // For example, *A*:Foo.* is a filter that matches any string that 00688 // contains the character 'A' or starts with "Foo.". 00689 bool should_run() const { return should_run_; } 00690 00691 // Returns true iff this test will appear in the XML report. 00692 bool is_reportable() const { 00693 // For now, the XML report includes all tests matching the filter. 00694 // In the future, we may trim tests that are excluded because of 00695 // sharding. 00696 return matches_filter_; 00697 } 00698 00699 // Returns the result of the test. 00700 const TestResult* result() const { return &result_; } 00701 00702 private: 00703 #if GTEST_HAS_DEATH_TEST 00704 friend class internal::DefaultDeathTestFactory; 00705 #endif // GTEST_HAS_DEATH_TEST 00706 friend class Test; 00707 friend class TestCase; 00708 friend class internal::UnitTestImpl; 00709 friend class internal::StreamingListenerTest; 00710 friend TestInfo* internal::MakeAndRegisterTestInfo( 00711 const char* test_case_name, 00712 const char* name, 00713 const char* type_param, 00714 const char* value_param, 00715 internal::TypeId fixture_class_id, 00716 Test::SetUpTestCaseFunc set_up_tc, 00717 Test::TearDownTestCaseFunc tear_down_tc, 00718 internal::TestFactoryBase* factory); 00719 00720 // Constructs a TestInfo object. The newly constructed instance assumes 00721 // ownership of the factory object. 00722 TestInfo(const std::string& test_case_name, 00723 const std::string& name, 00724 const char* a_type_param, // NULL if not a type-parameterized test 00725 const char* a_value_param, // NULL if not a value-parameterized test 00726 internal::TypeId fixture_class_id, 00727 internal::TestFactoryBase* factory); 00728 00729 // Increments the number of death tests encountered in this test so 00730 // far. 00731 int increment_death_test_count() { 00732 return result_.increment_death_test_count(); 00733 } 00734 00735 // Creates the test object, runs it, records its result, and then 00736 // deletes it. 00737 void Run(); 00738 00739 static void ClearTestResult(TestInfo* test_info) { 00740 test_info->result_.Clear(); 00741 } 00742 00743 // These fields are immutable properties of the test. 00744 const std::string test_case_name_; // Test case name 00745 const std::string name_; // Test name 00746 // Name of the parameter type, or NULL if this is not a typed or a 00747 // type-parameterized test. 00748 const internal::scoped_ptr<const ::std::string> type_param_; 00749 // Text representation of the value parameter, or NULL if this is not a 00750 // value-parameterized test. 00751 const internal::scoped_ptr<const ::std::string> value_param_; 00752 const internal::TypeId fixture_class_id_; // ID of the test fixture class 00753 bool should_run_; // True iff this test should run 00754 bool is_disabled_; // True iff this test is disabled 00755 bool matches_filter_; // True if this test matches the 00756 // user-specified filter. 00757 internal::TestFactoryBase* const factory_; // The factory that creates 00758 // the test object 00759 00760 // This field is mutable and needs to be reset before running the 00761 // test for the second time. 00762 TestResult result_; 00763 00764 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo); 00765 }; 00766 00767 // A test case, which consists of a vector of TestInfos. 00768 // 00769 // TestCase is not copyable. 00770 class GTEST_API_ TestCase { 00771 public: 00772 // Creates a TestCase with the given name. 00773 // 00774 // TestCase does NOT have a default constructor. Always use this 00775 // constructor to create a TestCase object. 00776 // 00777 // Arguments: 00778 // 00779 // name: name of the test case 00780 // a_type_param: the name of the test's type parameter, or NULL if 00781 // this is not a type-parameterized test. 00782 // set_up_tc: pointer to the function that sets up the test case 00783 // tear_down_tc: pointer to the function that tears down the test case 00784 TestCase(const char* name, const char* a_type_param, 00785 Test::SetUpTestCaseFunc set_up_tc, 00786 Test::TearDownTestCaseFunc tear_down_tc); 00787 00788 // Destructor of TestCase. 00789 virtual ~TestCase(); 00790 00791 // Gets the name of the TestCase. 00792 const char* name() const { return name_.c_str(); } 00793 00794 // Returns the name of the parameter type, or NULL if this is not a 00795 // type-parameterized test case. 00796 const char* type_param() const { 00797 if (type_param_.get() != NULL) 00798 return type_param_->c_str(); 00799 return NULL; 00800 } 00801 00802 // Returns true if any test in this test case should run. 00803 bool should_run() const { return should_run_; } 00804 00805 // Gets the number of successful tests in this test case. 00806 int successful_test_count() const; 00807 00808 // Gets the number of failed tests in this test case. 00809 int failed_test_count() const; 00810 00811 // Gets the number of disabled tests that will be reported in the XML report. 00812 int reportable_disabled_test_count() const; 00813 00814 // Gets the number of disabled tests in this test case. 00815 int disabled_test_count() const; 00816 00817 // Gets the number of tests to be printed in the XML report. 00818 int reportable_test_count() const; 00819 00820 // Get the number of tests in this test case that should run. 00821 int test_to_run_count() const; 00822 00823 // Gets the number of all tests in this test case. 00824 int total_test_count() const; 00825 00826 // Returns true iff the test case passed. 00827 bool Passed() const { return !Failed(); } 00828 00829 // Returns true iff the test case failed. 00830 bool Failed() const { return failed_test_count() > 0; } 00831 00832 // Returns the elapsed time, in milliseconds. 00833 TimeInMillis elapsed_time() const { return elapsed_time_; } 00834 00835 // Returns the i-th test among all the tests. i can range from 0 to 00836 // total_test_count() - 1. If i is not in that range, returns NULL. 00837 const TestInfo* GetTestInfo(int i) const; 00838 00839 // Returns the TestResult that holds test properties recorded during 00840 // execution of SetUpTestCase and TearDownTestCase. 00841 const TestResult& ad_hoc_test_result() const { return ad_hoc_test_result_; } 00842 00843 private: 00844 friend class Test; 00845 friend class internal::UnitTestImpl; 00846 00847 // Gets the (mutable) vector of TestInfos in this TestCase. 00848 std::vector<TestInfo*>& test_info_list() { return test_info_list_; } 00849 00850 // Gets the (immutable) vector of TestInfos in this TestCase. 00851 const std::vector<TestInfo*>& test_info_list() const { 00852 return test_info_list_; 00853 } 00854 00855 // Returns the i-th test among all the tests. i can range from 0 to 00856 // total_test_count() - 1. If i is not in that range, returns NULL. 00857 TestInfo* GetMutableTestInfo(int i); 00858 00859 // Sets the should_run member. 00860 void set_should_run(bool should) { should_run_ = should; } 00861 00862 // Adds a TestInfo to this test case. Will delete the TestInfo upon 00863 // destruction of the TestCase object. 00864 void AddTestInfo(TestInfo * test_info); 00865 00866 // Clears the results of all tests in this test case. 00867 void ClearResult(); 00868 00869 // Clears the results of all tests in the given test case. 00870 static void ClearTestCaseResult(TestCase* test_case) { 00871 test_case->ClearResult(); 00872 } 00873 00874 // Runs every test in this TestCase. 00875 void Run(); 00876 00877 // Runs SetUpTestCase() for this TestCase. This wrapper is needed 00878 // for catching exceptions thrown from SetUpTestCase(). 00879 void RunSetUpTestCase() { (*set_up_tc_)(); } 00880 00881 // Runs TearDownTestCase() for this TestCase. This wrapper is 00882 // needed for catching exceptions thrown from TearDownTestCase(). 00883 void RunTearDownTestCase() { (*tear_down_tc_)(); } 00884 00885 // Returns true iff test passed. 00886 static bool TestPassed(const TestInfo* test_info) { 00887 return test_info->should_run() && test_info->result()->Passed(); 00888 } 00889 00890 // Returns true iff test failed. 00891 static bool TestFailed(const TestInfo* test_info) { 00892 return test_info->should_run() && test_info->result()->Failed(); 00893 } 00894 00895 // Returns true iff the test is disabled and will be reported in the XML 00896 // report. 00897 static bool TestReportableDisabled(const TestInfo* test_info) { 00898 return test_info->is_reportable() && test_info->is_disabled_; 00899 } 00900 00901 // Returns true iff test is disabled. 00902 static bool TestDisabled(const TestInfo* test_info) { 00903 return test_info->is_disabled_; 00904 } 00905 00906 // Returns true iff this test will appear in the XML report. 00907 static bool TestReportable(const TestInfo* test_info) { 00908 return test_info->is_reportable(); 00909 } 00910 00911 // Returns true if the given test should run. 00912 static bool ShouldRunTest(const TestInfo* test_info) { 00913 return test_info->should_run(); 00914 } 00915 00916 // Shuffles the tests in this test case. 00917 void ShuffleTests(internal::Random* random); 00918 00919 // Restores the test order to before the first shuffle. 00920 void UnshuffleTests(); 00921 00922 // Name of the test case. 00923 std::string name_; 00924 // Name of the parameter type, or NULL if this is not a typed or a 00925 // type-parameterized test. 00926 const internal::scoped_ptr<const ::std::string> type_param_; 00927 // The vector of TestInfos in their original order. It owns the 00928 // elements in the vector. 00929 std::vector<TestInfo*> test_info_list_; 00930 // Provides a level of indirection for the test list to allow easy 00931 // shuffling and restoring the test order. The i-th element in this 00932 // vector is the index of the i-th test in the shuffled test list. 00933 std::vector<int> test_indices_; 00934 // Pointer to the function that sets up the test case. 00935 Test::SetUpTestCaseFunc set_up_tc_; 00936 // Pointer to the function that tears down the test case. 00937 Test::TearDownTestCaseFunc tear_down_tc_; 00938 // True iff any test in this test case should run. 00939 bool should_run_; 00940 // Elapsed time, in milliseconds. 00941 TimeInMillis elapsed_time_; 00942 // Holds test properties recorded during execution of SetUpTestCase and 00943 // TearDownTestCase. 00944 TestResult ad_hoc_test_result_; 00945 00946 // We disallow copying TestCases. 00947 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestCase); 00948 }; 00949 00950 // An Environment object is capable of setting up and tearing down an 00951 // environment. You should subclass this to define your own 00952 // environment(s). 00953 // 00954 // An Environment object does the set-up and tear-down in virtual 00955 // methods SetUp() and TearDown() instead of the constructor and the 00956 // destructor, as: 00957 // 00958 // 1. You cannot safely throw from a destructor. This is a problem 00959 // as in some cases Google Test is used where exceptions are enabled, and 00960 // we may want to implement ASSERT_* using exceptions where they are 00961 // available. 00962 // 2. You cannot use ASSERT_* directly in a constructor or 00963 // destructor. 00964 class Environment { 00965 public: 00966 // The d'tor is virtual as we need to subclass Environment. 00967 virtual ~Environment() {} 00968 00969 // Override this to define how to set up the environment. 00970 virtual void SetUp() {} 00971 00972 // Override this to define how to tear down the environment. 00973 virtual void TearDown() {} 00974 private: 00975 // If you see an error about overriding the following function or 00976 // about it being private, you have mis-spelled SetUp() as Setup(). 00977 struct Setup_should_be_spelled_SetUp {}; 00978 virtual Setup_should_be_spelled_SetUp* Setup() { return NULL; } 00979 }; 00980 00981 // The interface for tracing execution of tests. The methods are organized in 00982 // the order the corresponding events are fired. 00983 class TestEventListener { 00984 public: 00985 virtual ~TestEventListener() {} 00986 00987 // Fired before any test activity starts. 00988 virtual void OnTestProgramStart(const UnitTest& unit_test) = 0; 00989 00990 // Fired before each iteration of tests starts. There may be more than 00991 // one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration 00992 // index, starting from 0. 00993 virtual void OnTestIterationStart(const UnitTest& unit_test, 00994 int iteration) = 0; 00995 00996 // Fired before environment set-up for each iteration of tests starts. 00997 virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0; 00998 00999 // Fired after environment set-up for each iteration of tests ends. 01000 virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0; 01001 01002 // Fired before the test case starts. 01003 virtual void OnTestCaseStart(const TestCase& test_case) = 0; 01004 01005 // Fired before the test starts. 01006 virtual void OnTestStart(const TestInfo& test_info) = 0; 01007 01008 // Fired after a failed assertion or a SUCCEED() invocation. 01009 virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0; 01010 01011 // Fired after the test ends. 01012 virtual void OnTestEnd(const TestInfo& test_info) = 0; 01013 01014 // Fired after the test case ends. 01015 virtual void OnTestCaseEnd(const TestCase& test_case) = 0; 01016 01017 // Fired before environment tear-down for each iteration of tests starts. 01018 virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0; 01019 01020 // Fired after environment tear-down for each iteration of tests ends. 01021 virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0; 01022 01023 // Fired after each iteration of tests finishes. 01024 virtual void OnTestIterationEnd(const UnitTest& unit_test, 01025 int iteration) = 0; 01026 01027 // Fired after all test activities have ended. 01028 virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0; 01029 }; 01030 01031 // The convenience class for users who need to override just one or two 01032 // methods and are not concerned that a possible change to a signature of 01033 // the methods they override will not be caught during the build. For 01034 // comments about each method please see the definition of TestEventListener 01035 // above. 01036 class EmptyTestEventListener : public TestEventListener { 01037 public: 01038 virtual void OnTestProgramStart(const UnitTest& /*unit_test*/) {} 01039 virtual void OnTestIterationStart(const UnitTest& /*unit_test*/, 01040 int /*iteration*/) {} 01041 virtual void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) {} 01042 virtual void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) {} 01043 virtual void OnTestCaseStart(const TestCase& /*test_case*/) {} 01044 virtual void OnTestStart(const TestInfo& /*test_info*/) {} 01045 virtual void OnTestPartResult(const TestPartResult& /*test_part_result*/) {} 01046 virtual void OnTestEnd(const TestInfo& /*test_info*/) {} 01047 virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {} 01048 virtual void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) {} 01049 virtual void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) {} 01050 virtual void OnTestIterationEnd(const UnitTest& /*unit_test*/, 01051 int /*iteration*/) {} 01052 virtual void OnTestProgramEnd(const UnitTest& /*unit_test*/) {} 01053 }; 01054 01055 // TestEventListeners lets users add listeners to track events in Google Test. 01056 class GTEST_API_ TestEventListeners { 01057 public: 01058 TestEventListeners(); 01059 ~TestEventListeners(); 01060 01061 // Appends an event listener to the end of the list. Google Test assumes 01062 // the ownership of the listener (i.e. it will delete the listener when 01063 // the test program finishes). 01064 void Append(TestEventListener* listener); 01065 01066 // Removes the given event listener from the list and returns it. It then 01067 // becomes the caller's responsibility to delete the listener. Returns 01068 // NULL if the listener is not found in the list. 01069 TestEventListener* Release(TestEventListener* listener); 01070 01071 // Returns the standard listener responsible for the default console 01072 // output. Can be removed from the listeners list to shut down default 01073 // console output. Note that removing this object from the listener list 01074 // with Release transfers its ownership to the caller and makes this 01075 // function return NULL the next time. 01076 TestEventListener* default_result_printer() const { 01077 return default_result_printer_; 01078 } 01079 01080 // Returns the standard listener responsible for the default XML output 01081 // controlled by the --gtest_output=xml flag. Can be removed from the 01082 // listeners list by users who want to shut down the default XML output 01083 // controlled by this flag and substitute it with custom one. Note that 01084 // removing this object from the listener list with Release transfers its 01085 // ownership to the caller and makes this function return NULL the next 01086 // time. 01087 TestEventListener* default_xml_generator() const { 01088 return default_xml_generator_; 01089 } 01090 01091 private: 01092 friend class TestCase; 01093 friend class TestInfo; 01094 friend class internal::DefaultGlobalTestPartResultReporter; 01095 friend class internal::NoExecDeathTest; 01096 friend class internal::TestEventListenersAccessor; 01097 friend class internal::UnitTestImpl; 01098 01099 // Returns repeater that broadcasts the TestEventListener events to all 01100 // subscribers. 01101 TestEventListener* repeater(); 01102 01103 // Sets the default_result_printer attribute to the provided listener. 01104 // The listener is also added to the listener list and previous 01105 // default_result_printer is removed from it and deleted. The listener can 01106 // also be NULL in which case it will not be added to the list. Does 01107 // nothing if the previous and the current listener objects are the same. 01108 void SetDefaultResultPrinter(TestEventListener* listener); 01109 01110 // Sets the default_xml_generator attribute to the provided listener. The 01111 // listener is also added to the listener list and previous 01112 // default_xml_generator is removed from it and deleted. The listener can 01113 // also be NULL in which case it will not be added to the list. Does 01114 // nothing if the previous and the current listener objects are the same. 01115 void SetDefaultXmlGenerator(TestEventListener* listener); 01116 01117 // Controls whether events will be forwarded by the repeater to the 01118 // listeners in the list. 01119 bool EventForwardingEnabled() const; 01120 void SuppressEventForwarding(); 01121 01122 // The actual list of listeners. 01123 internal::TestEventRepeater* repeater_; 01124 // Listener responsible for the standard result output. 01125 TestEventListener* default_result_printer_; 01126 // Listener responsible for the creation of the XML output file. 01127 TestEventListener* default_xml_generator_; 01128 01129 // We disallow copying TestEventListeners. 01130 GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventListeners); 01131 }; 01132 01133 // A UnitTest consists of a vector of TestCases. 01134 // 01135 // This is a singleton class. The only instance of UnitTest is 01136 // created when UnitTest::GetInstance() is first called. This 01137 // instance is never deleted. 01138 // 01139 // UnitTest is not copyable. 01140 // 01141 // This class is thread-safe as long as the methods are called 01142 // according to their specification. 01143 class GTEST_API_ UnitTest { 01144 public: 01145 // Gets the singleton UnitTest object. The first time this method 01146 // is called, a UnitTest object is constructed and returned. 01147 // Consecutive calls will return the same object. 01148 static UnitTest* GetInstance(); 01149 01150 // Runs all tests in this UnitTest object and prints the result. 01151 // Returns 0 if successful, or 1 otherwise. 01152 // 01153 // This method can only be called from the main thread. 01154 // 01155 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 01156 int Run() GTEST_MUST_USE_RESULT_; 01157 01158 // Returns the working directory when the first TEST() or TEST_F() 01159 // was executed. The UnitTest object owns the string. 01160 const char* original_working_dir() const; 01161 01162 // Returns the TestCase object for the test that's currently running, 01163 // or NULL if no test is running. 01164 const TestCase* current_test_case() const 01165 GTEST_LOCK_EXCLUDED_(mutex_); 01166 01167 // Returns the TestInfo object for the test that's currently running, 01168 // or NULL if no test is running. 01169 const TestInfo* current_test_info() const 01170 GTEST_LOCK_EXCLUDED_(mutex_); 01171 01172 // Returns the random seed used at the start of the current test run. 01173 int random_seed() const; 01174 01175 #if GTEST_HAS_PARAM_TEST 01176 // Returns the ParameterizedTestCaseRegistry object used to keep track of 01177 // value-parameterized tests and instantiate and register them. 01178 // 01179 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 01180 internal::ParameterizedTestCaseRegistry& parameterized_test_registry() 01181 GTEST_LOCK_EXCLUDED_(mutex_); 01182 #endif // GTEST_HAS_PARAM_TEST 01183 01184 // Gets the number of successful test cases. 01185 int successful_test_case_count() const; 01186 01187 // Gets the number of failed test cases. 01188 int failed_test_case_count() const; 01189 01190 // Gets the number of all test cases. 01191 int total_test_case_count() const; 01192 01193 // Gets the number of all test cases that contain at least one test 01194 // that should run. 01195 int test_case_to_run_count() const; 01196 01197 // Gets the number of successful tests. 01198 int successful_test_count() const; 01199 01200 // Gets the number of failed tests. 01201 int failed_test_count() const; 01202 01203 // Gets the number of disabled tests that will be reported in the XML report. 01204 int reportable_disabled_test_count() const; 01205 01206 // Gets the number of disabled tests. 01207 int disabled_test_count() const; 01208 01209 // Gets the number of tests to be printed in the XML report. 01210 int reportable_test_count() const; 01211 01212 // Gets the number of all tests. 01213 int total_test_count() const; 01214 01215 // Gets the number of tests that should run. 01216 int test_to_run_count() const; 01217 01218 // Gets the time of the test program start, in ms from the start of the 01219 // UNIX epoch. 01220 TimeInMillis start_timestamp() const; 01221 01222 // Gets the elapsed time, in milliseconds. 01223 TimeInMillis elapsed_time() const; 01224 01225 // Returns true iff the unit test passed (i.e. all test cases passed). 01226 bool Passed() const; 01227 01228 // Returns true iff the unit test failed (i.e. some test case failed 01229 // or something outside of all tests failed). 01230 bool Failed() const; 01231 01232 // Gets the i-th test case among all the test cases. i can range from 0 to 01233 // total_test_case_count() - 1. If i is not in that range, returns NULL. 01234 const TestCase* GetTestCase(int i) const; 01235 01236 // Returns the TestResult containing information on test failures and 01237 // properties logged outside of individual test cases. 01238 const TestResult& ad_hoc_test_result() const; 01239 01240 // Returns the list of event listeners that can be used to track events 01241 // inside Google Test. 01242 TestEventListeners& listeners(); 01243 01244 private: 01245 // Registers and returns a global test environment. When a test 01246 // program is run, all global test environments will be set-up in 01247 // the order they were registered. After all tests in the program 01248 // have finished, all global test environments will be torn-down in 01249 // the *reverse* order they were registered. 01250 // 01251 // The UnitTest object takes ownership of the given environment. 01252 // 01253 // This method can only be called from the main thread. 01254 Environment* AddEnvironment(Environment* env); 01255 01256 // Adds a TestPartResult to the current TestResult object. All 01257 // Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) 01258 // eventually call this to report their results. The user code 01259 // should use the assertion macros instead of calling this directly. 01260 void AddTestPartResult(TestPartResult::Type result_type, 01261 const char* file_name, 01262 int line_number, 01263 const std::string& message, 01264 const std::string& os_stack_trace) 01265 GTEST_LOCK_EXCLUDED_(mutex_); 01266 01267 // Adds a TestProperty to the current TestResult object when invoked from 01268 // inside a test, to current TestCase's ad_hoc_test_result_ when invoked 01269 // from SetUpTestCase or TearDownTestCase, or to the global property set 01270 // when invoked elsewhere. If the result already contains a property with 01271 // the same key, the value will be updated. 01272 void RecordProperty(const std::string& key, const std::string& value); 01273 01274 // Gets the i-th test case among all the test cases. i can range from 0 to 01275 // total_test_case_count() - 1. If i is not in that range, returns NULL. 01276 TestCase* GetMutableTestCase(int i); 01277 01278 // Accessors for the implementation object. 01279 internal::UnitTestImpl* impl() { return impl_; } 01280 const internal::UnitTestImpl* impl() const { return impl_; } 01281 01282 // These classes and funcions are friends as they need to access private 01283 // members of UnitTest. 01284 friend class Test; 01285 friend class internal::AssertHelper; 01286 friend class internal::ScopedTrace; 01287 friend class internal::StreamingListenerTest; 01288 friend class internal::UnitTestRecordPropertyTestHelper; 01289 friend Environment* AddGlobalTestEnvironment(Environment* env); 01290 friend internal::UnitTestImpl* internal::GetUnitTestImpl(); 01291 friend void internal::ReportFailureInUnknownLocation( 01292 TestPartResult::Type result_type, 01293 const std::string& message); 01294 01295 // Creates an empty UnitTest. 01296 UnitTest(); 01297 01298 // D'tor 01299 virtual ~UnitTest(); 01300 01301 // Pushes a trace defined by SCOPED_TRACE() on to the per-thread 01302 // Google Test trace stack. 01303 void PushGTestTrace(const internal::TraceInfo& trace) 01304 GTEST_LOCK_EXCLUDED_(mutex_); 01305 01306 // Pops a trace from the per-thread Google Test trace stack. 01307 void PopGTestTrace() 01308 GTEST_LOCK_EXCLUDED_(mutex_); 01309 01310 // Protects mutable state in *impl_. This is mutable as some const 01311 // methods need to lock it too. 01312 mutable internal::Mutex mutex_; 01313 01314 // Opaque implementation object. This field is never changed once 01315 // the object is constructed. We don't mark it as const here, as 01316 // doing so will cause a warning in the constructor of UnitTest. 01317 // Mutable state in *impl_ is protected by mutex_. 01318 internal::UnitTestImpl* impl_; 01319 01320 // We disallow copying UnitTest. 01321 GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTest); 01322 }; 01323 01324 // A convenient wrapper for adding an environment for the test 01325 // program. 01326 // 01327 // You should call this before RUN_ALL_TESTS() is called, probably in 01328 // main(). If you use gtest_main, you need to call this before main() 01329 // starts for it to take effect. For example, you can define a global 01330 // variable like this: 01331 // 01332 // testing::Environment* const foo_env = 01333 // testing::AddGlobalTestEnvironment(new FooEnvironment); 01334 // 01335 // However, we strongly recommend you to write your own main() and 01336 // call AddGlobalTestEnvironment() there, as relying on initialization 01337 // of global variables makes the code harder to read and may cause 01338 // problems when you register multiple environments from different 01339 // translation units and the environments have dependencies among them 01340 // (remember that the compiler doesn't guarantee the order in which 01341 // global variables from different translation units are initialized). 01342 inline Environment* AddGlobalTestEnvironment(Environment* env) { 01343 return UnitTest::GetInstance()->AddEnvironment(env); 01344 } 01345 01346 // Initializes Google Test. This must be called before calling 01347 // RUN_ALL_TESTS(). In particular, it parses a command line for the 01348 // flags that Google Test recognizes. Whenever a Google Test flag is 01349 // seen, it is removed from argv, and *argc is decremented. 01350 // 01351 // No value is returned. Instead, the Google Test flag variables are 01352 // updated. 01353 // 01354 // Calling the function for the second time has no user-visible effect. 01355 GTEST_API_ void InitGoogleTest(int* argc, char** argv); 01356 01357 // This overloaded version can be used in Windows programs compiled in 01358 // UNICODE mode. 01359 GTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv); 01360 01361 namespace internal { 01362 01363 // FormatForComparison<ToPrint, OtherOperand>::Format(value) formats a 01364 // value of type ToPrint that is an operand of a comparison assertion 01365 // (e.g. ASSERT_EQ). OtherOperand is the type of the other operand in 01366 // the comparison, and is used to help determine the best way to 01367 // format the value. In particular, when the value is a C string 01368 // (char pointer) and the other operand is an STL string object, we 01369 // want to format the C string as a string, since we know it is 01370 // compared by value with the string object. If the value is a char 01371 // pointer but the other operand is not an STL string object, we don't 01372 // know whether the pointer is supposed to point to a NUL-terminated 01373 // string, and thus want to print it as a pointer to be safe. 01374 // 01375 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 01376 01377 // The default case. 01378 template <typename ToPrint, typename OtherOperand> 01379 class FormatForComparison { 01380 public: 01381 static ::std::string Format(const ToPrint& value) { 01382 return ::testing::PrintToString(value); 01383 } 01384 }; 01385 01386 // Array. 01387 template <typename ToPrint, size_t N, typename OtherOperand> 01388 class FormatForComparison<ToPrint[N], OtherOperand> { 01389 public: 01390 static ::std::string Format(const ToPrint* value) { 01391 return FormatForComparison<const ToPrint*, OtherOperand>::Format(value); 01392 } 01393 }; 01394 01395 // By default, print C string as pointers to be safe, as we don't know 01396 // whether they actually point to a NUL-terminated string. 01397 01398 #define GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(CharType) \ 01399 template <typename OtherOperand> \ 01400 class FormatForComparison<CharType*, OtherOperand> { \ 01401 public: \ 01402 static ::std::string Format(CharType* value) { \ 01403 return ::testing::PrintToString(static_cast<const void*>(value)); \ 01404 } \ 01405 } 01406 01407 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(char); 01408 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const char); 01409 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(wchar_t); 01410 GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_(const wchar_t); 01411 01412 #undef GTEST_IMPL_FORMAT_C_STRING_AS_POINTER_ 01413 01414 // If a C string is compared with an STL string object, we know it's meant 01415 // to point to a NUL-terminated string, and thus can print it as a string. 01416 01417 #define GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(CharType, OtherStringType) \ 01418 template <> \ 01419 class FormatForComparison<CharType*, OtherStringType> { \ 01420 public: \ 01421 static ::std::string Format(CharType* value) { \ 01422 return ::testing::PrintToString(value); \ 01423 } \ 01424 } 01425 01426 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::std::string); 01427 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::std::string); 01428 01429 #if GTEST_HAS_GLOBAL_STRING 01430 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(char, ::string); 01431 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const char, ::string); 01432 #endif 01433 01434 #if GTEST_HAS_GLOBAL_WSTRING 01435 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::wstring); 01436 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::wstring); 01437 #endif 01438 01439 #if GTEST_HAS_STD_WSTRING 01440 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(wchar_t, ::std::wstring); 01441 GTEST_IMPL_FORMAT_C_STRING_AS_STRING_(const wchar_t, ::std::wstring); 01442 #endif 01443 01444 #undef GTEST_IMPL_FORMAT_C_STRING_AS_STRING_ 01445 01446 // Formats a comparison assertion (e.g. ASSERT_EQ, EXPECT_LT, and etc) 01447 // operand to be used in a failure message. The type (but not value) 01448 // of the other operand may affect the format. This allows us to 01449 // print a char* as a raw pointer when it is compared against another 01450 // char* or void*, and print it as a C string when it is compared 01451 // against an std::string object, for example. 01452 // 01453 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 01454 template <typename T1, typename T2> 01455 std::string FormatForComparisonFailureMessage( 01456 const T1& value, const T2& /* other_operand */) { 01457 return FormatForComparison<T1, T2>::Format(value); 01458 } 01459 01460 // Separate the error generating code from the code path to reduce the stack 01461 // frame size of CmpHelperEQ. This helps reduce the overhead of some sanitizers 01462 // when calling EXPECT_* in a tight loop. 01463 template <typename T1, typename T2> 01464 AssertionResult CmpHelperEQFailure(const char* expected_expression, 01465 const char* actual_expression, 01466 const T1& expected, const T2& actual) { 01467 return EqFailure(expected_expression, 01468 actual_expression, 01469 FormatForComparisonFailureMessage(expected, actual), 01470 FormatForComparisonFailureMessage(actual, expected), 01471 false); 01472 } 01473 01474 // The helper function for {ASSERT|EXPECT}_EQ. 01475 template <typename T1, typename T2> 01476 AssertionResult CmpHelperEQ(const char* expected_expression, 01477 const char* actual_expression, 01478 const T1& expected, 01479 const T2& actual) { 01480 GTEST_DISABLE_MSC_WARNINGS_PUSH_(4389 /* signed/unsigned mismatch */) 01481 if (expected == actual) { 01482 return AssertionSuccess(); 01483 } 01484 GTEST_DISABLE_MSC_WARNINGS_POP_() 01485 01486 return CmpHelperEQFailure(expected_expression, actual_expression, expected, 01487 actual); 01488 } 01489 01490 // With this overloaded version, we allow anonymous enums to be used 01491 // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous enums 01492 // can be implicitly cast to BiggestInt. 01493 GTEST_API_ AssertionResult CmpHelperEQ(const char* expected_expression, 01494 const char* actual_expression, 01495 BiggestInt expected, 01496 BiggestInt actual); 01497 01498 // The helper class for {ASSERT|EXPECT}_EQ. The template argument 01499 // lhs_is_null_literal is true iff the first argument to ASSERT_EQ() 01500 // is a null pointer literal. The following default implementation is 01501 // for lhs_is_null_literal being false. 01502 template <bool lhs_is_null_literal> 01503 class EqHelper { 01504 public: 01505 // This templatized version is for the general case. 01506 template <typename T1, typename T2> 01507 static AssertionResult Compare(const char* expected_expression, 01508 const char* actual_expression, 01509 const T1& expected, 01510 const T2& actual) { 01511 return CmpHelperEQ(expected_expression, actual_expression, expected, 01512 actual); 01513 } 01514 01515 // With this overloaded version, we allow anonymous enums to be used 01516 // in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous 01517 // enums can be implicitly cast to BiggestInt. 01518 // 01519 // Even though its body looks the same as the above version, we 01520 // cannot merge the two, as it will make anonymous enums unhappy. 01521 static AssertionResult Compare(const char* expected_expression, 01522 const char* actual_expression, 01523 BiggestInt expected, 01524 BiggestInt actual) { 01525 return CmpHelperEQ(expected_expression, actual_expression, expected, 01526 actual); 01527 } 01528 }; 01529 01530 // This specialization is used when the first argument to ASSERT_EQ() 01531 // is a null pointer literal, like NULL, false, or 0. 01532 template <> 01533 class EqHelper<true> { 01534 public: 01535 // We define two overloaded versions of Compare(). The first 01536 // version will be picked when the second argument to ASSERT_EQ() is 01537 // NOT a pointer, e.g. ASSERT_EQ(0, AnIntFunction()) or 01538 // EXPECT_EQ(false, a_bool). 01539 template <typename T1, typename T2> 01540 static AssertionResult Compare( 01541 const char* expected_expression, 01542 const char* actual_expression, 01543 const T1& expected, 01544 const T2& actual, 01545 // The following line prevents this overload from being considered if T2 01546 // is not a pointer type. We need this because ASSERT_EQ(NULL, my_ptr) 01547 // expands to Compare("", "", NULL, my_ptr), which requires a conversion 01548 // to match the Secret* in the other overload, which would otherwise make 01549 // this template match better. 01550 typename EnableIf<!is_pointer<T2>::value>::type* = 0) { 01551 return CmpHelperEQ(expected_expression, actual_expression, expected, 01552 actual); 01553 } 01554 01555 // This version will be picked when the second argument to ASSERT_EQ() is a 01556 // pointer, e.g. ASSERT_EQ(NULL, a_pointer). 01557 template <typename T> 01558 static AssertionResult Compare( 01559 const char* expected_expression, 01560 const char* actual_expression, 01561 // We used to have a second template parameter instead of Secret*. That 01562 // template parameter would deduce to 'long', making this a better match 01563 // than the first overload even without the first overload's EnableIf. 01564 // Unfortunately, gcc with -Wconversion-null warns when "passing NULL to 01565 // non-pointer argument" (even a deduced integral argument), so the old 01566 // implementation caused warnings in user code. 01567 Secret* /* expected (NULL) */, 01568 T* actual) { 01569 // We already know that 'expected' is a null pointer. 01570 return CmpHelperEQ(expected_expression, actual_expression, 01571 static_cast<T*>(NULL), actual); 01572 } 01573 }; 01574 01575 // Separate the error generating code from the code path to reduce the stack 01576 // frame size of CmpHelperOP. This helps reduce the overhead of some sanitizers 01577 // when calling EXPECT_OP in a tight loop. 01578 template <typename T1, typename T2> 01579 AssertionResult CmpHelperOpFailure(const char* expr1, const char* expr2, 01580 const T1& val1, const T2& val2, 01581 const char* op) { 01582 return AssertionFailure() 01583 << "Expected: (" << expr1 << ") " << op << " (" << expr2 01584 << "), actual: " << FormatForComparisonFailureMessage(val1, val2) 01585 << " vs " << FormatForComparisonFailureMessage(val2, val1); 01586 } 01587 01588 // A macro for implementing the helper functions needed to implement 01589 // ASSERT_?? and EXPECT_??. It is here just to avoid copy-and-paste 01590 // of similar code. 01591 // 01592 // For each templatized helper function, we also define an overloaded 01593 // version for BiggestInt in order to reduce code bloat and allow 01594 // anonymous enums to be used with {ASSERT|EXPECT}_?? when compiled 01595 // with gcc 4. 01596 // 01597 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 01598 01599 #define GTEST_IMPL_CMP_HELPER_(op_name, op)\ 01600 template <typename T1, typename T2>\ 01601 AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ 01602 const T1& val1, const T2& val2) {\ 01603 if (val1 op val2) {\ 01604 return AssertionSuccess();\ 01605 } else {\ 01606 return CmpHelperOpFailure(expr1, expr2, val1, val2, #op);\ 01607 }\ 01608 }\ 01609 GTEST_API_ AssertionResult CmpHelper##op_name(\ 01610 const char* expr1, const char* expr2, BiggestInt val1, BiggestInt val2) 01611 01612 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 01613 01614 // Implements the helper function for {ASSERT|EXPECT}_NE 01615 GTEST_IMPL_CMP_HELPER_(NE, !=); 01616 // Implements the helper function for {ASSERT|EXPECT}_LE 01617 GTEST_IMPL_CMP_HELPER_(LE, <=); 01618 // Implements the helper function for {ASSERT|EXPECT}_LT 01619 GTEST_IMPL_CMP_HELPER_(LT, <); 01620 // Implements the helper function for {ASSERT|EXPECT}_GE 01621 GTEST_IMPL_CMP_HELPER_(GE, >=); 01622 // Implements the helper function for {ASSERT|EXPECT}_GT 01623 GTEST_IMPL_CMP_HELPER_(GT, >); 01624 01625 #undef GTEST_IMPL_CMP_HELPER_ 01626 01627 // The helper function for {ASSERT|EXPECT}_STREQ. 01628 // 01629 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 01630 GTEST_API_ AssertionResult CmpHelperSTREQ(const char* expected_expression, 01631 const char* actual_expression, 01632 const char* expected, 01633 const char* actual); 01634 01635 // The helper function for {ASSERT|EXPECT}_STRCASEEQ. 01636 // 01637 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 01638 GTEST_API_ AssertionResult CmpHelperSTRCASEEQ(const char* expected_expression, 01639 const char* actual_expression, 01640 const char* expected, 01641 const char* actual); 01642 01643 // The helper function for {ASSERT|EXPECT}_STRNE. 01644 // 01645 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 01646 GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression, 01647 const char* s2_expression, 01648 const char* s1, 01649 const char* s2); 01650 01651 // The helper function for {ASSERT|EXPECT}_STRCASENE. 01652 // 01653 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 01654 GTEST_API_ AssertionResult CmpHelperSTRCASENE(const char* s1_expression, 01655 const char* s2_expression, 01656 const char* s1, 01657 const char* s2); 01658 01659 01660 // Helper function for *_STREQ on wide strings. 01661 // 01662 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 01663 GTEST_API_ AssertionResult CmpHelperSTREQ(const char* expected_expression, 01664 const char* actual_expression, 01665 const wchar_t* expected, 01666 const wchar_t* actual); 01667 01668 // Helper function for *_STRNE on wide strings. 01669 // 01670 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 01671 GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression, 01672 const char* s2_expression, 01673 const wchar_t* s1, 01674 const wchar_t* s2); 01675 01676 } // namespace internal 01677 01678 // IsSubstring() and IsNotSubstring() are intended to be used as the 01679 // first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by 01680 // themselves. They check whether needle is a substring of haystack 01681 // (NULL is considered a substring of itself only), and return an 01682 // appropriate error message when they fail. 01683 // 01684 // The {needle,haystack}_expr arguments are the stringified 01685 // expressions that generated the two real arguments. 01686 GTEST_API_ AssertionResult IsSubstring( 01687 const char* needle_expr, const char* haystack_expr, 01688 const char* needle, const char* haystack); 01689 GTEST_API_ AssertionResult IsSubstring( 01690 const char* needle_expr, const char* haystack_expr, 01691 const wchar_t* needle, const wchar_t* haystack); 01692 GTEST_API_ AssertionResult IsNotSubstring( 01693 const char* needle_expr, const char* haystack_expr, 01694 const char* needle, const char* haystack); 01695 GTEST_API_ AssertionResult IsNotSubstring( 01696 const char* needle_expr, const char* haystack_expr, 01697 const wchar_t* needle, const wchar_t* haystack); 01698 GTEST_API_ AssertionResult IsSubstring( 01699 const char* needle_expr, const char* haystack_expr, 01700 const ::std::string& needle, const ::std::string& haystack); 01701 GTEST_API_ AssertionResult IsNotSubstring( 01702 const char* needle_expr, const char* haystack_expr, 01703 const ::std::string& needle, const ::std::string& haystack); 01704 01705 #if GTEST_HAS_STD_WSTRING 01706 GTEST_API_ AssertionResult IsSubstring( 01707 const char* needle_expr, const char* haystack_expr, 01708 const ::std::wstring& needle, const ::std::wstring& haystack); 01709 GTEST_API_ AssertionResult IsNotSubstring( 01710 const char* needle_expr, const char* haystack_expr, 01711 const ::std::wstring& needle, const ::std::wstring& haystack); 01712 #endif // GTEST_HAS_STD_WSTRING 01713 01714 namespace internal { 01715 01716 // Helper template function for comparing floating-points. 01717 // 01718 // Template parameter: 01719 // 01720 // RawType: the raw floating-point type (either float or double) 01721 // 01722 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 01723 template <typename RawType> 01724 AssertionResult CmpHelperFloatingPointEQ(const char* expected_expression, 01725 const char* actual_expression, 01726 RawType expected, 01727 RawType actual) { 01728 const FloatingPoint<RawType> lhs(expected), rhs(actual); 01729 01730 if (lhs.AlmostEquals(rhs)) { 01731 return AssertionSuccess(); 01732 } 01733 01734 ::std::stringstream expected_ss; 01735 expected_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) 01736 << expected; 01737 01738 ::std::stringstream actual_ss; 01739 actual_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) 01740 << actual; 01741 01742 return EqFailure(expected_expression, 01743 actual_expression, 01744 StringStreamToString(&expected_ss), 01745 StringStreamToString(&actual_ss), 01746 false); 01747 } 01748 01749 // Helper function for implementing ASSERT_NEAR. 01750 // 01751 // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. 01752 GTEST_API_ AssertionResult DoubleNearPredFormat(const char* expr1, 01753 const char* expr2, 01754 const char* abs_error_expr, 01755 double val1, 01756 double val2, 01757 double abs_error); 01758 01759 // INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. 01760 // A class that enables one to stream messages to assertion macros 01761 class GTEST_API_ AssertHelper { 01762 public: 01763 // Constructor. 01764 AssertHelper(TestPartResult::Type type, 01765 const char* file, 01766 int line, 01767 const char* message); 01768 ~AssertHelper(); 01769 01770 // Message assignment is a semantic trick to enable assertion 01771 // streaming; see the GTEST_MESSAGE_ macro below. 01772 void operator=(const Message& message) const; 01773 01774 private: 01775 // We put our data in a struct so that the size of the AssertHelper class can 01776 // be as small as possible. This is important because gcc is incapable of 01777 // re-using stack space even for temporary variables, so every EXPECT_EQ 01778 // reserves stack space for another AssertHelper. 01779 struct AssertHelperData { 01780 AssertHelperData(TestPartResult::Type t, 01781 const char* srcfile, 01782 int line_num, 01783 const char* msg) 01784 : type(t), file(srcfile), line(line_num), message(msg) { } 01785 01786 TestPartResult::Type const type; 01787 const char* const file; 01788 int const line; 01789 std::string const message; 01790 01791 private: 01792 GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelperData); 01793 }; 01794 01795 AssertHelperData* const data_; 01796 01797 GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelper); 01798 }; 01799 01800 } // namespace internal 01801 01802 #if GTEST_HAS_PARAM_TEST 01803 // The pure interface class that all value-parameterized tests inherit from. 01804 // A value-parameterized class must inherit from both ::testing::Test and 01805 // ::testing::WithParamInterface. In most cases that just means inheriting 01806 // from ::testing::TestWithParam, but more complicated test hierarchies 01807 // may need to inherit from Test and WithParamInterface at different levels. 01808 // 01809 // This interface has support for accessing the test parameter value via 01810 // the GetParam() method. 01811 // 01812 // Use it with one of the parameter generator defining functions, like Range(), 01813 // Values(), ValuesIn(), Bool(), and Combine(). 01814 // 01815 // class FooTest : public ::testing::TestWithParam<int> { 01816 // protected: 01817 // FooTest() { 01818 // // Can use GetParam() here. 01819 // } 01820 // virtual ~FooTest() { 01821 // // Can use GetParam() here. 01822 // } 01823 // virtual void SetUp() { 01824 // // Can use GetParam() here. 01825 // } 01826 // virtual void TearDown { 01827 // // Can use GetParam() here. 01828 // } 01829 // }; 01830 // TEST_P(FooTest, DoesBar) { 01831 // // Can use GetParam() method here. 01832 // Foo foo; 01833 // ASSERT_TRUE(foo.DoesBar(GetParam())); 01834 // } 01835 // INSTANTIATE_TEST_CASE_P(OneToTenRange, FooTest, ::testing::Range(1, 10)); 01836 01837 template <typename T> 01838 class WithParamInterface { 01839 public: 01840 typedef T ParamType; 01841 virtual ~WithParamInterface() {} 01842 01843 // The current parameter value. Is also available in the test fixture's 01844 // constructor. This member function is non-static, even though it only 01845 // references static data, to reduce the opportunity for incorrect uses 01846 // like writing 'WithParamInterface<bool>::GetParam()' for a test that 01847 // uses a fixture whose parameter type is int. 01848 const ParamType& GetParam() const { 01849 GTEST_CHECK_(parameter_ != NULL) 01850 << "GetParam() can only be called inside a value-parameterized test " 01851 << "-- did you intend to write TEST_P instead of TEST_F?"; 01852 return *parameter_; 01853 } 01854 01855 private: 01856 // Sets parameter value. The caller is responsible for making sure the value 01857 // remains alive and unchanged throughout the current test. 01858 static void SetParam(const ParamType* parameter) { 01859 parameter_ = parameter; 01860 } 01861 01862 // Static value used for accessing parameter during a test lifetime. 01863 static const ParamType* parameter_; 01864 01865 // TestClass must be a subclass of WithParamInterface<T> and Test. 01866 template <class TestClass> friend class internal::ParameterizedTestFactory; 01867 }; 01868 01869 template <typename T> 01870 const T* WithParamInterface<T>::parameter_ = NULL; 01871 01872 // Most value-parameterized classes can ignore the existence of 01873 // WithParamInterface, and can just inherit from ::testing::TestWithParam. 01874 01875 template <typename T> 01876 class TestWithParam : public Test, public WithParamInterface<T> { 01877 }; 01878 01879 #endif // GTEST_HAS_PARAM_TEST 01880 01881 // Macros for indicating success/failure in test code. 01882 01883 // ADD_FAILURE unconditionally adds a failure to the current test. 01884 // SUCCEED generates a success - it doesn't automatically make the 01885 // current test successful, as a test is only successful when it has 01886 // no failure. 01887 // 01888 // EXPECT_* verifies that a certain condition is satisfied. If not, 01889 // it behaves like ADD_FAILURE. In particular: 01890 // 01891 // EXPECT_TRUE verifies that a Boolean condition is true. 01892 // EXPECT_FALSE verifies that a Boolean condition is false. 01893 // 01894 // FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except 01895 // that they will also abort the current function on failure. People 01896 // usually want the fail-fast behavior of FAIL and ASSERT_*, but those 01897 // writing data-driven tests often find themselves using ADD_FAILURE 01898 // and EXPECT_* more. 01899 01900 // Generates a nonfatal failure with a generic message. 01901 #define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed") 01902 01903 // Generates a nonfatal failure at the given source file location with 01904 // a generic message. 01905 #define ADD_FAILURE_AT(file, line) \ 01906 GTEST_MESSAGE_AT_(file, line, "Failed", \ 01907 ::testing::TestPartResult::kNonFatalFailure) 01908 01909 // Generates a fatal failure with a generic message. 01910 #define GTEST_FAIL() GTEST_FATAL_FAILURE_("Failed") 01911 01912 // Define this macro to 1 to omit the definition of FAIL(), which is a 01913 // generic name and clashes with some other libraries. 01914 #if !GTEST_DONT_DEFINE_FAIL 01915 # define FAIL() GTEST_FAIL() 01916 #endif 01917 01918 // Generates a success with a generic message. 01919 #define GTEST_SUCCEED() GTEST_SUCCESS_("Succeeded") 01920 01921 // Define this macro to 1 to omit the definition of SUCCEED(), which 01922 // is a generic name and clashes with some other libraries. 01923 #if !GTEST_DONT_DEFINE_SUCCEED 01924 # define SUCCEED() GTEST_SUCCEED() 01925 #endif 01926 01927 // Macros for testing exceptions. 01928 // 01929 // * {ASSERT|EXPECT}_THROW(statement, expected_exception): 01930 // Tests that the statement throws the expected exception. 01931 // * {ASSERT|EXPECT}_NO_THROW(statement): 01932 // Tests that the statement doesn't throw any exception. 01933 // * {ASSERT|EXPECT}_ANY_THROW(statement): 01934 // Tests that the statement throws an exception. 01935 01936 #define EXPECT_THROW(statement, expected_exception) \ 01937 GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_) 01938 #define EXPECT_NO_THROW(statement) \ 01939 GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_) 01940 #define EXPECT_ANY_THROW(statement) \ 01941 GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_) 01942 #define ASSERT_THROW(statement, expected_exception) \ 01943 GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_) 01944 #define ASSERT_NO_THROW(statement) \ 01945 GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_) 01946 #define ASSERT_ANY_THROW(statement) \ 01947 GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_) 01948 01949 // Boolean assertions. Condition can be either a Boolean expression or an 01950 // AssertionResult. For more information on how to use AssertionResult with 01951 // these macros see comments on that class. 01952 #define EXPECT_TRUE(condition) \ 01953 GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \ 01954 GTEST_NONFATAL_FAILURE_) 01955 #define EXPECT_FALSE(condition) \ 01956 GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \ 01957 GTEST_NONFATAL_FAILURE_) 01958 #define ASSERT_TRUE(condition) \ 01959 GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \ 01960 GTEST_FATAL_FAILURE_) 01961 #define ASSERT_FALSE(condition) \ 01962 GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \ 01963 GTEST_FATAL_FAILURE_) 01964 01965 // Includes the auto-generated header that implements a family of 01966 // generic predicate assertion macros. 01967 #include "gtest/gtest_pred_impl.h" 01968 01969 // Macros for testing equalities and inequalities. 01970 // 01971 // * {ASSERT|EXPECT}_EQ(expected, actual): Tests that expected == actual 01972 // * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2 01973 // * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2 01974 // * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2 01975 // * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2 01976 // * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2 01977 // 01978 // When they are not, Google Test prints both the tested expressions and 01979 // their actual values. The values must be compatible built-in types, 01980 // or you will get a compiler error. By "compatible" we mean that the 01981 // values can be compared by the respective operator. 01982 // 01983 // Note: 01984 // 01985 // 1. It is possible to make a user-defined type work with 01986 // {ASSERT|EXPECT}_??(), but that requires overloading the 01987 // comparison operators and is thus discouraged by the Google C++ 01988 // Usage Guide. Therefore, you are advised to use the 01989 // {ASSERT|EXPECT}_TRUE() macro to assert that two objects are 01990 // equal. 01991 // 01992 // 2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on 01993 // pointers (in particular, C strings). Therefore, if you use it 01994 // with two C strings, you are testing how their locations in memory 01995 // are related, not how their content is related. To compare two C 01996 // strings by content, use {ASSERT|EXPECT}_STR*(). 01997 // 01998 // 3. {ASSERT|EXPECT}_EQ(expected, actual) is preferred to 01999 // {ASSERT|EXPECT}_TRUE(expected == actual), as the former tells you 02000 // what the actual value is when it fails, and similarly for the 02001 // other comparisons. 02002 // 02003 // 4. Do not depend on the order in which {ASSERT|EXPECT}_??() 02004 // evaluate their arguments, which is undefined. 02005 // 02006 // 5. These macros evaluate their arguments exactly once. 02007 // 02008 // Examples: 02009 // 02010 // EXPECT_NE(5, Foo()); 02011 // EXPECT_EQ(NULL, a_pointer); 02012 // ASSERT_LT(i, array_size); 02013 // ASSERT_GT(records.size(), 0) << "There is no record left."; 02014 02015 #define EXPECT_EQ(expected, actual) \ 02016 EXPECT_PRED_FORMAT2(::testing::internal:: \ 02017 EqHelper<GTEST_IS_NULL_LITERAL_(expected)>::Compare, \ 02018 expected, actual) 02019 #define EXPECT_NE(expected, actual) \ 02020 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, expected, actual) 02021 #define EXPECT_LE(val1, val2) \ 02022 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) 02023 #define EXPECT_LT(val1, val2) \ 02024 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) 02025 #define EXPECT_GE(val1, val2) \ 02026 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) 02027 #define EXPECT_GT(val1, val2) \ 02028 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) 02029 02030 #define GTEST_ASSERT_EQ(expected, actual) \ 02031 ASSERT_PRED_FORMAT2(::testing::internal:: \ 02032 EqHelper<GTEST_IS_NULL_LITERAL_(expected)>::Compare, \ 02033 expected, actual) 02034 #define GTEST_ASSERT_NE(val1, val2) \ 02035 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2) 02036 #define GTEST_ASSERT_LE(val1, val2) \ 02037 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) 02038 #define GTEST_ASSERT_LT(val1, val2) \ 02039 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) 02040 #define GTEST_ASSERT_GE(val1, val2) \ 02041 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) 02042 #define GTEST_ASSERT_GT(val1, val2) \ 02043 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) 02044 02045 // Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of 02046 // ASSERT_XY(), which clashes with some users' own code. 02047 02048 #if !GTEST_DONT_DEFINE_ASSERT_EQ 02049 # define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2) 02050 #endif 02051 02052 #if !GTEST_DONT_DEFINE_ASSERT_NE 02053 # define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2) 02054 #endif 02055 02056 #if !GTEST_DONT_DEFINE_ASSERT_LE 02057 # define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2) 02058 #endif 02059 02060 #if !GTEST_DONT_DEFINE_ASSERT_LT 02061 # define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2) 02062 #endif 02063 02064 #if !GTEST_DONT_DEFINE_ASSERT_GE 02065 # define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2) 02066 #endif 02067 02068 #if !GTEST_DONT_DEFINE_ASSERT_GT 02069 # define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2) 02070 #endif 02071 02072 // C-string Comparisons. All tests treat NULL and any non-NULL string 02073 // as different. Two NULLs are equal. 02074 // 02075 // * {ASSERT|EXPECT}_STREQ(s1, s2): Tests that s1 == s2 02076 // * {ASSERT|EXPECT}_STRNE(s1, s2): Tests that s1 != s2 02077 // * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case 02078 // * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case 02079 // 02080 // For wide or narrow string objects, you can use the 02081 // {ASSERT|EXPECT}_??() macros. 02082 // 02083 // Don't depend on the order in which the arguments are evaluated, 02084 // which is undefined. 02085 // 02086 // These macros evaluate their arguments exactly once. 02087 02088 #define EXPECT_STREQ(expected, actual) \ 02089 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual) 02090 #define EXPECT_STRNE(s1, s2) \ 02091 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) 02092 #define EXPECT_STRCASEEQ(expected, actual) \ 02093 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual) 02094 #define EXPECT_STRCASENE(s1, s2)\ 02095 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) 02096 02097 #define ASSERT_STREQ(expected, actual) \ 02098 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, expected, actual) 02099 #define ASSERT_STRNE(s1, s2) \ 02100 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) 02101 #define ASSERT_STRCASEEQ(expected, actual) \ 02102 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, expected, actual) 02103 #define ASSERT_STRCASENE(s1, s2)\ 02104 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) 02105 02106 // Macros for comparing floating-point numbers. 02107 // 02108 // * {ASSERT|EXPECT}_FLOAT_EQ(expected, actual): 02109 // Tests that two float values are almost equal. 02110 // * {ASSERT|EXPECT}_DOUBLE_EQ(expected, actual): 02111 // Tests that two double values are almost equal. 02112 // * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error): 02113 // Tests that v1 and v2 are within the given distance to each other. 02114 // 02115 // Google Test uses ULP-based comparison to automatically pick a default 02116 // error bound that is appropriate for the operands. See the 02117 // FloatingPoint template class in gtest-internal.h if you are 02118 // interested in the implementation details. 02119 02120 #define EXPECT_FLOAT_EQ(expected, actual)\ 02121 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \ 02122 expected, actual) 02123 02124 #define EXPECT_DOUBLE_EQ(expected, actual)\ 02125 EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \ 02126 expected, actual) 02127 02128 #define ASSERT_FLOAT_EQ(expected, actual)\ 02129 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \ 02130 expected, actual) 02131 02132 #define ASSERT_DOUBLE_EQ(expected, actual)\ 02133 ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \ 02134 expected, actual) 02135 02136 #define EXPECT_NEAR(val1, val2, abs_error)\ 02137 EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \ 02138 val1, val2, abs_error) 02139 02140 #define ASSERT_NEAR(val1, val2, abs_error)\ 02141 ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \ 02142 val1, val2, abs_error) 02143 02144 // These predicate format functions work on floating-point values, and 02145 // can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g. 02146 // 02147 // EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0); 02148 02149 // Asserts that val1 is less than, or almost equal to, val2. Fails 02150 // otherwise. In particular, it fails if either val1 or val2 is NaN. 02151 GTEST_API_ AssertionResult FloatLE(const char* expr1, const char* expr2, 02152 float val1, float val2); 02153 GTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2, 02154 double val1, double val2); 02155 02156 02157 #if GTEST_OS_WINDOWS 02158 02159 // Macros that test for HRESULT failure and success, these are only useful 02160 // on Windows, and rely on Windows SDK macros and APIs to compile. 02161 // 02162 // * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr) 02163 // 02164 // When expr unexpectedly fails or succeeds, Google Test prints the 02165 // expected result and the actual result with both a human-readable 02166 // string representation of the error, if available, as well as the 02167 // hex result code. 02168 # define EXPECT_HRESULT_SUCCEEDED(expr) \ 02169 EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) 02170 02171 # define ASSERT_HRESULT_SUCCEEDED(expr) \ 02172 ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) 02173 02174 # define EXPECT_HRESULT_FAILED(expr) \ 02175 EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) 02176 02177 # define ASSERT_HRESULT_FAILED(expr) \ 02178 ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) 02179 02180 #endif // GTEST_OS_WINDOWS 02181 02182 // Macros that execute statement and check that it doesn't generate new fatal 02183 // failures in the current thread. 02184 // 02185 // * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement); 02186 // 02187 // Examples: 02188 // 02189 // EXPECT_NO_FATAL_FAILURE(Process()); 02190 // ASSERT_NO_FATAL_FAILURE(Process()) << "Process() failed"; 02191 // 02192 #define ASSERT_NO_FATAL_FAILURE(statement) \ 02193 GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_) 02194 #define EXPECT_NO_FATAL_FAILURE(statement) \ 02195 GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_) 02196 02197 // Causes a trace (including the source file path, the current line 02198 // number, and the given message) to be included in every test failure 02199 // message generated by code in the current scope. The effect is 02200 // undone when the control leaves the current scope. 02201 // 02202 // The message argument can be anything streamable to std::ostream. 02203 // 02204 // In the implementation, we include the current line number as part 02205 // of the dummy variable name, thus allowing multiple SCOPED_TRACE()s 02206 // to appear in the same block - as long as they are on different 02207 // lines. 02208 #define SCOPED_TRACE(message) \ 02209 ::testing::internal::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\ 02210 __FILE__, __LINE__, ::testing::Message() << (message)) 02211 02212 // Compile-time assertion for type equality. 02213 // StaticAssertTypeEq<type1, type2>() compiles iff type1 and type2 are 02214 // the same type. The value it returns is not interesting. 02215 // 02216 // Instead of making StaticAssertTypeEq a class template, we make it a 02217 // function template that invokes a helper class template. This 02218 // prevents a user from misusing StaticAssertTypeEq<T1, T2> by 02219 // defining objects of that type. 02220 // 02221 // CAVEAT: 02222 // 02223 // When used inside a method of a class template, 02224 // StaticAssertTypeEq<T1, T2>() is effective ONLY IF the method is 02225 // instantiated. For example, given: 02226 // 02227 // template <typename T> class Foo { 02228 // public: 02229 // void Bar() { testing::StaticAssertTypeEq<int, T>(); } 02230 // }; 02231 // 02232 // the code: 02233 // 02234 // void Test1() { Foo<bool> foo; } 02235 // 02236 // will NOT generate a compiler error, as Foo<bool>::Bar() is never 02237 // actually instantiated. Instead, you need: 02238 // 02239 // void Test2() { Foo<bool> foo; foo.Bar(); } 02240 // 02241 // to cause a compiler error. 02242 template <typename T1, typename T2> 02243 bool StaticAssertTypeEq() { 02244 (void)internal::StaticAssertTypeEqHelper<T1, T2>(); 02245 return true; 02246 } 02247 02248 // Defines a test. 02249 // 02250 // The first parameter is the name of the test case, and the second 02251 // parameter is the name of the test within the test case. 02252 // 02253 // The convention is to end the test case name with "Test". For 02254 // example, a test case for the Foo class can be named FooTest. 02255 // 02256 // Test code should appear between braces after an invocation of 02257 // this macro. Example: 02258 // 02259 // TEST(FooTest, InitializesCorrectly) { 02260 // Foo foo; 02261 // EXPECT_TRUE(foo.StatusIsOK()); 02262 // } 02263 02264 // Note that we call GetTestTypeId() instead of GetTypeId< 02265 // ::testing::Test>() here to get the type ID of testing::Test. This 02266 // is to work around a suspected linker bug when using Google Test as 02267 // a framework on Mac OS X. The bug causes GetTypeId< 02268 // ::testing::Test>() to return different values depending on whether 02269 // the call is from the Google Test framework itself or from user test 02270 // code. GetTestTypeId() is guaranteed to always return the same 02271 // value, as it always calls GetTypeId<>() from the Google Test 02272 // framework. 02273 #define GTEST_TEST(test_case_name, test_name)\ 02274 GTEST_TEST_(test_case_name, test_name, \ 02275 ::testing::Test, ::testing::internal::GetTestTypeId()) 02276 02277 // Define this macro to 1 to omit the definition of TEST(), which 02278 // is a generic name and clashes with some other libraries. 02279 #if !GTEST_DONT_DEFINE_TEST 02280 # define TEST(test_case_name, test_name) GTEST_TEST(test_case_name, test_name) 02281 #endif 02282 02283 // Defines a test that uses a test fixture. 02284 // 02285 // The first parameter is the name of the test fixture class, which 02286 // also doubles as the test case name. The second parameter is the 02287 // name of the test within the test case. 02288 // 02289 // A test fixture class must be declared earlier. The user should put 02290 // his test code between braces after using this macro. Example: 02291 // 02292 // class FooTest : public testing::Test { 02293 // protected: 02294 // virtual void SetUp() { b_.AddElement(3); } 02295 // 02296 // Foo a_; 02297 // Foo b_; 02298 // }; 02299 // 02300 // TEST_F(FooTest, InitializesCorrectly) { 02301 // EXPECT_TRUE(a_.StatusIsOK()); 02302 // } 02303 // 02304 // TEST_F(FooTest, ReturnsElementCountCorrectly) { 02305 // EXPECT_EQ(0, a_.size()); 02306 // EXPECT_EQ(1, b_.size()); 02307 // } 02308 02309 #define TEST_F(test_fixture, test_name)\ 02310 GTEST_TEST_(test_fixture, test_name, test_fixture, \ 02311 ::testing::internal::GetTypeId<test_fixture>()) 02312 02313 } // namespace testing 02314 02315 // Use this function in main() to run all tests. It returns 0 if all 02316 // tests are successful, or 1 otherwise. 02317 // 02318 // RUN_ALL_TESTS() should be invoked after the command line has been 02319 // parsed by InitGoogleTest(). 02320 // 02321 // This function was formerly a macro; thus, it is in the global 02322 // namespace and has an all-caps name. 02323 int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_; 02324 02325 inline int RUN_ALL_TESTS() { 02326 return ::testing::UnitTest::GetInstance()->Run(); 02327 } 02328 02329 #endif // GTEST_INCLUDE_GTEST_GTEST_H_