| 1 |
// Copyright 2005, Google Inc. |
| 2 |
// All rights reserved. |
| 3 |
// |
| 4 |
// Redistribution and use in source and binary forms, with or without |
| 5 |
// modification, are permitted provided that the following conditions are |
| 6 |
// met: |
| 7 |
// |
| 8 |
// * Redistributions of source code must retain the above copyright |
| 9 |
// notice, this list of conditions and the following disclaimer. |
| 10 |
// * Redistributions in binary form must reproduce the above |
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
| 12 |
// in the documentation and/or other materials provided with the |
| 13 |
// distribution. |
| 14 |
// * Neither the name of Google Inc. nor the names of its |
| 15 |
// contributors may be used to endorse or promote products derived from |
| 16 |
// this software without specific prior written permission. |
| 17 |
// |
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 |
|
| 30 |
// |
| 31 |
// The Google C++ Testing and Mocking Framework (Google Test) |
| 32 |
// |
| 33 |
// This header file defines the public API for Google Test. It should be |
| 34 |
// included by any test program that uses Google Test. |
| 35 |
// |
| 36 |
// IMPORTANT NOTE: Due to limitation of the C++ language, we have to |
| 37 |
// leave some internal implementation details in this header file. |
| 38 |
// They are clearly marked by comments like this: |
| 39 |
// |
| 40 |
// // INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
| 41 |
// |
| 42 |
// Such code is NOT meant to be used by a user directly, and is subject |
| 43 |
// to CHANGE WITHOUT NOTICE. Therefore DO NOT DEPEND ON IT in a user |
| 44 |
// program! |
| 45 |
// |
| 46 |
// Acknowledgment: Google Test borrowed the idea of automatic test |
| 47 |
// registration from Barthelemy Dagenais' ([email protected]) |
| 48 |
// easyUnit framework. |
| 49 |
|
| 50 |
// GOOGLETEST_CM0001 DO NOT DELETE |
| 51 |
|
| 52 |
#ifndef GTEST_INCLUDE_GTEST_GTEST_H_ |
| 53 |
#define GTEST_INCLUDE_GTEST_GTEST_H_ |
| 54 |
|
| 55 |
#include <limits> |
| 56 |
#include <memory> |
| 57 |
#include <ostream> |
| 58 |
#include <vector> |
| 59 |
|
| 60 |
#include "gtest/internal/gtest-internal.h" |
| 61 |
#include "gtest/internal/gtest-string.h" |
| 62 |
#include "gtest/gtest-death-test.h" |
| 63 |
#include "gtest/gtest-matchers.h" |
| 64 |
#include "gtest/gtest-message.h" |
| 65 |
#include "gtest/gtest-param-test.h" |
| 66 |
#include "gtest/gtest-printers.h" |
| 67 |
#include "gtest/gtest_prod.h" |
| 68 |
#include "gtest/gtest-test-part.h" |
| 69 |
#include "gtest/gtest-typed-test.h" |
| 70 |
|
| 71 |
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4251 \ |
| 72 |
/* class A needs to have dll-interface to be used by clients of class B */) |
| 73 |
|
| 74 |
// Depending on the platform, different string classes are available. |
| 75 |
// On Linux, in addition to ::std::string, Google also makes use of |
| 76 |
// class ::string, which has the same interface as ::std::string, but |
| 77 |
// has a different implementation. |
| 78 |
// |
| 79 |
// You can define GTEST_HAS_GLOBAL_STRING to 1 to indicate that |
| 80 |
// ::string is available AND is a distinct type to ::std::string, or |
| 81 |
// define it to 0 to indicate otherwise. |
| 82 |
// |
| 83 |
// If ::std::string and ::string are the same class on your platform |
| 84 |
// due to aliasing, you should define GTEST_HAS_GLOBAL_STRING to 0. |
| 85 |
// |
| 86 |
// If you do not define GTEST_HAS_GLOBAL_STRING, it is defined |
| 87 |
// heuristically. |
| 88 |
|
| 89 |
namespace testing { |
| 90 |
|
| 91 |
// Silence C4100 (unreferenced formal parameter) and 4805 |
| 92 |
// unsafe mix of type 'const int' and type 'const bool' |
| 93 |
#ifdef _MSC_VER |
| 94 |
# pragma warning(push) |
| 95 |
# pragma warning(disable:4805) |
| 96 |
# pragma warning(disable:4100) |
| 97 |
#endif |
| 98 |
|
| 99 |
|
| 100 |
// Declares the flags. |
| 101 |
|
| 102 |
// This flag temporary enables the disabled tests. |
| 103 |
GTEST_DECLARE_bool_(also_run_disabled_tests); |
| 104 |
|
| 105 |
// This flag brings the debugger on an assertion failure. |
| 106 |
GTEST_DECLARE_bool_(break_on_failure); |
| 107 |
|
| 108 |
// This flag controls whether Google Test catches all test-thrown exceptions |
| 109 |
// and logs them as failures. |
| 110 |
GTEST_DECLARE_bool_(catch_exceptions); |
| 111 |
|
| 112 |
// This flag enables using colors in terminal output. Available values are |
| 113 |
// "yes" to enable colors, "no" (disable colors), or "auto" (the default) |
| 114 |
// to let Google Test decide. |
| 115 |
GTEST_DECLARE_string_(color); |
| 116 |
|
| 117 |
// This flag sets up the filter to select by name using a glob pattern |
| 118 |
// the tests to run. If the filter is not given all tests are executed. |
| 119 |
GTEST_DECLARE_string_(filter); |
| 120 |
|
| 121 |
// This flag controls whether Google Test installs a signal handler that dumps |
| 122 |
// debugging information when fatal signals are raised. |
| 123 |
GTEST_DECLARE_bool_(install_failure_signal_handler); |
| 124 |
|
| 125 |
// This flag causes the Google Test to list tests. None of the tests listed |
| 126 |
// are actually run if the flag is provided. |
| 127 |
GTEST_DECLARE_bool_(list_tests); |
| 128 |
|
| 129 |
// This flag controls whether Google Test emits a detailed XML report to a file |
| 130 |
// in addition to its normal textual output. |
| 131 |
GTEST_DECLARE_string_(output); |
| 132 |
|
| 133 |
// This flags control whether Google Test prints the elapsed time for each |
| 134 |
// test. |
| 135 |
GTEST_DECLARE_bool_(print_time); |
| 136 |
|
| 137 |
// This flags control whether Google Test prints UTF8 characters as text. |
| 138 |
GTEST_DECLARE_bool_(print_utf8); |
| 139 |
|
| 140 |
// This flag specifies the random number seed. |
| 141 |
GTEST_DECLARE_int32_(random_seed); |
| 142 |
|
| 143 |
// This flag sets how many times the tests are repeated. The default value |
| 144 |
// is 1. If the value is -1 the tests are repeating forever. |
| 145 |
GTEST_DECLARE_int32_(repeat); |
| 146 |
|
| 147 |
// This flag controls whether Google Test includes Google Test internal |
| 148 |
// stack frames in failure stack traces. |
| 149 |
GTEST_DECLARE_bool_(show_internal_stack_frames); |
| 150 |
|
| 151 |
// When this flag is specified, tests' order is randomized on every iteration. |
| 152 |
GTEST_DECLARE_bool_(shuffle); |
| 153 |
|
| 154 |
// This flag specifies the maximum number of stack frames to be |
| 155 |
// printed in a failure message. |
| 156 |
GTEST_DECLARE_int32_(stack_trace_depth); |
| 157 |
|
| 158 |
// When this flag is specified, a failed assertion will throw an |
| 159 |
// exception if exceptions are enabled, or exit the program with a |
| 160 |
// non-zero code otherwise. For use with an external test framework. |
| 161 |
GTEST_DECLARE_bool_(throw_on_failure); |
| 162 |
|
| 163 |
// When this flag is set with a "host:port" string, on supported |
| 164 |
// platforms test results are streamed to the specified port on |
| 165 |
// the specified host machine. |
| 166 |
GTEST_DECLARE_string_(stream_result_to); |
| 167 |
|
| 168 |
#if GTEST_USE_OWN_FLAGFILE_FLAG_ |
| 169 |
GTEST_DECLARE_string_(flagfile); |
| 170 |
#endif // GTEST_USE_OWN_FLAGFILE_FLAG_ |
| 171 |
|
| 172 |
// The upper limit for valid stack trace depths. |
| 173 |
const int kMaxStackTraceDepth = 100; |
| 174 |
|
| 175 |
namespace internal { |
| 176 |
|
| 177 |
class AssertHelper; |
| 178 |
class DefaultGlobalTestPartResultReporter; |
| 179 |
class ExecDeathTest; |
| 180 |
class NoExecDeathTest; |
| 181 |
class FinalSuccessChecker; |
| 182 |
class GTestFlagSaver; |
| 183 |
class StreamingListenerTest; |
| 184 |
class TestResultAccessor; |
| 185 |
class TestEventListenersAccessor; |
| 186 |
class TestEventRepeater; |
| 187 |
class UnitTestRecordPropertyTestHelper; |
| 188 |
class WindowsDeathTest; |
| 189 |
class FuchsiaDeathTest; |
| 190 |
class UnitTestImpl* GetUnitTestImpl(); |
| 191 |
void ReportFailureInUnknownLocation(TestPartResult::Type result_type, |
| 192 |
const std::string& message); |
| 193 |
|
| 194 |
} // namespace internal |
| 195 |
|
| 196 |
// The friend relationship of some of these classes is cyclic. |
| 197 |
// If we don't forward declare them the compiler might confuse the classes |
| 198 |
// in friendship clauses with same named classes on the scope. |
| 199 |
class Test; |
| 200 |
class TestSuite; |
| 201 |
|
| 202 |
// Old API is still available but deprecated |
| 203 |
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 204 |
using TestCase = TestSuite; |
| 205 |
#endif |
| 206 |
class TestInfo; |
| 207 |
class UnitTest; |
| 208 |
|
| 209 |
// A class for indicating whether an assertion was successful. When |
| 210 |
// the assertion wasn't successful, the AssertionResult object |
| 211 |
// remembers a non-empty message that describes how it failed. |
| 212 |
// |
| 213 |
// To create an instance of this class, use one of the factory functions |
| 214 |
// (AssertionSuccess() and AssertionFailure()). |
| 215 |
// |
| 216 |
// This class is useful for two purposes: |
| 217 |
// 1. Defining predicate functions to be used with Boolean test assertions |
| 218 |
// EXPECT_TRUE/EXPECT_FALSE and their ASSERT_ counterparts |
| 219 |
// 2. Defining predicate-format functions to be |
| 220 |
// used with predicate assertions (ASSERT_PRED_FORMAT*, etc). |
| 221 |
// |
| 222 |
// For example, if you define IsEven predicate: |
| 223 |
// |
| 224 |
// testing::AssertionResult IsEven(int n) { |
| 225 |
// if ((n % 2) == 0) |
| 226 |
// return testing::AssertionSuccess(); |
| 227 |
// else |
| 228 |
// return testing::AssertionFailure() << n << " is odd"; |
| 229 |
// } |
| 230 |
// |
| 231 |
// Then the failed expectation EXPECT_TRUE(IsEven(Fib(5))) |
| 232 |
// will print the message |
| 233 |
// |
| 234 |
// Value of: IsEven(Fib(5)) |
| 235 |
// Actual: false (5 is odd) |
| 236 |
// Expected: true |
| 237 |
// |
| 238 |
// instead of a more opaque |
| 239 |
// |
| 240 |
// Value of: IsEven(Fib(5)) |
| 241 |
// Actual: false |
| 242 |
// Expected: true |
| 243 |
// |
| 244 |
// in case IsEven is a simple Boolean predicate. |
| 245 |
// |
| 246 |
// If you expect your predicate to be reused and want to support informative |
| 247 |
// messages in EXPECT_FALSE and ASSERT_FALSE (negative assertions show up |
| 248 |
// about half as often as positive ones in our tests), supply messages for |
| 249 |
// both success and failure cases: |
| 250 |
// |
| 251 |
// testing::AssertionResult IsEven(int n) { |
| 252 |
// if ((n % 2) == 0) |
| 253 |
// return testing::AssertionSuccess() << n << " is even"; |
| 254 |
// else |
| 255 |
// return testing::AssertionFailure() << n << " is odd"; |
| 256 |
// } |
| 257 |
// |
| 258 |
// Then a statement EXPECT_FALSE(IsEven(Fib(6))) will print |
| 259 |
// |
| 260 |
// Value of: IsEven(Fib(6)) |
| 261 |
// Actual: true (8 is even) |
| 262 |
// Expected: false |
| 263 |
// |
| 264 |
// NB: Predicates that support negative Boolean assertions have reduced |
| 265 |
// performance in positive ones so be careful not to use them in tests |
| 266 |
// that have lots (tens of thousands) of positive Boolean assertions. |
| 267 |
// |
| 268 |
// To use this class with EXPECT_PRED_FORMAT assertions such as: |
| 269 |
// |
| 270 |
// // Verifies that Foo() returns an even number. |
| 271 |
// EXPECT_PRED_FORMAT1(IsEven, Foo()); |
| 272 |
// |
| 273 |
// you need to define: |
| 274 |
// |
| 275 |
// testing::AssertionResult IsEven(const char* expr, int n) { |
| 276 |
// if ((n % 2) == 0) |
| 277 |
// return testing::AssertionSuccess(); |
| 278 |
// else |
| 279 |
// return testing::AssertionFailure() |
| 280 |
// << "Expected: " << expr << " is even\n Actual: it's " << n; |
| 281 |
// } |
| 282 |
// |
| 283 |
// If Foo() returns 5, you will see the following message: |
| 284 |
// |
| 285 |
// Expected: Foo() is even |
| 286 |
// Actual: it's 5 |
| 287 |
// |
| 288 |
class GTEST_API_ AssertionResult { |
| 289 |
public: |
| 290 |
// Copy constructor. |
| 291 |
// Used in EXPECT_TRUE/FALSE(assertion_result). |
| 292 |
AssertionResult(const AssertionResult& other); |
| 293 |
|
| 294 |
#if defined(_MSC_VER) && _MSC_VER < 1910 |
| 295 |
GTEST_DISABLE_MSC_WARNINGS_PUSH_(4800 /* forcing value to bool */) |
| 296 |
#endif |
| 297 |
|
| 298 |
// Used in the EXPECT_TRUE/FALSE(bool_expression). |
| 299 |
// |
| 300 |
// T must be contextually convertible to bool. |
| 301 |
// |
| 302 |
// The second parameter prevents this overload from being considered if |
| 303 |
// the argument is implicitly convertible to AssertionResult. In that case |
| 304 |
// we want AssertionResult's copy constructor to be used. |
| 305 |
template <typename T> |
| 306 |
explicit AssertionResult( |
| 307 |
const T& success, |
| 308 |
typename internal::EnableIf< |
| 309 |
!std::is_convertible<T, AssertionResult>::value>::type* |
| 310 |
/*enabler*/ |
| 311 |
= nullptr) |
| 312 |
: success_(success) {} |
| 313 |
|
| 314 |
#if defined(_MSC_VER) && _MSC_VER < 1910 |
| 315 |
GTEST_DISABLE_MSC_WARNINGS_POP_() |
| 316 |
#endif |
| 317 |
|
| 318 |
// Assignment operator. |
| 319 |
AssertionResult& operator=(AssertionResult other) { |
| 320 |
swap(other); |
| 321 |
return *this; |
| 322 |
} |
| 323 |
|
| 324 |
// Returns true iff the assertion succeeded. |
| 325 |
operator bool() const { return success_; } // NOLINT |
| 326 |
|
| 327 |
// Returns the assertion's negation. Used with EXPECT/ASSERT_FALSE. |
| 328 |
AssertionResult operator!() const; |
| 329 |
|
| 330 |
// Returns the text streamed into this AssertionResult. Test assertions |
| 331 |
// use it when they fail (i.e., the predicate's outcome doesn't match the |
| 332 |
// assertion's expectation). When nothing has been streamed into the |
| 333 |
// object, returns an empty string. |
| 334 |
const char* message() const { |
| 335 |
return message_.get() != nullptr ? message_->c_str() : ""; |
| 336 |
} |
| 337 |
// Deprecated; please use message() instead. |
| 338 |
const char* failure_message() const { return message(); } |
| 339 |
|
| 340 |
// Streams a custom failure message into this object. |
| 341 |
template <typename T> AssertionResult& operator<<(const T& value) { |
| 342 |
AppendMessage(Message() << value); |
| 343 |
return *this; |
| 344 |
} |
| 345 |
|
| 346 |
// Allows streaming basic output manipulators such as endl or flush into |
| 347 |
// this object. |
| 348 |
AssertionResult& operator<<( |
| 349 |
::std::ostream& (*basic_manipulator)(::std::ostream& stream)) { |
| 350 |
AppendMessage(Message() << basic_manipulator); |
| 351 |
return *this; |
| 352 |
} |
| 353 |
|
| 354 |
private: |
| 355 |
// Appends the contents of message to message_. |
| 356 |
void AppendMessage(const Message& a_message) { |
| 357 |
if (message_.get() == nullptr) message_.reset(new ::std::string); |
| 358 |
message_->append(a_message.GetString().c_str()); |
| 359 |
} |
| 360 |
|
| 361 |
// Swap the contents of this AssertionResult with other. |
| 362 |
void swap(AssertionResult& other); |
| 363 |
|
| 364 |
// Stores result of the assertion predicate. |
| 365 |
bool success_; |
| 366 |
// Stores the message describing the condition in case the expectation |
| 367 |
// construct is not satisfied with the predicate's outcome. |
| 368 |
// Referenced via a pointer to avoid taking too much stack frame space |
| 369 |
// with test assertions. |
| 370 |
std::unique_ptr< ::std::string> message_; |
| 371 |
}; |
| 372 |
|
| 373 |
// Makes a successful assertion result. |
| 374 |
GTEST_API_ AssertionResult AssertionSuccess(); |
| 375 |
|
| 376 |
// Makes a failed assertion result. |
| 377 |
GTEST_API_ AssertionResult AssertionFailure(); |
| 378 |
|
| 379 |
// Makes a failed assertion result with the given failure message. |
| 380 |
// Deprecated; use AssertionFailure() << msg. |
| 381 |
GTEST_API_ AssertionResult AssertionFailure(const Message& msg); |
| 382 |
|
| 383 |
} // namespace testing |
| 384 |
|
| 385 |
// Includes the auto-generated header that implements a family of generic |
| 386 |
// predicate assertion macros. This include comes late because it relies on |
| 387 |
// APIs declared above. |
| 388 |
#include "gtest/gtest_pred_impl.h" |
| 389 |
|
| 390 |
namespace testing { |
| 391 |
|
| 392 |
// The abstract class that all tests inherit from. |
| 393 |
// |
| 394 |
// In Google Test, a unit test program contains one or many TestSuites, and |
| 395 |
// each TestSuite contains one or many Tests. |
| 396 |
// |
| 397 |
// When you define a test using the TEST macro, you don't need to |
| 398 |
// explicitly derive from Test - the TEST macro automatically does |
| 399 |
// this for you. |
| 400 |
// |
| 401 |
// The only time you derive from Test is when defining a test fixture |
| 402 |
// to be used in a TEST_F. For example: |
| 403 |
// |
| 404 |
// class FooTest : public testing::Test { |
| 405 |
// protected: |
| 406 |
// void SetUp() override { ... } |
| 407 |
// void TearDown() override { ... } |
| 408 |
// ... |
| 409 |
// }; |
| 410 |
// |
| 411 |
// TEST_F(FooTest, Bar) { ... } |
| 412 |
// TEST_F(FooTest, Baz) { ... } |
| 413 |
// |
| 414 |
// Test is not copyable. |
| 415 |
class GTEST_API_ Test { |
| 416 |
public: |
| 417 |
friend class TestInfo; |
| 418 |
|
| 419 |
// The d'tor is virtual as we intend to inherit from Test. |
| 420 |
virtual ~Test(); |
| 421 |
|
| 422 |
// Sets up the stuff shared by all tests in this test case. |
| 423 |
// |
| 424 |
// Google Test will call Foo::SetUpTestSuite() before running the first |
| 425 |
// test in test case Foo. Hence a sub-class can define its own |
| 426 |
// SetUpTestSuite() method to shadow the one defined in the super |
| 427 |
// class. |
| 428 |
static void SetUpTestSuite() {} |
| 429 |
|
| 430 |
// Tears down the stuff shared by all tests in this test case. |
| 431 |
// |
| 432 |
// Google Test will call Foo::TearDownTestSuite() after running the last |
| 433 |
// test in test case Foo. Hence a sub-class can define its own |
| 434 |
// TearDownTestSuite() method to shadow the one defined in the super |
| 435 |
// class. |
| 436 |
static void TearDownTestSuite() {} |
| 437 |
|
| 438 |
// Legacy API is deprecated but still available |
| 439 |
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 440 |
static void TearDownTestCase() {} |
| 441 |
static void SetUpTestCase() {} |
| 442 |
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 443 |
|
| 444 |
// Returns true iff the current test has a fatal failure. |
| 445 |
static bool HasFatalFailure(); |
| 446 |
|
| 447 |
// Returns true iff the current test has a non-fatal failure. |
| 448 |
static bool HasNonfatalFailure(); |
| 449 |
|
| 450 |
// Returns true iff the current test was skipped. |
| 451 |
static bool IsSkipped(); |
| 452 |
|
| 453 |
// Returns true iff the current test has a (either fatal or |
| 454 |
// non-fatal) failure. |
| 455 |
static bool HasFailure() { return HasFatalFailure() || HasNonfatalFailure(); } |
| 456 |
|
| 457 |
// Logs a property for the current test, test suite, or for the entire |
| 458 |
// invocation of the test program when used outside of the context of a |
| 459 |
// test suite. Only the last value for a given key is remembered. These |
| 460 |
// are public static so they can be called from utility functions that are |
| 461 |
// not members of the test fixture. Calls to RecordProperty made during |
| 462 |
// lifespan of the test (from the moment its constructor starts to the |
| 463 |
// moment its destructor finishes) will be output in XML as attributes of |
| 464 |
// the <testcase> element. Properties recorded from fixture's |
| 465 |
// SetUpTestSuite or TearDownTestSuite are logged as attributes of the |
| 466 |
// corresponding <testsuite> element. Calls to RecordProperty made in the |
| 467 |
// global context (before or after invocation of RUN_ALL_TESTS and from |
| 468 |
// SetUp/TearDown method of Environment objects registered with Google |
| 469 |
// Test) will be output as attributes of the <testsuites> element. |
| 470 |
static void RecordProperty(const std::string& key, const std::string& value); |
| 471 |
static void RecordProperty(const std::string& key, int value); |
| 472 |
|
| 473 |
protected: |
| 474 |
// Creates a Test object. |
| 475 |
Test(); |
| 476 |
|
| 477 |
// Sets up the test fixture. |
| 478 |
virtual void SetUp(); |
| 479 |
|
| 480 |
// Tears down the test fixture. |
| 481 |
virtual void TearDown(); |
| 482 |
|
| 483 |
private: |
| 484 |
// Returns true iff the current test has the same fixture class as |
| 485 |
// the first test in the current test suite. |
| 486 |
static bool HasSameFixtureClass(); |
| 487 |
|
| 488 |
// Runs the test after the test fixture has been set up. |
| 489 |
// |
| 490 |
// A sub-class must implement this to define the test logic. |
| 491 |
// |
| 492 |
// DO NOT OVERRIDE THIS FUNCTION DIRECTLY IN A USER PROGRAM. |
| 493 |
// Instead, use the TEST or TEST_F macro. |
| 494 |
virtual void TestBody() = 0; |
| 495 |
|
| 496 |
// Sets up, executes, and tears down the test. |
| 497 |
void Run(); |
| 498 |
|
| 499 |
// Deletes self. We deliberately pick an unusual name for this |
| 500 |
// internal method to avoid clashing with names used in user TESTs. |
| 501 |
void DeleteSelf_() { delete this; } |
| 502 |
|
| 503 |
const std::unique_ptr<GTEST_FLAG_SAVER_> gtest_flag_saver_; |
| 504 |
|
| 505 |
// Often a user misspells SetUp() as Setup() and spends a long time |
| 506 |
// wondering why it is never called by Google Test. The declaration of |
| 507 |
// the following method is solely for catching such an error at |
| 508 |
// compile time: |
| 509 |
// |
| 510 |
// - The return type is deliberately chosen to be not void, so it |
| 511 |
// will be a conflict if void Setup() is declared in the user's |
| 512 |
// test fixture. |
| 513 |
// |
| 514 |
// - This method is private, so it will be another compiler error |
| 515 |
// if the method is called from the user's test fixture. |
| 516 |
// |
| 517 |
// DO NOT OVERRIDE THIS FUNCTION. |
| 518 |
// |
| 519 |
// If you see an error about overriding the following function or |
| 520 |
// about it being private, you have mis-spelled SetUp() as Setup(). |
| 521 |
struct Setup_should_be_spelled_SetUp {}; |
| 522 |
virtual Setup_should_be_spelled_SetUp* Setup() { return nullptr; } |
| 523 |
|
| 524 |
// We disallow copying Tests. |
| 525 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(Test); |
| 526 |
}; |
| 527 |
|
| 528 |
typedef internal::TimeInMillis TimeInMillis; |
| 529 |
|
| 530 |
// A copyable object representing a user specified test property which can be |
| 531 |
// output as a key/value string pair. |
| 532 |
// |
| 533 |
// Don't inherit from TestProperty as its destructor is not virtual. |
| 534 |
class TestProperty { |
| 535 |
public: |
| 536 |
// C'tor. TestProperty does NOT have a default constructor. |
| 537 |
// Always use this constructor (with parameters) to create a |
| 538 |
// TestProperty object. |
| 539 |
TestProperty(const std::string& a_key, const std::string& a_value) : |
| 540 |
key_(a_key), value_(a_value) { |
| 541 |
} |
| 542 |
|
| 543 |
// Gets the user supplied key. |
| 544 |
const char* key() const { |
| 545 |
return key_.c_str(); |
| 546 |
} |
| 547 |
|
| 548 |
// Gets the user supplied value. |
| 549 |
const char* value() const { |
| 550 |
return value_.c_str(); |
| 551 |
} |
| 552 |
|
| 553 |
// Sets a new value, overriding the one supplied in the constructor. |
| 554 |
void SetValue(const std::string& new_value) { |
| 555 |
value_ = new_value; |
| 556 |
} |
| 557 |
|
| 558 |
private: |
| 559 |
// The key supplied by the user. |
| 560 |
std::string key_; |
| 561 |
// The value supplied by the user. |
| 562 |
std::string value_; |
| 563 |
}; |
| 564 |
|
| 565 |
// The result of a single Test. This includes a list of |
| 566 |
// TestPartResults, a list of TestProperties, a count of how many |
| 567 |
// death tests there are in the Test, and how much time it took to run |
| 568 |
// the Test. |
| 569 |
// |
| 570 |
// TestResult is not copyable. |
| 571 |
class GTEST_API_ TestResult { |
| 572 |
public: |
| 573 |
// Creates an empty TestResult. |
| 574 |
TestResult(); |
| 575 |
|
| 576 |
// D'tor. Do not inherit from TestResult. |
| 577 |
~TestResult(); |
| 578 |
|
| 579 |
// Gets the number of all test parts. This is the sum of the number |
| 580 |
// of successful test parts and the number of failed test parts. |
| 581 |
int total_part_count() const; |
| 582 |
|
| 583 |
// Returns the number of the test properties. |
| 584 |
int test_property_count() const; |
| 585 |
|
| 586 |
// Returns true iff the test passed (i.e. no test part failed). |
| 587 |
bool Passed() const { return !Skipped() && !Failed(); } |
| 588 |
|
| 589 |
// Returns true iff the test was skipped. |
| 590 |
bool Skipped() const; |
| 591 |
|
| 592 |
// Returns true iff the test failed. |
| 593 |
bool Failed() const; |
| 594 |
|
| 595 |
// Returns true iff the test fatally failed. |
| 596 |
bool HasFatalFailure() const; |
| 597 |
|
| 598 |
// Returns true iff the test has a non-fatal failure. |
| 599 |
bool HasNonfatalFailure() const; |
| 600 |
|
| 601 |
// Returns the elapsed time, in milliseconds. |
| 602 |
TimeInMillis elapsed_time() const { return elapsed_time_; } |
| 603 |
|
| 604 |
// Returns the i-th test part result among all the results. i can range from 0 |
| 605 |
// to total_part_count() - 1. If i is not in that range, aborts the program. |
| 606 |
const TestPartResult& GetTestPartResult(int i) const; |
| 607 |
|
| 608 |
// Returns the i-th test property. i can range from 0 to |
| 609 |
// test_property_count() - 1. If i is not in that range, aborts the |
| 610 |
// program. |
| 611 |
const TestProperty& GetTestProperty(int i) const; |
| 612 |
|
| 613 |
private: |
| 614 |
friend class TestInfo; |
| 615 |
friend class TestSuite; |
| 616 |
friend class UnitTest; |
| 617 |
friend class internal::DefaultGlobalTestPartResultReporter; |
| 618 |
friend class internal::ExecDeathTest; |
| 619 |
friend class internal::TestResultAccessor; |
| 620 |
friend class internal::UnitTestImpl; |
| 621 |
friend class internal::WindowsDeathTest; |
| 622 |
friend class internal::FuchsiaDeathTest; |
| 623 |
|
| 624 |
// Gets the vector of TestPartResults. |
| 625 |
const std::vector<TestPartResult>& test_part_results() const { |
| 626 |
return test_part_results_; |
| 627 |
} |
| 628 |
|
| 629 |
// Gets the vector of TestProperties. |
| 630 |
const std::vector<TestProperty>& test_properties() const { |
| 631 |
return test_properties_; |
| 632 |
} |
| 633 |
|
| 634 |
// Sets the elapsed time. |
| 635 |
void set_elapsed_time(TimeInMillis elapsed) { elapsed_time_ = elapsed; } |
| 636 |
|
| 637 |
// Adds a test property to the list. The property is validated and may add |
| 638 |
// a non-fatal failure if invalid (e.g., if it conflicts with reserved |
| 639 |
// key names). If a property is already recorded for the same key, the |
| 640 |
// value will be updated, rather than storing multiple values for the same |
| 641 |
// key. xml_element specifies the element for which the property is being |
| 642 |
// recorded and is used for validation. |
| 643 |
void RecordProperty(const std::string& xml_element, |
| 644 |
const TestProperty& test_property); |
| 645 |
|
| 646 |
// Adds a failure if the key is a reserved attribute of Google Test |
| 647 |
// testsuite tags. Returns true if the property is valid. |
| 648 |
// FIXME: Validate attribute names are legal and human readable. |
| 649 |
static bool ValidateTestProperty(const std::string& xml_element, |
| 650 |
const TestProperty& test_property); |
| 651 |
|
| 652 |
// Adds a test part result to the list. |
| 653 |
void AddTestPartResult(const TestPartResult& test_part_result); |
| 654 |
|
| 655 |
// Returns the death test count. |
| 656 |
int death_test_count() const { return death_test_count_; } |
| 657 |
|
| 658 |
// Increments the death test count, returning the new count. |
| 659 |
int increment_death_test_count() { return ++death_test_count_; } |
| 660 |
|
| 661 |
// Clears the test part results. |
| 662 |
void ClearTestPartResults(); |
| 663 |
|
| 664 |
// Clears the object. |
| 665 |
void Clear(); |
| 666 |
|
| 667 |
// Protects mutable state of the property vector and of owned |
| 668 |
// properties, whose values may be updated. |
| 669 |
internal::Mutex test_properites_mutex_; |
| 670 |
|
| 671 |
// The vector of TestPartResults |
| 672 |
std::vector<TestPartResult> test_part_results_; |
| 673 |
// The vector of TestProperties |
| 674 |
std::vector<TestProperty> test_properties_; |
| 675 |
// Running count of death tests. |
| 676 |
int death_test_count_; |
| 677 |
// The elapsed time, in milliseconds. |
| 678 |
TimeInMillis elapsed_time_; |
| 679 |
|
| 680 |
// We disallow copying TestResult. |
| 681 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestResult); |
| 682 |
}; // class TestResult |
| 683 |
|
| 684 |
// A TestInfo object stores the following information about a test: |
| 685 |
// |
| 686 |
// Test suite name |
| 687 |
// Test name |
| 688 |
// Whether the test should be run |
| 689 |
// A function pointer that creates the test object when invoked |
| 690 |
// Test result |
| 691 |
// |
| 692 |
// The constructor of TestInfo registers itself with the UnitTest |
| 693 |
// singleton such that the RUN_ALL_TESTS() macro knows which tests to |
| 694 |
// run. |
| 695 |
class GTEST_API_ TestInfo { |
| 696 |
public: |
| 697 |
// Destructs a TestInfo object. This function is not virtual, so |
| 698 |
// don't inherit from TestInfo. |
| 699 |
~TestInfo(); |
| 700 |
|
| 701 |
// Returns the test suite name. |
| 702 |
const char* test_suite_name() const { return test_suite_name_.c_str(); } |
| 703 |
|
| 704 |
// Legacy API is deprecated but still available |
| 705 |
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 706 |
const char* test_case_name() const { return test_suite_name(); } |
| 707 |
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 708 |
|
| 709 |
// Returns the test name. |
| 710 |
const char* name() const { return name_.c_str(); } |
| 711 |
|
| 712 |
// Returns the name of the parameter type, or NULL if this is not a typed |
| 713 |
// or a type-parameterized test. |
| 714 |
const char* type_param() const { |
| 715 |
if (type_param_.get() != nullptr) return type_param_->c_str(); |
| 716 |
return nullptr; |
| 717 |
} |
| 718 |
|
| 719 |
// Returns the text representation of the value parameter, or NULL if this |
| 720 |
// is not a value-parameterized test. |
| 721 |
const char* value_param() const { |
| 722 |
if (value_param_.get() != nullptr) return value_param_->c_str(); |
| 723 |
return nullptr; |
| 724 |
} |
| 725 |
|
| 726 |
// Returns the file name where this test is defined. |
| 727 |
const char* file() const { return location_.file.c_str(); } |
| 728 |
|
| 729 |
// Returns the line where this test is defined. |
| 730 |
int line() const { return location_.line; } |
| 731 |
|
| 732 |
// Return true if this test should not be run because it's in another shard. |
| 733 |
bool is_in_another_shard() const { return is_in_another_shard_; } |
| 734 |
|
| 735 |
// Returns true if this test should run, that is if the test is not |
| 736 |
// disabled (or it is disabled but the also_run_disabled_tests flag has |
| 737 |
// been specified) and its full name matches the user-specified filter. |
| 738 |
// |
| 739 |
// Google Test allows the user to filter the tests by their full names. |
| 740 |
// The full name of a test Bar in test suite Foo is defined as |
| 741 |
// "Foo.Bar". Only the tests that match the filter will run. |
| 742 |
// |
| 743 |
// A filter is a colon-separated list of glob (not regex) patterns, |
| 744 |
// optionally followed by a '-' and a colon-separated list of |
| 745 |
// negative patterns (tests to exclude). A test is run if it |
| 746 |
// matches one of the positive patterns and does not match any of |
| 747 |
// the negative patterns. |
| 748 |
// |
| 749 |
// For example, *A*:Foo.* is a filter that matches any string that |
| 750 |
// contains the character 'A' or starts with "Foo.". |
| 751 |
bool should_run() const { return should_run_; } |
| 752 |
|
| 753 |
// Returns true iff this test will appear in the XML report. |
| 754 |
bool is_reportable() const { |
| 755 |
// The XML report includes tests matching the filter, excluding those |
| 756 |
// run in other shards. |
| 757 |
return matches_filter_ && !is_in_another_shard_; |
| 758 |
} |
| 759 |
|
| 760 |
// Returns the result of the test. |
| 761 |
const TestResult* result() const { return &result_; } |
| 762 |
|
| 763 |
private: |
| 764 |
#if GTEST_HAS_DEATH_TEST |
| 765 |
friend class internal::DefaultDeathTestFactory; |
| 766 |
#endif // GTEST_HAS_DEATH_TEST |
| 767 |
friend class Test; |
| 768 |
friend class TestSuite; |
| 769 |
friend class internal::UnitTestImpl; |
| 770 |
friend class internal::StreamingListenerTest; |
| 771 |
friend TestInfo* internal::MakeAndRegisterTestInfo( |
| 772 |
const char* test_suite_name, const char* name, const char* type_param, |
| 773 |
const char* value_param, internal::CodeLocation code_location, |
| 774 |
internal::TypeId fixture_class_id, internal::SetUpTestSuiteFunc set_up_tc, |
| 775 |
internal::TearDownTestSuiteFunc tear_down_tc, |
| 776 |
internal::TestFactoryBase* factory); |
| 777 |
|
| 778 |
// Constructs a TestInfo object. The newly constructed instance assumes |
| 779 |
// ownership of the factory object. |
| 780 |
TestInfo(const std::string& test_suite_name, const std::string& name, |
| 781 |
const char* a_type_param, // NULL if not a type-parameterized test |
| 782 |
const char* a_value_param, // NULL if not a value-parameterized test |
| 783 |
internal::CodeLocation a_code_location, |
| 784 |
internal::TypeId fixture_class_id, |
| 785 |
internal::TestFactoryBase* factory); |
| 786 |
|
| 787 |
// Increments the number of death tests encountered in this test so |
| 788 |
// far. |
| 789 |
int increment_death_test_count() { |
| 790 |
return result_.increment_death_test_count(); |
| 791 |
} |
| 792 |
|
| 793 |
// Creates the test object, runs it, records its result, and then |
| 794 |
// deletes it. |
| 795 |
void Run(); |
| 796 |
|
| 797 |
static void ClearTestResult(TestInfo* test_info) { |
| 798 |
test_info->result_.Clear(); |
| 799 |
} |
| 800 |
|
| 801 |
// These fields are immutable properties of the test. |
| 802 |
const std::string test_suite_name_; // test suite name |
| 803 |
const std::string name_; // Test name |
| 804 |
// Name of the parameter type, or NULL if this is not a typed or a |
| 805 |
// type-parameterized test. |
| 806 |
const std::unique_ptr<const ::std::string> type_param_; |
| 807 |
// Text representation of the value parameter, or NULL if this is not a |
| 808 |
// value-parameterized test. |
| 809 |
const std::unique_ptr<const ::std::string> value_param_; |
| 810 |
internal::CodeLocation location_; |
| 811 |
const internal::TypeId fixture_class_id_; // ID of the test fixture class |
| 812 |
bool should_run_; // True iff this test should run |
| 813 |
bool is_disabled_; // True iff this test is disabled |
| 814 |
bool matches_filter_; // True if this test matches the |
| 815 |
// user-specified filter. |
| 816 |
bool is_in_another_shard_; // Will be run in another shard. |
| 817 |
internal::TestFactoryBase* const factory_; // The factory that creates |
| 818 |
// the test object |
| 819 |
|
| 820 |
// This field is mutable and needs to be reset before running the |
| 821 |
// test for the second time. |
| 822 |
TestResult result_; |
| 823 |
|
| 824 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestInfo); |
| 825 |
}; |
| 826 |
|
| 827 |
// A test suite, which consists of a vector of TestInfos. |
| 828 |
// |
| 829 |
// TestSuite is not copyable. |
| 830 |
class GTEST_API_ TestSuite { |
| 831 |
public: |
| 832 |
// Creates a TestSuite with the given name. |
| 833 |
// |
| 834 |
// TestSuite does NOT have a default constructor. Always use this |
| 835 |
// constructor to create a TestSuite object. |
| 836 |
// |
| 837 |
// Arguments: |
| 838 |
// |
| 839 |
// name: name of the test suite |
| 840 |
// a_type_param: the name of the test's type parameter, or NULL if |
| 841 |
// this is not a type-parameterized test. |
| 842 |
// set_up_tc: pointer to the function that sets up the test suite |
| 843 |
// tear_down_tc: pointer to the function that tears down the test suite |
| 844 |
TestSuite(const char* name, const char* a_type_param, |
| 845 |
internal::SetUpTestSuiteFunc set_up_tc, |
| 846 |
internal::TearDownTestSuiteFunc tear_down_tc); |
| 847 |
|
| 848 |
// Destructor of TestSuite. |
| 849 |
virtual ~TestSuite(); |
| 850 |
|
| 851 |
// Gets the name of the TestSuite. |
| 852 |
const char* name() const { return name_.c_str(); } |
| 853 |
|
| 854 |
// Returns the name of the parameter type, or NULL if this is not a |
| 855 |
// type-parameterized test suite. |
| 856 |
const char* type_param() const { |
| 857 |
if (type_param_.get() != nullptr) return type_param_->c_str(); |
| 858 |
return nullptr; |
| 859 |
} |
| 860 |
|
| 861 |
// Returns true if any test in this test suite should run. |
| 862 |
bool should_run() const { return should_run_; } |
| 863 |
|
| 864 |
// Gets the number of successful tests in this test suite. |
| 865 |
int successful_test_count() const; |
| 866 |
|
| 867 |
// Gets the number of skipped tests in this test suite. |
| 868 |
int skipped_test_count() const; |
| 869 |
|
| 870 |
// Gets the number of failed tests in this test suite. |
| 871 |
int failed_test_count() const; |
| 872 |
|
| 873 |
// Gets the number of disabled tests that will be reported in the XML report. |
| 874 |
int reportable_disabled_test_count() const; |
| 875 |
|
| 876 |
// Gets the number of disabled tests in this test suite. |
| 877 |
int disabled_test_count() const; |
| 878 |
|
| 879 |
// Gets the number of tests to be printed in the XML report. |
| 880 |
int reportable_test_count() const; |
| 881 |
|
| 882 |
// Get the number of tests in this test suite that should run. |
| 883 |
int test_to_run_count() const; |
| 884 |
|
| 885 |
// Gets the number of all tests in this test suite. |
| 886 |
int total_test_count() const; |
| 887 |
|
| 888 |
// Returns true iff the test suite passed. |
| 889 |
bool Passed() const { return !Failed(); } |
| 890 |
|
| 891 |
// Returns true iff the test suite failed. |
| 892 |
bool Failed() const { return failed_test_count() > 0; } |
| 893 |
|
| 894 |
// Returns the elapsed time, in milliseconds. |
| 895 |
TimeInMillis elapsed_time() const { return elapsed_time_; } |
| 896 |
|
| 897 |
// Returns the i-th test among all the tests. i can range from 0 to |
| 898 |
// total_test_count() - 1. If i is not in that range, returns NULL. |
| 899 |
const TestInfo* GetTestInfo(int i) const; |
| 900 |
|
| 901 |
// Returns the TestResult that holds test properties recorded during |
| 902 |
// execution of SetUpTestSuite and TearDownTestSuite. |
| 903 |
const TestResult& ad_hoc_test_result() const { return ad_hoc_test_result_; } |
| 904 |
|
| 905 |
private: |
| 906 |
friend class Test; |
| 907 |
friend class internal::UnitTestImpl; |
| 908 |
|
| 909 |
// Gets the (mutable) vector of TestInfos in this TestSuite. |
| 910 |
std::vector<TestInfo*>& test_info_list() { return test_info_list_; } |
| 911 |
|
| 912 |
// Gets the (immutable) vector of TestInfos in this TestSuite. |
| 913 |
const std::vector<TestInfo*>& test_info_list() const { |
| 914 |
return test_info_list_; |
| 915 |
} |
| 916 |
|
| 917 |
// Returns the i-th test among all the tests. i can range from 0 to |
| 918 |
// total_test_count() - 1. If i is not in that range, returns NULL. |
| 919 |
TestInfo* GetMutableTestInfo(int i); |
| 920 |
|
| 921 |
// Sets the should_run member. |
| 922 |
void set_should_run(bool should) { should_run_ = should; } |
| 923 |
|
| 924 |
// Adds a TestInfo to this test suite. Will delete the TestInfo upon |
| 925 |
// destruction of the TestSuite object. |
| 926 |
void AddTestInfo(TestInfo * test_info); |
| 927 |
|
| 928 |
// Clears the results of all tests in this test suite. |
| 929 |
void ClearResult(); |
| 930 |
|
| 931 |
// Clears the results of all tests in the given test suite. |
| 932 |
static void ClearTestSuiteResult(TestSuite* test_suite) { |
| 933 |
test_suite->ClearResult(); |
| 934 |
} |
| 935 |
|
| 936 |
// Runs every test in this TestSuite. |
| 937 |
void Run(); |
| 938 |
|
| 939 |
// Runs SetUpTestSuite() for this TestSuite. This wrapper is needed |
| 940 |
// for catching exceptions thrown from SetUpTestSuite(). |
| 941 |
void RunSetUpTestSuite() { |
| 942 |
if (set_up_tc_ != nullptr) { |
| 943 |
(*set_up_tc_)(); |
| 944 |
} |
| 945 |
} |
| 946 |
|
| 947 |
// Runs TearDownTestSuite() for this TestSuite. This wrapper is |
| 948 |
// needed for catching exceptions thrown from TearDownTestSuite(). |
| 949 |
void RunTearDownTestSuite() { |
| 950 |
if (tear_down_tc_ != nullptr) { |
| 951 |
(*tear_down_tc_)(); |
| 952 |
} |
| 953 |
} |
| 954 |
|
| 955 |
// Returns true iff test passed. |
| 956 |
static bool TestPassed(const TestInfo* test_info) { |
| 957 |
return test_info->should_run() && test_info->result()->Passed(); |
| 958 |
} |
| 959 |
|
| 960 |
// Returns true iff test skipped. |
| 961 |
static bool TestSkipped(const TestInfo* test_info) { |
| 962 |
return test_info->should_run() && test_info->result()->Skipped(); |
| 963 |
} |
| 964 |
|
| 965 |
// Returns true iff test failed. |
| 966 |
static bool TestFailed(const TestInfo* test_info) { |
| 967 |
return test_info->should_run() && test_info->result()->Failed(); |
| 968 |
} |
| 969 |
|
| 970 |
// Returns true iff the test is disabled and will be reported in the XML |
| 971 |
// report. |
| 972 |
static bool TestReportableDisabled(const TestInfo* test_info) { |
| 973 |
return test_info->is_reportable() && test_info->is_disabled_; |
| 974 |
} |
| 975 |
|
| 976 |
// Returns true iff test is disabled. |
| 977 |
static bool TestDisabled(const TestInfo* test_info) { |
| 978 |
return test_info->is_disabled_; |
| 979 |
} |
| 980 |
|
| 981 |
// Returns true iff this test will appear in the XML report. |
| 982 |
static bool TestReportable(const TestInfo* test_info) { |
| 983 |
return test_info->is_reportable(); |
| 984 |
} |
| 985 |
|
| 986 |
// Returns true if the given test should run. |
| 987 |
static bool ShouldRunTest(const TestInfo* test_info) { |
| 988 |
return test_info->should_run(); |
| 989 |
} |
| 990 |
|
| 991 |
// Shuffles the tests in this test suite. |
| 992 |
void ShuffleTests(internal::Random* random); |
| 993 |
|
| 994 |
// Restores the test order to before the first shuffle. |
| 995 |
void UnshuffleTests(); |
| 996 |
|
| 997 |
// Name of the test suite. |
| 998 |
std::string name_; |
| 999 |
// Name of the parameter type, or NULL if this is not a typed or a |
| 1000 |
// type-parameterized test. |
| 1001 |
const std::unique_ptr<const ::std::string> type_param_; |
| 1002 |
// The vector of TestInfos in their original order. It owns the |
| 1003 |
// elements in the vector. |
| 1004 |
std::vector<TestInfo*> test_info_list_; |
| 1005 |
// Provides a level of indirection for the test list to allow easy |
| 1006 |
// shuffling and restoring the test order. The i-th element in this |
| 1007 |
// vector is the index of the i-th test in the shuffled test list. |
| 1008 |
std::vector<int> test_indices_; |
| 1009 |
// Pointer to the function that sets up the test suite. |
| 1010 |
internal::SetUpTestSuiteFunc set_up_tc_; |
| 1011 |
// Pointer to the function that tears down the test suite. |
| 1012 |
internal::TearDownTestSuiteFunc tear_down_tc_; |
| 1013 |
// True iff any test in this test suite should run. |
| 1014 |
bool should_run_; |
| 1015 |
// Elapsed time, in milliseconds. |
| 1016 |
TimeInMillis elapsed_time_; |
| 1017 |
// Holds test properties recorded during execution of SetUpTestSuite and |
| 1018 |
// TearDownTestSuite. |
| 1019 |
TestResult ad_hoc_test_result_; |
| 1020 |
|
| 1021 |
// We disallow copying TestSuites. |
| 1022 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestSuite); |
| 1023 |
}; |
| 1024 |
|
| 1025 |
// An Environment object is capable of setting up and tearing down an |
| 1026 |
// environment. You should subclass this to define your own |
| 1027 |
// environment(s). |
| 1028 |
// |
| 1029 |
// An Environment object does the set-up and tear-down in virtual |
| 1030 |
// methods SetUp() and TearDown() instead of the constructor and the |
| 1031 |
// destructor, as: |
| 1032 |
// |
| 1033 |
// 1. You cannot safely throw from a destructor. This is a problem |
| 1034 |
// as in some cases Google Test is used where exceptions are enabled, and |
| 1035 |
// we may want to implement ASSERT_* using exceptions where they are |
| 1036 |
// available. |
| 1037 |
// 2. You cannot use ASSERT_* directly in a constructor or |
| 1038 |
// destructor. |
| 1039 |
class Environment { |
| 1040 |
public: |
| 1041 |
// The d'tor is virtual as we need to subclass Environment. |
| 1042 |
virtual ~Environment() {} |
| 1043 |
|
| 1044 |
// Override this to define how to set up the environment. |
| 1045 |
virtual void SetUp() {} |
| 1046 |
|
| 1047 |
// Override this to define how to tear down the environment. |
| 1048 |
virtual void TearDown() {} |
| 1049 |
private: |
| 1050 |
// If you see an error about overriding the following function or |
| 1051 |
// about it being private, you have mis-spelled SetUp() as Setup(). |
| 1052 |
struct Setup_should_be_spelled_SetUp {}; |
| 1053 |
virtual Setup_should_be_spelled_SetUp* Setup() { return nullptr; } |
| 1054 |
}; |
| 1055 |
|
| 1056 |
#if GTEST_HAS_EXCEPTIONS |
| 1057 |
|
| 1058 |
// Exception which can be thrown from TestEventListener::OnTestPartResult. |
| 1059 |
class GTEST_API_ AssertionException |
| 1060 |
: public internal::GoogleTestFailureException { |
| 1061 |
public: |
| 1062 |
explicit AssertionException(const TestPartResult& result) |
| 1063 |
: GoogleTestFailureException(result) {} |
| 1064 |
}; |
| 1065 |
|
| 1066 |
#endif // GTEST_HAS_EXCEPTIONS |
| 1067 |
|
| 1068 |
// The interface for tracing execution of tests. The methods are organized in |
| 1069 |
// the order the corresponding events are fired. |
| 1070 |
class TestEventListener { |
| 1071 |
public: |
| 1072 |
virtual ~TestEventListener() {} |
| 1073 |
|
| 1074 |
// Fired before any test activity starts. |
| 1075 |
virtual void OnTestProgramStart(const UnitTest& unit_test) = 0; |
| 1076 |
|
| 1077 |
// Fired before each iteration of tests starts. There may be more than |
| 1078 |
// one iteration if GTEST_FLAG(repeat) is set. iteration is the iteration |
| 1079 |
// index, starting from 0. |
| 1080 |
virtual void OnTestIterationStart(const UnitTest& unit_test, |
| 1081 |
int iteration) = 0; |
| 1082 |
|
| 1083 |
// Fired before environment set-up for each iteration of tests starts. |
| 1084 |
virtual void OnEnvironmentsSetUpStart(const UnitTest& unit_test) = 0; |
| 1085 |
|
| 1086 |
// Fired after environment set-up for each iteration of tests ends. |
| 1087 |
virtual void OnEnvironmentsSetUpEnd(const UnitTest& unit_test) = 0; |
| 1088 |
|
| 1089 |
// Fired before the test suite starts. |
| 1090 |
virtual void OnTestSuiteStart(const TestSuite& /*test_suite*/) {} |
| 1091 |
|
| 1092 |
// Legacy API is deprecated but still available |
| 1093 |
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 1094 |
virtual void OnTestCaseStart(const TestCase& /*test_case*/) {} |
| 1095 |
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 1096 |
|
| 1097 |
// Fired before the test starts. |
| 1098 |
virtual void OnTestStart(const TestInfo& test_info) = 0; |
| 1099 |
|
| 1100 |
// Fired after a failed assertion or a SUCCEED() invocation. |
| 1101 |
// If you want to throw an exception from this function to skip to the next |
| 1102 |
// TEST, it must be AssertionException defined above, or inherited from it. |
| 1103 |
virtual void OnTestPartResult(const TestPartResult& test_part_result) = 0; |
| 1104 |
|
| 1105 |
// Fired after the test ends. |
| 1106 |
virtual void OnTestEnd(const TestInfo& test_info) = 0; |
| 1107 |
|
| 1108 |
// Fired after the test suite ends. |
| 1109 |
virtual void OnTestSuiteEnd(const TestSuite& /*test_suite*/) {} |
| 1110 |
|
| 1111 |
// Legacy API is deprecated but still available |
| 1112 |
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 1113 |
virtual void OnTestCaseEnd(const TestCase& /*test_case*/) {} |
| 1114 |
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 1115 |
|
| 1116 |
// Fired before environment tear-down for each iteration of tests starts. |
| 1117 |
virtual void OnEnvironmentsTearDownStart(const UnitTest& unit_test) = 0; |
| 1118 |
|
| 1119 |
// Fired after environment tear-down for each iteration of tests ends. |
| 1120 |
virtual void OnEnvironmentsTearDownEnd(const UnitTest& unit_test) = 0; |
| 1121 |
|
| 1122 |
// Fired after each iteration of tests finishes. |
| 1123 |
virtual void OnTestIterationEnd(const UnitTest& unit_test, |
| 1124 |
int iteration) = 0; |
| 1125 |
|
| 1126 |
// Fired after all test activities have ended. |
| 1127 |
virtual void OnTestProgramEnd(const UnitTest& unit_test) = 0; |
| 1128 |
}; |
| 1129 |
|
| 1130 |
// The convenience class for users who need to override just one or two |
| 1131 |
// methods and are not concerned that a possible change to a signature of |
| 1132 |
// the methods they override will not be caught during the build. For |
| 1133 |
// comments about each method please see the definition of TestEventListener |
| 1134 |
// above. |
| 1135 |
class EmptyTestEventListener : public TestEventListener { |
| 1136 |
public: |
| 1137 |
void OnTestProgramStart(const UnitTest& /*unit_test*/) override {} |
| 1138 |
void OnTestIterationStart(const UnitTest& /*unit_test*/, |
| 1139 |
int /*iteration*/) override {} |
| 1140 |
void OnEnvironmentsSetUpStart(const UnitTest& /*unit_test*/) override {} |
| 1141 |
void OnEnvironmentsSetUpEnd(const UnitTest& /*unit_test*/) override {} |
| 1142 |
void OnTestSuiteStart(const TestSuite& /*test_suite*/) override {} |
| 1143 |
// Legacy API is deprecated but still available |
| 1144 |
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 1145 |
void OnTestCaseStart(const TestCase& /*test_case*/) override {} |
| 1146 |
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 1147 |
|
| 1148 |
void OnTestStart(const TestInfo& /*test_info*/) override {} |
| 1149 |
void OnTestPartResult(const TestPartResult& /*test_part_result*/) override {} |
| 1150 |
void OnTestEnd(const TestInfo& /*test_info*/) override {} |
| 1151 |
void OnTestSuiteEnd(const TestSuite& /*test_suite*/) override {} |
| 1152 |
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 1153 |
void OnTestCaseEnd(const TestCase& /*test_case*/) override {} |
| 1154 |
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 1155 |
|
| 1156 |
void OnEnvironmentsTearDownStart(const UnitTest& /*unit_test*/) override {} |
| 1157 |
void OnEnvironmentsTearDownEnd(const UnitTest& /*unit_test*/) override {} |
| 1158 |
void OnTestIterationEnd(const UnitTest& /*unit_test*/, |
| 1159 |
int /*iteration*/) override {} |
| 1160 |
void OnTestProgramEnd(const UnitTest& /*unit_test*/) override {} |
| 1161 |
}; |
| 1162 |
|
| 1163 |
// TestEventListeners lets users add listeners to track events in Google Test. |
| 1164 |
class GTEST_API_ TestEventListeners { |
| 1165 |
public: |
| 1166 |
TestEventListeners(); |
| 1167 |
~TestEventListeners(); |
| 1168 |
|
| 1169 |
// Appends an event listener to the end of the list. Google Test assumes |
| 1170 |
// the ownership of the listener (i.e. it will delete the listener when |
| 1171 |
// the test program finishes). |
| 1172 |
void Append(TestEventListener* listener); |
| 1173 |
|
| 1174 |
// Removes the given event listener from the list and returns it. It then |
| 1175 |
// becomes the caller's responsibility to delete the listener. Returns |
| 1176 |
// NULL if the listener is not found in the list. |
| 1177 |
TestEventListener* Release(TestEventListener* listener); |
| 1178 |
|
| 1179 |
// Returns the standard listener responsible for the default console |
| 1180 |
// output. Can be removed from the listeners list to shut down default |
| 1181 |
// console output. Note that removing this object from the listener list |
| 1182 |
// with Release transfers its ownership to the caller and makes this |
| 1183 |
// function return NULL the next time. |
| 1184 |
TestEventListener* default_result_printer() const { |
| 1185 |
return default_result_printer_; |
| 1186 |
} |
| 1187 |
|
| 1188 |
// Returns the standard listener responsible for the default XML output |
| 1189 |
// controlled by the --gtest_output=xml flag. Can be removed from the |
| 1190 |
// listeners list by users who want to shut down the default XML output |
| 1191 |
// controlled by this flag and substitute it with custom one. Note that |
| 1192 |
// removing this object from the listener list with Release transfers its |
| 1193 |
// ownership to the caller and makes this function return NULL the next |
| 1194 |
// time. |
| 1195 |
TestEventListener* default_xml_generator() const { |
| 1196 |
return default_xml_generator_; |
| 1197 |
} |
| 1198 |
|
| 1199 |
private: |
| 1200 |
friend class TestSuite; |
| 1201 |
friend class TestInfo; |
| 1202 |
friend class internal::DefaultGlobalTestPartResultReporter; |
| 1203 |
friend class internal::NoExecDeathTest; |
| 1204 |
friend class internal::TestEventListenersAccessor; |
| 1205 |
friend class internal::UnitTestImpl; |
| 1206 |
|
| 1207 |
// Returns repeater that broadcasts the TestEventListener events to all |
| 1208 |
// subscribers. |
| 1209 |
TestEventListener* repeater(); |
| 1210 |
|
| 1211 |
// Sets the default_result_printer attribute to the provided listener. |
| 1212 |
// The listener is also added to the listener list and previous |
| 1213 |
// default_result_printer is removed from it and deleted. The listener can |
| 1214 |
// also be NULL in which case it will not be added to the list. Does |
| 1215 |
// nothing if the previous and the current listener objects are the same. |
| 1216 |
void SetDefaultResultPrinter(TestEventListener* listener); |
| 1217 |
|
| 1218 |
// Sets the default_xml_generator attribute to the provided listener. The |
| 1219 |
// listener is also added to the listener list and previous |
| 1220 |
// default_xml_generator is removed from it and deleted. The listener can |
| 1221 |
// also be NULL in which case it will not be added to the list. Does |
| 1222 |
// nothing if the previous and the current listener objects are the same. |
| 1223 |
void SetDefaultXmlGenerator(TestEventListener* listener); |
| 1224 |
|
| 1225 |
// Controls whether events will be forwarded by the repeater to the |
| 1226 |
// listeners in the list. |
| 1227 |
bool EventForwardingEnabled() const; |
| 1228 |
void SuppressEventForwarding(); |
| 1229 |
|
| 1230 |
// The actual list of listeners. |
| 1231 |
internal::TestEventRepeater* repeater_; |
| 1232 |
// Listener responsible for the standard result output. |
| 1233 |
TestEventListener* default_result_printer_; |
| 1234 |
// Listener responsible for the creation of the XML output file. |
| 1235 |
TestEventListener* default_xml_generator_; |
| 1236 |
|
| 1237 |
// We disallow copying TestEventListeners. |
| 1238 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(TestEventListeners); |
| 1239 |
}; |
| 1240 |
|
| 1241 |
// A UnitTest consists of a vector of TestSuites. |
| 1242 |
// |
| 1243 |
// This is a singleton class. The only instance of UnitTest is |
| 1244 |
// created when UnitTest::GetInstance() is first called. This |
| 1245 |
// instance is never deleted. |
| 1246 |
// |
| 1247 |
// UnitTest is not copyable. |
| 1248 |
// |
| 1249 |
// This class is thread-safe as long as the methods are called |
| 1250 |
// according to their specification. |
| 1251 |
class GTEST_API_ UnitTest { |
| 1252 |
public: |
| 1253 |
// Gets the singleton UnitTest object. The first time this method |
| 1254 |
// is called, a UnitTest object is constructed and returned. |
| 1255 |
// Consecutive calls will return the same object. |
| 1256 |
static UnitTest* GetInstance(); |
| 1257 |
|
| 1258 |
// Runs all tests in this UnitTest object and prints the result. |
| 1259 |
// Returns 0 if successful, or 1 otherwise. |
| 1260 |
// |
| 1261 |
// This method can only be called from the main thread. |
| 1262 |
// |
| 1263 |
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
| 1264 |
int Run() GTEST_MUST_USE_RESULT_; |
| 1265 |
|
| 1266 |
// Returns the working directory when the first TEST() or TEST_F() |
| 1267 |
// was executed. The UnitTest object owns the string. |
| 1268 |
const char* original_working_dir() const; |
| 1269 |
|
| 1270 |
// Returns the TestSuite object for the test that's currently running, |
| 1271 |
// or NULL if no test is running. |
| 1272 |
const TestSuite* current_test_suite() const GTEST_LOCK_EXCLUDED_(mutex_); |
| 1273 |
|
| 1274 |
// Legacy API is still available but deprecated |
| 1275 |
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 1276 |
const TestCase* current_test_case() const GTEST_LOCK_EXCLUDED_(mutex_); |
| 1277 |
#endif |
| 1278 |
|
| 1279 |
// Returns the TestInfo object for the test that's currently running, |
| 1280 |
// or NULL if no test is running. |
| 1281 |
const TestInfo* current_test_info() const |
| 1282 |
GTEST_LOCK_EXCLUDED_(mutex_); |
| 1283 |
|
| 1284 |
// Returns the random seed used at the start of the current test run. |
| 1285 |
int random_seed() const; |
| 1286 |
|
| 1287 |
// Returns the ParameterizedTestSuiteRegistry object used to keep track of |
| 1288 |
// value-parameterized tests and instantiate and register them. |
| 1289 |
// |
| 1290 |
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
| 1291 |
internal::ParameterizedTestSuiteRegistry& parameterized_test_registry() |
| 1292 |
GTEST_LOCK_EXCLUDED_(mutex_); |
| 1293 |
|
| 1294 |
// Gets the number of successful test suites. |
| 1295 |
int successful_test_suite_count() const; |
| 1296 |
|
| 1297 |
// Gets the number of failed test suites. |
| 1298 |
int failed_test_suite_count() const; |
| 1299 |
|
| 1300 |
// Gets the number of all test suites. |
| 1301 |
int total_test_suite_count() const; |
| 1302 |
|
| 1303 |
// Gets the number of all test suites that contain at least one test |
| 1304 |
// that should run. |
| 1305 |
int test_suite_to_run_count() const; |
| 1306 |
|
| 1307 |
// Legacy API is deprecated but still available |
| 1308 |
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 1309 |
int successful_test_case_count() const; |
| 1310 |
int failed_test_case_count() const; |
| 1311 |
int total_test_case_count() const; |
| 1312 |
int test_case_to_run_count() const; |
| 1313 |
#endif // EMOVE_LEGACY_TEST_CASEAPI |
| 1314 |
|
| 1315 |
// Gets the number of successful tests. |
| 1316 |
int successful_test_count() const; |
| 1317 |
|
| 1318 |
// Gets the number of skipped tests. |
| 1319 |
int skipped_test_count() const; |
| 1320 |
|
| 1321 |
// Gets the number of failed tests. |
| 1322 |
int failed_test_count() const; |
| 1323 |
|
| 1324 |
// Gets the number of disabled tests that will be reported in the XML report. |
| 1325 |
int reportable_disabled_test_count() const; |
| 1326 |
|
| 1327 |
// Gets the number of disabled tests. |
| 1328 |
int disabled_test_count() const; |
| 1329 |
|
| 1330 |
// Gets the number of tests to be printed in the XML report. |
| 1331 |
int reportable_test_count() const; |
| 1332 |
|
| 1333 |
// Gets the number of all tests. |
| 1334 |
int total_test_count() const; |
| 1335 |
|
| 1336 |
// Gets the number of tests that should run. |
| 1337 |
int test_to_run_count() const; |
| 1338 |
|
| 1339 |
// Gets the time of the test program start, in ms from the start of the |
| 1340 |
// UNIX epoch. |
| 1341 |
TimeInMillis start_timestamp() const; |
| 1342 |
|
| 1343 |
// Gets the elapsed time, in milliseconds. |
| 1344 |
TimeInMillis elapsed_time() const; |
| 1345 |
|
| 1346 |
// Returns true iff the unit test passed (i.e. all test suites passed). |
| 1347 |
bool Passed() const; |
| 1348 |
|
| 1349 |
// Returns true iff the unit test failed (i.e. some test suite failed |
| 1350 |
// or something outside of all tests failed). |
| 1351 |
bool Failed() const; |
| 1352 |
|
| 1353 |
// Gets the i-th test suite among all the test suites. i can range from 0 to |
| 1354 |
// total_test_suite_count() - 1. If i is not in that range, returns NULL. |
| 1355 |
const TestSuite* GetTestSuite(int i) const; |
| 1356 |
|
| 1357 |
// Legacy API is deprecated but still available |
| 1358 |
#ifndef GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 1359 |
const TestCase* GetTestCase(int i) const; |
| 1360 |
#endif // GTEST_REMOVE_LEGACY_TEST_CASEAPI_ |
| 1361 |
|
| 1362 |
// Returns the TestResult containing information on test failures and |
| 1363 |
// properties logged outside of individual test suites. |
| 1364 |
const TestResult& ad_hoc_test_result() const; |
| 1365 |
|
| 1366 |
// Returns the list of event listeners that can be used to track events |
| 1367 |
// inside Google Test. |
| 1368 |
TestEventListeners& listeners(); |
| 1369 |
|
| 1370 |
private: |
| 1371 |
// Registers and returns a global test environment. When a test |
| 1372 |
// program is run, all global test environments will be set-up in |
| 1373 |
// the order they were registered. After all tests in the program |
| 1374 |
// have finished, all global test environments will be torn-down in |
| 1375 |
// the *reverse* order they were registered. |
| 1376 |
// |
| 1377 |
// The UnitTest object takes ownership of the given environment. |
| 1378 |
// |
| 1379 |
// This method can only be called from the main thread. |
| 1380 |
Environment* AddEnvironment(Environment* env); |
| 1381 |
|
| 1382 |
// Adds a TestPartResult to the current TestResult object. All |
| 1383 |
// Google Test assertion macros (e.g. ASSERT_TRUE, EXPECT_EQ, etc) |
| 1384 |
// eventually call this to report their results. The user code |
| 1385 |
// should use the assertion macros instead of calling this directly. |
| 1386 |
void AddTestPartResult(TestPartResult::Type result_type, |
| 1387 |
const char* file_name, |
| 1388 |
int line_number, |
| 1389 |
const std::string& message, |
| 1390 |
const std::string& os_stack_trace) |
| 1391 |
GTEST_LOCK_EXCLUDED_(mutex_); |
| 1392 |
|
| 1393 |
// Adds a TestProperty to the current TestResult object when invoked from |
| 1394 |
// inside a test, to current TestSuite's ad_hoc_test_result_ when invoked |
| 1395 |
// from SetUpTestSuite or TearDownTestSuite, or to the global property set |
| 1396 |
// when invoked elsewhere. If the result already contains a property with |
| 1397 |
// the same key, the value will be updated. |
| 1398 |
void RecordProperty(const std::string& key, const std::string& value); |
| 1399 |
|
| 1400 |
// Gets the i-th test suite among all the test suites. i can range from 0 to |
| 1401 |
// total_test_suite_count() - 1. If i is not in that range, returns NULL. |
| 1402 |
TestSuite* GetMutableTestSuite(int i); |
| 1403 |
|
| 1404 |
// Accessors for the implementation object. |
| 1405 |
internal::UnitTestImpl* impl() { return impl_; } |
| 1406 |
const internal::UnitTestImpl* impl() const { return impl_; } |
| 1407 |
|
| 1408 |
// These classes and functions are friends as they need to access private |
| 1409 |
// members of UnitTest. |
| 1410 |
friend class ScopedTrace; |
| 1411 |
friend class Test; |
| 1412 |
friend class internal::AssertHelper; |
| 1413 |
friend class internal::StreamingListenerTest; |
| 1414 |
friend class internal::UnitTestRecordPropertyTestHelper; |
| 1415 |
friend Environment* AddGlobalTestEnvironment(Environment* env); |
| 1416 |
friend internal::UnitTestImpl* internal::GetUnitTestImpl(); |
| 1417 |
friend void internal::ReportFailureInUnknownLocation( |
| 1418 |
TestPartResult::Type result_type, |
| 1419 |
const std::string& message); |
| 1420 |
|
| 1421 |
// Creates an empty UnitTest. |
| 1422 |
UnitTest(); |
| 1423 |
|
| 1424 |
// D'tor |
| 1425 |
virtual ~UnitTest(); |
| 1426 |
|
| 1427 |
// Pushes a trace defined by SCOPED_TRACE() on to the per-thread |
| 1428 |
// Google Test trace stack. |
| 1429 |
void PushGTestTrace(const internal::TraceInfo& trace) |
| 1430 |
GTEST_LOCK_EXCLUDED_(mutex_); |
| 1431 |
|
| 1432 |
// Pops a trace from the per-thread Google Test trace stack. |
| 1433 |
void PopGTestTrace() |
| 1434 |
GTEST_LOCK_EXCLUDED_(mutex_); |
| 1435 |
|
| 1436 |
// Protects mutable state in *impl_. This is mutable as some const |
| 1437 |
// methods need to lock it too. |
| 1438 |
mutable internal::Mutex mutex_; |
| 1439 |
|
| 1440 |
// Opaque implementation object. This field is never changed once |
| 1441 |
// the object is constructed. We don't mark it as const here, as |
| 1442 |
// doing so will cause a warning in the constructor of UnitTest. |
| 1443 |
// Mutable state in *impl_ is protected by mutex_. |
| 1444 |
internal::UnitTestImpl* impl_; |
| 1445 |
|
| 1446 |
// We disallow copying UnitTest. |
| 1447 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(UnitTest); |
| 1448 |
}; |
| 1449 |
|
| 1450 |
// A convenient wrapper for adding an environment for the test |
| 1451 |
// program. |
| 1452 |
// |
| 1453 |
// You should call this before RUN_ALL_TESTS() is called, probably in |
| 1454 |
// main(). If you use gtest_main, you need to call this before main() |
| 1455 |
// starts for it to take effect. For example, you can define a global |
| 1456 |
// variable like this: |
| 1457 |
// |
| 1458 |
// testing::Environment* const foo_env = |
| 1459 |
// testing::AddGlobalTestEnvironment(new FooEnvironment); |
| 1460 |
// |
| 1461 |
// However, we strongly recommend you to write your own main() and |
| 1462 |
// call AddGlobalTestEnvironment() there, as relying on initialization |
| 1463 |
// of global variables makes the code harder to read and may cause |
| 1464 |
// problems when you register multiple environments from different |
| 1465 |
// translation units and the environments have dependencies among them |
| 1466 |
// (remember that the compiler doesn't guarantee the order in which |
| 1467 |
// global variables from different translation units are initialized). |
| 1468 |
inline Environment* AddGlobalTestEnvironment(Environment* env) { |
| 1469 |
return UnitTest::GetInstance()->AddEnvironment(env); |
| 1470 |
} |
| 1471 |
|
| 1472 |
// Initializes Google Test. This must be called before calling |
| 1473 |
// RUN_ALL_TESTS(). In particular, it parses a command line for the |
| 1474 |
// flags that Google Test recognizes. Whenever a Google Test flag is |
| 1475 |
// seen, it is removed from argv, and *argc is decremented. |
| 1476 |
// |
| 1477 |
// No value is returned. Instead, the Google Test flag variables are |
| 1478 |
// updated. |
| 1479 |
// |
| 1480 |
// Calling the function for the second time has no user-visible effect. |
| 1481 |
GTEST_API_ void InitGoogleTest(int* argc, char** argv); |
| 1482 |
|
| 1483 |
// This overloaded version can be used in Windows programs compiled in |
| 1484 |
// UNICODE mode. |
| 1485 |
GTEST_API_ void InitGoogleTest(int* argc, wchar_t** argv); |
| 1486 |
|
| 1487 |
// This overloaded version can be used on Arduino/embedded platforms where |
| 1488 |
// there is no argc/argv. |
| 1489 |
GTEST_API_ void InitGoogleTest(); |
| 1490 |
|
| 1491 |
namespace internal { |
| 1492 |
|
| 1493 |
// Separate the error generating code from the code path to reduce the stack |
| 1494 |
// frame size of CmpHelperEQ. This helps reduce the overhead of some sanitizers |
| 1495 |
// when calling EXPECT_* in a tight loop. |
| 1496 |
template <typename T1, typename T2> |
| 1497 |
AssertionResult CmpHelperEQFailure(const char* lhs_expression, |
| 1498 |
const char* rhs_expression, |
| 1499 |
const T1& lhs, const T2& rhs) { |
| 1500 |
return EqFailure(lhs_expression, |
| 1501 |
rhs_expression, |
| 1502 |
FormatForComparisonFailureMessage(lhs, rhs), |
| 1503 |
FormatForComparisonFailureMessage(rhs, lhs), |
| 1504 |
false); |
| 1505 |
} |
| 1506 |
|
| 1507 |
// This block of code defines operator==/!= |
| 1508 |
// to block lexical scope lookup. |
| 1509 |
// It prevents using invalid operator==/!= defined at namespace scope. |
| 1510 |
struct faketype {}; |
| 1511 |
inline bool operator==(faketype, faketype) { return true; } |
| 1512 |
inline bool operator!=(faketype, faketype) { return false; } |
| 1513 |
|
| 1514 |
// The helper function for {ASSERT|EXPECT}_EQ. |
| 1515 |
template <typename T1, typename T2> |
| 1516 |
AssertionResult CmpHelperEQ(const char* lhs_expression, |
| 1517 |
const char* rhs_expression, |
| 1518 |
const T1& lhs, |
| 1519 |
const T2& rhs) { |
| 1520 |
if (lhs == rhs) { |
| 1521 |
return AssertionSuccess(); |
| 1522 |
} |
| 1523 |
|
| 1524 |
return CmpHelperEQFailure(lhs_expression, rhs_expression, lhs, rhs); |
| 1525 |
} |
| 1526 |
|
| 1527 |
// With this overloaded version, we allow anonymous enums to be used |
| 1528 |
// in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous enums |
| 1529 |
// can be implicitly cast to BiggestInt. |
| 1530 |
GTEST_API_ AssertionResult CmpHelperEQ(const char* lhs_expression, |
| 1531 |
const char* rhs_expression, |
| 1532 |
BiggestInt lhs, |
| 1533 |
BiggestInt rhs); |
| 1534 |
|
| 1535 |
// The helper class for {ASSERT|EXPECT}_EQ. The template argument |
| 1536 |
// lhs_is_null_literal is true iff the first argument to ASSERT_EQ() |
| 1537 |
// is a null pointer literal. The following default implementation is |
| 1538 |
// for lhs_is_null_literal being false. |
| 1539 |
template <bool lhs_is_null_literal> |
| 1540 |
class EqHelper { |
| 1541 |
public: |
| 1542 |
// This templatized version is for the general case. |
| 1543 |
template <typename T1, typename T2> |
| 1544 |
static AssertionResult Compare(const char* lhs_expression, |
| 1545 |
const char* rhs_expression, |
| 1546 |
const T1& lhs, |
| 1547 |
const T2& rhs) { |
| 1548 |
return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs); |
| 1549 |
} |
| 1550 |
|
| 1551 |
// With this overloaded version, we allow anonymous enums to be used |
| 1552 |
// in {ASSERT|EXPECT}_EQ when compiled with gcc 4, as anonymous |
| 1553 |
// enums can be implicitly cast to BiggestInt. |
| 1554 |
// |
| 1555 |
// Even though its body looks the same as the above version, we |
| 1556 |
// cannot merge the two, as it will make anonymous enums unhappy. |
| 1557 |
static AssertionResult Compare(const char* lhs_expression, |
| 1558 |
const char* rhs_expression, |
| 1559 |
BiggestInt lhs, |
| 1560 |
BiggestInt rhs) { |
| 1561 |
return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs); |
| 1562 |
} |
| 1563 |
}; |
| 1564 |
|
| 1565 |
// This specialization is used when the first argument to ASSERT_EQ() |
| 1566 |
// is a null pointer literal, like NULL, false, or 0. |
| 1567 |
template <> |
| 1568 |
class EqHelper<true> { |
| 1569 |
public: |
| 1570 |
// We define two overloaded versions of Compare(). The first |
| 1571 |
// version will be picked when the second argument to ASSERT_EQ() is |
| 1572 |
// NOT a pointer, e.g. ASSERT_EQ(0, AnIntFunction()) or |
| 1573 |
// EXPECT_EQ(false, a_bool). |
| 1574 |
template <typename T1, typename T2> |
| 1575 |
static AssertionResult Compare( |
| 1576 |
const char* lhs_expression, const char* rhs_expression, const T1& lhs, |
| 1577 |
const T2& rhs, |
| 1578 |
// The following line prevents this overload from being considered if T2 |
| 1579 |
// is not a pointer type. We need this because ASSERT_EQ(NULL, my_ptr) |
| 1580 |
// expands to Compare("", "", NULL, my_ptr), which requires a conversion |
| 1581 |
// to match the Secret* in the other overload, which would otherwise make |
| 1582 |
// this template match better. |
| 1583 |
typename EnableIf<!std::is_pointer<T2>::value>::type* = nullptr) { |
| 1584 |
return CmpHelperEQ(lhs_expression, rhs_expression, lhs, rhs); |
| 1585 |
} |
| 1586 |
|
| 1587 |
// This version will be picked when the second argument to ASSERT_EQ() is a |
| 1588 |
// pointer, e.g. ASSERT_EQ(NULL, a_pointer). |
| 1589 |
template <typename T> |
| 1590 |
static AssertionResult Compare( |
| 1591 |
const char* lhs_expression, |
| 1592 |
const char* rhs_expression, |
| 1593 |
// We used to have a second template parameter instead of Secret*. That |
| 1594 |
// template parameter would deduce to 'long', making this a better match |
| 1595 |
// than the first overload even without the first overload's EnableIf. |
| 1596 |
// Unfortunately, gcc with -Wconversion-null warns when "passing NULL to |
| 1597 |
// non-pointer argument" (even a deduced integral argument), so the old |
| 1598 |
// implementation caused warnings in user code. |
| 1599 |
Secret* /* lhs (NULL) */, |
| 1600 |
T* rhs) { |
| 1601 |
// We already know that 'lhs' is a null pointer. |
| 1602 |
return CmpHelperEQ(lhs_expression, rhs_expression, static_cast<T*>(nullptr), |
| 1603 |
rhs); |
| 1604 |
} |
| 1605 |
}; |
| 1606 |
|
| 1607 |
// Separate the error generating code from the code path to reduce the stack |
| 1608 |
// frame size of CmpHelperOP. This helps reduce the overhead of some sanitizers |
| 1609 |
// when calling EXPECT_OP in a tight loop. |
| 1610 |
template <typename T1, typename T2> |
| 1611 |
AssertionResult CmpHelperOpFailure(const char* expr1, const char* expr2, |
| 1612 |
const T1& val1, const T2& val2, |
| 1613 |
const char* op) { |
| 1614 |
return AssertionFailure() |
| 1615 |
<< "Expected: (" << expr1 << ") " << op << " (" << expr2 |
| 1616 |
<< "), actual: " << FormatForComparisonFailureMessage(val1, val2) |
| 1617 |
<< " vs " << FormatForComparisonFailureMessage(val2, val1); |
| 1618 |
} |
| 1619 |
|
| 1620 |
// A macro for implementing the helper functions needed to implement |
| 1621 |
// ASSERT_?? and EXPECT_??. It is here just to avoid copy-and-paste |
| 1622 |
// of similar code. |
| 1623 |
// |
| 1624 |
// For each templatized helper function, we also define an overloaded |
| 1625 |
// version for BiggestInt in order to reduce code bloat and allow |
| 1626 |
// anonymous enums to be used with {ASSERT|EXPECT}_?? when compiled |
| 1627 |
// with gcc 4. |
| 1628 |
// |
| 1629 |
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
| 1630 |
|
| 1631 |
#define GTEST_IMPL_CMP_HELPER_(op_name, op)\ |
| 1632 |
template <typename T1, typename T2>\ |
| 1633 |
AssertionResult CmpHelper##op_name(const char* expr1, const char* expr2, \ |
| 1634 |
const T1& val1, const T2& val2) {\ |
| 1635 |
if (val1 op val2) {\ |
| 1636 |
return AssertionSuccess();\ |
| 1637 |
} else {\ |
| 1638 |
return CmpHelperOpFailure(expr1, expr2, val1, val2, #op);\ |
| 1639 |
}\ |
| 1640 |
}\ |
| 1641 |
GTEST_API_ AssertionResult CmpHelper##op_name(\ |
| 1642 |
const char* expr1, const char* expr2, BiggestInt val1, BiggestInt val2) |
| 1643 |
|
| 1644 |
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
| 1645 |
|
| 1646 |
// Implements the helper function for {ASSERT|EXPECT}_NE |
| 1647 |
GTEST_IMPL_CMP_HELPER_(NE, !=); |
| 1648 |
// Implements the helper function for {ASSERT|EXPECT}_LE |
| 1649 |
GTEST_IMPL_CMP_HELPER_(LE, <=); |
| 1650 |
// Implements the helper function for {ASSERT|EXPECT}_LT |
| 1651 |
GTEST_IMPL_CMP_HELPER_(LT, <); |
| 1652 |
// Implements the helper function for {ASSERT|EXPECT}_GE |
| 1653 |
GTEST_IMPL_CMP_HELPER_(GE, >=); |
| 1654 |
// Implements the helper function for {ASSERT|EXPECT}_GT |
| 1655 |
GTEST_IMPL_CMP_HELPER_(GT, >); |
| 1656 |
|
| 1657 |
#undef GTEST_IMPL_CMP_HELPER_ |
| 1658 |
|
| 1659 |
// The helper function for {ASSERT|EXPECT}_STREQ. |
| 1660 |
// |
| 1661 |
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
| 1662 |
GTEST_API_ AssertionResult CmpHelperSTREQ(const char* s1_expression, |
| 1663 |
const char* s2_expression, |
| 1664 |
const char* s1, |
| 1665 |
const char* s2); |
| 1666 |
|
| 1667 |
// The helper function for {ASSERT|EXPECT}_STRCASEEQ. |
| 1668 |
// |
| 1669 |
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
| 1670 |
GTEST_API_ AssertionResult CmpHelperSTRCASEEQ(const char* s1_expression, |
| 1671 |
const char* s2_expression, |
| 1672 |
const char* s1, |
| 1673 |
const char* s2); |
| 1674 |
|
| 1675 |
// The helper function for {ASSERT|EXPECT}_STRNE. |
| 1676 |
// |
| 1677 |
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
| 1678 |
GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression, |
| 1679 |
const char* s2_expression, |
| 1680 |
const char* s1, |
| 1681 |
const char* s2); |
| 1682 |
|
| 1683 |
// The helper function for {ASSERT|EXPECT}_STRCASENE. |
| 1684 |
// |
| 1685 |
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
| 1686 |
GTEST_API_ AssertionResult CmpHelperSTRCASENE(const char* s1_expression, |
| 1687 |
const char* s2_expression, |
| 1688 |
const char* s1, |
| 1689 |
const char* s2); |
| 1690 |
|
| 1691 |
|
| 1692 |
// Helper function for *_STREQ on wide strings. |
| 1693 |
// |
| 1694 |
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
| 1695 |
GTEST_API_ AssertionResult CmpHelperSTREQ(const char* s1_expression, |
| 1696 |
const char* s2_expression, |
| 1697 |
const wchar_t* s1, |
| 1698 |
const wchar_t* s2); |
| 1699 |
|
| 1700 |
// Helper function for *_STRNE on wide strings. |
| 1701 |
// |
| 1702 |
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
| 1703 |
GTEST_API_ AssertionResult CmpHelperSTRNE(const char* s1_expression, |
| 1704 |
const char* s2_expression, |
| 1705 |
const wchar_t* s1, |
| 1706 |
const wchar_t* s2); |
| 1707 |
|
| 1708 |
} // namespace internal |
| 1709 |
|
| 1710 |
// IsSubstring() and IsNotSubstring() are intended to be used as the |
| 1711 |
// first argument to {EXPECT,ASSERT}_PRED_FORMAT2(), not by |
| 1712 |
// themselves. They check whether needle is a substring of haystack |
| 1713 |
// (NULL is considered a substring of itself only), and return an |
| 1714 |
// appropriate error message when they fail. |
| 1715 |
// |
| 1716 |
// The {needle,haystack}_expr arguments are the stringified |
| 1717 |
// expressions that generated the two real arguments. |
| 1718 |
GTEST_API_ AssertionResult IsSubstring( |
| 1719 |
const char* needle_expr, const char* haystack_expr, |
| 1720 |
const char* needle, const char* haystack); |
| 1721 |
GTEST_API_ AssertionResult IsSubstring( |
| 1722 |
const char* needle_expr, const char* haystack_expr, |
| 1723 |
const wchar_t* needle, const wchar_t* haystack); |
| 1724 |
GTEST_API_ AssertionResult IsNotSubstring( |
| 1725 |
const char* needle_expr, const char* haystack_expr, |
| 1726 |
const char* needle, const char* haystack); |
| 1727 |
GTEST_API_ AssertionResult IsNotSubstring( |
| 1728 |
const char* needle_expr, const char* haystack_expr, |
| 1729 |
const wchar_t* needle, const wchar_t* haystack); |
| 1730 |
GTEST_API_ AssertionResult IsSubstring( |
| 1731 |
const char* needle_expr, const char* haystack_expr, |
| 1732 |
const ::std::string& needle, const ::std::string& haystack); |
| 1733 |
GTEST_API_ AssertionResult IsNotSubstring( |
| 1734 |
const char* needle_expr, const char* haystack_expr, |
| 1735 |
const ::std::string& needle, const ::std::string& haystack); |
| 1736 |
|
| 1737 |
#if GTEST_HAS_STD_WSTRING |
| 1738 |
GTEST_API_ AssertionResult IsSubstring( |
| 1739 |
const char* needle_expr, const char* haystack_expr, |
| 1740 |
const ::std::wstring& needle, const ::std::wstring& haystack); |
| 1741 |
GTEST_API_ AssertionResult IsNotSubstring( |
| 1742 |
const char* needle_expr, const char* haystack_expr, |
| 1743 |
const ::std::wstring& needle, const ::std::wstring& haystack); |
| 1744 |
#endif // GTEST_HAS_STD_WSTRING |
| 1745 |
|
| 1746 |
namespace internal { |
| 1747 |
|
| 1748 |
// Helper template function for comparing floating-points. |
| 1749 |
// |
| 1750 |
// Template parameter: |
| 1751 |
// |
| 1752 |
// RawType: the raw floating-point type (either float or double) |
| 1753 |
// |
| 1754 |
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
| 1755 |
template <typename RawType> |
| 1756 |
AssertionResult CmpHelperFloatingPointEQ(const char* lhs_expression, |
| 1757 |
const char* rhs_expression, |
| 1758 |
RawType lhs_value, |
| 1759 |
RawType rhs_value) { |
| 1760 |
const FloatingPoint<RawType> lhs(lhs_value), rhs(rhs_value); |
| 1761 |
|
| 1762 |
if (lhs.AlmostEquals(rhs)) { |
| 1763 |
return AssertionSuccess(); |
| 1764 |
} |
| 1765 |
|
| 1766 |
::std::stringstream lhs_ss; |
| 1767 |
lhs_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) |
| 1768 |
<< lhs_value; |
| 1769 |
|
| 1770 |
::std::stringstream rhs_ss; |
| 1771 |
rhs_ss << std::setprecision(std::numeric_limits<RawType>::digits10 + 2) |
| 1772 |
<< rhs_value; |
| 1773 |
|
| 1774 |
return EqFailure(lhs_expression, |
| 1775 |
rhs_expression, |
| 1776 |
StringStreamToString(&lhs_ss), |
| 1777 |
StringStreamToString(&rhs_ss), |
| 1778 |
false); |
| 1779 |
} |
| 1780 |
|
| 1781 |
// Helper function for implementing ASSERT_NEAR. |
| 1782 |
// |
| 1783 |
// INTERNAL IMPLEMENTATION - DO NOT USE IN A USER PROGRAM. |
| 1784 |
GTEST_API_ AssertionResult DoubleNearPredFormat(const char* expr1, |
| 1785 |
const char* expr2, |
| 1786 |
const char* abs_error_expr, |
| 1787 |
double val1, |
| 1788 |
double val2, |
| 1789 |
double abs_error); |
| 1790 |
|
| 1791 |
// INTERNAL IMPLEMENTATION - DO NOT USE IN USER CODE. |
| 1792 |
// A class that enables one to stream messages to assertion macros |
| 1793 |
class GTEST_API_ AssertHelper { |
| 1794 |
public: |
| 1795 |
// Constructor. |
| 1796 |
AssertHelper(TestPartResult::Type type, |
| 1797 |
const char* file, |
| 1798 |
int line, |
| 1799 |
const char* message); |
| 1800 |
~AssertHelper(); |
| 1801 |
|
| 1802 |
// Message assignment is a semantic trick to enable assertion |
| 1803 |
// streaming; see the GTEST_MESSAGE_ macro below. |
| 1804 |
void operator=(const Message& message) const; |
| 1805 |
|
| 1806 |
private: |
| 1807 |
// We put our data in a struct so that the size of the AssertHelper class can |
| 1808 |
// be as small as possible. This is important because gcc is incapable of |
| 1809 |
// re-using stack space even for temporary variables, so every EXPECT_EQ |
| 1810 |
// reserves stack space for another AssertHelper. |
| 1811 |
struct AssertHelperData { |
| 1812 |
AssertHelperData(TestPartResult::Type t, |
| 1813 |
const char* srcfile, |
| 1814 |
int line_num, |
| 1815 |
const char* msg) |
| 1816 |
: type(t), file(srcfile), line(line_num), message(msg) { } |
| 1817 |
|
| 1818 |
TestPartResult::Type const type; |
| 1819 |
const char* const file; |
| 1820 |
int const line; |
| 1821 |
std::string const message; |
| 1822 |
|
| 1823 |
private: |
| 1824 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelperData); |
| 1825 |
}; |
| 1826 |
|
| 1827 |
AssertHelperData* const data_; |
| 1828 |
|
| 1829 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(AssertHelper); |
| 1830 |
}; |
| 1831 |
|
| 1832 |
enum GTestColor { COLOR_DEFAULT, COLOR_RED, COLOR_GREEN, COLOR_YELLOW }; |
| 1833 |
|
| 1834 |
GTEST_API_ GTEST_ATTRIBUTE_PRINTF_(2, 3) void ColoredPrintf(GTestColor color, |
| 1835 |
const char* fmt, |
| 1836 |
...); |
| 1837 |
|
| 1838 |
} // namespace internal |
| 1839 |
|
| 1840 |
// The pure interface class that all value-parameterized tests inherit from. |
| 1841 |
// A value-parameterized class must inherit from both ::testing::Test and |
| 1842 |
// ::testing::WithParamInterface. In most cases that just means inheriting |
| 1843 |
// from ::testing::TestWithParam, but more complicated test hierarchies |
| 1844 |
// may need to inherit from Test and WithParamInterface at different levels. |
| 1845 |
// |
| 1846 |
// This interface has support for accessing the test parameter value via |
| 1847 |
// the GetParam() method. |
| 1848 |
// |
| 1849 |
// Use it with one of the parameter generator defining functions, like Range(), |
| 1850 |
// Values(), ValuesIn(), Bool(), and Combine(). |
| 1851 |
// |
| 1852 |
// class FooTest : public ::testing::TestWithParam<int> { |
| 1853 |
// protected: |
| 1854 |
// FooTest() { |
| 1855 |
// // Can use GetParam() here. |
| 1856 |
// } |
| 1857 |
// ~FooTest() override { |
| 1858 |
// // Can use GetParam() here. |
| 1859 |
// } |
| 1860 |
// void SetUp() override { |
| 1861 |
// // Can use GetParam() here. |
| 1862 |
// } |
| 1863 |
// void TearDown override { |
| 1864 |
// // Can use GetParam() here. |
| 1865 |
// } |
| 1866 |
// }; |
| 1867 |
// TEST_P(FooTest, DoesBar) { |
| 1868 |
// // Can use GetParam() method here. |
| 1869 |
// Foo foo; |
| 1870 |
// ASSERT_TRUE(foo.DoesBar(GetParam())); |
| 1871 |
// } |
| 1872 |
// INSTANTIATE_TEST_SUITE_P(OneToTenRange, FooTest, ::testing::Range(1, 10)); |
| 1873 |
|
| 1874 |
template <typename T> |
| 1875 |
class WithParamInterface { |
| 1876 |
public: |
| 1877 |
typedef T ParamType; |
| 1878 |
virtual ~WithParamInterface() {} |
| 1879 |
|
| 1880 |
// The current parameter value. Is also available in the test fixture's |
| 1881 |
// constructor. |
| 1882 |
static const ParamType& GetParam() { |
| 1883 |
GTEST_CHECK_(parameter_ != nullptr) |
| 1884 |
<< "GetParam() can only be called inside a value-parameterized test " |
| 1885 |
<< "-- did you intend to write TEST_P instead of TEST_F?"; |
| 1886 |
return *parameter_; |
| 1887 |
} |
| 1888 |
|
| 1889 |
private: |
| 1890 |
// Sets parameter value. The caller is responsible for making sure the value |
| 1891 |
// remains alive and unchanged throughout the current test. |
| 1892 |
static void SetParam(const ParamType* parameter) { |
| 1893 |
parameter_ = parameter; |
| 1894 |
} |
| 1895 |
|
| 1896 |
// Static value used for accessing parameter during a test lifetime. |
| 1897 |
static const ParamType* parameter_; |
| 1898 |
|
| 1899 |
// TestClass must be a subclass of WithParamInterface<T> and Test. |
| 1900 |
template <class TestClass> friend class internal::ParameterizedTestFactory; |
| 1901 |
}; |
| 1902 |
|
| 1903 |
template <typename T> |
| 1904 |
const T* WithParamInterface<T>::parameter_ = nullptr; |
| 1905 |
|
| 1906 |
// Most value-parameterized classes can ignore the existence of |
| 1907 |
// WithParamInterface, and can just inherit from ::testing::TestWithParam. |
| 1908 |
|
| 1909 |
template <typename T> |
| 1910 |
class TestWithParam : public Test, public WithParamInterface<T> { |
| 1911 |
}; |
| 1912 |
|
| 1913 |
// Macros for indicating success/failure in test code. |
| 1914 |
|
| 1915 |
// Skips test in runtime. |
| 1916 |
// Skipping test aborts current function. |
| 1917 |
// Skipped tests are neither successful nor failed. |
| 1918 |
#define GTEST_SKIP() GTEST_SKIP_("Skipped") |
| 1919 |
|
| 1920 |
// ADD_FAILURE unconditionally adds a failure to the current test. |
| 1921 |
// SUCCEED generates a success - it doesn't automatically make the |
| 1922 |
// current test successful, as a test is only successful when it has |
| 1923 |
// no failure. |
| 1924 |
// |
| 1925 |
// EXPECT_* verifies that a certain condition is satisfied. If not, |
| 1926 |
// it behaves like ADD_FAILURE. In particular: |
| 1927 |
// |
| 1928 |
// EXPECT_TRUE verifies that a Boolean condition is true. |
| 1929 |
// EXPECT_FALSE verifies that a Boolean condition is false. |
| 1930 |
// |
| 1931 |
// FAIL and ASSERT_* are similar to ADD_FAILURE and EXPECT_*, except |
| 1932 |
// that they will also abort the current function on failure. People |
| 1933 |
// usually want the fail-fast behavior of FAIL and ASSERT_*, but those |
| 1934 |
// writing data-driven tests often find themselves using ADD_FAILURE |
| 1935 |
// and EXPECT_* more. |
| 1936 |
|
| 1937 |
// Generates a nonfatal failure with a generic message. |
| 1938 |
#define ADD_FAILURE() GTEST_NONFATAL_FAILURE_("Failed") |
| 1939 |
|
| 1940 |
// Generates a nonfatal failure at the given source file location with |
| 1941 |
// a generic message. |
| 1942 |
#define ADD_FAILURE_AT(file, line) \ |
| 1943 |
GTEST_MESSAGE_AT_(file, line, "Failed", \ |
| 1944 |
::testing::TestPartResult::kNonFatalFailure) |
| 1945 |
|
| 1946 |
// Generates a fatal failure with a generic message. |
| 1947 |
#define GTEST_FAIL() GTEST_FATAL_FAILURE_("Failed") |
| 1948 |
|
| 1949 |
// Define this macro to 1 to omit the definition of FAIL(), which is a |
| 1950 |
// generic name and clashes with some other libraries. |
| 1951 |
#if !GTEST_DONT_DEFINE_FAIL |
| 1952 |
# define FAIL() GTEST_FAIL() |
| 1953 |
#endif |
| 1954 |
|
| 1955 |
// Generates a success with a generic message. |
| 1956 |
#define GTEST_SUCCEED() GTEST_SUCCESS_("Succeeded") |
| 1957 |
|
| 1958 |
// Define this macro to 1 to omit the definition of SUCCEED(), which |
| 1959 |
// is a generic name and clashes with some other libraries. |
| 1960 |
#if !GTEST_DONT_DEFINE_SUCCEED |
| 1961 |
# define SUCCEED() GTEST_SUCCEED() |
| 1962 |
#endif |
| 1963 |
|
| 1964 |
// Macros for testing exceptions. |
| 1965 |
// |
| 1966 |
// * {ASSERT|EXPECT}_THROW(statement, expected_exception): |
| 1967 |
// Tests that the statement throws the expected exception. |
| 1968 |
// * {ASSERT|EXPECT}_NO_THROW(statement): |
| 1969 |
// Tests that the statement doesn't throw any exception. |
| 1970 |
// * {ASSERT|EXPECT}_ANY_THROW(statement): |
| 1971 |
// Tests that the statement throws an exception. |
| 1972 |
|
| 1973 |
#define EXPECT_THROW(statement, expected_exception) \ |
| 1974 |
GTEST_TEST_THROW_(statement, expected_exception, GTEST_NONFATAL_FAILURE_) |
| 1975 |
#define EXPECT_NO_THROW(statement) \ |
| 1976 |
GTEST_TEST_NO_THROW_(statement, GTEST_NONFATAL_FAILURE_) |
| 1977 |
#define EXPECT_ANY_THROW(statement) \ |
| 1978 |
GTEST_TEST_ANY_THROW_(statement, GTEST_NONFATAL_FAILURE_) |
| 1979 |
#define ASSERT_THROW(statement, expected_exception) \ |
| 1980 |
GTEST_TEST_THROW_(statement, expected_exception, GTEST_FATAL_FAILURE_) |
| 1981 |
#define ASSERT_NO_THROW(statement) \ |
| 1982 |
GTEST_TEST_NO_THROW_(statement, GTEST_FATAL_FAILURE_) |
| 1983 |
#define ASSERT_ANY_THROW(statement) \ |
| 1984 |
GTEST_TEST_ANY_THROW_(statement, GTEST_FATAL_FAILURE_) |
| 1985 |
|
| 1986 |
// Boolean assertions. Condition can be either a Boolean expression or an |
| 1987 |
// AssertionResult. For more information on how to use AssertionResult with |
| 1988 |
// these macros see comments on that class. |
| 1989 |
#define EXPECT_TRUE(condition) \ |
| 1990 |
GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \ |
| 1991 |
GTEST_NONFATAL_FAILURE_) |
| 1992 |
#define EXPECT_FALSE(condition) \ |
| 1993 |
GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \ |
| 1994 |
GTEST_NONFATAL_FAILURE_) |
| 1995 |
#define ASSERT_TRUE(condition) \ |
| 1996 |
GTEST_TEST_BOOLEAN_(condition, #condition, false, true, \ |
| 1997 |
GTEST_FATAL_FAILURE_) |
| 1998 |
#define ASSERT_FALSE(condition) \ |
| 1999 |
GTEST_TEST_BOOLEAN_(!(condition), #condition, true, false, \ |
| 2000 |
GTEST_FATAL_FAILURE_) |
| 2001 |
|
| 2002 |
// Macros for testing equalities and inequalities. |
| 2003 |
// |
| 2004 |
// * {ASSERT|EXPECT}_EQ(v1, v2): Tests that v1 == v2 |
| 2005 |
// * {ASSERT|EXPECT}_NE(v1, v2): Tests that v1 != v2 |
| 2006 |
// * {ASSERT|EXPECT}_LT(v1, v2): Tests that v1 < v2 |
| 2007 |
// * {ASSERT|EXPECT}_LE(v1, v2): Tests that v1 <= v2 |
| 2008 |
// * {ASSERT|EXPECT}_GT(v1, v2): Tests that v1 > v2 |
| 2009 |
// * {ASSERT|EXPECT}_GE(v1, v2): Tests that v1 >= v2 |
| 2010 |
// |
| 2011 |
// When they are not, Google Test prints both the tested expressions and |
| 2012 |
// their actual values. The values must be compatible built-in types, |
| 2013 |
// or you will get a compiler error. By "compatible" we mean that the |
| 2014 |
// values can be compared by the respective operator. |
| 2015 |
// |
| 2016 |
// Note: |
| 2017 |
// |
| 2018 |
// 1. It is possible to make a user-defined type work with |
| 2019 |
// {ASSERT|EXPECT}_??(), but that requires overloading the |
| 2020 |
// comparison operators and is thus discouraged by the Google C++ |
| 2021 |
// Usage Guide. Therefore, you are advised to use the |
| 2022 |
// {ASSERT|EXPECT}_TRUE() macro to assert that two objects are |
| 2023 |
// equal. |
| 2024 |
// |
| 2025 |
// 2. The {ASSERT|EXPECT}_??() macros do pointer comparisons on |
| 2026 |
// pointers (in particular, C strings). Therefore, if you use it |
| 2027 |
// with two C strings, you are testing how their locations in memory |
| 2028 |
// are related, not how their content is related. To compare two C |
| 2029 |
// strings by content, use {ASSERT|EXPECT}_STR*(). |
| 2030 |
// |
| 2031 |
// 3. {ASSERT|EXPECT}_EQ(v1, v2) is preferred to |
| 2032 |
// {ASSERT|EXPECT}_TRUE(v1 == v2), as the former tells you |
| 2033 |
// what the actual value is when it fails, and similarly for the |
| 2034 |
// other comparisons. |
| 2035 |
// |
| 2036 |
// 4. Do not depend on the order in which {ASSERT|EXPECT}_??() |
| 2037 |
// evaluate their arguments, which is undefined. |
| 2038 |
// |
| 2039 |
// 5. These macros evaluate their arguments exactly once. |
| 2040 |
// |
| 2041 |
// Examples: |
| 2042 |
// |
| 2043 |
// EXPECT_NE(Foo(), 5); |
| 2044 |
// EXPECT_EQ(a_pointer, NULL); |
| 2045 |
// ASSERT_LT(i, array_size); |
| 2046 |
// ASSERT_GT(records.size(), 0) << "There is no record left."; |
| 2047 |
|
| 2048 |
#define EXPECT_EQ(val1, val2) \ |
| 2049 |
EXPECT_PRED_FORMAT2(::testing::internal:: \ |
| 2050 |
EqHelper<GTEST_IS_NULL_LITERAL_(val1)>::Compare, \ |
| 2051 |
val1, val2) |
| 2052 |
#define EXPECT_NE(val1, val2) \ |
| 2053 |
EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2) |
| 2054 |
#define EXPECT_LE(val1, val2) \ |
| 2055 |
EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) |
| 2056 |
#define EXPECT_LT(val1, val2) \ |
| 2057 |
EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) |
| 2058 |
#define EXPECT_GE(val1, val2) \ |
| 2059 |
EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) |
| 2060 |
#define EXPECT_GT(val1, val2) \ |
| 2061 |
EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) |
| 2062 |
|
| 2063 |
#define GTEST_ASSERT_EQ(val1, val2) \ |
| 2064 |
ASSERT_PRED_FORMAT2(::testing::internal:: \ |
| 2065 |
EqHelper<GTEST_IS_NULL_LITERAL_(val1)>::Compare, \ |
| 2066 |
val1, val2) |
| 2067 |
#define GTEST_ASSERT_NE(val1, val2) \ |
| 2068 |
ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperNE, val1, val2) |
| 2069 |
#define GTEST_ASSERT_LE(val1, val2) \ |
| 2070 |
ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLE, val1, val2) |
| 2071 |
#define GTEST_ASSERT_LT(val1, val2) \ |
| 2072 |
ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperLT, val1, val2) |
| 2073 |
#define GTEST_ASSERT_GE(val1, val2) \ |
| 2074 |
ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGE, val1, val2) |
| 2075 |
#define GTEST_ASSERT_GT(val1, val2) \ |
| 2076 |
ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperGT, val1, val2) |
| 2077 |
|
| 2078 |
// Define macro GTEST_DONT_DEFINE_ASSERT_XY to 1 to omit the definition of |
| 2079 |
// ASSERT_XY(), which clashes with some users' own code. |
| 2080 |
|
| 2081 |
#if !GTEST_DONT_DEFINE_ASSERT_EQ |
| 2082 |
# define ASSERT_EQ(val1, val2) GTEST_ASSERT_EQ(val1, val2) |
| 2083 |
#endif |
| 2084 |
|
| 2085 |
#if !GTEST_DONT_DEFINE_ASSERT_NE |
| 2086 |
# define ASSERT_NE(val1, val2) GTEST_ASSERT_NE(val1, val2) |
| 2087 |
#endif |
| 2088 |
|
| 2089 |
#if !GTEST_DONT_DEFINE_ASSERT_LE |
| 2090 |
# define ASSERT_LE(val1, val2) GTEST_ASSERT_LE(val1, val2) |
| 2091 |
#endif |
| 2092 |
|
| 2093 |
#if !GTEST_DONT_DEFINE_ASSERT_LT |
| 2094 |
# define ASSERT_LT(val1, val2) GTEST_ASSERT_LT(val1, val2) |
| 2095 |
#endif |
| 2096 |
|
| 2097 |
#if !GTEST_DONT_DEFINE_ASSERT_GE |
| 2098 |
# define ASSERT_GE(val1, val2) GTEST_ASSERT_GE(val1, val2) |
| 2099 |
#endif |
| 2100 |
|
| 2101 |
#if !GTEST_DONT_DEFINE_ASSERT_GT |
| 2102 |
# define ASSERT_GT(val1, val2) GTEST_ASSERT_GT(val1, val2) |
| 2103 |
#endif |
| 2104 |
|
| 2105 |
// C-string Comparisons. All tests treat NULL and any non-NULL string |
| 2106 |
// as different. Two NULLs are equal. |
| 2107 |
// |
| 2108 |
// * {ASSERT|EXPECT}_STREQ(s1, s2): Tests that s1 == s2 |
| 2109 |
// * {ASSERT|EXPECT}_STRNE(s1, s2): Tests that s1 != s2 |
| 2110 |
// * {ASSERT|EXPECT}_STRCASEEQ(s1, s2): Tests that s1 == s2, ignoring case |
| 2111 |
// * {ASSERT|EXPECT}_STRCASENE(s1, s2): Tests that s1 != s2, ignoring case |
| 2112 |
// |
| 2113 |
// For wide or narrow string objects, you can use the |
| 2114 |
// {ASSERT|EXPECT}_??() macros. |
| 2115 |
// |
| 2116 |
// Don't depend on the order in which the arguments are evaluated, |
| 2117 |
// which is undefined. |
| 2118 |
// |
| 2119 |
// These macros evaluate their arguments exactly once. |
| 2120 |
|
| 2121 |
#define EXPECT_STREQ(s1, s2) \ |
| 2122 |
EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2) |
| 2123 |
#define EXPECT_STRNE(s1, s2) \ |
| 2124 |
EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) |
| 2125 |
#define EXPECT_STRCASEEQ(s1, s2) \ |
| 2126 |
EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2) |
| 2127 |
#define EXPECT_STRCASENE(s1, s2)\ |
| 2128 |
EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) |
| 2129 |
|
| 2130 |
#define ASSERT_STREQ(s1, s2) \ |
| 2131 |
ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTREQ, s1, s2) |
| 2132 |
#define ASSERT_STRNE(s1, s2) \ |
| 2133 |
ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRNE, s1, s2) |
| 2134 |
#define ASSERT_STRCASEEQ(s1, s2) \ |
| 2135 |
ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASEEQ, s1, s2) |
| 2136 |
#define ASSERT_STRCASENE(s1, s2)\ |
| 2137 |
ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperSTRCASENE, s1, s2) |
| 2138 |
|
| 2139 |
// Macros for comparing floating-point numbers. |
| 2140 |
// |
| 2141 |
// * {ASSERT|EXPECT}_FLOAT_EQ(val1, val2): |
| 2142 |
// Tests that two float values are almost equal. |
| 2143 |
// * {ASSERT|EXPECT}_DOUBLE_EQ(val1, val2): |
| 2144 |
// Tests that two double values are almost equal. |
| 2145 |
// * {ASSERT|EXPECT}_NEAR(v1, v2, abs_error): |
| 2146 |
// Tests that v1 and v2 are within the given distance to each other. |
| 2147 |
// |
| 2148 |
// Google Test uses ULP-based comparison to automatically pick a default |
| 2149 |
// error bound that is appropriate for the operands. See the |
| 2150 |
// FloatingPoint template class in gtest-internal.h if you are |
| 2151 |
// interested in the implementation details. |
| 2152 |
|
| 2153 |
#define EXPECT_FLOAT_EQ(val1, val2)\ |
| 2154 |
EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \ |
| 2155 |
val1, val2) |
| 2156 |
|
| 2157 |
#define EXPECT_DOUBLE_EQ(val1, val2)\ |
| 2158 |
EXPECT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \ |
| 2159 |
val1, val2) |
| 2160 |
|
| 2161 |
#define ASSERT_FLOAT_EQ(val1, val2)\ |
| 2162 |
ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<float>, \ |
| 2163 |
val1, val2) |
| 2164 |
|
| 2165 |
#define ASSERT_DOUBLE_EQ(val1, val2)\ |
| 2166 |
ASSERT_PRED_FORMAT2(::testing::internal::CmpHelperFloatingPointEQ<double>, \ |
| 2167 |
val1, val2) |
| 2168 |
|
| 2169 |
#define EXPECT_NEAR(val1, val2, abs_error)\ |
| 2170 |
EXPECT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \ |
| 2171 |
val1, val2, abs_error) |
| 2172 |
|
| 2173 |
#define ASSERT_NEAR(val1, val2, abs_error)\ |
| 2174 |
ASSERT_PRED_FORMAT3(::testing::internal::DoubleNearPredFormat, \ |
| 2175 |
val1, val2, abs_error) |
| 2176 |
|
| 2177 |
// These predicate format functions work on floating-point values, and |
| 2178 |
// can be used in {ASSERT|EXPECT}_PRED_FORMAT2*(), e.g. |
| 2179 |
// |
| 2180 |
// EXPECT_PRED_FORMAT2(testing::DoubleLE, Foo(), 5.0); |
| 2181 |
|
| 2182 |
// Asserts that val1 is less than, or almost equal to, val2. Fails |
| 2183 |
// otherwise. In particular, it fails if either val1 or val2 is NaN. |
| 2184 |
GTEST_API_ AssertionResult FloatLE(const char* expr1, const char* expr2, |
| 2185 |
float val1, float val2); |
| 2186 |
GTEST_API_ AssertionResult DoubleLE(const char* expr1, const char* expr2, |
| 2187 |
double val1, double val2); |
| 2188 |
|
| 2189 |
|
| 2190 |
#if GTEST_OS_WINDOWS |
| 2191 |
|
| 2192 |
// Macros that test for HRESULT failure and success, these are only useful |
| 2193 |
// on Windows, and rely on Windows SDK macros and APIs to compile. |
| 2194 |
// |
| 2195 |
// * {ASSERT|EXPECT}_HRESULT_{SUCCEEDED|FAILED}(expr) |
| 2196 |
// |
| 2197 |
// When expr unexpectedly fails or succeeds, Google Test prints the |
| 2198 |
// expected result and the actual result with both a human-readable |
| 2199 |
// string representation of the error, if available, as well as the |
| 2200 |
// hex result code. |
| 2201 |
# define EXPECT_HRESULT_SUCCEEDED(expr) \ |
| 2202 |
EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) |
| 2203 |
|
| 2204 |
# define ASSERT_HRESULT_SUCCEEDED(expr) \ |
| 2205 |
ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTSuccess, (expr)) |
| 2206 |
|
| 2207 |
# define EXPECT_HRESULT_FAILED(expr) \ |
| 2208 |
EXPECT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) |
| 2209 |
|
| 2210 |
# define ASSERT_HRESULT_FAILED(expr) \ |
| 2211 |
ASSERT_PRED_FORMAT1(::testing::internal::IsHRESULTFailure, (expr)) |
| 2212 |
|
| 2213 |
#endif // GTEST_OS_WINDOWS |
| 2214 |
|
| 2215 |
// Macros that execute statement and check that it doesn't generate new fatal |
| 2216 |
// failures in the current thread. |
| 2217 |
// |
| 2218 |
// * {ASSERT|EXPECT}_NO_FATAL_FAILURE(statement); |
| 2219 |
// |
| 2220 |
// Examples: |
| 2221 |
// |
| 2222 |
// EXPECT_NO_FATAL_FAILURE(Process()); |
| 2223 |
// ASSERT_NO_FATAL_FAILURE(Process()) << "Process() failed"; |
| 2224 |
// |
| 2225 |
#define ASSERT_NO_FATAL_FAILURE(statement) \ |
| 2226 |
GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_FATAL_FAILURE_) |
| 2227 |
#define EXPECT_NO_FATAL_FAILURE(statement) \ |
| 2228 |
GTEST_TEST_NO_FATAL_FAILURE_(statement, GTEST_NONFATAL_FAILURE_) |
| 2229 |
|
| 2230 |
// Causes a trace (including the given source file path and line number, |
| 2231 |
// and the given message) to be included in every test failure message generated |
| 2232 |
// by code in the scope of the lifetime of an instance of this class. The effect |
| 2233 |
// is undone with the destruction of the instance. |
| 2234 |
// |
| 2235 |
// The message argument can be anything streamable to std::ostream. |
| 2236 |
// |
| 2237 |
// Example: |
| 2238 |
// testing::ScopedTrace trace("file.cc", 123, "message"); |
| 2239 |
// |
| 2240 |
class GTEST_API_ ScopedTrace { |
| 2241 |
public: |
| 2242 |
// The c'tor pushes the given source file location and message onto |
| 2243 |
// a trace stack maintained by Google Test. |
| 2244 |
|
| 2245 |
// Template version. Uses Message() to convert the values into strings. |
| 2246 |
// Slow, but flexible. |
| 2247 |
template <typename T> |
| 2248 |
ScopedTrace(const char* file, int line, const T& message) { |
| 2249 |
PushTrace(file, line, (Message() << message).GetString()); |
| 2250 |
} |
| 2251 |
|
| 2252 |
// Optimize for some known types. |
| 2253 |
ScopedTrace(const char* file, int line, const char* message) { |
| 2254 |
PushTrace(file, line, message ? message : "(null)"); |
| 2255 |
} |
| 2256 |
|
| 2257 |
#if GTEST_HAS_GLOBAL_STRING |
| 2258 |
ScopedTrace(const char* file, int line, const ::string& message) { |
| 2259 |
PushTrace(file, line, message); |
| 2260 |
} |
| 2261 |
#endif |
| 2262 |
|
| 2263 |
ScopedTrace(const char* file, int line, const std::string& message) { |
| 2264 |
PushTrace(file, line, message); |
| 2265 |
} |
| 2266 |
|
| 2267 |
// The d'tor pops the info pushed by the c'tor. |
| 2268 |
// |
| 2269 |
// Note that the d'tor is not virtual in order to be efficient. |
| 2270 |
// Don't inherit from ScopedTrace! |
| 2271 |
~ScopedTrace(); |
| 2272 |
|
| 2273 |
private: |
| 2274 |
void PushTrace(const char* file, int line, std::string message); |
| 2275 |
|
| 2276 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(ScopedTrace); |
| 2277 |
} GTEST_ATTRIBUTE_UNUSED_; // A ScopedTrace object does its job in its |
| 2278 |
// c'tor and d'tor. Therefore it doesn't |
| 2279 |
// need to be used otherwise. |
| 2280 |
|
| 2281 |
// Causes a trace (including the source file path, the current line |
| 2282 |
// number, and the given message) to be included in every test failure |
| 2283 |
// message generated by code in the current scope. The effect is |
| 2284 |
// undone when the control leaves the current scope. |
| 2285 |
// |
| 2286 |
// The message argument can be anything streamable to std::ostream. |
| 2287 |
// |
| 2288 |
// In the implementation, we include the current line number as part |
| 2289 |
// of the dummy variable name, thus allowing multiple SCOPED_TRACE()s |
| 2290 |
// to appear in the same block - as long as they are on different |
| 2291 |
// lines. |
| 2292 |
// |
| 2293 |
// Assuming that each thread maintains its own stack of traces. |
| 2294 |
// Therefore, a SCOPED_TRACE() would (correctly) only affect the |
| 2295 |
// assertions in its own thread. |
| 2296 |
#define SCOPED_TRACE(message) \ |
| 2297 |
::testing::ScopedTrace GTEST_CONCAT_TOKEN_(gtest_trace_, __LINE__)(\ |
| 2298 |
__FILE__, __LINE__, (message)) |
| 2299 |
|
| 2300 |
|
| 2301 |
// Compile-time assertion for type equality. |
| 2302 |
// StaticAssertTypeEq<type1, type2>() compiles iff type1 and type2 are |
| 2303 |
// the same type. The value it returns is not interesting. |
| 2304 |
// |
| 2305 |
// Instead of making StaticAssertTypeEq a class template, we make it a |
| 2306 |
// function template that invokes a helper class template. This |
| 2307 |
// prevents a user from misusing StaticAssertTypeEq<T1, T2> by |
| 2308 |
// defining objects of that type. |
| 2309 |
// |
| 2310 |
// CAVEAT: |
| 2311 |
// |
| 2312 |
// When used inside a method of a class template, |
| 2313 |
// StaticAssertTypeEq<T1, T2>() is effective ONLY IF the method is |
| 2314 |
// instantiated. For example, given: |
| 2315 |
// |
| 2316 |
// template <typename T> class Foo { |
| 2317 |
// public: |
| 2318 |
// void Bar() { testing::StaticAssertTypeEq<int, T>(); } |
| 2319 |
// }; |
| 2320 |
// |
| 2321 |
// the code: |
| 2322 |
// |
| 2323 |
// void Test1() { Foo<bool> foo; } |
| 2324 |
// |
| 2325 |
// will NOT generate a compiler error, as Foo<bool>::Bar() is never |
| 2326 |
// actually instantiated. Instead, you need: |
| 2327 |
// |
| 2328 |
// void Test2() { Foo<bool> foo; foo.Bar(); } |
| 2329 |
// |
| 2330 |
// to cause a compiler error. |
| 2331 |
template <typename T1, typename T2> |
| 2332 |
bool StaticAssertTypeEq() { |
| 2333 |
(void)internal::StaticAssertTypeEqHelper<T1, T2>(); |
| 2334 |
return true; |
| 2335 |
} |
| 2336 |
|
| 2337 |
// Defines a test. |
| 2338 |
// |
| 2339 |
// The first parameter is the name of the test suite, and the second |
| 2340 |
// parameter is the name of the test within the test suite. |
| 2341 |
// |
| 2342 |
// The convention is to end the test suite name with "Test". For |
| 2343 |
// example, a test suite for the Foo class can be named FooTest. |
| 2344 |
// |
| 2345 |
// Test code should appear between braces after an invocation of |
| 2346 |
// this macro. Example: |
| 2347 |
// |
| 2348 |
// TEST(FooTest, InitializesCorrectly) { |
| 2349 |
// Foo foo; |
| 2350 |
// EXPECT_TRUE(foo.StatusIsOK()); |
| 2351 |
// } |
| 2352 |
|
| 2353 |
// Note that we call GetTestTypeId() instead of GetTypeId< |
| 2354 |
// ::testing::Test>() here to get the type ID of testing::Test. This |
| 2355 |
// is to work around a suspected linker bug when using Google Test as |
| 2356 |
// a framework on Mac OS X. The bug causes GetTypeId< |
| 2357 |
// ::testing::Test>() to return different values depending on whether |
| 2358 |
// the call is from the Google Test framework itself or from user test |
| 2359 |
// code. GetTestTypeId() is guaranteed to always return the same |
| 2360 |
// value, as it always calls GetTypeId<>() from the Google Test |
| 2361 |
// framework. |
| 2362 |
#define GTEST_TEST(test_suite_name, test_name) \ |
| 2363 |
GTEST_TEST_(test_suite_name, test_name, ::testing::Test, \ |
| 2364 |
::testing::internal::GetTestTypeId()) |
| 2365 |
|
| 2366 |
// Define this macro to 1 to omit the definition of TEST(), which |
| 2367 |
// is a generic name and clashes with some other libraries. |
| 2368 |
#if !GTEST_DONT_DEFINE_TEST |
| 2369 |
#define TEST(test_suite_name, test_name) GTEST_TEST(test_suite_name, test_name) |
| 2370 |
#endif |
| 2371 |
|
| 2372 |
// Defines a test that uses a test fixture. |
| 2373 |
// |
| 2374 |
// The first parameter is the name of the test fixture class, which |
| 2375 |
// also doubles as the test suite name. The second parameter is the |
| 2376 |
// name of the test within the test suite. |
| 2377 |
// |
| 2378 |
// A test fixture class must be declared earlier. The user should put |
| 2379 |
// the test code between braces after using this macro. Example: |
| 2380 |
// |
| 2381 |
// class FooTest : public testing::Test { |
| 2382 |
// protected: |
| 2383 |
// void SetUp() override { b_.AddElement(3); } |
| 2384 |
// |
| 2385 |
// Foo a_; |
| 2386 |
// Foo b_; |
| 2387 |
// }; |
| 2388 |
// |
| 2389 |
// TEST_F(FooTest, InitializesCorrectly) { |
| 2390 |
// EXPECT_TRUE(a_.StatusIsOK()); |
| 2391 |
// } |
| 2392 |
// |
| 2393 |
// TEST_F(FooTest, ReturnsElementCountCorrectly) { |
| 2394 |
// EXPECT_EQ(a_.size(), 0); |
| 2395 |
// EXPECT_EQ(b_.size(), 1); |
| 2396 |
// } |
| 2397 |
// |
| 2398 |
// GOOGLETEST_CM0011 DO NOT DELETE |
| 2399 |
#define TEST_F(test_fixture, test_name)\ |
| 2400 |
GTEST_TEST_(test_fixture, test_name, test_fixture, \ |
| 2401 |
::testing::internal::GetTypeId<test_fixture>()) |
| 2402 |
|
| 2403 |
// Returns a path to temporary directory. |
| 2404 |
// Tries to determine an appropriate directory for the platform. |
| 2405 |
GTEST_API_ std::string TempDir(); |
| 2406 |
|
| 2407 |
#ifdef _MSC_VER |
| 2408 |
# pragma warning(pop) |
| 2409 |
#endif |
| 2410 |
|
| 2411 |
// Dynamically registers a test with the framework. |
| 2412 |
// |
| 2413 |
// This is an advanced API only to be used when the `TEST` macros are |
| 2414 |
// insufficient. The macros should be preferred when possible, as they avoid |
| 2415 |
// most of the complexity of calling this function. |
| 2416 |
// |
| 2417 |
// The `factory` argument is a factory callable (move-constructible) object or |
| 2418 |
// function pointer that creates a new instance of the Test object. It |
| 2419 |
// handles ownership to the caller. The signature of the callable is |
| 2420 |
// `Fixture*()`, where `Fixture` is the test fixture class for the test. All |
| 2421 |
// tests registered with the same `test_suite_name` must return the same |
| 2422 |
// fixture type. This is checked at runtime. |
| 2423 |
// |
| 2424 |
// The framework will infer the fixture class from the factory and will call |
| 2425 |
// the `SetUpTestSuite` and `TearDownTestSuite` for it. |
| 2426 |
// |
| 2427 |
// Must be called before `RUN_ALL_TESTS()` is invoked, otherwise behavior is |
| 2428 |
// undefined. |
| 2429 |
// |
| 2430 |
// Use case example: |
| 2431 |
// |
| 2432 |
// class MyFixture : public ::testing::Test { |
| 2433 |
// public: |
| 2434 |
// // All of these optional, just like in regular macro usage. |
| 2435 |
// static void SetUpTestSuite() { ... } |
| 2436 |
// static void TearDownTestSuite() { ... } |
| 2437 |
// void SetUp() override { ... } |
| 2438 |
// void TearDown() override { ... } |
| 2439 |
// }; |
| 2440 |
// |
| 2441 |
// class MyTest : public MyFixture { |
| 2442 |
// public: |
| 2443 |
// explicit MyTest(int data) : data_(data) {} |
| 2444 |
// void TestBody() override { ... } |
| 2445 |
// |
| 2446 |
// private: |
| 2447 |
// int data_; |
| 2448 |
// }; |
| 2449 |
// |
| 2450 |
// void RegisterMyTests(const std::vector<int>& values) { |
| 2451 |
// for (int v : values) { |
| 2452 |
// ::testing::RegisterTest( |
| 2453 |
// "MyFixture", ("Test" + std::to_string(v)).c_str(), nullptr, |
| 2454 |
// std::to_string(v).c_str(), |
| 2455 |
// __FILE__, __LINE__, |
| 2456 |
// // Important to use the fixture type as the return type here. |
| 2457 |
// [=]() -> MyFixture* { return new MyTest(v); }); |
| 2458 |
// } |
| 2459 |
// } |
| 2460 |
// ... |
| 2461 |
// int main(int argc, char** argv) { |
| 2462 |
// std::vector<int> values_to_test = LoadValuesFromConfig(); |
| 2463 |
// RegisterMyTests(values_to_test); |
| 2464 |
// ... |
| 2465 |
// return RUN_ALL_TESTS(); |
| 2466 |
// } |
| 2467 |
// |
| 2468 |
template <int&... ExplicitParameterBarrier, typename Factory> |
| 2469 |
TestInfo* RegisterTest(const char* test_suite_name, const char* test_name, |
| 2470 |
const char* type_param, const char* value_param, |
| 2471 |
const char* file, int line, Factory factory) { |
| 2472 |
using TestT = typename std::remove_pointer<decltype(factory())>::type; |
| 2473 |
|
| 2474 |
class FactoryImpl : public internal::TestFactoryBase { |
| 2475 |
public: |
| 2476 |
explicit FactoryImpl(Factory f) : factory_(std::move(f)) {} |
| 2477 |
Test* CreateTest() override { return factory_(); } |
| 2478 |
|
| 2479 |
private: |
| 2480 |
Factory factory_; |
| 2481 |
}; |
| 2482 |
|
| 2483 |
return internal::MakeAndRegisterTestInfo( |
| 2484 |
test_suite_name, test_name, type_param, value_param, |
| 2485 |
internal::CodeLocation(file, line), internal::GetTypeId<TestT>(), |
| 2486 |
internal::SuiteApiResolver<TestT>::GetSetUpCaseOrSuite(), |
| 2487 |
internal::SuiteApiResolver<TestT>::GetTearDownCaseOrSuite(), |
| 2488 |
new FactoryImpl{std::move(factory)}); |
| 2489 |
} |
| 2490 |
|
| 2491 |
} // namespace testing |
| 2492 |
|
| 2493 |
// Use this function in main() to run all tests. It returns 0 if all |
| 2494 |
// tests are successful, or 1 otherwise. |
| 2495 |
// |
| 2496 |
// RUN_ALL_TESTS() should be invoked after the command line has been |
| 2497 |
// parsed by InitGoogleTest(). |
| 2498 |
// |
| 2499 |
// This function was formerly a macro; thus, it is in the global |
| 2500 |
// namespace and has an all-caps name. |
| 2501 |
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_; |
| 2502 |
|
| 2503 |
inline int RUN_ALL_TESTS() { |
| 2504 |
return ::testing::UnitTest::GetInstance()->Run(); |
| 2505 |
} |
| 2506 |
|
| 2507 |
GTEST_DISABLE_MSC_WARNINGS_POP_() // 4251 |
| 2508 |
|
| 2509 |
#endif // GTEST_INCLUDE_GTEST_GTEST_H_ |