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